UNPKG

@ledgerhq/coin-tron

Version:
156 lines 7.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatTrongridTxResponse = exports.formatTrongridTrc20TxResponse = exports.encode58Check = exports.decode58Check = void 0; const logs_1 = require("@ledgerhq/logs"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const bs58check_1 = __importDefault(require("bs58check")); const get_1 = __importDefault(require("lodash/get")); const decode58Check = (base58) => Buffer.from(bs58check_1.default.decode(base58)).toString("hex"); exports.decode58Check = decode58Check; const encode58Check = (hex) => bs58check_1.default.encode(Buffer.from(hex, "hex")); exports.encode58Check = encode58Check; const formatTrongridTrc20TxResponse = (tx) => { try { const { from, to, block_timestamp, detail, value, transaction_id, token_info, type } = tx; const txID = transaction_id; let txType; let tokenId; const fee = tx.detail.ret[0].fee || undefined; const bnFee = new bignumber_js_1.default(fee || 0); let formattedValue; // token_info.address is missing for unindexed contracts (e.g. LP/DEX pool tokens not in TronGrid registry) // fall back to the contract_address from the raw transaction, which is always present const contractAddressHex = detail.raw_data?.contract?.[0]?.parameter?.value?.contract_address; const tokenAddress = token_info.address ?? (contractAddressHex ? (0, exports.encode58Check)(contractAddressHex) : undefined); switch (type) { case "Approval": txType = "ContractApproval"; formattedValue = bnFee; break; default: txType = "TriggerSmartContract"; tokenId = tokenAddress; formattedValue = value ? new bignumber_js_1.default(value) : new bignumber_js_1.default(0); break; } const date = new Date(block_timestamp); const blockHeight = detail ? detail.blockNumber : undefined; const ownerAddressHex = detail.raw_data?.contract?.[0]?.parameter?.value?.owner_address; const feesPayer = ownerAddressHex ? (0, exports.encode58Check)(ownerAddressHex) : from; return { txID, date, type: txType, tokenId: tokenId, tokenAddress, tokenType: "trc20", from, to, blockHeight, value: formattedValue, fee: bnFee, hasFailed: false, // trc20 txs are succeeded if returned by trongrid, feesPayer, }; } catch (e) { (0, logs_1.log)("tron-error", `could not parse transaction ${tx}`); throw e; } }; exports.formatTrongridTrc20TxResponse = formatTrongridTrc20TxResponse; const formatTrongridTxResponse = async (tx, getValidatorName) => { try { const { txID, block_timestamp, blockNumber, unfreeze_amount, withdraw_amount } = tx; const date = new Date(block_timestamp); const type = tx.raw_data.contract[0].type; const { amount, asset_name, owner_address, to_address, contract_address, quant, frozen_balance, votes, unfreeze_balance, balance, receiver_address, } = tx.raw_data.contract[0].parameter.value; const hasFailed = (0, get_1.default)(tx, "ret[0].contractRet", "SUCCESS") !== "SUCCESS"; const tokenId = type === "TransferAssetContract" ? asset_name : type === "TriggerSmartContract" && contract_address ? (0, exports.encode58Check)(contract_address) : undefined; const from = (0, exports.encode58Check)(owner_address); const to = to_address ? (0, exports.encode58Check)(to_address) : undefined; const getValue = () => { switch (type) { case "WithdrawBalanceContract": return new bignumber_js_1.default(withdraw_amount || 0); case "ExchangeTransactionContract": return new bignumber_js_1.default(quant || 0); default: return amount ? new bignumber_js_1.default(amount) : new bignumber_js_1.default(0); } }; const value = getValue(); const fee = (0, get_1.default)(tx, "ret[0].fee", undefined); const blockHeight = blockNumber; const isTrc20 = type === "TriggerSmartContract" && contract_address; const isTrc10 = type === "TransferAssetContract"; const tokenType = isTrc10 ? "trc10" : isTrc20 ? "trc20" : undefined; const txInfo = { txID, date, type, tokenId, // TRX native is TransferContract, TRC20 uses TriggerSmartContract tokenType, tokenAddress: isTrc20 ? (0, exports.encode58Check)(contract_address) : undefined, from, to, value: !value.isNaN() ? value : new bignumber_js_1.default(0), fee: new bignumber_js_1.default(fee || 0), blockHeight, hasFailed, feesPayer: from, }; const getExtra = async () => { switch (type) { case "VoteWitnessContract": return { votes: votes && (await Promise.all(votes.map(async (v) => ({ name: await getValidatorName((0, exports.encode58Check)(v.vote_address)), address: (0, exports.encode58Check)(v.vote_address), voteCount: v.vote_count, })))), }; case "FreezeBalanceContract": case "FreezeBalanceV2Contract": return { frozenAmount: new bignumber_js_1.default(frozen_balance), }; case "UnfreezeBalanceV2Contract": return { unfreezeAmount: new bignumber_js_1.default(unfreeze_balance), }; case "UnDelegateResourceContract": return { unDelegatedAmount: new bignumber_js_1.default(balance), receiverAddress: (0, exports.encode58Check)(receiver_address), }; case "UnfreezeBalanceContract": return { unfreezeAmount: new bignumber_js_1.default(unfreeze_amount || 0), }; default: return undefined; } }; const extra = await getExtra(); if (extra) { txInfo.extra = extra; } return txInfo; } catch { (0, logs_1.log)("tron-error", "could not parse transaction", tx); return undefined; } }; exports.formatTrongridTxResponse = formatTrongridTxResponse; //# sourceMappingURL=format.js.map