@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
141 lines • 6.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCoinAndAmounts = exports.getEventFAAddress = exports.getEventCoinAddress = exports.checkFAOwner = exports.compareAddress = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../constants");
const getResourceAddress_1 = require("./getResourceAddress");
const isWriteSetChangeWriteResource_1 = require("./isWriteSetChangeWriteResource");
const CLEAN_HEX_REGEXP = /^0x0*|^0+/;
function compareAddress(addressA, addressB) {
return (addressA.replace(CLEAN_HEX_REGEXP, "").toLowerCase() ===
addressB.replace(CLEAN_HEX_REGEXP, "").toLowerCase());
}
exports.compareAddress = compareAddress;
function checkFAOwner(tx, event, user_address) {
for (const change of tx.changes) {
if ((0, isWriteSetChangeWriteResource_1.isWriteSetChangeWriteResource)(change)) {
const storeData = change.data;
if (change.address === event.data.store &&
storeData.type === constants_1.APTOS_OBJECT_CORE &&
storeData.data.owner === user_address) {
return true;
}
}
}
return false;
}
exports.checkFAOwner = checkFAOwner;
/**
* Extracts the address from a string like "0x1::coin::CoinStore<address::module::type>"
* @param {string} str - The input string containing the address.
* @returns {string | null} - The extracted address or null if not found.
*/
function extractAddress(str) {
const match = /<([^<>]+)>$/.exec(str);
return match ? match[1] : null;
}
function getEventCoinAddress(change, event, event_name) {
const change_data = change.data;
const mr = change_data; // -> this is data that we want to parse
if (!(event_name in mr.data)) {
return null;
}
const change_event_data = mr.data[event_name];
if (change_event_data.guid.id.addr !== event.guid.account_address ||
change_event_data.guid.id.creation_num !== event.guid.creation_number) {
return null;
}
const address = extractAddress(mr.type);
return address;
}
exports.getEventCoinAddress = getEventCoinAddress;
function getEventFAAddress(change, event, _event_name) {
const change_data = change.data;
if (change_data.type !== constants_1.APTOS_FUNGIBLE_STORE) {
return null;
}
const mr = change_data;
if (change.address !== event.data.store) {
return null;
}
return mr.data.metadata.inner;
}
exports.getEventFAAddress = getEventFAAddress;
function getCoinAndAmounts(tx, address) {
let coin_id = null;
let amount_in = (0, bignumber_js_1.default)(0);
let amount_out = (0, bignumber_js_1.default)(0);
let type = constants_1.OP_TYPE.UNKNOWN;
// Check if it is a staking transaction
const stakingTx = !!tx.events.find(event => constants_1.STAKING_EVENTS.includes(event.type));
// Collect all events related to the address and calculate the overall amounts
if (stakingTx) {
tx.events.forEach(event => {
if (constants_1.ADD_STAKE_EVENTS.includes(event.type) &&
compareAddress(tx.sender, address) &&
amount_out.isZero()) {
coin_id = constants_1.APTOS_ASSET_ID;
type = constants_1.OP_TYPE.STAKE;
amount_out = amount_out.plus(event.data.amount_added || event.data.amount);
}
else if (constants_1.REACTIVATE_STAKE_EVENTS.includes(event.type) &&
compareAddress(tx.sender, address) &&
amount_out.isZero()) {
coin_id = constants_1.APTOS_ASSET_ID;
type = constants_1.OP_TYPE.STAKE;
amount_out = amount_out.plus(event.data.amount_reactivated || event.data.amount);
}
else if (constants_1.UNLOCK_STAKE_EVENTS.includes(event.type) &&
compareAddress(tx.sender, address) &&
amount_in.isZero()) {
coin_id = constants_1.APTOS_ASSET_ID;
type = constants_1.OP_TYPE.UNSTAKE;
amount_in = amount_in.plus(event.data.amount_unlocked || event.data.amount);
}
else if (constants_1.WITHDRAW_STAKE_EVENTS.includes(event.type) &&
compareAddress(tx.sender, address) &&
amount_in.isZero()) {
coin_id = constants_1.APTOS_ASSET_ID;
type = constants_1.OP_TYPE.WITHDRAW;
amount_in = amount_in.plus(event.data.amount_withdrawn || event.data.amount);
}
});
}
else {
tx.events.forEach(event => {
if (event.type === "0x1::coin::WithdrawEvent" &&
compareAddress(event.guid.account_address, address)) {
coin_id = (0, getResourceAddress_1.getResourceAddress)(tx, event, "withdraw_events", getEventCoinAddress);
amount_out = amount_out.plus(event.data.amount);
}
else if (event.type === "0x1::coin::DepositEvent" &&
compareAddress(event.guid.account_address, address)) {
coin_id = (0, getResourceAddress_1.getResourceAddress)(tx, event, "deposit_events", getEventCoinAddress);
amount_in = amount_in.plus(event.data.amount);
}
else if (event.type === "0x1::fungible_asset::Withdraw" &&
checkFAOwner(tx, event, address)) {
coin_id = (0, getResourceAddress_1.getResourceAddress)(tx, event, "withdraw_events", getEventFAAddress);
amount_out = amount_out.plus(event.data.amount);
}
else if (event.type === "0x1::fungible_asset::Deposit" &&
checkFAOwner(tx, event, address)) {
coin_id = (0, getResourceAddress_1.getResourceAddress)(tx, event, "deposit_events", getEventFAAddress);
amount_in = amount_in.plus(event.data.amount);
}
else if (event.type === "0x1::transaction_fee::FeeStatement" && tx.sender === address) {
coin_id ??= constants_1.APTOS_ASSET_ID;
if (coin_id === constants_1.APTOS_ASSET_ID) {
const fees = (0, bignumber_js_1.default)(tx.gas_unit_price).times((0, bignumber_js_1.default)(tx.gas_used));
amount_out = amount_out.plus(fees);
}
}
});
}
return { coin_id, amount_in, amount_out, type };
}
exports.getCoinAndAmounts = getCoinAndAmounts;
//# sourceMappingURL=getCoinAndAmounts.js.map