Skip to content

Commit

Permalink
feat: Investigation into optimising fee option rpc calls (#1751)
Browse files Browse the repository at this point in the history
  • Loading branch information
shirren committed May 10, 2024
1 parent cd3e050 commit 48af142
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/passport/sdk/src/zkEvm/sendTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BigNumber, utils } from 'ethers';
import { BigNumber } from 'ethers';
import {
StaticJsonRpcProvider,
TransactionRequest,
} from '@ethersproject/providers';
import { Flow } from '@imtbl/metrics';
import { Signer } from '@ethersproject/abstract-signer';
import {
digestOfTransactionsAndNonce,
digestOfTransactions,
encodeMessageSubDigest,
getEip155ChainId,
getNonce,
Expand Down Expand Up @@ -38,15 +38,11 @@ const getFeeOption = async (
walletAddress: string,
relayerClient: RelayerClient,
): Promise<FeeOption> => {
const nonce = metaTransaction.nonce || BigNumber.from(0); // NOTE: We don't need a valid nonce to estimate the fee
const normalisedMetaTransaction = getNormalisedTransactions([metaTransaction]);

const digest = digestOfTransactionsAndNonce(nonce, normalisedMetaTransaction);
const digest = digestOfTransactions(normalisedMetaTransaction);
const completePayload = encodeMessageSubDigest(chainId, walletAddress, digest);
const feeOptions = await relayerClient.imGetFeeOptions(walletAddress, completePayload);

const hash = utils.keccak256(completePayload);

const feeOptions = await relayerClient.imGetFeeOptions(walletAddress, hash);
const imxFeeOption = feeOptions.find((feeOption) => feeOption.tokenSymbol === 'IMX');
if (!imxFeeOption) {
throw new Error('Failed to retrieve fees for IMX token');
Expand Down
8 changes: 8 additions & 0 deletions packages/passport/sdk/src/zkEvm/walletHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export function digestOfTransactionsAndNonce(nonce: BigNumberish, normalisedTran
return utils.keccak256(packMetaTransactionsNonceData);
}

export function digestOfTransactions(normalisedTransactions: MetaTransactionNormalised[]): string {
const packMetaTransactionsNonceData = utils.defaultAbiCoder.encode(
[META_TRANSACTIONS_TYPE],
[normalisedTransactions],
);
return utils.keccak256(packMetaTransactionsNonceData);
}

export const getNonce = async (
rpcProvider: StaticJsonRpcProvider,
smartContractWalletAddress: string,
Expand Down

0 comments on commit 48af142

Please sign in to comment.