UNPKG

@ledgerhq/coin-aptos

Version:
71 lines 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isTokenType = void 0; const index_1 = require("@ledgerhq/coin-framework/account/index"); const constants_1 = require("../constants"); const normalizeTransactionOptions_1 = require("./normalizeTransactionOptions"); const buildTransaction = async (account, transaction, aptosClient, contractAddress, tokenType) => { const subAccount = (0, index_1.findSubAccountById)(account, transaction.subAccountId ?? ""); const payloadContracAddress = subAccount ? subAccount.token.contractAddress : contractAddress; const payloadTokenType = subAccount?.token?.tokenType ?? tokenType; const txPayload = getPayload({ transaction, tokenType: payloadTokenType, contractAddress: payloadContracAddress, }); const txOptions = (0, normalizeTransactionOptions_1.normalizeTransactionOptions)(transaction.options); const tx = await aptosClient.generateTransaction(account.freshAddress, txPayload, txOptions); return tx; }; const getPayload = ({ transaction, tokenType, contractAddress, }) => { const { amount, recipient, mode } = transaction; switch (mode) { case "stake": return { function: "0x1::delegation_pool::add_stake", typeArguments: [], functionArguments: [recipient, amount.toString()], }; case "unstake": return { function: "0x1::delegation_pool::unlock", typeArguments: [], functionArguments: [recipient, amount.toString()], }; case "restake": return { function: "0x1::delegation_pool::reactivate_stake", typeArguments: [], functionArguments: [recipient, amount.toString()], }; case "withdraw": return { function: "0x1::delegation_pool::withdraw", typeArguments: [], functionArguments: [recipient, amount.toString()], }; case "send": if (tokenType !== undefined && !(0, exports.isTokenType)(tokenType)) { throw new Error(`Token type ${tokenType} not supported`); } if (tokenType === constants_1.TOKEN_TYPE.FUNGIBLE_ASSET) { return { function: "0x1::primary_fungible_store::transfer", typeArguments: ["0x1::fungible_asset::Metadata"], functionArguments: [contractAddress, recipient, amount.toString()], }; } } return { function: "0x1::aptos_account::transfer_coins", typeArguments: [contractAddress ?? constants_1.APTOS_ASSET_ID], functionArguments: [recipient, amount.toString()], }; }; // FIXME: terminology overlop, using tokentype here when AssetInfo refers to `standard` const isTokenType = (value) => { return Object.values(constants_1.TOKEN_TYPE).includes(value); }; exports.isTokenType = isTokenType; exports.default = buildTransaction; //# sourceMappingURL=buildTransaction.js.map