@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
200 lines • 6.84 kB
JavaScript
import { encodeOperationId } from "@ledgerhq/ledger-wallet-framework/operation";
import get from "lodash/get";
import { BigNumber } from "bignumber.js";
import { getTronResources as getTronResourcesLogic } from "../logic/utils";
import { accountNamesCache, getTronAccountNetwork, getUnwithdrawnReward } from "../network";
import { encode58Check } from "../network/format";
const parentTx = [
"TransferContract",
"VoteWitnessContract",
"WithdrawBalanceContract",
"ExchangeTransactionContract",
"FreezeBalanceV2Contract",
"UnfreezeBalanceV2Contract",
"WithdrawExpireUnfreezeContract",
"UnDelegateResourceContract",
"FreezeBalanceContract",
"UnfreezeBalanceContract",
"ContractApproval",
];
export const isParentTx = (tx) => parentTx.includes(tx.type);
// This is an estimation, there is no endpoint to calculate the real size of a block before broadcasting it.
export 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(285);
if (subAccount.token.tokenType === "trc20")
return new BigNumber(350);
}
return new BigNumber(270);
}
case "freeze":
case "unfreeze":
case "claimReward":
case "withdrawExpireUnfreeze":
case "unDelegateResource":
case "legacyUnfreeze":
return new BigNumber(260);
case "vote":
return new BigNumber(290 + t.votes.length * 19);
default:
return new BigNumber(0);
}
};
export 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";
}
};
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;
}
};
export const txInfoToOperation = (id, address, tx) => {
const { txID, date, from, to, type, value = new BigNumber(0), fee = new BigNumber(0), blockHeight, extra = {}, hasFailed, } = tx;
const hash = txID;
const operationType = getOperationType(tx, address);
if (operationType) {
return {
id: 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;
};
export 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(0),
freeLimit: new BigNumber(0),
gainedUsed: new BigNumber(0),
gainedLimit: new BigNumber(0),
};
};
export const 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(0),
bandwidth: {
freeUsed: new BigNumber(0),
freeLimit: new BigNumber(0),
gainedUsed: new BigNumber(0),
gainedLimit: new BigNumber(0),
},
unwithdrawnReward: new BigNumber(0),
lastWithdrawnRewardDate: undefined,
lastVotedDate: undefined,
};
export async function getTronResources(acc, txs) {
const encodedAddress = encode58Check(acc.address);
const tronNetworkInfo = await getTronAccountNetwork(encodedAddress);
const unwithdrawnReward = await getUnwithdrawnReward(encodedAddress);
const energy = tronNetworkInfo.energyLimit.minus(tronNetworkInfo.energyUsed);
const bandwidth = 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 = get(acc, "votes", []).sort((a, b) => b.vote_count - a.vote_count);
const votes = await Promise.all(rawVotes.map(async (v) => ({
name: await accountNamesCache(v.vote_address),
address: v.vote_address,
voteCount: v.vote_count,
})));
return {
...getTronResourcesLogic(acc),
votes,
energy,
bandwidth,
unwithdrawnReward,
lastVotedDate,
};
}
export function isAccountEmpty({ tronResources }) {
return tronResources.bandwidth.freeLimit.eq(0);
}
//# sourceMappingURL=utils.js.map