UNPKG

@ledgerhq/coin-aptos

Version:
89 lines 3.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.transactionsToOperations = exports.convertFunctionPayloadResponseToInputEntryFunctionData = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const constants_1 = require("../constants"); const getCoinAndAmounts_1 = require("./getCoinAndAmounts"); const calculateAmount_1 = require("./calculateAmount"); const processRecipients_1 = require("./processRecipients"); const getFunctionAddress_1 = require("./getFunctionAddress"); const normalizeAddress_1 = require("./normalizeAddress"); const convertFunctionPayloadResponseToInputEntryFunctionData = (payload) => ({ function: payload.function, typeArguments: payload.type_arguments, functionArguments: payload.arguments, }); exports.convertFunctionPayloadResponseToInputEntryFunctionData = convertFunctionPayloadResponseToInputEntryFunctionData; const detectType = (address, tx, value) => { let type = (0, getCoinAndAmounts_1.compareAddress)(tx.sender, address) ? constants_1.OP_TYPE.OUT : constants_1.OP_TYPE.IN; if (!value) { // skip transaction that result no Aptos change type = constants_1.OP_TYPE.UNKNOWN; } return type; }; const getTokenStandard = (coin_id) => { const parts = coin_id.split("::"); if (parts.length === 3) { return "coin"; } return "fungible_asset"; }; function transactionsToOperations(address, txs) { const operations = []; return txs.reduce((acc, tx) => { if (tx === null) { return acc; } const payload = (0, exports.convertFunctionPayloadResponseToInputEntryFunctionData)(tx.payload); const function_address = (0, getFunctionAddress_1.getFunctionAddress)(payload); if (!function_address) { return acc; // skip transaction without functions in payload } const { coin_id, amount_in, amount_out } = (0, getCoinAndAmounts_1.getCoinAndAmounts)(tx, address); const value = (0, calculateAmount_1.calculateAmount)(tx.sender, address, amount_in, amount_out); const type = detectType(address, tx, value); const op = { id: tx.hash, type, senders: [], recipients: [], value: BigInt(0), asset: { type: "native" }, details: { hasFailed: !tx.success, }, tx: { hash: tx.hash, block: { height: 0 }, fees: BigInt(0), date: new Date(parseInt(tx.timestamp) / 1000), }, }; const fees = new bignumber_js_1.default(tx.gas_used).multipliedBy(new bignumber_js_1.default(tx.gas_unit_price)); op.tx.fees = BigInt(fees.toString()); op.value = BigInt(value.isNaN() ? 0 : value.toString()); op.senders.push((0, normalizeAddress_1.normalizeAddress)(tx.sender)); (0, processRecipients_1.processRecipients)(payload, address, op, function_address); if (op.type !== constants_1.OP_TYPE.UNKNOWN && coin_id !== null) { if (coin_id === constants_1.APTOS_ASSET_ID) { acc.push(op); return acc; } else { op.asset = { type: "token", standard: getTokenStandard(coin_id), contractAddress: coin_id, }; acc.push(op); } } return acc; }, operations); } exports.transactionsToOperations = transactionsToOperations; //# sourceMappingURL=transactionsToOperations.js.map