@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
213 lines • 7.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultTronResources = exports.extractBandwidthInfo = exports.txInfoToOperation = exports.getOperationTypefromMode = exports.getEstimatedBlockSize = exports.isParentTx = void 0;
exports.getTronResources = getTronResources;
exports.isAccountEmpty = isAccountEmpty;
const operation_1 = require("@ledgerhq/ledger-wallet-framework/operation");
const get_1 = __importDefault(require("lodash/get"));
const bignumber_js_1 = require("bignumber.js");
const utils_1 = require("../logic/utils");
const network_1 = require("../network");
const format_1 = require("../network/format");
const parentTx = [
"TransferContract",
"VoteWitnessContract",
"WithdrawBalanceContract",
"ExchangeTransactionContract",
"FreezeBalanceV2Contract",
"UnfreezeBalanceV2Contract",
"WithdrawExpireUnfreezeContract",
"UnDelegateResourceContract",
"FreezeBalanceContract",
"UnfreezeBalanceContract",
"ContractApproval",
];
const isParentTx = (tx) => parentTx.includes(tx.type);
exports.isParentTx = isParentTx;
// This is an estimation, there is no endpoint to calculate the real size of a block before broadcasting it.
const getEstimatedBlockSize = (a, t) => {
switch (t.mode) {
case "send": {
const subAccount = t.subAccountId && a.subAccounts ? a.subAccounts.find(sa => sa.id === t.subAccountId) : null;
if (subAccount && subAccount.type === "TokenAccount") {
if (subAccount.token.tokenType === "trc10")
return new bignumber_js_1.BigNumber(285);
if (subAccount.token.tokenType === "trc20")
return new bignumber_js_1.BigNumber(350);
}
return new bignumber_js_1.BigNumber(270);
}
case "freeze":
case "unfreeze":
case "claimReward":
case "withdrawExpireUnfreeze":
case "unDelegateResource":
case "legacyUnfreeze":
return new bignumber_js_1.BigNumber(260);
case "vote":
return new bignumber_js_1.BigNumber(290 + t.votes.length * 19);
default:
return new bignumber_js_1.BigNumber(0);
}
};
exports.getEstimatedBlockSize = getEstimatedBlockSize;
const getOperationTypefromMode = (mode) => {
switch (mode) {
case "send":
return "OUT";
case "freeze":
return "FREEZE";
case "unfreeze":
return "UNFREEZE";
case "vote":
return "VOTE";
case "claimReward":
return "REWARD";
case "withdrawExpireUnfreeze":
return "WITHDRAW_EXPIRE_UNFREEZE";
case "unDelegateResource":
return "UNDELEGATE_RESOURCE";
case "legacyUnfreeze":
return "LEGACY_UNFREEZE";
default:
return "OUT";
}
};
exports.getOperationTypefromMode = getOperationTypefromMode;
const getOperationType = (tx, accountAddr) => {
switch (tx.type) {
case "TransferContract":
case "TransferAssetContract":
case "TriggerSmartContract":
return tx.from === accountAddr ? "OUT" : "IN";
case "ContractApproval":
return "APPROVE";
case "ExchangeTransactionContract":
return "OUT";
case "VoteWitnessContract":
return "VOTE";
case "WithdrawBalanceContract":
return "REWARD";
case "FreezeBalanceContract":
case "FreezeBalanceV2Contract":
return "FREEZE";
case "UnfreezeBalanceV2Contract":
return "UNFREEZE";
case "WithdrawExpireUnfreezeContract":
return "WITHDRAW_EXPIRE_UNFREEZE";
case "UnDelegateResourceContract":
return "UNDELEGATE_RESOURCE";
case "UnfreezeBalanceContract":
return "LEGACY_UNFREEZE";
default:
return undefined;
}
};
const txInfoToOperation = (id, address, tx) => {
const { txID, date, from, to, type, value = new bignumber_js_1.BigNumber(0), fee = new bignumber_js_1.BigNumber(0), blockHeight, extra = {}, hasFailed, } = tx;
const hash = txID;
const operationType = getOperationType(tx, address);
if (operationType) {
return {
id: (0, operation_1.encodeOperationId)(id, hash, operationType),
hash,
type: operationType,
value: operationType === "OUT" && type === "TransferContract" ? value.plus(fee) : value,
// fee is not charged in TRC tokens
fee: fee,
blockHeight,
blockHash: null,
accountId: id,
senders: [from],
recipients: to ? [to] : [],
date,
extra,
hasFailed,
};
}
return undefined;
};
exports.txInfoToOperation = txInfoToOperation;
const extractBandwidthInfo = (networkInfo) => {
// Calculate bandwidth info :
if (networkInfo) {
const { freeNetUsed, freeNetLimit, netUsed, netLimit } = networkInfo;
return {
freeUsed: freeNetUsed,
freeLimit: freeNetLimit,
gainedUsed: netUsed,
gainedLimit: netLimit,
};
}
return {
freeUsed: new bignumber_js_1.BigNumber(0),
freeLimit: new bignumber_js_1.BigNumber(0),
gainedUsed: new bignumber_js_1.BigNumber(0),
gainedLimit: new bignumber_js_1.BigNumber(0),
};
};
exports.extractBandwidthInfo = extractBandwidthInfo;
exports.defaultTronResources = {
frozen: {
bandwidth: undefined,
energy: undefined,
},
unFrozen: {
bandwidth: undefined,
energy: undefined,
},
delegatedFrozen: {
bandwidth: undefined,
energy: undefined,
},
legacyFrozen: {
bandwidth: undefined,
energy: undefined,
},
votes: [],
tronPower: 0,
energy: new bignumber_js_1.BigNumber(0),
bandwidth: {
freeUsed: new bignumber_js_1.BigNumber(0),
freeLimit: new bignumber_js_1.BigNumber(0),
gainedUsed: new bignumber_js_1.BigNumber(0),
gainedLimit: new bignumber_js_1.BigNumber(0),
},
unwithdrawnReward: new bignumber_js_1.BigNumber(0),
lastWithdrawnRewardDate: undefined,
lastVotedDate: undefined,
};
async function getTronResources(acc, txs) {
const encodedAddress = (0, format_1.encode58Check)(acc.address);
const tronNetworkInfo = await (0, network_1.getTronAccountNetwork)(encodedAddress);
const unwithdrawnReward = await (0, network_1.getUnwithdrawnReward)(encodedAddress);
const energy = tronNetworkInfo.energyLimit.minus(tronNetworkInfo.energyUsed);
const bandwidth = (0, exports.extractBandwidthInfo)(tronNetworkInfo);
// TODO: rely on the account object when trongrid will provide this info.
const getLastVotedDate = (txs) => {
const lastOp = txs.find(({ type }) => type === "VoteWitnessContract");
return lastOp ? lastOp.date : null;
};
const lastVotedDate = txs ? getLastVotedDate(txs) : undefined;
const rawVotes = (0, get_1.default)(acc, "votes", []).sort((a, b) => b.vote_count - a.vote_count);
const votes = await Promise.all(rawVotes.map(async (v) => ({
name: await (0, network_1.accountNamesCache)(v.vote_address),
address: v.vote_address,
voteCount: v.vote_count,
})));
return {
...(0, utils_1.getTronResources)(acc),
votes,
energy,
bandwidth,
unwithdrawnReward,
lastVotedDate,
};
}
function isAccountEmpty({ tronResources }) {
return tronResources.bandwidth.freeLimit.eq(0);
}
//# sourceMappingURL=utils.js.map