@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
93 lines • 4.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareTransaction = void 0;
const helpers_1 = require("@ledgerhq/ledger-wallet-framework/account/helpers");
const live_env_1 = require("@ledgerhq/live-env");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../constants");
const estimateFees_1 = require("../logic/estimateFees");
const utils_1 = require("../logic/utils");
const utils_2 = require("./utils");
/**
* Gather any more neccessary information for a transaction,
* potentially from a network.
*
* Hedera has fully client-side transactions and the fee
* is not possible to estimate ahead-of-time.
*
*/
const prepareTransaction = async (account, transaction) => {
let estimateFeesParams;
let operationType;
const subAccount = (0, helpers_1.findSubAccountById)(account, transaction?.subAccountId || "");
const isHTSTokenTransaction = transaction.mode === constants_1.HEDERA_TRANSACTION_MODES.Send && subAccount?.token.tokenType === "hts";
const isERC20TokenTransaction = transaction.mode === constants_1.HEDERA_TRANSACTION_MODES.Send && subAccount?.token.tokenType === "erc20";
if ((0, utils_1.isTokenAssociateTransaction)(transaction)) {
operationType = constants_1.HEDERA_OPERATION_TYPES.TokenAssociate;
}
else if (isHTSTokenTransaction) {
operationType = constants_1.HEDERA_OPERATION_TYPES.TokenTransfer;
}
else if (isERC20TokenTransaction) {
operationType = constants_1.HEDERA_OPERATION_TYPES.ContractCall;
}
else if ((0, utils_1.isStakingTransaction)(transaction)) {
operationType = constants_1.HEDERA_OPERATION_TYPES.CryptoUpdate;
}
else {
operationType = constants_1.HEDERA_OPERATION_TYPES.CryptoTransfer;
}
// build different estimation params for ERC20 ContractCall transactions
if (operationType === constants_1.HEDERA_OPERATION_TYPES.ContractCall) {
estimateFeesParams = {
operationType,
txIntent: {
intentType: "transaction",
type: constants_1.HEDERA_TRANSACTION_MODES.Send,
asset: {
type: "erc20",
assetReference: subAccount?.token.contractAddress ?? "",
assetOwner: account.freshAddress,
},
amount: BigInt(transaction.amount.toString()),
sender: account.freshAddress,
recipient: transaction.recipient,
},
};
}
else {
estimateFeesParams = {
currency: account.currency,
operationType,
};
}
// explicitly calculate transaction amount to account for `useAllAmount` flag (send max flow)
// i.e. if `useAllAmount` has been toggled to true, this is where it will update the transaction to reflect that action
const [calculatedAmount, estimatedFees] = await Promise.all([
(0, utils_2.calculateAmount)({ account, transaction }),
(0, estimateFees_1.estimateFees)(estimateFeesParams),
]);
transaction.amount = calculatedAmount.amount;
// `maxFee` must be explicitly set to avoid the @hashgraph/sdk default fallback
// this ensures device app validation passes (e.g. during swap flow)
// it's applied via `tx.setMaxTransactionFee` when building the transaction
transaction.maxFee = estimatedFees.tinybars;
// ERC20 transactions should have gas limit set (tinybars fee is calculated based on gas)
if (isERC20TokenTransaction && estimatedFees.gas) {
transaction.gasLimit = estimatedFees.gas;
}
if ((0, utils_1.isStakingTransaction)(transaction)) {
transaction.memo = constants_1.MAP_STAKING_MODE_TO_MEMO[transaction.mode];
// claiming staking rewards is triggered by sending 1 tinybar to staking reward account
if (transaction.mode === constants_1.HEDERA_TRANSACTION_MODES.ClaimRewards) {
transaction.recipient = (0, live_env_1.getEnv)("HEDERA_CLAIM_REWARDS_RECIPIENT_ACCOUNT_ID");
transaction.amount = new bignumber_js_1.default(1);
}
}
return transaction;
};
exports.prepareTransaction = prepareTransaction;
//# sourceMappingURL=prepareTransaction.js.map