UNPKG

@ledgerhq/coin-tron

Version:
504 lines 21.2 kB
import { stringify } from "querystring"; import { InvalidTransactionError } from "@ledgerhq/errors"; import network from "@ledgerhq/live-network"; import { hours, makeLRUCache } from "@ledgerhq/live-network/cache"; import { log } from "@ledgerhq/logs"; import { BigNumber } from "bignumber.js"; import compact from "lodash/compact"; import drop from "lodash/drop"; import sumBy from "lodash/sumBy"; import take from "lodash/take"; import { TronWeb, providers } from "tronweb"; import coinConfig from "../config"; import { TronTransactionExpired } from "../types/errors"; import { decode58Check, encode58Check, formatTrongridTrc20TxResponse, formatTrongridTxResponse, } from "./format"; import { isTransactionTronAPI, } from "./types"; import { abiEncodeTrc20Transfer, hexToAscii } from "./utils"; const getBaseApiUrl = () => coinConfig.getCoinConfig().explorer.url; function isValidNativeTx(tx) { // tx_id indicates a malformed/duplicated entry from TronGrid — these must be excluded. // Transactions with internal_transactions are valid and should be included. return !tx.tx_id; } function isSuccessfulTriggerSmartContract(tx) { return tx.type === "TriggerSmartContract" && !tx.hasFailed; } export async function post(endPoint, body) { const { data } = await network({ method: "POST", url: `${getBaseApiUrl()}${endPoint}`, data: body, }); // Ugly but trongrid send a 200 status event if there are errors if ("Error" in data) { const error = data.Error; const message = stringify(error); const nonEmptyMessage = message === "" ? error.toString() : message; log("tron-error", nonEmptyMessage, { endPoint, body }); throw new Error(nonEmptyMessage); } return data; } async function fetch(endPoint) { return fetchWithBaseUrl(`${getBaseApiUrl()}${endPoint}`); } async function fetchWithBaseUrl(url) { const { data } = await network({ url }); // Ugly but trongrid send a 200 status event if there are errors if ("Error" in data) { log("tron-error", stringify(data.Error), { url, }); throw new Error(stringify(data.Error)); } return data; } export const freezeTronTransaction = async (account, transaction) => { const txData = { frozen_balance: transaction.amount.toNumber(), resource: transaction.resource, owner_address: decode58Check(account.freshAddress), }; const url = `/wallet/freezebalancev2`; const result = await post(url, txData); return result; }; export const unfreezeTronTransaction = async (account, transaction) => { const txData = { owner_address: decode58Check(account.freshAddress), resource: transaction.resource, unfreeze_balance: transaction.amount.toNumber(), }; const url = `/wallet/unfreezebalancev2`; const result = await post(url, txData); return result; }; export const withdrawExpireUnfreezeTronTransaction = async (account, _transaction) => { const txData = { owner_address: decode58Check(account.freshAddress), }; const url = `/wallet/withdrawexpireunfreeze`; const result = await post(url, txData); return result; }; export const unDelegateResourceTransaction = async (account, transaction) => { const txData = { balance: transaction.amount.toNumber(), resource: transaction.resource, owner_address: decode58Check(account.freshAddress), receiver_address: decode58Check(transaction.recipient), }; const url = `/wallet/undelegateresource`; const result = await post(url, txData); return result; }; export const legacyUnfreezeTronTransaction = async (account, transaction) => { const txData = { resource: transaction.resource, owner_address: decode58Check(account.freshAddress), receiver_address: transaction.recipient ? decode58Check(transaction.recipient) : undefined, }; const url = `/wallet/unfreezebalance`; const result = await post(url, txData); return result; }; export async function getDelegatedResource(account, transaction, resource) { const url = `/wallet/getdelegatedresourcev2`; const { delegatedResource = [], } = await post(url, { fromAddress: decode58Check(account.freshAddress), toAddress: decode58Check(transaction.recipient), }); const { frozen_balance_for_bandwidth, frozen_balance_for_energy } = delegatedResource.reduce((accum, cur) => { if (cur.frozen_balance_for_bandwidth) { accum.frozen_balance_for_bandwidth += cur.frozen_balance_for_bandwidth; } if (cur.frozen_balance_for_energy) { accum.frozen_balance_for_energy += cur.frozen_balance_for_energy; } return accum; }, { frozen_balance_for_bandwidth: 0, frozen_balance_for_energy: 0 }); const amount = resource === "BANDWIDTH" ? frozen_balance_for_bandwidth : frozen_balance_for_energy; return new BigNumber(amount); } export const DEFAULT_TRC20_FEES_LIMIT = 50000000; export async function craftTrc20Transaction(tokenAddress, recipientAddress, senderAddress, amount, customFees, expiration) { const txData = { function_selector: "transfer(address,uint256)", fee_limit: customFees ? customFees : DEFAULT_TRC20_FEES_LIMIT, call_value: 0, contract_address: decode58Check(tokenAddress), parameter: abiEncodeTrc20Transfer(recipientAddress, new BigNumber(amount.toString())), owner_address: senderAddress, }; const url = `/wallet/triggersmartcontract`; const { transaction: preparedTransaction } = await post(url, txData); return await extendExpiration(preparedTransaction, expiration); } export async function craftStandardTransaction(tokenAddress, recipientAddress, senderAddress, amount, isTransferAsset, memo, expiration) { const url = isTransferAsset ? `/wallet/transferasset` : `/wallet/createtransaction`; const txData = { to_address: recipientAddress, owner_address: senderAddress, amount: Number(amount), asset_name: tokenAddress && Buffer.from(tokenAddress).toString("hex"), extra_data: memo && Buffer.from(memo).toString("hex"), }; const preparedTransaction = await post(url, txData); return await extendExpiration(preparedTransaction, expiration); } const getTokenInfo = (subAccount) => { const tokenInfo = subAccount && subAccount.type === "TokenAccount" ? drop(subAccount.token.id.split("/"), 1) : [undefined, undefined]; return tokenInfo; }; // Send trx or trc10/trc20 tokens export const createTronTransaction = async (account, transaction, subAccount) => { const [tokenType, tokenId] = getTokenInfo(subAccount); const decodeRecipient = decode58Check(transaction.recipient); const decodeSender = decode58Check(account.freshAddress); // trc20 if (tokenType === "trc20" && tokenId) { const tokenContractAddress = subAccount.token.contractAddress; return craftTrc20Transaction(tokenContractAddress, decodeRecipient, decodeSender, transaction.amount); } else { const isTransferAsset = subAccount ? true : false; return craftStandardTransaction(tokenId, decodeRecipient, decodeSender, transaction.amount, isTransferAsset); } }; /** Default expiration of 10 minutes (in seconds) after crafting time. */ export const DEFAULT_EXPIRATION = 600; async function extendExpiration(preparedTransaction, expiration) { const extension = expiration ?? DEFAULT_EXPIRATION; const nodeExpiration = preparedTransaction.raw_data.expiration; const minFinalExpiration = Date.now() + 3000; // Tron nodes may not be properly synced, returning an expiration date in the past. // We throw an error that encourages users to drop their transaction and re-create a new one. // https://github.com/tronprotocol/tronweb/blob/9f8b559377d9215a4f5360e8526c6e7197bf5a5b/src/lib/TransactionBuilder/TransactionBuilder.ts#L2449-L2450 if (nodeExpiration + extension * 1000 <= minFinalExpiration) { log("tron/extendExpiration", "Invalid extension provided", { preparedTransaction, extensionInS: extension, extensionInMs: extension * 1000, minFinalExpiration, }); throw new InvalidTransactionError(); } const HttpProvider = providers.HttpProvider; const fullNode = new HttpProvider(getBaseApiUrl()); const solidityNode = new HttpProvider(getBaseApiUrl()); const eventServer = new HttpProvider(getBaseApiUrl()); const tronWeb = new TronWeb(fullNode, solidityNode, eventServer); return tronWeb.transactionBuilder.extendExpiration(preparedTransaction, extension); } /** * @see https://github.com/tronprotocol/java-tron/blob/develop/framework/src/main/java/org/tron/core/services/http/BroadcastServlet.java * @param trxTransaction * @returns Transaction ID */ export const broadcastTron = async (trxTransaction) => { const result = await post("/wallet/broadcasttransaction", trxTransaction); if (result.result !== true) { if (result.code === "TRANSACTION_EXPIRATION_ERROR") { throw new TronTransactionExpired(); } else { throw new Error(`${result.code}: ${result.message}`); } } return result.txid; }; export const broadcastHexTron = async (rawTransaction) => { const result = await post(`/wallet/broadcasthex`, { transaction: rawTransaction }); if (!result.result) { throw Error(`Broadcast failed due to ${result.code}`); } return result.txid; }; /** * {@link https://github.com/tronprotocol/java-tron/blob/develop/framework/src/main/java/org/tron/core/services/http/GetAccountServlet.java | Tron Framework} */ export async function fetchTronAccount(addr) { try { const data = await fetch(`/v1/accounts/${addr}`); return data.data; } catch { return []; } } export async function getLastBlock() { const data = await fetch(`/wallet/getnowblock`); return toBlock(data); } export async function getBlock(blockNumber) { const data = await post(`/wallet/getblock`, { id_or_num: String(blockNumber), detail: false, }); return toBlock(data); } export async function getBlockWithTransactions(blockNumber) { return post(`/wallet/getblock`, { id_or_num: String(blockNumber), detail: true }); } function toBlock(data) { const timestamp = data.block_header.raw_data.timestamp; const ret = { height: data.block_header.raw_data.number, hash: data.blockID, }; if (timestamp) { ret.time = new Date(timestamp); } return ret; } export async function getTransactionInfoByBlockNum(blockNum) { return post(`/wallet/gettransactioninfobyblocknum`, { num: blockNum }); } async function getAllTransactions(initialUrl, shouldFetchMoreTxs, getTxs) { let all = []; let url = initialUrl; while (url && shouldFetchMoreTxs(all)) { const { nextUrl, results } = await getTxs(url); url = nextUrl; all = all.concat(results); } return all; } const getTransactions = async (url) => { const transactions = await fetchWithBaseUrl(url); const nextUrl = transactions.meta.links?.next?.replace(/https:\/\/api(\.[a-z]*)?.trongrid.io/, getBaseApiUrl()); const results = transactions.data ?? []; return { results, nextUrl, }; }; const getTrc20 = async (url) => { const transactions = await fetchWithBaseUrl(url); return { results: transactions.data, nextUrl: transactions.meta.links?.next?.replace(/https:\/\/api(\.[a-z]*)?.trongrid.io/, getBaseApiUrl()), }; }; export const defaultFetchParams = { limitPerCall: 100, minTimestamp: 0, order: "desc", }; async function fetchSinglePage(url, getTxs) { const { results, nextUrl } = await getTxs(url); return { results, hasNextPage: !!nextUrl }; } export async function fetchTronAccountTxsPage(addr, params) { const maxTimestampParam = params.maxTimestamp !== undefined ? `&max_timestamp=${params.maxTimestamp}` : ""; const queryParams = `limit=${params.limit}&min_timestamp=${params.minTimestamp}${maxTimestampParam}&order_by=block_timestamp,${params.order}`; const [nativeResult, trc20Result] = await Promise.all([ fetchSinglePage(`${getBaseApiUrl()}/v1/accounts/${addr}/transactions?${queryParams}`, getTransactions), fetchSinglePage(`${getBaseApiUrl()}/v1/accounts/${addr}/transactions/trc20?${queryParams}&get_detail=true`, getTrc20), ]); const nativeTxsFormatted = await Promise.all(nativeResult.results .filter(isTransactionTronAPI) .filter(isValidNativeTx) .map(tx => formatTrongridTxResponse(tx, accountNamesCache))); const trc20TxsFormatted = compact(trc20Result.results.map(formatTrongridTrc20TxResponse)); const trc20TxIds = new Set(trc20TxsFormatted.map(t => t.txID)); const nativeDeduped = compact(nativeTxsFormatted) .filter(tx => !trc20TxIds.has(tx.txID)) .filter(tx => !isSuccessfulTriggerSmartContract(tx)); return { nativeTxs: { txs: nativeDeduped, hasNextPage: nativeResult.hasNextPage }, trc20Txs: { txs: trc20TxsFormatted, hasNextPage: trc20Result.hasNextPage }, }; } export async function fetchTronAccountTxs(addr, shouldFetchMoreTxs, params) { const adjustedLimitPerCall = params.hintGlobalLimit ? Math.min(params.limitPerCall, params.hintGlobalLimit) : params.limitPerCall; const queryParams = `limit=${adjustedLimitPerCall}&min_timestamp=${params.minTimestamp}&order_by=block_timestamp,${params.order}`; const nativeTxs = await Promise.all((await getAllTransactions(`${getBaseApiUrl()}/v1/accounts/${addr}/transactions?${queryParams}`, shouldFetchMoreTxs, getTransactions)) .filter(isTransactionTronAPI) .filter(isValidNativeTx) .map(tx => formatTrongridTxResponse(tx, accountNamesCache))); // we need to fetch and filter trc20 transactions from another endpoint // doc https://developers.tron.network/reference/get-trc20-transaction-info-by-account-address const callTrc20Endpoint = async () => await getAllTransactions(`${getBaseApiUrl()}/v1/accounts/${addr}/transactions/trc20?${queryParams}&get_detail=true`, shouldFetchMoreTxs, getTrc20); function isValid(tx) { const ret = tx?.detail?.ret; return Array.isArray(ret) && ret.length > 0; } function getInvalidTxIndexes(txs) { const invalids = []; for (let i = 0; i < txs.length; i++) { if (!isValid(txs[i])) { invalids.push(i); } } txs.filter(tx => !isValid(tx)).map((_tx, index) => index); return invalids; } function assert(predicate, message) { if (!predicate) { throw new Error(message); } } // Merge the two results function mergeAccs(acc1, acc2) { assert(acc1.txs.length === acc2.txs.length, "accs should have the same length"); const accRet = { txs: acc1.txs, invalids: [] }; acc1.invalids.forEach(invalidIndex => { acc2.invalids.includes(invalidIndex) ? accRet.invalids.push(invalidIndex) : (accRet.txs[invalidIndex] = acc2.txs[invalidIndex]); }); return accRet; } // see LIVE-18992 for an explanation to why we need this async function getTrc20TxsWithRetry(acc, times) { assert(times > 0, "getTrc20TxsWithRetry: couldn't fetch trc20 transactions after several attempts"); const ret = await callTrc20Endpoint(); const thisAcc = { txs: ret, invalids: getInvalidTxIndexes(ret), }; const newAcc = acc ? mergeAccs(acc, thisAcc) : thisAcc; if (newAcc.invalids.length === 0) { return newAcc.txs; } else { log("coin-tron", `getTrc20TxsWithRetry: got ${newAcc.invalids.length} invalid trc20 transactions, retrying...`); return await getTrc20TxsWithRetry(newAcc, times - 1); } } const trc20Txs = compact((await getTrc20TxsWithRetry(null, 3)).map(formatTrongridTrc20TxResponse)); const trc20TxIds = new Set(trc20Txs.map(t => t.txID)); const nativeDeduped = compact(nativeTxs) .filter(tx => !trc20TxIds.has(tx.txID)) .filter(tx => !isSuccessfulTriggerSmartContract(tx)); const txInfos = nativeDeduped .concat(trc20Txs) .sort((a, b) => b.date.getTime() - a.date.getTime()); return txInfos; } export const getContractUserEnergyRatioConsumption = async (address) => { const result = await fetchTronContract(address); if (result) { const { consume_user_resource_percent } = result; return consume_user_resource_percent; } return 0; }; export const fetchTronContract = async (addr) => { try { const data = await post(`/wallet/getcontract`, { value: decode58Check(addr), }); return Object.keys(data).length !== 0 ? data : undefined; } catch { return undefined; } }; export const getTronAccountNetwork = async (address) => { const result = await fetch(`/wallet/getaccountresource?address=${encodeURIComponent(decode58Check(address))}`); const { freeNetUsed = 0, freeNetLimit = 0, NetUsed = 0, NetLimit = 0, EnergyUsed = 0, EnergyLimit = 0, } = result; return { family: "tron", freeNetUsed: new BigNumber(freeNetUsed), freeNetLimit: new BigNumber(freeNetLimit), netUsed: new BigNumber(NetUsed), netLimit: new BigNumber(NetLimit), energyUsed: new BigNumber(EnergyUsed), energyLimit: new BigNumber(EnergyLimit), }; }; export const validateAddress = async (address) => { try { const result = await post(`/wallet/validateaddress`, { address: decode58Check(address), }); return result.result || false; } catch (e) { // FIXME we should not silent errors! log("tron-error", "validateAddress fails with " + e.message, { address, }); return false; } }; // cache for account names (name is unchanged over time) export const accountNamesCache = makeLRUCache(async (addr) => getAccountName(addr), (addr) => addr, hours(3, 300)); export const getAccountName = async (addr) => { const tronAcc = await fetchTronAccount(addr); const acc = tronAcc[0]; const accountName = acc && acc.account_name ? hexToAscii(acc.account_name) : undefined; accountNamesCache.hydrate(addr, accountName); // put it in cache return accountName; }; const superRepresentativesCache = makeLRUCache(async () => { const superRepresentatives = await fetchSuperRepresentatives(); log("tron/superRepresentatives", "loaded " + superRepresentatives.length + " super representatives"); return superRepresentatives; }, () => "", hours(1, 300)); export const getTronSuperRepresentatives = async () => { return await superRepresentativesCache(); }; export const hydrateSuperRepresentatives = (list) => { log("tron/superRepresentatives", "hydrate " + list.length + " super representatives"); superRepresentativesCache.hydrate("", list); }; const fetchSuperRepresentatives = async () => { const result = await fetch(`/wallet/listwitnesses`); const sorted = result.witnesses.sort((a, b) => b.voteCount - a.voteCount); const superRepresentatives = sorted.map(w => ({ ...w, address: encode58Check(w.address), voteCount: w.voteCount || 0, isJobs: w.isJobs || false, })); hydrateSuperRepresentatives(superRepresentatives); // put it in cache return superRepresentatives; }; export const getNextVotingDate = async () => { const { num } = await fetch(`/wallet/getnextmaintenancetime`); return new Date(num); }; export const getTronSuperRepresentativeData = async (max) => { const list = await getTronSuperRepresentatives(); const nextVotingDate = await getNextVotingDate(); return { list: max ? take(list, max) : list, totalVotes: sumBy(list, "voteCount"), nextVotingDate, }; }; export const voteTronSuperRepresentatives = async (account, transaction) => { const payload = { owner_address: decode58Check(account.freshAddress), votes: transaction.votes.map(v => ({ vote_address: decode58Check(v.address), vote_count: v.voteCount, })), }; return await post(`/wallet/votewitnessaccount`, payload); }; export const getUnwithdrawnReward = async (addr) => { try { const { reward = 0 } = await fetch(`/wallet/getReward?address=${encodeURIComponent(decode58Check(addr))}`); return new BigNumber(reward); } catch { return Promise.resolve(new BigNumber(0)); } }; export const claimRewardTronTransaction = async (account) => { const url = `/wallet/withdrawbalance`; const data = { owner_address: decode58Check(account.freshAddress), }; const result = await post(url, data); return result; }; //# sourceMappingURL=index.js.map