@zubari/sdk 0.2.7 → 0.3.0

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.
Files changed (39) hide show
  1. package/dist/{TransactionService-8xSEGoWA.d.mts → TransactionService-DURp3bRL.d.ts} +34 -10
  2. package/dist/{TransactionService-CaIcCoqY.d.ts → TransactionService-DuMJmrG3.d.mts} +34 -10
  3. package/dist/{WalletManager-B1qvFF4K.d.mts → WalletManager-D0xMpgfo.d.mts} +133 -50
  4. package/dist/{WalletManager-CCs4Jsv7.d.ts → WalletManager-DsAg7MwL.d.ts} +133 -50
  5. package/dist/{index-Cx389p_j.d.mts → index-DF0Gf8NK.d.mts} +7 -1
  6. package/dist/{index-Cx389p_j.d.ts → index-DF0Gf8NK.d.ts} +7 -1
  7. package/dist/{index-xZYY0MEX.d.mts → index-N2u4haqL.d.mts} +23 -11
  8. package/dist/{index-BPojlGT6.d.ts → index-kS-xopkl.d.ts} +23 -11
  9. package/dist/index.d.mts +6 -5
  10. package/dist/index.d.ts +6 -5
  11. package/dist/index.js +3070 -1772
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +3070 -1772
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/protocols/index.d.mts +54 -22
  16. package/dist/protocols/index.d.ts +54 -22
  17. package/dist/protocols/index.js +1008 -76
  18. package/dist/protocols/index.js.map +1 -1
  19. package/dist/protocols/index.mjs +1008 -76
  20. package/dist/protocols/index.mjs.map +1 -1
  21. package/dist/react/index.d.mts +5 -4
  22. package/dist/react/index.d.ts +5 -4
  23. package/dist/react/index.js +884 -884
  24. package/dist/react/index.js.map +1 -1
  25. package/dist/react/index.mjs +884 -884
  26. package/dist/react/index.mjs.map +1 -1
  27. package/dist/services/index.d.mts +2 -2
  28. package/dist/services/index.d.ts +2 -2
  29. package/dist/services/index.js +152 -71
  30. package/dist/services/index.js.map +1 -1
  31. package/dist/services/index.mjs +152 -71
  32. package/dist/services/index.mjs.map +1 -1
  33. package/dist/wallet/index.d.mts +5 -4
  34. package/dist/wallet/index.d.ts +5 -4
  35. package/dist/wallet/index.js +1358 -1107
  36. package/dist/wallet/index.js.map +1 -1
  37. package/dist/wallet/index.mjs +1358 -1107
  38. package/dist/wallet/index.mjs.map +1 -1
  39. package/package.json +9 -6
@@ -13,111 +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-30
20
- registry: "0xe5CE1Eb986f58BE42EEDFe5C18ee5956803b2BDC",
21
- nft: "0xCb1AB134a75c4D504792233efC5dE949aDE3f29f",
22
- marketplace: "0xfcEfDa6C73aC357b8695E5F8F8d17820750BF207",
23
- tips: "0x86a9A306C7fCC9e0B8cd6859f6f15498d0046BB7",
24
- subscriptions: "0xaB7F17A85F61d9ab9f96bCB4e73e910D019978F7",
25
- payouts: "0x8aaB23Fb2a83A259E965Aae8Ee0938b1400B7E6b",
26
- entryPoint: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
27
- paymaster: ZERO_ADDRESS,
28
- // Deploy with: npx hardhat run deploy/deploy.ts --network ethereum-sepolia
29
- accountFactory: ZERO_ADDRESS,
30
- // Not yet deployed
31
- usdt: "0xaA8E23Fb1079EA71e0a56F48a2aA51851D8433D0",
32
- // USDT on Sepolia
33
- weth: "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14"
34
- },
35
- mainnet: {
36
- // Ethereum Mainnet (1)
37
- registry: ZERO_ADDRESS,
38
- nft: ZERO_ADDRESS,
39
- marketplace: ZERO_ADDRESS,
40
- tips: ZERO_ADDRESS,
41
- subscriptions: ZERO_ADDRESS,
42
- payouts: ZERO_ADDRESS,
43
- entryPoint: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789",
44
- paymaster: ZERO_ADDRESS,
45
- accountFactory: ZERO_ADDRESS,
46
- usdt: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
47
- // USDT on Ethereum
48
- weth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
49
- }
50
- };
51
16
  var PLATFORM_CONFIG = {
52
17
  // Default slippage tolerance for swaps (50 = 0.5%)
53
- defaultSlippageBps: 50};
54
- function getContractAddresses(network) {
55
- return ZUBARI_CONTRACTS[network];
56
- }
18
+ defaultSlippageBps: 50,
19
+ // Swap deadline in seconds (30 minutes)
20
+ swapDeadlineSecs: 30 * 60
21
+ };
57
22
 
