@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
81 lines • 3.03 kB
JavaScript
import BigNumber from "bignumber.js";
import { APTOS_ASSET_ID, OP_TYPE } from "../constants";
import { compareAddress, getCoinAndAmounts } from "./getCoinAndAmounts";
import { calculateAmount } from "./calculateAmount";
import { processRecipients } from "./processRecipients";
import { getFunctionAddress } from "./getFunctionAddress";
import { normalizeAddress } from "./normalizeAddress";
export const convertFunctionPayloadResponseToInputEntryFunctionData = (payload) => ({
function: payload.function,
typeArguments: payload.type_arguments,
functionArguments: payload.arguments,
});
const detectType = (address, tx, value) => {
let type = compareAddress(tx.sender, address) ? OP_TYPE.OUT : OP_TYPE.IN;
if (!value) {
// skip transaction that result no Aptos change
type = OP_TYPE.UNKNOWN;
}
return type;
};
const getTokenStandard = (coin_id) => {
const parts = coin_id.split("::");
if (parts.length === 3) {
return "coin";
}
return "fungible_asset";
};
export function transactionsToOperations(address, txs) {
const operations = [];
return txs.reduce((acc, tx) => {
if (tx === null) {
return acc;
}
const payload = convertFunctionPayloadResponseToInputEntryFunctionData(tx.payload);
const function_address = getFunctionAddress(payload);
if (!function_address) {
return acc; // skip transaction without functions in payload
}
const { coin_id, amount_in, amount_out } = getCoinAndAmounts(tx, address);
const value = 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(tx.gas_used).multipliedBy(new BigNumber(tx.gas_unit_price));
op.tx.fees = BigInt(fees.toString());
op.value = BigInt(value.isNaN() ? 0 : value.toString());
op.senders.push(normalizeAddress(tx.sender));
processRecipients(payload, address, op, function_address);
if (op.type !== OP_TYPE.UNKNOWN && coin_id !== null) {
if (coin_id === 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);
}
//# sourceMappingURL=transactionsToOperations.js.map