@ledgerhq/coin-stellar
Version:
Ledger Stellar Coin integration
62 lines • 2.45 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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const operation_1 = require("@ledgerhq/coin-framework/operation");
const logic_1 = require("./logic");
const network_1 = require("../network");
async function buildOptimisticOperation(account, transaction) {
const transactionSequenceNumber = await (0, network_1.fetchSequence)(account);
const fees = transaction.fees ?? new bignumber_js_1.default(0);
const type = transaction.mode === "changeTrust" ? "OPT_IN" : "OUT";
const operation = {
id: (0, operation_1.encodeOperationId)(account.id, "", type),
hash: "",
type,
value: transaction.subAccountId ? fees : (0, logic_1.getAmountValue)(account, transaction, fees),
fee: fees,
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: account.id,
date: new Date(),
transactionSequenceNumber: transactionSequenceNumber?.plus(1).toNumber(),
extra: {
ledgerOpType: type,
blockTime: new Date(),
index: "0",
},
};
const { subAccountId } = transaction;
const { subAccounts } = account;
const tokenAccount = !subAccountId
? null
: subAccounts && subAccounts.find(ta => ta.id === subAccountId);
if (tokenAccount && subAccountId) {
operation.subOperations = [
{
id: `${subAccountId}--OUT`,
hash: "",
type: "OUT",
value: transaction.useAllAmount ? tokenAccount.balance : transaction.amount,
fee: new bignumber_js_1.default(0),
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: subAccountId,
date: new Date(),
extra: {
ledgerOpType: type,
},
},
];
}
return operation;
}
exports.buildOptimisticOperation = buildOptimisticOperation;
//# sourceMappingURL=buildOptimisticOperation.js.map