@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
114 lines • 5.56 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 cryptoassets_1 = require("@ledgerhq/cryptoassets");
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, transaction, gas, gasPrice) => {
const tokenAccount = (0, index_1.findSubAccountById)(account, transaction?.subAccountId ?? "");
const fromTokenAccount = tokenAccount && (0, index_1.isTokenAccount)(tokenAccount);
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 fromTokenAccount
? tokenAccount.spendableBalance
: account.spendableBalance.gt(totalGas)
? account.spendableBalance.minus(totalGas)
: new bignumber_js_1.default(0);
};
exports.getMaxSendBalance = getMaxSendBalance;
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: parseInt(tx.sequence_number),
hasFailed: false,
});
exports.getBlankOperation = getBlankOperation;
const txsToOps = (info, id, txs) => {
const { address } = info;
const ops = [];
const opsTokens = [];
const opsStaking = [];
txs.forEach(tx => {
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) {
return; // 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) {
ops.push(op);
}
else {
const token = (0, cryptoassets_1.findTokenByAddressInCurrency)(coin_id.toLowerCase(), "aptos");
if (token !== undefined) {
op.accountId = (0, index_1.encodeTokenAccountId)(id, token);
opsTokens.push(op);
if (op.type === constants_1.OP_TYPE.OUT) {
ops.push({
...op,
accountId: (0, index_1.decodeTokenAccountId)(op.accountId).accountId,
value: op.fee,
type: "FEES",
});
}
}
}
}
}
});
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