@zubari/sdk 0.2.8 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{TransactionService-8xSEGoWA.d.mts → TransactionService-DURp3bRL.d.ts} +34 -10
- package/dist/{TransactionService-CaIcCoqY.d.ts → TransactionService-DuMJmrG3.d.mts} +34 -10
- package/dist/{WalletManager-CCs4Jsv7.d.ts → WalletManager-CEjN-YBF.d.ts} +12 -12
- package/dist/{WalletManager-B1qvFF4K.d.mts → WalletManager-bo35aHOJ.d.mts} +12 -12
- package/dist/{index-BOc9U2TK.d.mts → index-BpjMiC3n.d.ts} +22 -11
- package/dist/{index-Cx389p_j.d.mts → index-DF0Gf8NK.d.mts} +7 -1
- package/dist/{index-Cx389p_j.d.ts → index-DF0Gf8NK.d.ts} +7 -1
- package/dist/{index-Cqrpp3XA.d.ts → index-DJnsirV5.d.mts} +22 -11
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3064 -1770
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3064 -1770
- package/dist/index.mjs.map +1 -1
- package/dist/protocols/index.d.mts +54 -22
- package/dist/protocols/index.d.ts +54 -22
- package/dist/protocols/index.js +1008 -76
- package/dist/protocols/index.js.map +1 -1
- package/dist/protocols/index.mjs +1008 -76
- package/dist/protocols/index.mjs.map +1 -1
- package/dist/react/index.d.mts +3 -3
- package/dist/react/index.d.ts +3 -3
- package/dist/react/index.js +884 -884
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +884 -884
- package/dist/react/index.mjs.map +1 -1
- package/dist/services/index.d.mts +2 -2
- package/dist/services/index.d.ts +2 -2
- package/dist/services/index.js +152 -75
- package/dist/services/index.js.map +1 -1
- package/dist/services/index.mjs +152 -75
- package/dist/services/index.mjs.map +1 -1
- package/dist/wallet/index.d.mts +3 -3
- package/dist/wallet/index.d.ts +3 -3
- package/dist/wallet/index.js +1352 -1105
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +1352 -1105
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +13 -12
package/dist/services/index.mjs
CHANGED
|
@@ -13,115 +13,171 @@ var __export = (target, all) => {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
// src/config/contracts.ts
|
|
16
|
-
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
17
|
-
var ZUBARI_CONTRACTS = {
|
|
18
|
-
testnet: {
|
|
19
|
-
// Ethereum Sepolia (11155111) - Deployed 2024-12-31
|
|
20
|
-
registry: "0xe5CE1Eb986f58BE42EEDFe5C18ee5956803b2BDC",
|
|
21
|
-
nft: "0xc165b8C6843e8f0B1489525D0f853d23f22c677B",
|
|
22
|
-
// ZubariNFT ERC-721 with payment
|
|
23
|
-
nft1155: "0x5e618B5bEaE1dc41369E7aa235Cc3b9245905192",
|
|
24
|
-
// Zubari1155 ERC-1155 multi-edition
|
|
25
|
-
marketplace: "0xfcEfDa6C73aC357b8695E5F8F8d17820750BF207",
|
|
26
|
-
tips: "0x86a9A306C7fCC9e0B8cd6859f6f15498d0046BB7",
|
|
27
|
-
subscriptions: "0xaB7F17A85F61d9ab9f96bCB4e73e910D019978F7",
|
|
28
|
-
payouts: "0x8aaB23Fb2a83A259E965Aae8Ee0938b1400B7E6b",
|
|
29
|
-
entryPoint: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
|
|
30
|
-
paymaster: ZERO_ADDRESS,
|
|
31
|
-
// Deploy with: npx hardhat run deploy/deploy.ts --network ethereum-sepolia
|
|
32
|
-
accountFactory: ZERO_ADDRESS,
|
|
33
|
-
// Not yet deployed
|
|
34
|
-
usdt: "0xaA8E23Fb1079EA71e0a56F48a2aA51851D8433D0",
|
|
35
|
-
// USDT on Sepolia
|
|
36
|
-
weth: "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14"
|
|
37
|
-
},
|
|
38
|
-
mainnet: {
|
|
39
|
-
// Ethereum Mainnet (1)
|
|
40
|
-
registry: ZERO_ADDRESS,
|
|
41
|
-
nft: ZERO_ADDRESS,
|
|
42
|
-
nft1155: ZERO_ADDRESS,
|
|
43
|
-
marketplace: ZERO_ADDRESS,
|
|
44
|
-
tips: ZERO_ADDRESS,
|
|
45
|
-
subscriptions: ZERO_ADDRESS,
|
|
46
|
-
payouts: ZERO_ADDRESS,
|
|
47
|
-
entryPoint: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
|
|
48
|
-
paymaster: ZERO_ADDRESS,
|
|
49
|
-
accountFactory: ZERO_ADDRESS,
|
|
50
|
-
usdt: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
51
|
-
// USDT on Ethereum
|
|
52
|
-
weth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
16
|
var PLATFORM_CONFIG = {
|
|
56
17
|
// Default slippage tolerance for swaps (50 = 0.5%)
|
|
57
|
-
defaultSlippageBps: 50
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
18
|
+
defaultSlippageBps: 50,
|
|
19
|
+
// Swap deadline in seconds (30 minutes)
|
|
20
|
+
swapDeadlineSecs: 30 * 60
|
|
21
|
+
};
|
|
61
22
|
|
|
62
23
|
// src/services/SwapService.ts
|
|
24
|
+
var UNISWAP_ADDRESSES = {
|
|
25
|
+
mainnet: {
|
|
26
|
+
swapRouter: "0xE592427A0AEce92De3Edee1F18E0157C05861564",
|
|
27
|
+
quoter: "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6",
|
|
28
|
+
factory: "0x1F98431c8aD98523631AE4a59f267346ea31F984"
|
|
29
|
+
},
|
|
30
|
+
sepolia: {
|
|
31
|
+
swapRouter: "0x3bFA4769FB09eefC5a80d6E87c3B9C650f7Ae48E",
|
|
32
|
+
quoter: "0xEd1f6473345F45b75F8179591dd5bA1888cf2FB3",
|
|
33
|
+
factory: "0x0227628f3F023bb0B980b67D528571c95c6DaC1c"
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var UNISWAP_SELECTORS = {
|
|
37
|
+
quoteExactInputSingle: "0xf7729d43",
|
|
38
|
+
// quoteExactInputSingle(address,address,uint24,uint256,uint160)
|
|
39
|
+
exactInputSingle: "0x414bf389"
|
|
40
|
+
// exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))
|
|
41
|
+
};
|
|
42
|
+
var TOKENS = {
|
|
43
|
+
mainnet: {
|
|
44
|
+
WETH: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
45
|
+
USDT: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
|
46
|
+
USDC: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
|
47
|
+
},
|
|
48
|
+
sepolia: {
|
|
49
|
+
WETH: "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14",
|
|
50
|
+
USDT: "0x7169D38820dfd117C3FA1f22a697dBA58d90BA06",
|
|
51
|
+
USDC: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var ETH_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
63
55
|
var SwapService = class {
|
|
64
|
-
|
|
56
|
+
chainId;
|
|
65
57
|
isTestnet;
|
|
58
|
+
addresses;
|
|
59
|
+
tokens;
|
|
66
60
|
constructor(chainId, isTestnet = false) {
|
|
67
|
-
this.
|
|
61
|
+
this.chainId = chainId;
|
|
68
62
|
this.isTestnet = isTestnet;
|
|
63
|
+
this.addresses = isTestnet ? UNISWAP_ADDRESSES.sepolia : UNISWAP_ADDRESSES.mainnet;
|
|
64
|
+
this.tokens = isTestnet ? TOKENS.sepolia : TOKENS.mainnet;
|
|
69
65
|
}
|
|
70
66
|
/**
|
|
71
|
-
* Get a swap quote
|
|
67
|
+
* Get a swap quote from Uniswap V3 Quoter
|
|
72
68
|
*/
|
|
73
|
-
async getQuote(tokenIn, tokenOut, amountIn) {
|
|
69
|
+
async getQuote(tokenIn, tokenOut, amountIn, provider) {
|
|
74
70
|
if (amountIn <= 0n) {
|
|
75
71
|
throw new Error("Amount must be greater than 0");
|
|
76
72
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
73
|
+
const actualTokenIn = tokenIn === ETH_ADDRESS ? this.tokens.WETH : tokenIn;
|
|
74
|
+
const actualTokenOut = tokenOut === ETH_ADDRESS ? this.tokens.WETH : tokenOut;
|
|
75
|
+
if (!provider) {
|
|
76
|
+
console.warn("No provider for quote, returning estimate");
|
|
77
|
+
return {
|
|
78
|
+
tokenIn,
|
|
79
|
+
tokenOut,
|
|
80
|
+
amountIn,
|
|
81
|
+
amountOut: amountIn * BigInt(95) / BigInt(100),
|
|
82
|
+
// Rough estimate
|
|
83
|
+
priceImpact: 0.5,
|
|
84
|
+
route: [tokenIn, tokenOut],
|
|
85
|
+
estimatedGas: BigInt(15e4)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const encodedTokenIn = actualTokenIn.toLowerCase().replace("0x", "").padStart(64, "0");
|
|
90
|
+
const encodedTokenOut = actualTokenOut.toLowerCase().replace("0x", "").padStart(64, "0");
|
|
91
|
+
const encodedFee = 3e3.toString(16).padStart(64, "0");
|
|
92
|
+
const encodedAmountIn = amountIn.toString(16).padStart(64, "0");
|
|
93
|
+
const encodedSqrtPriceLimit = "0".padStart(64, "0");
|
|
94
|
+
const data = `${UNISWAP_SELECTORS.quoteExactInputSingle}${encodedTokenIn}${encodedTokenOut}${encodedFee}${encodedAmountIn}${encodedSqrtPriceLimit}`;
|
|
95
|
+
const result = await provider.call({
|
|
96
|
+
to: this.addresses.quoter,
|
|
97
|
+
data
|
|
98
|
+
});
|
|
99
|
+
const amountOut = BigInt(result);
|
|
100
|
+
const priceImpact = this.calculatePriceImpact(amountIn, amountOut, tokenIn, tokenOut);
|
|
101
|
+
return {
|
|
102
|
+
tokenIn,
|
|
103
|
+
tokenOut,
|
|
104
|
+
amountIn,
|
|
105
|
+
amountOut,
|
|
106
|
+
priceImpact,
|
|
107
|
+
route: [tokenIn, tokenOut],
|
|
108
|
+
estimatedGas: BigInt(15e4)
|
|
109
|
+
};
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error("Failed to get quote:", error);
|
|
112
|
+
throw new Error(`Quote failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
113
|
+
}
|
|
86
114
|
}
|
|
87
115
|
/**
|
|
88
|
-
* Execute a swap with slippage protection
|
|
116
|
+
* Execute a swap with slippage protection via Uniswap V3 SwapRouter
|
|
89
117
|
*/
|
|
90
|
-
async executeSwap(quote, slippageToleranceBps = PLATFORM_CONFIG.defaultSlippageBps) {
|
|
91
|
-
quote.amountOut - quote.amountOut * BigInt(slippageToleranceBps) / BigInt(1e4);
|
|
118
|
+
async executeSwap(quote, signer, recipient, slippageToleranceBps = PLATFORM_CONFIG.defaultSlippageBps) {
|
|
119
|
+
const minAmountOut = quote.amountOut - quote.amountOut * BigInt(slippageToleranceBps) / BigInt(1e4);
|
|
120
|
+
const deadline = Math.floor(Date.now() / 1e3) + PLATFORM_CONFIG.swapDeadlineSecs;
|
|
92
121
|
if (quote.priceImpact > 5) {
|
|
93
122
|
console.warn(`High price impact: ${quote.priceImpact}%`);
|
|
94
123
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
124
|
+
const actualTokenIn = quote.tokenIn === ETH_ADDRESS ? this.tokens.WETH : quote.tokenIn;
|
|
125
|
+
const actualTokenOut = quote.tokenOut === ETH_ADDRESS ? this.tokens.WETH : quote.tokenOut;
|
|
126
|
+
const isETHIn = quote.tokenIn === ETH_ADDRESS;
|
|
127
|
+
try {
|
|
128
|
+
const encodedTokenIn = actualTokenIn.toLowerCase().replace("0x", "").padStart(64, "0");
|
|
129
|
+
const encodedTokenOut = actualTokenOut.toLowerCase().replace("0x", "").padStart(64, "0");
|
|
130
|
+
const encodedFee = 3e3.toString(16).padStart(64, "0");
|
|
131
|
+
const encodedRecipient = recipient.toLowerCase().replace("0x", "").padStart(64, "0");
|
|
132
|
+
const encodedDeadline = deadline.toString(16).padStart(64, "0");
|
|
133
|
+
const encodedAmountIn = quote.amountIn.toString(16).padStart(64, "0");
|
|
134
|
+
const encodedMinAmountOut = minAmountOut.toString(16).padStart(64, "0");
|
|
135
|
+
const encodedSqrtPriceLimit = "0".padStart(64, "0");
|
|
136
|
+
const data = `${UNISWAP_SELECTORS.exactInputSingle}${encodedTokenIn}${encodedTokenOut}${encodedFee}${encodedRecipient}${encodedDeadline}${encodedAmountIn}${encodedMinAmountOut}${encodedSqrtPriceLimit}`;
|
|
137
|
+
const tx = await signer.sendTransaction({
|
|
138
|
+
to: this.addresses.swapRouter,
|
|
139
|
+
data,
|
|
140
|
+
value: isETHIn ? `0x${quote.amountIn.toString(16)}` : "0x0"
|
|
141
|
+
});
|
|
142
|
+
return {
|
|
143
|
+
hash: tx.hash,
|
|
144
|
+
network: "ethereum",
|
|
145
|
+
status: "pending",
|
|
146
|
+
metadata: {
|
|
147
|
+
tokenIn: quote.tokenIn,
|
|
148
|
+
tokenOut: quote.tokenOut,
|
|
149
|
+
amountIn: quote.amountIn.toString(),
|
|
150
|
+
minAmountOut: minAmountOut.toString(),
|
|
151
|
+
deadline
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error("Failed to execute swap:", error);
|
|
156
|
+
throw new Error(`Swap failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
157
|
+
}
|
|
100
158
|
}
|
|
101
159
|
/**
|
|
102
160
|
* Convert earnings to USDT, keeping some ETH for gas
|
|
103
161
|
*/
|
|
104
|
-
async convertEarningsToStable(ethBalance, reserveForGas = BigInt("10000000000000000")) {
|
|
162
|
+
async convertEarningsToStable(ethBalance, signer, recipient, provider, reserveForGas = BigInt("10000000000000000")) {
|
|
105
163
|
const amountToSwap = ethBalance - reserveForGas;
|
|
106
164
|
if (amountToSwap <= 0n) {
|
|
107
165
|
throw new Error("Insufficient balance after gas reserve");
|
|
108
166
|
}
|
|
109
|
-
const contracts = getContractAddresses(this.isTestnet ? "testnet" : "mainnet");
|
|
110
167
|
const quote = await this.getQuote(
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
amountToSwap
|
|
168
|
+
ETH_ADDRESS,
|
|
169
|
+
this.tokens.USDT,
|
|
170
|
+
amountToSwap,
|
|
171
|
+
provider
|
|
116
172
|
);
|
|
117
|
-
return this.executeSwap(quote);
|
|
173
|
+
return this.executeSwap(quote, signer, recipient);
|
|
118
174
|
}
|
|
119
175
|
/**
|
|
120
176
|
* Check if a swap route exists
|
|
121
177
|
*/
|
|
122
|
-
async hasRoute(tokenIn, tokenOut) {
|
|
178
|
+
async hasRoute(tokenIn, tokenOut, provider) {
|
|
123
179
|
try {
|
|
124
|
-
const quote = await this.getQuote(tokenIn, tokenOut, BigInt(
|
|
180
|
+
const quote = await this.getQuote(tokenIn, tokenOut, BigInt(10 ** 18), provider);
|
|
125
181
|
return quote.amountOut > 0n;
|
|
126
182
|
} catch {
|
|
127
183
|
return false;
|
|
@@ -131,7 +187,28 @@ var SwapService = class {
|
|
|
131
187
|
* Get supported tokens for swapping
|
|
132
188
|
*/
|
|
133
189
|
async getSupportedTokens() {
|
|
134
|
-
return [
|
|
190
|
+
return [
|
|
191
|
+
{ address: ETH_ADDRESS, symbol: "ETH", decimals: 18, name: "Ether" },
|
|
192
|
+
{ address: this.tokens.WETH, symbol: "WETH", decimals: 18, name: "Wrapped Ether" },
|
|
193
|
+
{ address: this.tokens.USDT, symbol: "USDT", decimals: 6, name: "Tether USD" },
|
|
194
|
+
{ address: this.tokens.USDC, symbol: "USDC", decimals: 6, name: "USD Coin" }
|
|
195
|
+
];
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get Uniswap router address for approvals
|
|
199
|
+
*/
|
|
200
|
+
getRouterAddress() {
|
|
201
|
+
return this.addresses.swapRouter;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Calculate price impact (simplified)
|
|
205
|
+
*/
|
|
206
|
+
calculatePriceImpact(amountIn, amountOut, _tokenIn, _tokenOut) {
|
|
207
|
+
if (amountIn === 0n || amountOut === 0n) return 0;
|
|
208
|
+
const expectedOut = amountIn;
|
|
209
|
+
const actualRatio = Number(amountOut) / Number(expectedOut);
|
|
210
|
+
const impact = (1 - actualRatio) * 100;
|
|
211
|
+
return Math.max(0, impact);
|
|
135
212
|
}
|
|
136
213
|
};
|
|
137
214
|
|