UNPKG

@biconomy/abstractjs

Version:

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

82 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.signOnChainQuote = exports.ON_CHAIN_PREFIX = exports.FUSION_NATIVE_TRANSFER_PREFIX = void 0; const viem_1 = require("viem"); const viem_2 = require("viem"); const constants_1 = require("../../../constants/index.js"); const ForwarderAbi_1 = require("../../../constants/abi/ForwarderAbi.js"); exports.FUSION_NATIVE_TRANSFER_PREFIX = "0x150b7a02"; exports.ON_CHAIN_PREFIX = "0x177eee01"; const generateTriggerCallFromTrigger = async ({ account, trigger, recipient, scaAddress }) => { let triggerCall; if (trigger.call) { triggerCall = trigger.call; } else if (trigger.tokenAddress === viem_1.zeroAddress) { const forwardCalldata = (0, viem_2.encodeFunctionData)({ abi: ForwarderAbi_1.ForwarderAbi, functionName: "forward", args: [recipient] }); const [{ calls: [ethForwardCall] }] = await account.build({ type: "default", data: { calls: [ { to: constants_1.FORWARDER_ADDRESS, data: forwardCalldata, value: trigger.amount } ], chainId: trigger.chainId } }); triggerCall = ethForwardCall; } else { if (trigger.approvalAmount && trigger.amount !== undefined && trigger.approvalAmount < trigger.amount) { throw new Error(`Approval amount must be bigger or equal with the amount from the trigger (triggerAmount: ${trigger.amount} amount: ${trigger.approvalAmount})`); } const amount = trigger.approvalAmount ?? trigger.amount; const [{ calls: [approveCall] }] = await account.build({ type: "approve", data: { spender: scaAddress, tokenAddress: trigger.tokenAddress, chainId: trigger.chainId, amount } }); triggerCall = approveCall; } return triggerCall; }; const signOnChainQuote = async (client, params) => { const { confirmations = 2, companionAccount: account_ = client.account, fusionQuote: { quote, trigger } } = params; const chainId = trigger.chainId; const { chain, walletClient, address: scaAddress } = account_.deploymentOn(chainId, true); const recipient = trigger.recipientAddress || scaAddress; const triggerCall = await generateTriggerCallFromTrigger({ account: account_, trigger, recipient, scaAddress }); const dataOrPrefix = triggerCall?.data ?? exports.FUSION_NATIVE_TRANSFER_PREFIX; const call = { ...triggerCall, data: (0, viem_1.concatHex)([dataOrPrefix, quote.hash]) }; const hash = await walletClient.sendTransaction(call); await walletClient.waitForTransactionReceipt({ hash, confirmations }); const signature = (0, viem_1.concatHex)([ exports.ON_CHAIN_PREFIX, (0, viem_1.encodeAbiParameters)([{ type: "bytes32" }, { type: "uint256" }], [hash, BigInt(chain.id)]) ]); return { ...quote, signature }; }; exports.signOnChainQuote = signOnChainQuote; exports.default = exports.signOnChainQuote; //# sourceMappingURL=signOnChainQuote.js.map