@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
101 lines • 4.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildOptimisticOperation = void 0;
const operation_1 = require("@ledgerhq/ledger-wallet-framework/operation");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const get_1 = __importDefault(require("lodash/get"));
const utils_1 = require("./utils");
const getValue = (account, subAccount, transaction, fee) => {
switch (transaction.mode) {
case "send":
return subAccount ? fee : new bignumber_js_1.default(transaction.amount || 0).plus(fee);
case "claimReward": {
return account.tronResources ? account.tronResources.unwithdrawnReward : new bignumber_js_1.default(0);
}
default:
return new bignumber_js_1.default(0);
}
};
const getExtra = (account, transaction, resource) => {
switch (transaction.mode) {
case "freeze":
return {
frozenAmount: transaction.amount,
};
case "unfreeze":
return {
unfreezeAmount: transaction.amount,
};
case "vote":
return {
votes: transaction.votes,
};
case "unDelegateResource":
return {
unDelegatedAmount: transaction.amount,
receiverAddress: transaction.recipient,
};
case "legacyUnfreeze":
return {
unfreezeAmount: (0, get_1.default)(account.tronResources, `frozen.${resource.toLocaleLowerCase()}.amount`, new bignumber_js_1.default(0)),
};
default:
return undefined;
}
};
const buildOptimisticOperation = (account, subAccount, transaction, fee, hash) => {
const value = getValue(account, subAccount, transaction, fee);
const operationType = (0, utils_1.getOperationTypefromMode)(transaction.mode);
const resource = transaction.resource || "BANDWIDTH";
const extra = getExtra(account, transaction, resource) || {};
/**
* FIXME
*
* This is not working and cannot work simply because this "NONE" type doesn't exist during a sync,
* as well as subOperations which are never created either.
*
* And even after fixing this, we're getting wrong fee estimation for TRC20 transactions
* which are considered as 0 all the time, while it always being between 1 and 10 TRX.
*/
const operation = {
id: (0, operation_1.encodeOperationId)(account.id, hash, operationType),
hash,
// if it's a token op and there is no fee, this operation does not exist and is a "NONE"
type: subAccount && value.eq(0) ? "NONE" : operationType,
value,
fee,
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: account.id,
date: new Date(),
extra,
subOperations: subAccount
? [
{
id: (0, operation_1.encodeOperationId)(subAccount.id, hash, "OUT"),
hash,
type: "OUT",
value: transaction.useAllAmount && subAccount
? subAccount.balance
: new bignumber_js_1.default(transaction.amount || 0),
fee: new bignumber_js_1.default(0),
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: subAccount.id,
date: new Date(),
extra: {},
},
]
: [],
};
return operation;
};
exports.buildOptimisticOperation = buildOptimisticOperation;
//# sourceMappingURL=buildOptimisticOperation.js.map