@lifi/sdk 2.2.2 → 3.0.0-alpha.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.
Files changed (183) hide show
  1. package/dist/LiFi.d.ts +69 -62
  2. package/dist/LiFi.js +71 -60
  3. package/dist/allowance/getAllowance.d.ts +6 -0
  4. package/dist/allowance/getAllowance.js +78 -0
  5. package/dist/allowance/index.d.ts +3 -2
  6. package/dist/allowance/index.js +3 -2
  7. package/dist/allowance/setAllowance.d.ts +5 -0
  8. package/dist/allowance/setAllowance.js +50 -0
  9. package/dist/allowance/types.d.ts +27 -0
  10. package/dist/allowance/types.js +1 -0
  11. package/dist/balance/getBalance.d.ts +2 -0
  12. package/dist/balance/getBalance.js +93 -0
  13. package/dist/balance/getTokenBalance.d.ts +2 -2
  14. package/dist/balance/getTokenBalance.js +4 -4
  15. package/dist/balance/index.d.ts +1 -12
  16. package/dist/balance/index.js +1 -9
  17. package/dist/cjs/LiFi.d.ts +69 -62
  18. package/dist/cjs/LiFi.js +69 -58
  19. package/dist/cjs/allowance/getAllowance.d.ts +6 -0
  20. package/dist/cjs/allowance/getAllowance.js +85 -0
  21. package/dist/cjs/allowance/index.d.ts +3 -2
  22. package/dist/cjs/allowance/index.js +3 -2
  23. package/dist/cjs/allowance/setAllowance.d.ts +5 -0
  24. package/dist/cjs/allowance/setAllowance.js +56 -0
  25. package/dist/cjs/allowance/types.d.ts +27 -0
  26. package/dist/cjs/allowance/types.js +2 -0
  27. package/dist/cjs/balance/getBalance.d.ts +2 -0
  28. package/dist/cjs/balance/getBalance.js +97 -0
  29. package/dist/cjs/balance/getTokenBalance.d.ts +2 -2
  30. package/dist/cjs/balance/getTokenBalance.js +6 -9
  31. package/dist/cjs/balance/index.d.ts +1 -12
  32. package/dist/cjs/balance/index.js +1 -9
  33. package/dist/cjs/connectors.d.ts +7 -5
  34. package/dist/cjs/connectors.js +45 -37
  35. package/dist/cjs/constants.d.ts +6 -0
  36. package/dist/cjs/constants.js +9 -0
  37. package/dist/cjs/execution/RouteExecutionManager.d.ts +24 -30
  38. package/dist/cjs/execution/RouteExecutionManager.js +27 -45
  39. package/dist/cjs/execution/StatusManager.d.ts +22 -23
  40. package/dist/cjs/execution/StatusManager.js +21 -22
  41. package/dist/cjs/execution/StepExecutionManager.d.ts +3 -3
  42. package/dist/cjs/execution/StepExecutionManager.js +118 -125
  43. package/dist/cjs/execution/StepExecutor.d.ts +4 -4
  44. package/dist/cjs/execution/StepExecutor.js +6 -6
  45. package/dist/cjs/execution/checkAllowance.d.ts +4 -0
  46. package/dist/cjs/execution/checkAllowance.js +77 -0
  47. package/dist/cjs/execution/checkBalance.d.ts +2 -0
  48. package/dist/cjs/execution/checkBalance.js +42 -0
  49. package/dist/cjs/execution/multisig.d.ts +4 -3
  50. package/dist/cjs/execution/multisig.js +9 -11
  51. package/dist/cjs/execution/prepareRestart.d.ts +3 -0
  52. package/dist/cjs/execution/prepareRestart.js +65 -0
  53. package/dist/cjs/execution/stepComparison.d.ts +3 -3
  54. package/dist/cjs/execution/stepComparison.js +2 -2
  55. package/dist/cjs/execution/switchChain.d.ts +13 -8
  56. package/dist/cjs/execution/switchChain.js +16 -11
  57. package/dist/cjs/execution/utils.d.ts +9 -3
  58. package/dist/cjs/execution/utils.js +19 -66
  59. package/dist/cjs/execution/waitForReceivingTransaction.d.ts +3 -0
  60. package/dist/cjs/execution/waitForReceivingTransaction.js +58 -0
  61. package/dist/cjs/helpers.d.ts +7 -17
  62. package/dist/cjs/helpers.js +6 -38
  63. package/dist/cjs/request.js +1 -1
  64. package/dist/cjs/services/ApiService.d.ts +2 -2
  65. package/dist/cjs/services/ApiService.js +2 -0
  66. package/dist/cjs/services/ChainsService.d.ts +1 -1
  67. package/dist/cjs/services/ConfigService.d.ts +1 -1
  68. package/dist/cjs/services/ConfigService.js +1 -2
  69. package/dist/cjs/typeguards.d.ts +1 -1
  70. package/dist/cjs/types/abi.d.ts +5 -0
  71. package/dist/cjs/types/abi.js +45 -0
  72. package/dist/cjs/types/index.d.ts +2 -2
  73. package/dist/cjs/types/index.js +3 -3
  74. package/dist/cjs/types/internal.types.d.ts +24 -30
  75. package/dist/cjs/utils/errors.d.ts +15 -14
  76. package/dist/cjs/utils/errors.js +38 -37
  77. package/dist/cjs/utils/getMaxPriorityFeePerGas.d.ts +2 -0
  78. package/dist/cjs/utils/getMaxPriorityFeePerGas.js +22 -0
  79. package/dist/cjs/utils/index.d.ts +5 -0
  80. package/dist/cjs/utils/index.js +21 -0
  81. package/dist/cjs/utils/median.d.ts +1 -0
  82. package/dist/cjs/utils/median.js +15 -0
  83. package/dist/cjs/utils/parseError.d.ts +4 -4
  84. package/dist/cjs/utils/parseError.js +41 -38
  85. package/dist/cjs/utils/utils.d.ts +1 -16
  86. package/dist/cjs/utils/utils.js +9 -78
  87. package/dist/cjs/version.d.ts +1 -1
  88. package/dist/cjs/version.js +1 -1
  89. package/dist/connectors.d.ts +7 -5
  90. package/dist/connectors.js +19 -35
  91. package/dist/constants.d.ts +6 -0
  92. package/dist/constants.js +6 -0
  93. package/dist/execution/RouteExecutionManager.d.ts +24 -30
  94. package/dist/execution/RouteExecutionManager.js +27 -45
  95. package/dist/execution/StatusManager.d.ts +22 -23
  96. package/dist/execution/StatusManager.js +22 -23
  97. package/dist/execution/StepExecutionManager.d.ts +3 -3
  98. package/dist/execution/StepExecutionManager.js +116 -123
  99. package/dist/execution/StepExecutor.d.ts +4 -4
  100. package/dist/execution/StepExecutor.js +6 -6
  101. package/dist/execution/checkAllowance.d.ts +4 -0
  102. package/dist/execution/checkAllowance.js +73 -0
  103. package/dist/execution/checkBalance.d.ts +2 -0
  104. package/dist/execution/checkBalance.js +38 -0
  105. package/dist/execution/multisig.d.ts +4 -3
  106. package/dist/execution/multisig.js +10 -12
  107. package/dist/execution/prepareRestart.d.ts +3 -0
  108. package/dist/execution/prepareRestart.js +61 -0
  109. package/dist/execution/stepComparison.d.ts +3 -3
  110. package/dist/execution/stepComparison.js +3 -3
  111. package/dist/execution/switchChain.d.ts +13 -8
  112. package/dist/execution/switchChain.js +17 -12
  113. package/dist/execution/utils.d.ts +9 -3
  114. package/dist/execution/utils.js +18 -61
  115. package/dist/execution/waitForReceivingTransaction.d.ts +3 -0
  116. package/dist/execution/waitForReceivingTransaction.js +51 -0
  117. package/dist/helpers.d.ts +7 -17
  118. package/dist/helpers.js +5 -34
  119. package/dist/request.js +2 -2
  120. package/dist/services/ApiService.d.ts +2 -2
  121. package/dist/services/ApiService.js +2 -0
  122. package/dist/services/ChainsService.d.ts +1 -1
  123. package/dist/services/ConfigService.d.ts +1 -1
  124. package/dist/services/ConfigService.js +2 -3
  125. package/dist/typeguards.d.ts +1 -1
  126. package/dist/types/abi.d.ts +5 -0
  127. package/dist/types/abi.js +42 -0
  128. package/dist/types/index.d.ts +2 -2
  129. package/dist/types/index.js +2 -2
  130. package/dist/types/internal.types.d.ts +24 -30
  131. package/dist/utils/errors.d.ts +15 -14
  132. package/dist/utils/errors.js +36 -35
  133. package/dist/utils/getMaxPriorityFeePerGas.d.ts +2 -0
  134. package/dist/utils/getMaxPriorityFeePerGas.js +18 -0
  135. package/dist/utils/index.d.ts +5 -0
  136. package/dist/utils/index.js +5 -0
  137. package/dist/utils/median.d.ts +1 -0
  138. package/dist/utils/median.js +11 -0
  139. package/dist/utils/parseError.d.ts +4 -4
  140. package/dist/utils/parseError.js +42 -39
  141. package/dist/utils/utils.d.ts +1 -16
  142. package/dist/utils/utils.js +7 -69
  143. package/dist/version.d.ts +1 -1
  144. package/dist/version.js +1 -1
  145. package/package.json +28 -27
  146. package/dist/allowance/checkAllowance.d.ts +0 -4
  147. package/dist/allowance/checkAllowance.js +0 -96
  148. package/dist/allowance/tokenApproval.d.ts +0 -22
  149. package/dist/allowance/tokenApproval.js +0 -59
  150. package/dist/allowance/utils.d.ts +0 -15
  151. package/dist/allowance/utils.js +0 -77
  152. package/dist/balance/checkBalance.d.ts +0 -3
  153. package/dist/balance/checkBalance.js +0 -38
  154. package/dist/balance/utils.d.ts +0 -6
  155. package/dist/balance/utils.js +0 -143
  156. package/dist/cjs/allowance/checkAllowance.d.ts +0 -4
  157. package/dist/cjs/allowance/checkAllowance.js +0 -103
  158. package/dist/cjs/allowance/tokenApproval.d.ts +0 -22
  159. package/dist/cjs/allowance/tokenApproval.js +0 -69
  160. package/dist/cjs/allowance/utils.d.ts +0 -15
  161. package/dist/cjs/allowance/utils.js +0 -87
  162. package/dist/cjs/balance/checkBalance.d.ts +0 -3
  163. package/dist/cjs/balance/checkBalance.js +0 -45
  164. package/dist/cjs/balance/utils.d.ts +0 -6
  165. package/dist/cjs/balance/utils.js +0 -150
  166. package/dist/cjs/types/ERC20.d.ts +0 -22
  167. package/dist/cjs/types/ERC20.js +0 -53
  168. package/dist/cjs/utils/getProvider.d.ts +0 -3
  169. package/dist/cjs/utils/getProvider.js +0 -11
  170. package/dist/cjs/utils/multicall.d.ts +0 -10
  171. package/dist/cjs/utils/multicall.js +0 -68
  172. package/dist/cjs/utils/multicallAbi.json +0 -313
  173. package/dist/cjs/utils/preRestart.d.ts +0 -3
  174. package/dist/cjs/utils/preRestart.js +0 -56
  175. package/dist/types/ERC20.d.ts +0 -22
  176. package/dist/types/ERC20.js +0 -50
  177. package/dist/utils/getProvider.d.ts +0 -3
  178. package/dist/utils/getProvider.js +0 -7
  179. package/dist/utils/multicall.d.ts +0 -10
  180. package/dist/utils/multicall.js +0 -61
  181. package/dist/utils/multicallAbi.json +0 -313
  182. package/dist/utils/preRestart.d.ts +0 -3
  183. package/dist/utils/preRestart.js +0 -52
