@ledgerhq/coin-celo
Version:
52 lines • 2.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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const operation_1 = require("@ledgerhq/coin-framework/operation");
const index_1 = require("@ledgerhq/coin-framework/account/index");
const MODE_TO_TYPE = {
send: "OUT",
lock: "LOCK",
unlock: "UNLOCK",
withdraw: "WITHDRAW",
vote: "VOTE",
revoke: "REVOKE",
activate: "ACTIVATE",
register: "REGISTER",
default: "FEES",
};
const buildOptimisticOperation = (account, transaction, fee) => {
const type = MODE_TO_TYPE[transaction.mode] ?? MODE_TO_TYPE.default;
const tokenAccount = (0, index_1.findSubAccountById)(account, transaction.subAccountId || "");
const isTokenTransaction = tokenAccount?.type === "TokenAccount";
const value = type === "OUT" || type === "LOCK"
? new bignumber_js_1.default(transaction.amount).plus(isTokenTransaction ? 0 : fee)
: new bignumber_js_1.default(transaction.amount);
const operation = {
id: (0, operation_1.encodeOperationId)(account.id, "", type),
hash: "",
type,
value,
fee,
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient].filter(Boolean),
accountId: isTokenTransaction ? tokenAccount.id : account.id,
date: new Date(),
extra: {
...(["ACTIVATE", "VOTE", "REVOKE"].includes(type)
? {
celoOperationValue: new bignumber_js_1.default(transaction.amount),
celoSourceValidator: transaction.recipient,
}
: {}),
},
};
return operation;
};
exports.buildOptimisticOperation = buildOptimisticOperation;
//# sourceMappingURL=buildOptimisticOperation.js.map