@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
130 lines • 4.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.valueFromUnit = exports.getSubAccount = exports.getAccountShape = exports.getTxToBroadcast = exports.getAddress = exports.mapTxToOps = void 0;
const account_1 = require("@ledgerhq/ledger-wallet-framework/account");
const operation_1 = require("@ledgerhq/ledger-wallet-framework/operation");
const bignumber_js_1 = require("bignumber.js");
const flatMap_1 = __importDefault(require("lodash/flatMap"));
const api_1 = require("../network/api");
const tokenAccounts_1 = require("../erc20/tokenAccounts");
const types_1 = require("../types");
const mapTxToOps = (accountId, { address }) => (tx) => {
const { to, from, hash, timestamp, amount, fee_data, status } = tx;
const ops = [];
const date = new Date(timestamp * 1000);
const value = new bignumber_js_1.BigNumber(amount);
const feeToUse = new bignumber_js_1.BigNumber(fee_data?.TotalCost || 0);
const isSending = address === from;
const isReceiving = address === to;
const hasFailed = status !== types_1.TxStatus.Ok;
if (isSending) {
const type = value.eq(0) ? "FEES" : "OUT";
ops.push({
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
hash,
type,
value: value.plus(feeToUse),
fee: feeToUse,
blockHeight: tx.height,
blockHash: null,
accountId,
senders: [from],
recipients: [to],
date,
extra: {},
hasFailed,
});
}
if (isReceiving) {
const type = value.eq(0) ? "FEES" : "IN";
ops.push({
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
hash,
type,
value,
fee: feeToUse,
blockHeight: tx.height,
blockHash: null,
accountId,
senders: [from],
recipients: [to],
date,
extra: {},
hasFailed,
});
}
return ops;
};
exports.mapTxToOps = mapTxToOps;
const getAddress = (a) => ({ address: a.freshAddress, derivationPath: a.freshAddressPath });
exports.getAddress = getAddress;
const getTxToBroadcast = (signature, rawData) => {
const { sender, recipient, gasLimit, gasFeeCap, gasPremium, method, version, nonce, signatureType, params, value, } = rawData;
return {
message: {
version,
method,
nonce,
params: params ?? "",
to: recipient,
from: sender,
gaslimit: gasLimit.toNumber(),
gaspremium: gasPremium.toString(),
gasfeecap: gasFeeCap.toString(),
value,
},
signature: {
type: signatureType,
data: signature,
},
};
};
exports.getTxToBroadcast = getTxToBroadcast;
const getAccountShape = async (info) => {
const { address, currency, derivationMode, initialAccount } = info;
const blockSafeDelta = 1200;
let lastHeight = (initialAccount?.blockHeight ?? 0) - blockSafeDelta;
if (lastHeight < 0)
lastHeight = 0;
const accountId = (0, account_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: address,
derivationMode,
});
const [blockHeight, balance, rawTxs, tokenAccounts] = await Promise.all([
(0, api_1.fetchBlockHeight)(),
(0, api_1.fetchBalances)(address),
(0, api_1.fetchTxsWithPages)(address, lastHeight),
(0, tokenAccounts_1.buildTokenAccounts)(address, lastHeight, accountId, info.initialAccount),
]);
const result = {
id: accountId,
subAccounts: tokenAccounts,
balance: new bignumber_js_1.BigNumber(balance.total_balance),
spendableBalance: new bignumber_js_1.BigNumber(balance.spendable_balance),
operations: (0, flatMap_1.default)(rawTxs, (0, exports.mapTxToOps)(accountId, info)).sort((a, b) => b.date.getTime() - a.date.getTime()),
blockHeight: blockHeight.current_block_identifier.index,
};
return result;
};
exports.getAccountShape = getAccountShape;
const getSubAccount = (account, tx) => {
const subAccount = tx.subAccountId && account.subAccounts
? account.subAccounts.find(sa => sa.id === tx.subAccountId)
: null;
return subAccount;
};
exports.getSubAccount = getSubAccount;
/**
* convert a value in a given unit to a normalized value
* For instance, for 1 BTC, valueFromUnit(1, btcUnit) returns 100000000
* @memberof countervalue
*/
const valueFromUnit = (valueInUnit, unit) => valueInUnit.times(new bignumber_js_1.BigNumber(10).pow(unit.magnitude));
exports.valueFromUnit = valueFromUnit;
//# sourceMappingURL=utils.js.map