package/dist/cjs/LiFi.js CHANGED
@@ -42,40 +42,40 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
42
42
  super(configUpdate);
43
43
  /**
44
44
  * Get the current configuration of the SDK
45
- * @return {Config} - The config object
45
+ * @returns - The config object
46
46
  */
47
47
  this.getConfig = () => {
48
48
  return this.configService.getConfig();
49
49
  };
50
50
  /**
51
51
  * Get the SDK configuration after all setup calls are finished
52
- * @return {Promise<Config>} - The config object
52
+ * @returns - The config object
53
53
  */
54
54
  this.getConfigAsync = () => {
55
55
  return this.configService.getConfigAsync();
56
56
  };
57
57
  /**
58
58
  * Get an instance of a provider for a specific chain
59
- * @param {number} chainId - Id of the chain the provider is for
60
- * @param {boolean} archive - Whether to use an archive provider that is based on a default rpc or not. defaults to false
61
- * @return {FallbackProvider} The provider for the given chain
59
+ * @param chainId - Id of the chain the provider is for
60
+ * @returns The public client for the given chain
62
61
  */
63
- this.getRpcProvider = (chainId, archive = false) => {
64
- return (0, connectors_1.getRpcProvider)(chainId, archive);
62
+ this.getPublicClient = (chainId) => {
63
+ return (0, connectors_1.getPublicClient)(chainId);
65
64
  };
66
65
  /**
67
66
  * Set a new confuration for the SDK
68
- * @param {ConfigUpdate} configUpdate - An object containing the configuration fields that should be updated.
69
- * @return {Config} The renewed config object
67
+ * @param configUpdate - An object containing the configuration fields that should be updated.
68
+ * @returns The renewed config object
70
69
  */
71
70
  this.setConfig = (configUpdate) => {
72
71
  return this.configService.updateConfig(configUpdate);
73
72
  };
74
73
  /**
75
74
  * Get a set of current possibilities based on a request that specifies which chains, exchanges and bridges are preferred or unwanted.
76
- * @param {PossibilitiesRequest} request - Object defining preferences regarding chain, exchanges and bridges
77
- * @return {Promise<PossibilitiesResponse>} Object listing current possibilities for any-to-any cross-chain-swaps based on the provided preferences.
78
- * @throws {LifiError} Throws a LifiError if request fails.
75
+ * @param request - Object defining preferences regarding chain, exchanges and bridges
76
+ * @param options
77
+ * @returns Object listing current possibilities for any-to-any cross-chain-swaps based on the provided preferences.
78
+ * @throws {LiFiError} Throws a LiFiError if request fails.
79
79
  * @deprecated We don't want to support this endpoint anymore in the future. /chains, /tools, /connections, and /tokens should be used instead
80
80
  */
81
81
  this.getPossibilities = async (request, options) => {
@@ -83,40 +83,47 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
83
83
  };
84
84
  /**
85
85
  * Fetch information about a Token
86
- * @param {ChainKey | ChainId} chain - Id or key of the chain that contains the token
87
- * @param {string} token - Address or symbol of the token on the requested chain
88
- * @throws {LifiError} - Throws a LifiError if request fails
86
+ * @param chain - Id or key of the chain that contains the token
87
+ * @param token - Address or symbol of the token on the requested chain
88
+ * @param options
89
+ * @throws {LiFiError} - Throws a LiFiError if request fails
89
90
  */
90
91
  this.getToken = async (chain, token, options) => {
91
92
  return ApiService_1.default.getToken(chain, token, options);
92
93
  };
93
94
  /**
94
95
  * Get a quote for a token transfer
95
- * @param {QuoteRequest} request - The configuration of the requested quote
96
- * @throws {LifiError} - Throws a LifiError if request fails
96
+ * @param request - The configuration of the requested quote
97
+ * @param options
98
+ * @throws {LiFiError} - Throws a LiFiError if request fails
97
99
  */
98
100
  this.getQuote = async (request, options) => {
99
101
  return ApiService_1.default.getQuote(request, options);
100
102
  };
101
103
  /**
102
104
  * Get a quote for a destination contract call
103
- * @param {ContractCallQuoteRequest} request - The configuration of the requested destination call
104
- * @throws {LifiError} - Throws a LifiError if request fails
105
+ * @param request - The configuration of the requested destination call
106
+ * @param options
107
+ * @throws {LiFiError} - Throws a LiFiError if request fails
108
+ * @returns - Returns step.
105
109
  */
106
110
  this.getContractCallQuote = async (request, options) => {
107
111
  return ApiService_1.default.getContractCallQuote(request, options);
108
112
  };
109
113
  /**
110
114
  * Check the status of a transfer. For cross chain transfers, the "bridge" parameter is required.
111
- * @param {GetStatusRequest} request - Configuration of the requested status
112
- * @throws {LifiError} - Throws a LifiError if request fails
115
+ * @param request - Configuration of the requested status
116
+ * @param options - Rrquest options.
117
+ * @throws {LiFiError} - Throws a LiFiError if request fails
118
+ * @returns Returns status response.
113
119
  */
114
120
  this.getStatus = async (request, options) => {
115
121
  return ApiService_1.default.getStatus(request, options);
116
122
  };
117
123
  /**
118
124
  * Get the available tools to bridge and swap tokens.
119
- * @param {ToolsRequest?} request - The configuration of the requested tools
125
+ * @param request - The configuration of the requested tools
126
+ * @param options
120
127
  * @returns The tools that are available on the requested chains
121
128
  */
122
129
  this.getTools = async (request, options) => {
@@ -124,7 +131,8 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
124
131
  };
125
132
  /**
126
133
  * Get all known tokens.
127
- * @param {TokensRequest?} request - The configuration of the requested tokens
134
+ * @param request - The configuration of the requested tokens
135
+ * @param options
128
136
  * @returns The tokens that are available on the requested chains
129
137
  */
130
138
  this.getTokens = async (request, options) => {
@@ -132,43 +140,46 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
132
140
  };
133
141
  /**
134
142
  * Get all available chains
135
- * @return {Promise<Chain[]>} A list of all available chains
136
- * @throws {LifiError} Throws a LifiError if request fails.
143
+ * @returns A list of all available chains
144
+ * @throws {LiFiError} Throws a LiFiError if request fails.
137
145
  */
138
146
  this.getChains = async () => {
139
147
  return this.chainsService.getChains();
140
148
  };
141
149
  /**
142
150
  * Get a set of routes for a request that describes a transfer of tokens.
143
- * @param {RoutesRequest} request - A description of the transfer.
144
- * @return {Promise<RoutesResponse>} The resulting routes that can be used to realize the described transfer of tokens.
145
- * @throws {LifiError} Throws a LifiError if request fails.
151
+ * @param request - A description of the transfer.
152
+ * @param options
153
+ * @returns The resulting routes that can be used to realize the described transfer of tokens.
154
+ * @throws {LiFiError} Throws a LiFiError if request fails.
146
155
  */
147
156
  this.getRoutes = async (request, options) => {
148
157
  return ApiService_1.default.getRoutes(request, options);
149
158
  };
150
159
  /**
151
160
  * Get the transaction data for a single step of a route
152
- * @param {LifiStep} step - The step object.
153
- * @return {Promise<LifiStep>} The step populated with the transaction data.
154
- * @throws {LifiError} Throws a LifiError if request fails.
161
+ * @param step - The step object.
162
+ * @param options
163
+ * @returns The step populated with the transaction data.
164
+ * @throws {LiFiError} Throws a LiFiError if request fails.
155
165
  */
156
166
  this.getStepTransaction = async (step, options) => {
157
167
  return ApiService_1.default.getStepTransaction(step, options);
158
168
  };
159
169
  /**
160
170
  * Get gas recommendation for a certain chain
161
- * @param {GasRecommendationRequest} request - Configuration of the requested recommendation.
162
- * @throws {LifiError} Throws a LifiError if request fails.
171
+ * @param request - Configuration of the requested recommendation.
172
+ * @param options
173
+ * @throws {LiFiError} Throws a LiFiError if request fails.
163
174
  */
164
175
  this.getGasRecommendation = async (request, options) => {
165
176
  return ApiService_1.default.getGasRecommendation(request, options);
166
177
  };
167
178
  /**
168
179
  * Returns the balances of a specific token a wallet holds across all aggregated chains.
169
- * @param {string} walletAddress - A wallet address.
170
- * @param {Token} token - A Token object.
171
- * @return {Promise<TokenAmount | null>} An object containing the token and the amounts on different chains.
180
+ * @param walletAddress - A wallet address.
181
+ * @param token - A Token object.
182
+ * @returns An object containing the token and the amounts on different chains.
172
183
  * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
173
184
  */
174
185
  this.getTokenBalance = async (walletAddress, token) => {
@@ -182,9 +193,9 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
182
193
  };
183
194
  /**
184
195
  * Returns the balances for a list tokens a wallet holds across all aggregated chains.
185
- * @param {string} walletAddress - A wallet address.
186
- * @param {Token[]} tokens - A list of Token objects.
187
- * @return {Promise<TokenAmount[]>} A list of objects containing the tokens and the amounts on different chains.
196
+ * @param walletAddress - A wallet address.
197
+ * @param tokens - A list of Token objects.
198
+ * @returns A list of objects containing the tokens and the amounts on different chains.
188
199
  * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
189
200
  */
190
201
  this.getTokenBalances = async (walletAddress, tokens) => {
@@ -199,12 +210,12 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
199
210
  };
200
211
  /**
201
212
  * This method queries the balances of tokens for a specific list of chains for a given wallet.
202
- * @param {string} walletAddress - A walletaddress.
203
- * @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
204
- * @return {Promise<{ [chainId: number]: TokenAmount[] }>} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
213
+ * @param walletAddress - A walletaddress.
214
+ * @param tokensByChain - A list of Token objects organized by chain ids.
215
+ * @returns A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
205
216
  * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
206
217
  */
207
- this.getTokenBalancesForChains = async (walletAddress, tokensByChain) => {
218
+ this.getTokenBalancesByChain = async (walletAddress, tokensByChain) => {
208
219
  if (!walletAddress) {
209
220
  throw new errors_1.ValidationError('Missing walletAddress.');
210
221
  }
@@ -213,35 +224,35 @@ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
213
224
  if (invalidTokens.length) {
214
225
  throw new errors_1.ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
215
226
  }
216
- return balance.getTokenBalancesForChains(walletAddress, tokensByChain);
227
+ return balance.getTokenBalancesByChain(walletAddress, tokensByChain);
217
228
  };
218
229
  /**
219
- * Get the current approval for a certain token.
220
- * @param signer - The signer owning the token
230
+ * Get the current allowance for a certain token.
221
231
  * @param token - The token that should be checked
222
- * @param approvalAddress - The address that has be approved
232
+ * @param ownerAddress - The owner of the token
233
+ * @param spenderAddress - The spender address that has to be approved
223
234
  */
224
- this.getTokenApproval = async (signer, token, approvalAddress) => {
225
- return (0, allowance_1.getTokenApproval)(signer, token, approvalAddress);
235
+ this.getTokenAllowance = async (token, ownerAddress, spenderAddress) => {
236
+ return (0, allowance_1.getTokenAllowance)(token, ownerAddress, spenderAddress);
226
237
  };
227
238
  /**
228
- * Get the current approval for a list of token / approval address pairs.
229
- * @param signer - The signer owning the tokens
230
- * @param tokenData - A list of token and approval address pairs
239
+ * Get the current allowance for a list of token / spender address pairs.
240
+ * @param ownerAddress - The owner of the tokens
241
+ * @param tokens - A list of token and spender address pairs
231
242
  */
232
- this.bulkGetTokenApproval = async (signer, tokenData) => {
233
- return (0, allowance_1.bulkGetTokenApproval)(signer, tokenData);
243
+ this.getTokenAllowanceMulticall = async (ownerAddress, tokens) => {
244
+ return (0, allowance_1.getTokenAllowanceMulticall)(ownerAddress, tokens);
234
245
  };
235
246
  /**
236
247
  * Set approval for a certain token and amount.
237
- * @param { ApproveTokenRequest } request - The approval request
248
+ * @param request - The approval request
238
249
  */
239
- this.approveToken = (request) => {
240
- return (0, allowance_1.approveToken)(request);
250
+ this.setTokenApproval = (request) => {
251
+ return (0, allowance_1.setTokenAllowance)(request);
241
252
  };
242
253
  /**
243
254
  * Revoke approval for a certain token.
244
- * @param { RevokeApprovalRequest } request - The revoke request
255
+ * @param request - The revoke request
245
256
  */
246
257
  this.revokeTokenApproval = (request) => {
247
258
  return (0, allowance_1.revokeTokenApproval)(request);
@@ -0,0 +1,6 @@
1
+ import type { ChainId, Token } from '@lifi/types';
2
+ import type { TokenAllowance, TokenSpender, TokenSpenderAllowance } from './types';
3
+ export declare const getAllowance: (chainId: ChainId, tokenAddress: string, ownerAddress: string, spenderAddress: string) => Promise<bigint>;
4
+ export declare const getAllowanceMulticall: (chainId: ChainId, tokens: TokenSpender[], ownerAddress: string) => Promise<TokenSpenderAllowance[]>;
5
+ export declare const getTokenAllowance: (token: Token, ownerAddress: string, spenderAddress: string) => Promise<bigint | undefined>;
6
+ export declare const getTokenAllowanceMulticall: (ownerAddress: string, tokens: TokenSpender[]) => Promise<TokenAllowance[]>;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTokenAllowanceMulticall = exports.getTokenAllowance = exports.getAllowanceMulticall = exports.getAllowance = void 0;
4
+ const viem_1 = require("viem");
5
+ const connectors_1 = require("../connectors");
6
+ const constants_1 = require("../constants");
7
+ const types_1 = require("../types");
8
+ const utils_1 = require("../utils/utils");
9
+ const getAllowance = async (chainId, tokenAddress, ownerAddress, spenderAddress) => {
10
+ const client = await (0, connectors_1.getPublicClient)(chainId);
11
+ const contract = (0, viem_1.getContract)({
12
+ address: tokenAddress,
13
+ abi: types_1.allowanceAbi,
14
+ publicClient: client,
15
+ });
16
+ try {
17
+ const approved = (await contract.read.allowance([
18
+ ownerAddress,
19
+ spenderAddress,
20
+ ]));
21
+ return approved;
22
+ }
23
+ catch (e) {
24
+ return 0n;
25
+ }
26
+ };
27
+ exports.getAllowance = getAllowance;
28
+ const getAllowanceMulticall = async (chainId, tokens, ownerAddress) => {
29
+ if (!tokens.length) {
30
+ return [];
31
+ }
32
+ const multicallAddress = await (0, connectors_1.getMulticallAddress)(chainId);
33
+ if (!multicallAddress) {
34
+ throw new Error(`No multicall address configured for chainId ${chainId}.`);
35
+ }
36
+ const client = await (0, connectors_1.getPublicClient)(chainId);
37
+ const contracts = tokens.map((token) => ({
38
+ address: token.token.address,
39
+ abi: types_1.allowanceAbi,
40
+ functionName: 'allowance',
41
+ args: [ownerAddress, token.spenderAddress],
42
+ }));
43
+ const results = await client.multicall({
44
+ contracts,
45
+ multicallAddress: multicallAddress,
46
+ batchSize: constants_1.MulticallBatchSize,
47
+ });
48
+ if (!results.length) {
49
+ throw new Error(`Couldn't load allowance from chainId ${chainId} using multicall.`);
50
+ }
51
+ return tokens.map(({ token, spenderAddress }, i) => ({
52
+ token,
53
+ spenderAddress,
54
+ allowance: results[i].result,
55
+ }));
56
+ };
57
+ exports.getAllowanceMulticall = getAllowanceMulticall;
58
+ const getTokenAllowance = async (token, ownerAddress, spenderAddress) => {
59
+ // native token don't need approval
60
+ if ((0, utils_1.isNativeTokenAddress)(token.address)) {
61
+ return;
62
+ }
63
+ const approved = await (0, exports.getAllowance)(token.chainId, token.address, ownerAddress, spenderAddress);
64
+ return approved;
65
+ };
66
+ exports.getTokenAllowance = getTokenAllowance;
67
+ const getTokenAllowanceMulticall = async (ownerAddress, tokens) => {
68
+ // filter out native tokens
69
+ const filteredTokens = tokens.filter(({ token }) => !(0, utils_1.isNativeTokenAddress)(token.address));
70
+ // group by chain
71
+ const tokenDataByChain = {};
72
+ filteredTokens.forEach((data) => {
73
+ if (!tokenDataByChain[data.token.chainId]) {
74
+ tokenDataByChain[data.token.chainId] = [];
75
+ }
76
+ tokenDataByChain[data.token.chainId].push(data);
77
+ });
78
+ const chainKeys = Object.keys(tokenDataByChain).map(Number.parseInt);
79
+ const allowances = (await Promise.all(chainKeys.map(async (chainId) => {
80
+ // get allowances for current chain and token list
81
+ return (0, exports.getAllowanceMulticall)(chainId, tokenDataByChain[chainId], ownerAddress);
82
+ }))).flat();
83
+ return allowances;
84
+ };
85
+ exports.getTokenAllowanceMulticall = getTokenAllowanceMulticall;
@@ -1,2 +1,3 @@
1
- export * from './checkAllowance';
2
- export * from './tokenApproval';
1
+ export * from './getAllowance';
2
+ export * from './setAllowance';
3
+ export * from './types';
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./checkAllowance"), exports);
18
- __exportStar(require("./tokenApproval"), exports);
17
+ __exportStar(require("./getAllowance"), exports);
18
+ __exportStar(require("./setAllowance"), exports);
19
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,5 @@
1
+ import type { Hash, WalletClient } from 'viem';
2
+ import type { ApproveTokenRequest, RevokeApprovalRequest } from './types';
3
+ export declare const setAllowance: (walletClient: WalletClient, tokenAddress: string, contractAddress: string, amount: bigint, returnPopulatedTransaction?: boolean) => Promise<Hash>;
4
+ export declare const setTokenAllowance: ({ walletClient, token, spenderAddress, amount, infiniteApproval, }: ApproveTokenRequest) => Promise<Hash | undefined>;
5
+ export declare const revokeTokenApproval: ({ walletClient, token, spenderAddress, }: RevokeApprovalRequest) => Promise<Hash | undefined>;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.revokeTokenApproval = exports.setTokenAllowance = exports.setAllowance = void 0;
4
+ const viem_1 = require("viem");
5
+ const types_1 = require("../types");
6
+ const utils_1 = require("../utils");
7
+ const utils_2 = require("../utils/utils");
8
+ const getAllowance_1 = require("./getAllowance");
9
+ const setAllowance = async (walletClient, tokenAddress, contractAddress, amount, returnPopulatedTransaction) => {
10
+ const data = (0, viem_1.encodeFunctionData)({
11
+ abi: types_1.approveAbi,
12
+ functionName: 'approve',
13
+ args: [contractAddress, amount],
14
+ });
15
+ if (returnPopulatedTransaction) {
16
+ return data;
17
+ }
18
+ const client = walletClient.extend(viem_1.publicActions);
19
+ let maxPriorityFeePerGas;
20
+ if (walletClient.account?.type === 'local') {
21
+ maxPriorityFeePerGas = await (0, utils_1.getMaxPriorityFeePerGas)(client);
22
+ }
23
+ return client.sendTransaction({
24
+ to: tokenAddress,
25
+ account: walletClient.account,
26
+ data,
27
+ maxPriorityFeePerGas,
28
+ chain: null,
29
+ });
30
+ };
31
+ exports.setAllowance = setAllowance;
32
+ const setTokenAllowance = async ({ walletClient, token, spenderAddress, amount, infiniteApproval = false, }) => {
33
+ // native token don't need approval
34
+ if ((0, utils_2.isNativeTokenAddress)(token.address)) {
35
+ return;
36
+ }
37
+ const approvedAmount = await (0, getAllowance_1.getAllowance)(token.chainId, token.address, walletClient.account.address, spenderAddress);
38
+ if (amount > approvedAmount) {
39
+ const approvalAmount = infiniteApproval ? viem_1.maxUint256 : amount;
40
+ const approveTx = await (0, exports.setAllowance)(walletClient, token.address, spenderAddress, approvalAmount);
41
+ return approveTx;
42
+ }
43
+ };
44
+ exports.setTokenAllowance = setTokenAllowance;
45
+ const revokeTokenApproval = async ({ walletClient, token, spenderAddress, }) => {
46
+ // native token don't need approval
47
+ if ((0, utils_2.isNativeTokenAddress)(token.address)) {
48
+ return;
49
+ }
50
+ const approvedAmount = await (0, getAllowance_1.getAllowance)(token.chainId, token.address, walletClient.account.address, spenderAddress);
51
+ if (approvedAmount > 0) {
52
+ const approveTx = await (0, exports.setAllowance)(walletClient, token.address, spenderAddress, 0n);
53
+ return approveTx;
54
+ }
55
+ };
56
+ exports.revokeTokenApproval = revokeTokenApproval;
@@ -0,0 +1,27 @@
1
+ import type { Token } from '@lifi/types';
2
+ import type { WalletClient } from 'viem';
3
+ export type TokenSpender = {
4
+ token: Token;
5
+ spenderAddress: string;
6
+ };
7
+ export type TokenAllowance = {
8
+ token: Token;
9
+ allowance?: bigint;
10
+ };
11
+ export type TokenSpenderAllowance = {
12
+ token: Token;
13
+ spenderAddress: string;
14
+ allowance?: bigint;
15
+ };
16
+ export interface ApproveTokenRequest {
17
+ walletClient: WalletClient;
18
+ token: Token;
19
+ spenderAddress: string;
20
+ amount: bigint;
21
+ infiniteApproval?: boolean;
22
+ }
23
+ export interface RevokeApprovalRequest {
24
+ walletClient: WalletClient;
25
+ token: Token;
26
+ spenderAddress: string;
27
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import type { Token, TokenAmount } from '@lifi/types';
2
+ export declare const getBalance: (walletAddress: string, tokens: Token[]) => Promise<TokenAmount[]>;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBalance = void 0;
4
+ const connectors_1 = require("../connectors");
5
+ const constants_1 = require("../constants");
6
+ const types_1 = require("../types");
7
+ const utils_1 = require("../utils/utils");
8
+ const getBalance = async (walletAddress, tokens) => {
9
+ if (tokens.length === 0) {
10
+ return [];
11
+ }
12
+ const { chainId } = tokens[0];
13
+ tokens.forEach((token) => {
14
+ if (token.chainId !== chainId) {
15
+ console.warn(`Requested tokens have to be on the same chain.`);
16
+ return [];
17
+ }
18
+ });
19
+ const multicallAddress = await (0, connectors_1.getMulticallAddress)(chainId);
20
+ if (multicallAddress && tokens.length > 1) {
21
+ return getBalanceMulticall(chainId, tokens, walletAddress, multicallAddress);
22
+ }
23
+ else {
24
+ return getBalanceDefault(chainId, tokens, walletAddress);
25
+ }
26
+ };
27
+ exports.getBalance = getBalance;
28
+ const getBalanceMulticall = async (chainId, tokens, walletAddress, multicallAddress) => {
29
+ const client = await (0, connectors_1.getPublicClient)(chainId);
30
+ const contracts = tokens.map((token) => {
31
+ if ((0, utils_1.isZeroAddress)(token.address)) {
32
+ return {
33
+ address: multicallAddress,
34
+ abi: types_1.getEthBalanceAbi,
35
+ functionName: 'getEthBalance',
36
+ args: [walletAddress],
37
+ };
38
+ }
39
+ return {
40
+ address: token.address,
41
+ abi: types_1.balanceOfAbi,
42
+ functionName: 'balanceOf',
43
+ args: [walletAddress],
44
+ };
45
+ });
46
+ const blockNumber = await client.getBlockNumber();
47
+ const results = await client.multicall({
48
+ contracts,
49
+ multicallAddress: multicallAddress,
50
+ blockNumber,
51
+ batchSize: constants_1.MulticallBatchSize,
52
+ });
53
+ if (!results.length) {
54
+ return [];
55
+ }
56
+ return tokens.map((token, i) => {
57
+ return {
58
+ ...token,
59
+ amount: results[i].result,
60
+ blockNumber,
61
+ };
62
+ });
63
+ };
64
+ const getBalanceDefault = async (chainId, tokens, walletAddress) => {
65
+ const client = await (0, connectors_1.getPublicClient)(chainId);
66
+ const blockNumber = await client.getBlockNumber();
67
+ const queue = tokens.map((token) => {
68
+ if ((0, utils_1.isZeroAddress)(token.address)) {
69
+ return client.getBalance({
70
+ address: walletAddress,
71
+ });
72
+ }
73
+ return client.readContract({
74
+ address: token.address,
75
+ abi: types_1.balanceOfAbi,
76
+ functionName: 'balanceOf',
77
+ args: [walletAddress],
78
+ });
79
+ });
80
+ const results = await Promise.allSettled(queue);
81
+ const tokenAmounts = tokens.map((token, index) => {
82
+ const result = results[index];
83
+ if (result.status === 'rejected') {
84
+ console.warn(result.reason);
85
+ return {
86
+ ...token,
87
+ blockNumber,
88
+ };
89
+ }
90
+ return {
91
+ ...token,
92
+ amount: result.value,
93
+ blockNumber,
94
+ };
95
+ });
96
+ return tokenAmounts;
97
+ };
@@ -1,7 +1,7 @@
1
- import { Token, TokenAmount } from '@lifi/types';
1
+ import type { Token, TokenAmount } from '@lifi/types';
2
2
  export declare const getTokenBalance: (walletAddress: string, token: Token) => Promise<TokenAmount | null>;
3
3
  export declare const getTokenBalances: (walletAddress: string, tokens: Token[]) => Promise<TokenAmount[]>;
4
- export declare const getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
4
+ export declare const getTokenBalancesByChain: (walletAddress: string, tokensByChain: {
5
5
  [chainId: number]: Token[];
6
6
  }) => Promise<{
7
7
  [chainId: number]: TokenAmount[];
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getTokenBalancesForChains = exports.getTokenBalances = exports.getTokenBalance = void 0;
7
- const utils_1 = __importDefault(require("./utils"));
3
+ exports.getTokenBalancesByChain = exports.getTokenBalances = exports.getTokenBalance = void 0;
4
+ const getBalance_1 = require("./getBalance");
8
5
  const getTokenBalance = async (walletAddress, token) => {
9
6
  const tokenAmounts = await (0, exports.getTokenBalances)(walletAddress, [token]);
10
7
  return tokenAmounts.length ? tokenAmounts[0] : null;
@@ -19,18 +16,18 @@ const getTokenBalances = async (walletAddress, tokens) => {
19
16
  }
20
17
  tokensByChain[token.chainId].push(token);
21
18
  });
22
- const tokenAmountsByChain = await (0, exports.getTokenBalancesForChains)(walletAddress, tokensByChain);
19
+ const tokenAmountsByChain = await (0, exports.getTokenBalancesByChain)(walletAddress, tokensByChain);
23
20
  return Object.values(tokenAmountsByChain).flat();
24
21
  };
25
22
  exports.getTokenBalances = getTokenBalances;
26
- const getTokenBalancesForChains = async (walletAddress, tokensByChain) => {
23
+ const getTokenBalancesByChain = async (walletAddress, tokensByChain) => {
27
24
  const tokenAmountsByChain = {};
28
25
  const promises = Object.keys(tokensByChain).map(async (chainIdStr) => {
29
26
  const chainId = parseInt(chainIdStr);
30
- const tokenAmounts = await utils_1.default.getBalances(walletAddress, tokensByChain[chainId]);
27
+ const tokenAmounts = await (0, getBalance_1.getBalance)(walletAddress, tokensByChain[chainId]);
31
28
  tokenAmountsByChain[chainId] = tokenAmounts;
32
29
  });
33
30
  await Promise.allSettled(promises);
34
31
  return tokenAmountsByChain;
35
32
  };
36
- exports.getTokenBalancesForChains = getTokenBalancesForChains;
33
+ exports.getTokenBalancesByChain = getTokenBalancesByChain;
@@ -1,13 +1,2 @@
1
- export * from './checkBalance';
1
+ export * from './getBalance';
2
2
  export * from './getTokenBalance';
3
- declare const _default: {
4
- checkBalance: (signer: import("ethers").Signer, step: import("@lifi/types").LifiStep, depth?: number) => Promise<void>;
5
- getTokenBalance: (walletAddress: string, token: import("@lifi/types").Token) => Promise<import("@lifi/types").TokenAmount | null>;
6
- getTokenBalances: (walletAddress: string, tokens: import("@lifi/types").Token[]) => Promise<import("@lifi/types").TokenAmount[]>;
7
- getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
8
- [chainId: number]: import("@lifi/types").Token[];
9
- }) => Promise<{
10
- [chainId: number]: import("@lifi/types").TokenAmount[];
11
- }>;
12
- };
13
- export default _default;
@@ -14,13 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./checkBalance"), exports);
17
+ __exportStar(require("./getBalance"), exports);
18
18
  __exportStar(require("./getTokenBalance"), exports);
19
- const checkBalance_1 = require("./checkBalance");
20
- const getTokenBalance_1 = require("./getTokenBalance");
21
- exports.default = {
22
- checkBalance: checkBalance_1.checkBalance,
23
- getTokenBalance: getTokenBalance_1.getTokenBalance,
24
- getTokenBalances: getTokenBalance_1.getTokenBalances,
25
- getTokenBalancesForChains: getTokenBalance_1.getTokenBalancesForChains,
26
- };