@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
117 lines • 5.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenAccount = exports.txsToOps = exports.getBlankOperation = exports.getMaxSendBalance = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const index_1 = require("@ledgerhq/coin-framework/account/index");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const state_1 = require("@ledgerhq/cryptoassets/state");
const constants_1 = require("../constants");
const transactionsToOperations_1 = require("../logic/transactionsToOperations");
const getCoinAndAmounts_1 = require("../logic/getCoinAndAmounts");
const calculateAmount_1 = require("../logic/calculateAmount");
const processRecipients_1 = require("../logic/processRecipients");
const getFunctionAddress_1 = require("../logic/getFunctionAddress");
const getMaxSendBalance = (account, parentAccount, gas, gasPrice) => {
gas = gas ?? (0, bignumber_js_1.default)(constants_1.DEFAULT_GAS);
gasPrice = gasPrice ?? (0, bignumber_js_1.default)(constants_1.DEFAULT_GAS_PRICE);
const totalGas = gas.multipliedBy(gasPrice);
return parentAccount
? account.spendableBalance
: getMaxSendBalanceFromAccount(account, totalGas);
};
exports.getMaxSendBalance = getMaxSendBalance;
const getMaxSendBalanceFromAccount = (account, totalGas) => account.spendableBalance.gt(totalGas)
? account.spendableBalance.minus(totalGas)
: new bignumber_js_1.default(0);
const getBlankOperation = (tx, id) => ({
id: "",
hash: tx.hash,
type: "",
value: new bignumber_js_1.default(0),
fee: new bignumber_js_1.default(0),
blockHash: tx.block?.hash,
blockHeight: tx.block?.height,
senders: [],
recipients: [],
accountId: id,
date: new Date(parseInt(tx.timestamp) / 1000),
extra: { version: tx.version },
transactionSequenceNumber: new bignumber_js_1.default(parseInt(tx.sequence_number)),
hasFailed: false,
});
exports.getBlankOperation = getBlankOperation;
const txsToOps = async (info, id, txs) => {
const { address } = info;
const ops = [];
const opsTokens = [];
const opsStaking = [];
for (const tx of txs) {
if (tx !== null) {
const op = (0, exports.getBlankOperation)(tx, id);
op.fee = new bignumber_js_1.default(tx.gas_used).multipliedBy(new bignumber_js_1.default(tx.gas_unit_price));
const payload = (0, transactionsToOperations_1.convertFunctionPayloadResponseToInputEntryFunctionData)(tx.payload);
const function_address = (0, getFunctionAddress_1.getFunctionAddress)(payload);
if (!function_address) {
continue; // skip transaction without functions in payload
}
const { coin_id, amount_in, amount_out, type } = (0, getCoinAndAmounts_1.getCoinAndAmounts)(tx, address);
op.value = (0, calculateAmount_1.calculateAmount)(tx.sender, address, amount_in, amount_out);
op.type =
type !== constants_1.OP_TYPE.UNKNOWN
? type
: (0, getCoinAndAmounts_1.compareAddress)(tx.sender, address)
? constants_1.OP_TYPE.OUT
: constants_1.OP_TYPE.IN;
op.senders.push(tx.sender);
op.hasFailed = !tx.success;
op.id = (0, operation_1.encodeOperationId)(op.accountId, tx.hash, op.type);
(0, processRecipients_1.processRecipients)(payload, address, op, function_address);
if (op.value.isZero()) {
// skip transaction that result no Aptos change
op.type = constants_1.OP_TYPE.UNKNOWN;
}
if (op.type === constants_1.OP_TYPE.STAKE ||
op.type === constants_1.OP_TYPE.UNSTAKE ||
op.type === constants_1.OP_TYPE.WITHDRAW) {
ops.push(op);
opsStaking.push(op);
}
else if (op.type !== constants_1.OP_TYPE.UNKNOWN && coin_id !== null) {
if (coin_id === constants_1.APTOS_ASSET_ID || coin_id === constants_1.APTOS_ASSET_FUNGIBLE_ID) {
ops.push(op);
}
else {
const token = await (0, state_1.getCryptoAssetsStore)().findTokenByAddressInCurrency(coin_id.toLowerCase(), "aptos");
if (token !== undefined) {
const tokenAccountId = (0, index_1.encodeTokenAccountId)(id, token);
op.accountId = tokenAccountId;
opsTokens.push(op);
if (op.type === constants_1.OP_TYPE.OUT) {
const accountId = tokenAccountId.split("+")[0];
// Create FEES operation with decoded main account ID
const feesOp = {
...op,
accountId,
value: op.fee,
type: "FEES",
};
ops.push(feesOp);
}
}
}
}
}
}
return [ops, opsTokens, opsStaking];
};
exports.txsToOps = txsToOps;
function getTokenAccount(account, transaction) {
const tokenAccount = (0, index_1.findSubAccountById)(account, transaction.subAccountId ?? "");
const fromTokenAccount = tokenAccount && (0, index_1.isTokenAccount)(tokenAccount);
return fromTokenAccount ? tokenAccount : undefined;
}
exports.getTokenAccount = getTokenAccount;
//# sourceMappingURL=logic.js.map