From 727614c271b28a7039e90227c35aa1b8d4da377a Mon Sep 17 00:00:00 2001 From: Evgeny Taktarov Date: Tue, 26 Nov 2024 17:45:12 +0700 Subject: [PATCH] feat: estimate approve for erc20 --- packages/sdk/src/erc20/erc20.ts | 25 ++++++++++++++++ packages/sdk/src/l2/l2.ts | 13 ++++++++ .../tests/utils/expect/expect-erc20-wallet.ts | 30 ++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/erc20/erc20.ts b/packages/sdk/src/erc20/erc20.ts index 52f0c58..6817364 100644 --- a/packages/sdk/src/erc20/erc20.ts +++ b/packages/sdk/src/erc20/erc20.ts @@ -127,6 +127,21 @@ export abstract class AbstractLidoSDKErc20 extends LidoSDKModule { : contract.simulate.transfer([to, amount], { account }); } + @Logger('Utils:') + @ErrorHandler() + public async estimateTransfer(props: NoCallback) { + const parsedProps = await this.parseProps(props); + const { account, amount, to, from = account.address } = parsedProps; + const isTransferFrom = from !== account.address; + + const contract = await this.getContract(); + return isTransferFrom + ? contract.estimateGas.transferFrom([from, to, amount], { + account, + }) + : contract.estimateGas.transfer([to, amount], { account }); + } + // PERMIT @Logger('Permit:') @ErrorHandler() @@ -224,6 +239,16 @@ export abstract class AbstractLidoSDKErc20 extends LidoSDKModule { }); } + @Logger('Utils:') + @ErrorHandler() + public async estimateApprove(props: NoCallback) { + const { account, amount, to } = await this.parseProps(props); + const contract = await this.getContract(); + return contract.estimateGas.approve([to, amount], { + account, + }); + } + @Logger('Views:') public async allowance({ account: accountProp, diff --git a/packages/sdk/src/l2/l2.ts b/packages/sdk/src/l2/l2.ts index 85095aa..58d2876 100644 --- a/packages/sdk/src/l2/l2.ts +++ b/packages/sdk/src/l2/l2.ts @@ -94,6 +94,19 @@ export class LidoSDKL2 extends LidoSDKModule { }); } + @Logger('Call:') + @ErrorHandler() + public async approveWstethForWrapEstimateGas( + props: WrapProps, + ): Promise { + const stethAddress = await this.contractAddress(); + return this.wsteth.estimateApprove({ + ...props, + amount: props.value, + to: stethAddress, + }); + } + @Logger('Utils:') @ErrorHandler() public async getWstethForWrapAllowance( diff --git a/packages/sdk/tests/utils/expect/expect-erc20-wallet.ts b/packages/sdk/tests/utils/expect/expect-erc20-wallet.ts index 434b2fc..a0e7847 100644 --- a/packages/sdk/tests/utils/expect/expect-erc20-wallet.ts +++ b/packages/sdk/tests/utils/expect/expect-erc20-wallet.ts @@ -17,7 +17,10 @@ import { } from '../../../tests/utils/expect/expect-populated-tx.js'; import { erc20abi } from '../../../src/erc20/abi/erc20abi.js'; import { expectTxCallback } from '../../../tests/utils/expect/expect-tx-callback.js'; -import { expectAlmostEqualBn } from '../../../tests/utils/expect/expect-bn.js'; +import { + expectAlmostEqualBn, + expectPositiveBn, +} from '../../../tests/utils/expect/expect-bn.js'; import { SPENDING_TIMEOUT, testSpending, @@ -140,6 +143,16 @@ export const expectERC20Wallet = ({ expect(tx.from).toBe(account); }); + test('estimateApprove', async () => { + const { address: altAddress } = useAltAccount(); + const params = { + to: altAddress, + amount: 100n, + }; + const gas = await token.estimateApprove(params); + expectPositiveBn(gas); + }); + test('simulateApprove', async () => { const { address: altAddress } = useAltAccount(); const contractAddress = await getTokenAddress(); @@ -268,6 +281,21 @@ export const expectERC20Wallet = ({ expect(tx.request.args[1]).toBe(params.to); expect(tx.request.args[2]).toBe(params.amount); }); + + test('estimateTransfer', async () => { + const { address } = useAccount(); + const { address: altAddress } = useAltAccount(); + const params = { + account: altAddress, + to: altAddress, + from: address, + amount: 100n, + }; + + const gas = await token.estimateTransfer(params); + + expectPositiveBn(gas); + }); }); /**