Skip to content

Commit

Permalink
Merge pull request #174 from lidofinance/feature/si-1649-support-widg…
Browse files Browse the repository at this point in the history
…et-functional

Feature/si 1649 support widget functional
  • Loading branch information
Jeday authored Nov 20, 2024
2 parents 169fee9 + ede73df commit 80836e5
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 49 deletions.
7 changes: 4 additions & 3 deletions packages/sdk/src/core/__tests__/core-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
LidoSDKStake,
PerformTransactionGasLimit,
PerformTransactionSendTransaction,
TransactionCallback,
VIEM_CHAINS,
} from '../../index.js';
import {
Expand Down Expand Up @@ -177,7 +178,7 @@ describe('Account hoisting', () => {
});

test('useAccount requests account from web3Provider', async () => {
const mockFn = jest.fn();
const mockFn = jest.fn<TransactionCallback>();
const mockTransport = useMockTransport(async (args, originalRequest) => {
mockFn(args.method);
if (args.method === 'eth_requestAccounts') {
Expand Down Expand Up @@ -239,7 +240,7 @@ describe('Perform Transaction', () => {
(options) =>
rawContract.write.submit([zeroAddress], { ...options, value }),
);
const mockTxCallback = jest.fn();
const mockTxCallback = jest.fn<TransactionCallback>();

const txResult = await core.performTransaction({
getGasLimit: mockGetGasLimit,
Expand Down Expand Up @@ -303,7 +304,7 @@ describe('Perform Transaction', () => {
(options) =>
rawContract.write.submit([zeroAddress], { ...options, value }),
);
const mockTxCallback = jest.fn();
const mockTxCallback = jest.fn<TransactionCallback>();

const txResult = await core.performTransaction({
getGasLimit: mockGetGasLimit,
Expand Down
7 changes: 6 additions & 1 deletion packages/sdk/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,12 @@ export default class LidoSDKCore extends LidoSDKCacheable {
}
}

callback({ stage: TransactionCallbackStage.SIGN, payload: overrides.gas });
const customGas = callback({
stage: TransactionCallbackStage.SIGN,
payload: overrides.gas,
});

if (typeof customGas === 'bigint') overrides.gas = customGas;

const hash = await withSDKError(
sendTransaction({
Expand Down
10 changes: 9 additions & 1 deletion packages/sdk/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ export type TransactionCallbackProps =
| { stage: TransactionCallbackStage.MULTISIG_DONE; payload?: undefined }
| { stage: TransactionCallbackStage.ERROR; payload: SDKError };

export type TransactionCallback = (props: TransactionCallbackProps) => void;
export type TransactionCallbackResult<TProps> = TProps extends {
stage: TransactionCallbackStage.SIGN;
}
? bigint | undefined
: void;

export type TransactionCallback = (
props: TransactionCallbackProps,
) => TransactionCallbackResult<TransactionCallbackProps>;

export type PermitCallbackProps =
| { stage: TransactionCallbackStage.SIGN; payload?: undefined }
Expand Down
9 changes: 5 additions & 4 deletions packages/sdk/src/l2/__test__/l2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
expectPopulatedTx,
expectPopulatedTxToRun,
} from '../../../tests/utils/expect/expect-populated-tx.js';
import { TransactionCallback } from '../../core/types.js';

const prepareL2Wsteth = async () => {
const l2 = useL2();
Expand Down Expand Up @@ -121,7 +122,7 @@ describe('LidoSDKL2 wrap', () => {
beforeAll(prepareL2Wsteth);

testSpending('set allowance', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await l2.approveWstethForWrap({ value, callback: mock });
expectTxCallback(mock, tx);
await expect(l2.getWstethForWrapAllowance(account)).resolves.toEqual(value);
Expand Down Expand Up @@ -150,7 +151,7 @@ describe('LidoSDKL2 wrap', () => {
const stethValue = await l2.steth.convertToSteth(value);
const stethBalanceBefore = await l2.steth.balance(account.address);
const wstethBalanceBefore = await l2.wsteth.balance(account.address);
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await l2.wrapWstethToSteth({ value, callback: mock });
expectTxCallback(mock, tx);
const stethBalanceAfter = await l2.steth.balance(account.address);
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('LidoSDKL2 wrap', () => {
const stethValue = await l2.steth.convertToSteth(value);
const stethBalanceBefore = await l2.steth.balance(account.address);
const wstethBalanceBefore = await l2.wsteth.balance(account.address);
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await l2.unwrapStethToWsteth({
value: stethValue,
callback: mock,
Expand Down Expand Up @@ -324,7 +325,7 @@ describe('LidoSDKL2Steth shares', () => {
const amountSteth = await l2.steth.convertToSteth(amount);
const balanceStethBefore = await l2.steth.balance(account.address);
const balanceSharesBefore = await l2.steth.balanceShares(account.address);
const mockTxCallback = jest.fn();
const mockTxCallback = jest.fn<TransactionCallback>();

const tx = await l2.steth.transferShares({
amount,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/shares/__tests__/shares-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../../tests/utils/fixtures/use-wallet-client.js';
import { expectTxCallback } from '../../../tests/utils/expect/expect-tx-callback.js';
import { expectAlmostEqualBn } from '../../../tests/utils/expect/expect-bn.js';
import { TransactionCallback } from '../../core/types.js';

describe('LidoSDKStake wallet methods', () => {
const steth = useSteth();
Expand All @@ -25,7 +26,7 @@ describe('LidoSDKStake wallet methods', () => {
const amountSteth = await shares.convertToShares(amount);
const balanceStethBefore = await steth.balance(address);
const balanceSharesBefore = await shares.balance(address);
const mockTxCallback = jest.fn();
const mockTxCallback = jest.fn<TransactionCallback>();
const tx = await shares.transfer({
amount,
to: alt.address,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/stake/__tests__/stake-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { expectTxCallback } from '../../../tests/utils/expect/expect-tx-callback
import { expectAlmostEqualBn } from '../../../tests/utils/expect/expect-bn.js';
import { useSteth } from '../../../tests/utils/fixtures/use-steth.js';
import { useShares } from '../../../tests/utils/fixtures/use-shares.js';
import { TransactionCallback } from '../../core/types.js';

describe('LidoSDKStake wallet methods', () => {
const stake = useStake();
Expand All @@ -19,7 +20,7 @@ describe('LidoSDKStake wallet methods', () => {
const stakeValue = 100n;
const balanceBefore = await steth.balance();
const balanceSharesBefore = await shares.balance();
const mockTxCallback = jest.fn();
const mockTxCallback = jest.fn<TransactionCallback>();
const tx = await stake.stakeEth({
value: stakeValue,
callback: mockTxCallback,
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/stake/__tests__/stake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../../tests/utils/expect/expect-populated-tx.js';
import { useStake } from '../../../tests/utils/fixtures/use-stake.js';
import { expectSDKModule } from '../../../tests/utils/expect/expect-sdk-module.js';
import { expectPositiveBn } from '../../../tests/utils/expect/expect-bn.js';

describe('LidoSDKStake constructor', () => {
test('is correct module', () => {
Expand Down Expand Up @@ -36,6 +37,11 @@ describe('LidoSDKStake read methods', () => {
await expectPopulatedTxToRun(tx, stake.core.rpcProvider);
});

test('stakeEthEstimateGas', async () => {
const gas = await stake.stakeEthEstimateGas({ value: 100n });
expectPositiveBn(gas);
});

test('stakeEthSimulateTx', async () => {
const simulationResult = await stake.stakeEthSimulateTx({ value: 100n });
expect(simulationResult).toBeDefined();
Expand Down
17 changes: 9 additions & 8 deletions packages/sdk/src/stake/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
type PopulatedTransaction,
TransactionOptions,
} from '../core/index.js';
import type { NoCallback } from '../core/types.js';
import { ERROR_CODE, invariant } from '../common/utils/sdk-error.js';
import { Logger, Cache, ErrorHandler } from '../common/decorators/index.js';
import {
Expand Down Expand Up @@ -94,7 +95,7 @@ export class LidoSDKStake extends LidoSDKModule {
callback,
account,
getGasLimit: async (options) =>
this.submitGasLimit(value, referralAddress, options),
this.stakeEthEstimateGas({ value, account, referralAddress }, options),
sendTransaction: (options) =>
contract.write.submit([referralAddress], { ...options, value }),
decodeResult: async (receipt) => this.submitParseEvents(receipt, address),
Expand Down Expand Up @@ -146,15 +147,15 @@ export class LidoSDKStake extends LidoSDKModule {

@Logger('Utils:')
@Cache(30 * 1000, ['core.chain.id'])
private async submitGasLimit(
value: bigint,
referralAddress: Address,
options: TransactionOptions,
public async stakeEthEstimateGas(
props: NoCallback<StakeProps>,
options?: TransactionOptions,
): Promise<bigint> {
const { referralAddress, value, account } = await this.parseProps(props);
const contract = await this.getContractStETH();
const originalGasLimit = await contract.estimateGas.submit(
[referralAddress],
{ ...options, value },
{ account, ...options, value },
);

const gasLimit =
Expand Down Expand Up @@ -239,9 +240,9 @@ export class LidoSDKStake extends LidoSDKModule {
): Promise<PopulatedTransaction> {
const { referralAddress, value, account } = await this.parseProps(props);
const data = this.stakeEthEncodeData({ referralAddress });
const gas = await this.submitGasLimit(value, referralAddress, {
account,
const gas = await this.stakeEthEstimateGas(props, {
chain: this.core.chain,
account,
});
const address = await this.contractAddressStETH();
return {
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk/src/unsteth/__tests__/unsteth-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
testSpending,
} from '../../../tests/utils/test-spending.js';
import { useRpcCore } from '../../../tests/utils/fixtures/use-core.js';
import { LIDO_CONTRACT_NAMES } from '../../index.js';
import { LIDO_CONTRACT_NAMES, TransactionCallback } from '../../index.js';
import {
expectPopulatedTx,
expectPopulatedTxToRun,
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('unsteth wallet tests', () => {
});

testSpending('can transfer token', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await unsteth.transfer({
id: nftId,
to: altAccount.address,
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('unsteth wallet tests', () => {
});

testSpending('can approve single token', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await unsteth.setSingleTokenApproval({
id: nftId,
to: altAccount.address,
Expand All @@ -195,7 +195,7 @@ describe('unsteth wallet tests', () => {
});

testSpending('can revoke approve single token', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await unsteth.setSingleTokenApproval({
id: nftId,
callback: mock,
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('unsteth wallet tests', () => {
});

testSpending('can approve for all tokens', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await unsteth.setAllTokensApproval({
to: altAccount.address,
allow: true,
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('unsteth wallet tests', () => {
});

testSpending('can revoke approve for all tokens', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await unsteth.setAllTokensApproval({
to: altAccount.address,
allow: false,
Expand Down
12 changes: 10 additions & 2 deletions packages/sdk/src/withdraw/__test__/withdraw-claim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import {
} from '../../../tests/utils/expect/expect-populated-tx.js';
import { expectAddress } from '../../../tests/utils/expect/expect-address.js';
import { useRpcCore } from '../../../tests/utils/fixtures/use-core.js';
import { LIDO_CONTRACT_NAMES } from '../../index.js';
import { LIDO_CONTRACT_NAMES, TransactionCallback } from '../../index.js';
import { expectTxCallback } from '../../../tests/utils/expect/expect-tx-callback.js';
import { expectPositiveBn } from '../../../tests/utils/expect/expect-bn.js';

describe('withdraw request claim', () => {
const core = useRpcCore();
Expand Down Expand Up @@ -62,11 +63,18 @@ describe('withdraw request claim', () => {
expectAddress(tx.request.functionName, 'claimWithdrawals');
});

testSpending('can estimate claim', async () => {
const gasLimit = await claim.claimRequestsEstimateGas({
requestsIds: [request.id],
});
expectPositiveBn(gasLimit);
});

testSpending('can claim', async () => {
const balanceBefore = await core.balanceETH(address);
const owner = await unsteth.getAccountByNFT(request.id);
expectAddress(owner, address);
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await claim.claimRequests({
requestsIds: [request.id],
callback: mock,
Expand Down
29 changes: 25 additions & 4 deletions packages/sdk/src/withdraw/__test__/withdraw-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { useWrap } from '../../../tests/utils/fixtures/use-wrap.js';
import { useAccount } from '../../../tests/utils/fixtures/use-wallet-client.js';
import { expectAddress } from '../../../tests/utils/expect/expect-address.js';
import { useWeb3Core } from '../../../tests/utils/fixtures/use-core.js';
import { ERROR_CODE, PermitSignature } from '../../index.js';
import {
ERROR_CODE,
PermitSignature,
TransactionCallback,
} from '../../index.js';
import { useStake } from '../../../tests/utils/fixtures/use-stake.js';
import {
expectPopulatedTx,
Expand Down Expand Up @@ -96,10 +100,19 @@ const testWithdrawalsWithPermit = (
expectAddress(tx.request.address, wqAddress);
});

testSpending('can estimate request', async () => {
const gasLimit = await request.requestWithdrawalWithPermitEstimateGas({
permit,
token,
amount,
});
expectPositiveBn(gasLimit);
});

testSpending('can request withdrawals with permit', async () => {
const balanceBefore = await tokenContract.balance(address);
const nftsBefore = await unsteth.getNFTsByAccount(address);
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await request.requestWithdrawalWithPermit({
permit,
token,
Expand Down Expand Up @@ -215,7 +228,7 @@ const testWithdrawals = (token: WithdrawableTokens, ethAmount: bigint) => {
});

testSpending('can approve', async () => {
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await approval.approve({
token,
amount,
Expand Down Expand Up @@ -264,10 +277,18 @@ const testWithdrawals = (token: WithdrawableTokens, ethAmount: bigint) => {
expectAddress(tx.request.address, wqAddress);
});

testSpending('can estimate request', async () => {
const gasLimit = await request.requestWithdrawalEstimateGas({
token,
amount,
});
expectPositiveBn(gasLimit);
});

testSpending('can request withdrawals', async () => {
const balanceBefore = await tokenContract.balance(address);
const nftsBefore = await unsteth.getNFTsByAccount(address);
const mock = jest.fn();
const mock = jest.fn<TransactionCallback>();
const tx = await request.requestWithdrawal({
token,
amount,
Expand Down
16 changes: 16 additions & 0 deletions packages/sdk/src/withdraw/claim/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ export class LidoSDKWithdrawClaim extends BusModule {
});
}

@Logger('Views:')
@ErrorHandler()
public async claimRequestsEstimateGas(props: NoCallback<ClaimRequestsProps>) {
const account = await this.bus.core.useAccount(props.account);
const { requestsIds, hints } = await this.sortRequestsWithHints(
props.requestsIds,
props.hints,
);

const contract = await this.bus.contract.getContractWithdrawalQueue();

return contract.estimateGas.claimWithdrawals([requestsIds, hints], {
account,
});
}

@Logger('Views:')
@ErrorHandler()
public async claimRequestsSimulateTx(props: NoCallback<ClaimRequestsProps>) {
Expand Down
Loading

0 comments on commit 80836e5

Please sign in to comment.