|
1 | | -import {Contract, ethers} from 'ethers'; |
| 1 | +import {Contract, ethers, TransactionLike} from 'ethers'; |
2 | 2 | import {Contract as MulticallContract} from "@curvefi/ethcall"; |
3 | 3 | import BigNumber from 'bignumber.js'; |
4 | 4 | import { |
@@ -310,6 +310,32 @@ export async function ensureAllowance(this: Curve, coins: string[], amounts: (nu |
310 | 310 | return await _ensureAllowance.call(this, coinAddresses, _amounts, spender, isMax) |
311 | 311 | } |
312 | 312 |
|
| 313 | +export async function populateApprove(this: Curve, coins: string[], amounts: (number | string)[], spender: string, isMax = true): Promise<TransactionLike[]> { |
| 314 | + const coinAddresses = _getCoinAddresses.call(this, coins); |
| 315 | + const decimals = _getCoinDecimals.call(this, coinAddresses); |
| 316 | + const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i])); |
| 317 | + |
| 318 | + const address = this.signerAddress; |
| 319 | + const allowance = await _getAllowance.call(this, coinAddresses, address, spender); |
| 320 | + |
| 321 | + const transactions: TransactionLike[] = []; |
| 322 | + |
| 323 | + for (let i = 0; i < allowance.length; i++) { |
| 324 | + if (allowance[i] < _amounts[i]) { |
| 325 | + const contract = this.contracts[coinAddresses[i]].contract; |
| 326 | + const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i]; |
| 327 | + |
| 328 | + if (allowance[i] > parseUnits("0")) { |
| 329 | + transactions.push(await contract.approve.populateTransaction(spender, parseUnits("0"))); |
| 330 | + } |
| 331 | + |
| 332 | + transactions.push(await contract.approve.populateTransaction(spender, _approveAmount)); |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + return transactions; |
| 337 | +} |
| 338 | + |
313 | 339 | export function getPoolIdBySwapAddress(this: Curve, swapAddress: string): string { |
314 | 340 | const poolsData = this.getPoolsData(); |
315 | 341 | const poolIds = Object.entries(poolsData).filter(([, poolData]) => poolData.swap_address.toLowerCase() === swapAddress.toLowerCase()); |
|
0 commit comments