UNPKG

@ledgerhq/coin-algorand

Version:
90 lines 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.craftTransaction = craftTransaction; exports.craftOptInTransaction = craftOptInTransaction; exports.craftApiTransaction = craftApiTransaction; const utils_1 = require("@ledgerhq/coin-module-framework/utils"); const algosdk_1 = require("algosdk"); const network_1 = require("../network"); const estimateFees_1 = require("./estimateFees"); /** * Craft an unsigned Algorand transaction * @param input - Transaction parameters * @returns Serialized unsigned transaction and payload */ async function craftTransaction(input) { const { sender, recipient, amount, memo, assetId, fees } = input; const note = memo ? new TextEncoder().encode(memo) : undefined; const params = await (0, network_1.getTransactionParams)(); if (typeof fees === "bigint") { params.fee = 0; params.minFee = Number(fees); } const suggestedParams = { ...params, firstValid: params.lastRound, lastValid: params.lastRound + 1000, genesisHash: (0, algosdk_1.base64ToBytes)(params.genesisHash), }; const algoTxn = assetId ? (0, algosdk_1.makeAssetTransferTxnWithSuggestedParamsFromObject)({ sender, receiver: recipient, amount: Number(amount), assetIndex: Number(assetId), suggestedParams, ...(note ? { note } : {}), }) : (0, algosdk_1.makePaymentTxnWithSuggestedParamsFromObject)({ sender, receiver: recipient, amount: Number(amount), suggestedParams, ...(note ? { note } : {}), }); const msgPackEncoded = (0, algosdk_1.encodeMsgpack)(algoTxn); const serializedTransaction = Buffer.from(msgPackEncoded).toString("hex"); return { serializedTransaction, txPayload: algoTxn, }; } /** * Craft an opt-in transaction for an ASA token * @param sender - The sender address (also recipient for opt-in) * @param assetId - The ASA token ID to opt into * @param fees - Optional fee override * @returns Serialized unsigned transaction */ async function craftOptInTransaction(sender, assetId, fees) { return craftTransaction({ sender, recipient: sender, // Opt-in sends to self amount: 0n, assetId, fees, }); } async function craftApiTransaction(transactionIntent, customFees) { if (!(0, utils_1.isSendTransactionIntent)(transactionIntent)) { throw new Error("Only send transaction intent is supported"); } const fees = customFees?.value ?? (await (0, estimateFees_1.estimateFees)()).value; const memo = transactionIntent.memo?.type === "string" ? transactionIntent.memo.value : undefined; const assetId = transactionIntent.asset.type === "asa" ? transactionIntent.asset.assetReference : undefined; const result = await craftTransaction({ sender: transactionIntent.sender, recipient: transactionIntent.recipient, amount: transactionIntent.amount, memo, assetId, fees, }); return { transaction: result.serializedTransaction, details: { txPayload: result.txPayload, }, }; } //# sourceMappingURL=craftTransaction.js.map