UNPKG

viem

Version:

TypeScript Interface for Ethereum

74 lines 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareTransactionRequest = void 0; const parseAccount_js_1 = require("../../accounts/utils/parseAccount.js"); const estimateFeesPerGas_js_1 = require("../../actions/public/estimateFeesPerGas.js"); const estimateGas_js_1 = require("../../actions/public/estimateGas.js"); const getBlock_js_1 = require("../../actions/public/getBlock.js"); const getTransactionCount_js_1 = require("../../actions/public/getTransactionCount.js"); const fee_js_1 = require("../../errors/fee.js"); const getAction_js_1 = require("../../utils/getAction.js"); const assertRequest_js_1 = require("../../utils/transaction/assertRequest.js"); const getTransactionType_js_1 = require("../../utils/transaction/getTransactionType.js"); async function prepareTransactionRequest(client, args) { const { account: account_ = client.account, chain, gas, nonce, parameters = ['fees', 'gas', 'nonce', 'type'], type, } = args; const account = account_ ? (0, parseAccount_js_1.parseAccount)(account_) : undefined; const block = await (0, getAction_js_1.getAction)(client, getBlock_js_1.getBlock, 'getBlock')({ blockTag: 'latest' }); const request = { ...args, ...(account ? { from: account?.address } : {}) }; if (parameters.includes('nonce') && typeof nonce === 'undefined' && account) request.nonce = await (0, getAction_js_1.getAction)(client, getTransactionCount_js_1.getTransactionCount, 'getTransactionCount')({ address: account.address, blockTag: 'pending', }); if ((parameters.includes('fees') || parameters.includes('type')) && typeof type === 'undefined') { try { request.type = (0, getTransactionType_js_1.getTransactionType)(request); } catch { request.type = typeof block.baseFeePerGas === 'bigint' ? 'eip1559' : 'legacy'; } } if (parameters.includes('fees')) { if (request.type === 'eip1559') { const { maxFeePerGas, maxPriorityFeePerGas } = await (0, estimateFeesPerGas_js_1.internal_estimateFeesPerGas)(client, { block: block, chain, request: request, }); if (typeof args.maxPriorityFeePerGas === 'undefined' && args.maxFeePerGas && args.maxFeePerGas < maxPriorityFeePerGas) throw new fee_js_1.MaxFeePerGasTooLowError({ maxPriorityFeePerGas, }); request.maxPriorityFeePerGas = maxPriorityFeePerGas; request.maxFeePerGas = maxFeePerGas; } else { if (typeof args.maxFeePerGas !== 'undefined' || typeof args.maxPriorityFeePerGas !== 'undefined') throw new fee_js_1.Eip1559FeesNotSupportedError(); const { gasPrice: gasPrice_ } = await (0, estimateFeesPerGas_js_1.internal_estimateFeesPerGas)(client, { block: block, chain, request: request, type: 'legacy', }); request.gasPrice = gasPrice_; } } if (parameters.includes('gas') && typeof gas === 'undefined') request.gas = await (0, getAction_js_1.getAction)(client, estimateGas_js_1.estimateGas, 'estimateGas')({ ...request, account: account ? { address: account.address, type: 'json-rpc' } : undefined, }); (0, assertRequest_js_1.assertRequest)(request); delete request.parameters; return request; } exports.prepareTransactionRequest = prepareTransactionRequest; //# sourceMappingURL=prepareTransactionRequest.js.map