@ledgerhq/coin-mina
Version:
155 lines • 6.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sync = exports.getAccountShape = exports.mapRosettaTxnToOperation = void 0;
const accountId_1 = require("@ledgerhq/coin-framework/account/accountId");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const api_1 = require("../api");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const logs_1 = require("@ledgerhq/logs");
const invariant_1 = __importDefault(require("invariant"));
const mapRosettaTxnToOperation = async (accountId, address, txn) => {
try {
const hash = txn.transaction.transaction_identifier.hash;
const blockHeight = txn.block_identifier.index;
const blockHash = txn.block_identifier.hash;
const date = new Date(txn.timestamp ?? (await (0, api_1.getBlockInfo)(blockHeight)).block.timestamp);
const memo = txn.transaction.metadata?.memo || "";
let value = new bignumber_js_1.default(0);
let fee = new bignumber_js_1.default(0);
let accountCreationFee = new bignumber_js_1.default(0);
let fromAccount = "";
let toAccount = "";
let isSending = false;
let failed = false;
let redelegateTransaction = false;
for (const op of txn.transaction.operations) {
failed = failed || op.status === "Failed";
const opValue = failed ? new bignumber_js_1.default(0) : new bignumber_js_1.default(op.amount?.value ?? 0);
switch (op.type) {
case "fee_payment": {
fee = fee.plus(opValue.times(-1));
continue;
}
case "payment_receiver_inc": {
toAccount = op.account.address;
value = value.plus(opValue);
continue;
}
case "payment_source_dec": {
fromAccount = op.account.address;
if (fromAccount === address) {
isSending = true;
}
continue;
}
case "zkapp_fee_payer_dec": {
fromAccount = op.account.address;
continue;
}
case "zkapp_balance_update": {
toAccount = op.account.address;
value = value.plus(opValue);
continue;
}
case "delegate_change": {
fromAccount = op.account.address;
toAccount = op.metadata?.delegate_change_target || toAccount || "unknown";
redelegateTransaction = true;
continue;
}
case "account_creation_fee_via_payment": {
accountCreationFee = opValue.times(-1);
continue;
}
}
}
(0, invariant_1.default)(fromAccount, "mina: missing fromAccount");
(0, invariant_1.default)(toAccount, "mina: missing toAccount");
const op = {
id: "",
type: "NONE",
hash,
value,
fee,
blockHeight,
hasFailed: failed,
blockHash,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
memo,
accountCreationFee: accountCreationFee.toString(),
},
};
const ops = [];
if (isSending) {
const type = "OUT";
ops.push({
...op,
value: value.minus(accountCreationFee).plus(fee),
type,
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
});
}
else if (redelegateTransaction) {
// delegate change
const type = "REDELEGATE";
ops.push({
...op,
value: new bignumber_js_1.default(0),
type,
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
});
}
else {
const type = "IN";
ops.push({
...op,
value: value.minus(accountCreationFee),
type,
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
});
}
return ops;
}
catch (e) {
(0, logs_1.log)("error", "mina: failed to convert txn to operation", {
error: e,
transaction: txn,
});
return [];
}
};
exports.mapRosettaTxnToOperation = mapRosettaTxnToOperation;
const getAccountShape = async (info) => {
const { address, initialAccount, currency, derivationMode } = info;
const oldOperations = initialAccount?.operations || [];
const accountId = (0, accountId_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: address,
derivationMode,
});
const { blockHeight, balance, spendableBalance } = await (0, api_1.getAccount)(address);
const rosettaTxns = await (0, api_1.getTransactions)(address, initialAccount?.operations.length);
const newOperations = await Promise.all(rosettaTxns.flatMap(t => (0, exports.mapRosettaTxnToOperation)(accountId, address, t)));
const operations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations.flat());
const shape = {
id: accountId,
balance,
spendableBalance,
operationsCount: operations.length,
blockHeight,
};
return { ...shape, operations };
};
exports.getAccountShape = getAccountShape;
exports.sync = (0, jsHelpers_1.makeSync)({ getAccountShape: exports.getAccountShape });
//# sourceMappingURL=synchronisation.js.map