Skip to content

Commit

Permalink
feat: estimate approve for erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeday committed Nov 26, 2024
1 parent 589ead5 commit 727614c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/sdk/src/erc20/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ export abstract class AbstractLidoSDKErc20 extends LidoSDKModule {
: contract.simulate.transfer([to, amount], { account });
}

@Logger('Utils:')
@ErrorHandler()
public async estimateTransfer(props: NoCallback<TransferProps>) {
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()
Expand Down Expand Up @@ -224,6 +239,16 @@ export abstract class AbstractLidoSDKErc20 extends LidoSDKModule {
});
}

@Logger('Utils:')
@ErrorHandler()
public async estimateApprove(props: NoCallback<ApproveProps>) {
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,
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk/src/l2/l2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ export class LidoSDKL2 extends LidoSDKModule {
});
}

@Logger('Call:')
@ErrorHandler()
public async approveWstethForWrapEstimateGas(
props: WrapProps,
): Promise<bigint> {
const stethAddress = await this.contractAddress();
return this.wsteth.estimateApprove({
...props,
amount: props.value,
to: stethAddress,
});
}

@Logger('Utils:')
@ErrorHandler()
public async getWstethForWrapAllowance(
Expand Down
30 changes: 29 additions & 1 deletion packages/sdk/tests/utils/expect/expect-erc20-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -140,6 +143,16 @@ export const expectERC20Wallet = <I extends AbstractLidoSDKErc20>({
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();
Expand Down Expand Up @@ -268,6 +281,21 @@ export const expectERC20Wallet = <I extends AbstractLidoSDKErc20>({
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);
});
});

/**
Expand Down

0 comments on commit 727614c

Please sign in to comment.