@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
118 lines • 4.22 kB
JavaScript
import { encodeAccountId } from "@ledgerhq/ledger-wallet-framework/account";
import { encodeOperationId } from "@ledgerhq/ledger-wallet-framework/operation";
import { BigNumber } from "bignumber.js";
import flatMap from "lodash/flatMap";
import { fetchBalances, fetchBlockHeight, fetchTxsWithPages } from "../network/api";
import { buildTokenAccounts } from "../erc20/tokenAccounts";
import { TxStatus } from "../types";
export 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(amount);
const feeToUse = new BigNumber(fee_data?.TotalCost || 0);
const isSending = address === from;
const isReceiving = address === to;
const hasFailed = status !== TxStatus.Ok;
if (isSending) {
const type = value.eq(0) ? "FEES" : "OUT";
ops.push({
id: 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: encodeOperationId(accountId, hash, type),
hash,
type,
value,
fee: feeToUse,
blockHeight: tx.height,
blockHash: null,
accountId,
senders: [from],
recipients: [to],
date,
extra: {},
hasFailed,
});
}
return ops;
};
export const getAddress = (a) => ({ address: a.freshAddress, derivationPath: a.freshAddressPath });
export 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,
},
};
};
export 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 = encodeAccountId({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: address,
derivationMode,
});
const [blockHeight, balance, rawTxs, tokenAccounts] = await Promise.all([
fetchBlockHeight(),
fetchBalances(address),
fetchTxsWithPages(address, lastHeight),
buildTokenAccounts(address, lastHeight, accountId, info.initialAccount),
]);
const result = {
id: accountId,
subAccounts: tokenAccounts,
balance: new BigNumber(balance.total_balance),
spendableBalance: new BigNumber(balance.spendable_balance),
operations: flatMap(rawTxs, mapTxToOps(accountId, info)).sort((a, b) => b.date.getTime() - a.date.getTime()),
blockHeight: blockHeight.current_block_identifier.index,
};
return result;
};
export const getSubAccount = (account, tx) => {
const subAccount = tx.subAccountId && account.subAccounts
? account.subAccounts.find(sa => sa.id === tx.subAccountId)
: null;
return subAccount;
};
/**
* convert a value in a given unit to a normalized value
* For instance, for 1 BTC, valueFromUnit(1, btcUnit) returns 100000000
* @memberof countervalue
*/
export const valueFromUnit = (valueInUnit, unit) => valueInUnit.times(new BigNumber(10).pow(unit.magnitude));
//# sourceMappingURL=utils.js.map