58
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";
59
55
  var SwapService = class {
60
- _chainId;
56
+ chainId;
61
57
  isTestnet;
58
+ addresses;
59
+ tokens;
62
60
  constructor(chainId, isTestnet = false) {
63
- this._chainId = chainId;
61
+ this.chainId = chainId;
64
62
  this.isTestnet = isTestnet;
63
+ this.addresses = isTestnet ? UNISWAP_ADDRESSES.sepolia : UNISWAP_ADDRESSES.mainnet;
64
+ this.tokens = isTestnet ? TOKENS.sepolia : TOKENS.mainnet;
65
65
  }
66
66
  /**
67
- * Get a swap quote
67
+ * Get a swap quote from Uniswap V3 Quoter
68
68
  */
69
- async getQuote(tokenIn, tokenOut, amountIn) {
69
+ async getQuote(tokenIn, tokenOut, amountIn, provider) {
70
70
  if (amountIn <= 0n) {
71
71
  throw new Error("Amount must be greater than 0");
72
72
  }
73
- return {
74
- tokenIn,
75
- tokenOut,
76
- amountIn,
77
- amountOut: BigInt(0),
78
- priceImpact: 0,
79
- route: [tokenIn, tokenOut],
80
- estimatedGas: BigInt(0)
81
- };
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
+ }
82
114
  }
83
115
  /**
84
- * Execute a swap with slippage protection
116
+ * Execute a swap with slippage protection via Uniswap V3 SwapRouter
85
117
  */
86
- async executeSwap(quote, slippageToleranceBps = PLATFORM_CONFIG.defaultSlippageBps) {
87
- 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;
88
121
  if (quote.priceImpact > 5) {
89
122
  console.warn(`High price impact: ${quote.priceImpact}%`);
90
123
  }
91
- return {
92
- hash: "",
93
- network: "ethereum",
94
- status: "pending"
95
- };
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
+ }
96
158
  }
97
159
  /**
98
160
  * Convert earnings to USDT, keeping some ETH for gas
99
161
  */
100
- async convertEarningsToStable(ethBalance, reserveForGas = BigInt("10000000000000000")) {
162
+ async convertEarningsToStable(ethBalance, signer, recipient, provider, reserveForGas = BigInt("10000000000000000")) {
101
163
  const amountToSwap = ethBalance - reserveForGas;
102
164
  if (amountToSwap <= 0n) {
103
165
  throw new Error("Insufficient balance after gas reserve");
104
166
  }
105
- const contracts = getContractAddresses(this.isTestnet ? "testnet" : "mainnet");
106
167
  const quote = await this.getQuote(
107
- "0x0000000000000000000000000000000000000000",
108
- // ETH
109
- contracts.usdt,
110
- // USDT on Ethereum
111
- amountToSwap
168
+ ETH_ADDRESS,
169
+ this.tokens.USDT,
170
+ amountToSwap,
171
+ provider
112
172
  );
113
- return this.executeSwap(quote);
173
+ return this.executeSwap(quote, signer, recipient);
114
174
  }
115
175
  /**
116
176
  * Check if a swap route exists
117
177
  */
118
- async hasRoute(tokenIn, tokenOut) {
178
+ async hasRoute(tokenIn, tokenOut, provider) {
119
179
  try {
120
- const quote = await this.getQuote(tokenIn, tokenOut, BigInt(1));
180
+ const quote = await this.getQuote(tokenIn, tokenOut, BigInt(10 ** 18), provider);
121
181
  return quote.amountOut > 0n;
122
182
  } catch {
123
183
  return false;
@@ -127,7 +187,28 @@ var SwapService = class {
127
187
  * Get supported tokens for swapping
128
188
  */
129
189
  async getSupportedTokens() {
130
- 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);
131
212
  }
132
213
  };
133
214