UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

118 lines 4.47 kB
import { batchInstructions } from "../../../account/utils/batchInstructions.js"; import { resolveInstructions } from "../../../account/utils/resolveInstructions.js"; import { prepareInstructions } from "./getFusionQuote.js"; import { DEFAULT_GAS_LIMIT, getQuote } from "./getQuote.js"; /** * Gets a quote for an on-chain transaction from the MEE service. * This method is used when the payment token doesn't support ERC20Permit * or when a standard on-chain transaction is preferred. * * @param client - The base MEE client instance * @param parameters - Parameters for the quote request * @param parameters.trigger - Payment token and amount information * @param parameters.instructions - Array of transaction instructions to execute * @param [parameters.account] - Optional account to use (defaults to client.account) * * @returns Promise resolving to quote payload with trigger information * * @example * ```typescript * const quote = await getOnChainQuote(meeClient, { * instructions: [ * mcNexus.build({ * type: "default", * data: { * calls: [ * { * to: "0x0000000000000000000000000000000000000000", * gasLimit: 50000n, * value: 0n * } * ], * chainId: base.id * } * }) * ], * trigger: { * paymentToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC * amount: "1000000" // 1 USDC (6 decimals) * } * }); * ``` * * @throws Will throw an error if the token does not support ERC20Permit */ export const getOnChainQuote = async (client, parameters) => { const { account: account_ = client.account, trigger, cleanUps, instructions, gasLimit, ...rest } = parameters; const resolvedInstructions = await resolveInstructions(instructions); if (trigger.call) { const batchedInstructions = await batchInstructions({ account: account_, instructions: resolvedInstructions }); const quote = await getQuote(client, { path: "quote", eoa: account_.signer.address, instructions: batchedInstructions, gasLimit: gasLimit || DEFAULT_GAS_LIMIT, ...(cleanUps ? { cleanUps } : {}), ...rest }); return { quote, trigger }; } const sender = account_.signer.address; const scaAddress = account_.addressOn(trigger.chainId, true); // By default the trigger amount will be deposited to sca account. // if a custom recipient is defined ? It will deposit to the recipient address const recipient = trigger.recipientAddress || scaAddress; const { triggerGasLimit, triggerAmount, batchedInstructions } = await prepareInstructions(client, { resolvedInstructions, trigger, sender, scaAddress, recipient, account: account_ }); // It uses the same endpoint (path) for onchain and permit quotes, as currently // the fusion on-chain txn for erc-20 tokens will always be 'approve' and never 'transfer' // so the MEE Node endpoint can be the same for both // there is also just a 'quote' endpoint, which applies to non-fusion superTxns const quote = await getQuote(client, { path: "quote-permit", eoa: account_.signer.address, instructions: batchedInstructions, gasLimit: gasLimit || triggerGasLimit, ...(cleanUps ? { cleanUps } : {}), ...rest }); // For useMaxAvailableFunds case, fees will be taken from max available funds. // else it will be explicitly defined here let fees = trigger.useMaxAvailableFunds ? 0n : BigInt(quote.paymentInfo.tokenWeiAmount); if (rest.sponsorship) { // For sponsorship, user will never pay fee. So the trigger amount never include fees fees = 0n; } const amount = triggerAmount + fees; return { quote, trigger: { tokenAddress: trigger.tokenAddress, chainId: trigger.chainId, gasLimit: triggerGasLimit, amount, ...(trigger.approvalAmount ? { approvalAmount: trigger.approvalAmount } : {}), ...(trigger.recipientAddress ? { recipientAddress: trigger.recipientAddress } : {}) } }; }; export default getOnChainQuote; //# sourceMappingURL=getOnChainQuote.js.map