@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
311 lines • 12.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMockERC20Transfer = exports.createMockEstimatedFeesResponse = exports.createMockTransactionResponse = exports.createMockBalanceResponse = exports.createMockTokenAccount = exports.createMockOperation = exports.createMockTransaction = exports.createMockAccount = exports.TEST_BLOCK_HEIGHTS = exports.TEST_TRANSACTION_HASHES = exports.TEST_ADDRESSES = void 0;
const index_1 = require("@ledgerhq/ledger-wallet-framework/account/index");
const operation_1 = require("@ledgerhq/ledger-wallet-framework/operation");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
/**
* Sample Filecoin addresses for testing
*/
exports.TEST_ADDRESSES = {
// f1 address (SECP256K1)
F1_ADDRESS: "f1abjxfbp274xpdqcpuaykwkfb43omjotacm2p3za",
// f4 address (delegated)
F4_ADDRESS: "f410fkkld55ioe7qg24wvt7fu6pbknb56ht7ptloy",
// Another f1 address for recipient
RECIPIENT_F1: "f1z4nykg7q6q5qnxs7h4zknhlqbqhq5jxcqm5qw4y",
// Another f4 address for recipient
RECIPIENT_F4: "f410fagkr6pfqzd5q2kj42qrj54g3sxqjsrqn4fhoy",
// ERC20 contract address (f4)
ERC20_CONTRACT: "f410f3huuiyauahgjp7xjbvb7yfpuhvpyxvnqx3qy",
};
/**
* Sample transaction hashes for testing
*/
exports.TEST_TRANSACTION_HASHES = {
VALID: "bafy2bzacedpqzd6qm2r7nvxj5oetpqvhujwwmvkhz4u3xnfzdvwzxpjzuqhpa",
};
/**
* Sample block heights for testing
*/
exports.TEST_BLOCK_HEIGHTS = {
CURRENT: 3000000,
RECENT: 2999999,
OLD: 2500000,
};
/**
* Create a mock Filecoin account for testing
*
* @param options Optional parameters to override default values
* @returns A mock Filecoin account
*/
const createMockAccount = (options) => {
const seedIdentifier = options?.seedIdentifier || exports.TEST_ADDRESSES.F1_ADDRESS;
// For Filecoin, we need to handle the currency ID appropriately
// Using a mock currency structure similar to what getCryptoCurrencyById would return
const mockCurrency = {
type: "CryptoCurrency",
id: "filecoin",
coinType: 461,
name: "Filecoin",
managerAppName: "Filecoin",
ticker: "FIL",
scheme: "filecoin",
color: "#0090FF",
family: "filecoin",
units: [
{
name: "FIL",
code: "FIL",
magnitude: 18,
},
{
name: "attoFIL",
code: "attoFIL",
magnitude: 0,
},
],
explorerViews: [
{
address: "https://filfox.info/en/address/$address",
tx: "https://filfox.info/en/message/$hash",
},
],
};
const account = {
type: "Account",
id: options?.id ||
(0, index_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: "filecoin",
xpubOrAddress: seedIdentifier,
derivationMode: "",
}),
seedIdentifier,
derivationMode: "",
index: options?.index || 0,
freshAddress: options?.freshAddress || seedIdentifier,
freshAddressPath: options?.freshAddressPath || "44'/461'/0'/0/0",
blockHeight: options?.blockHeight || exports.TEST_BLOCK_HEIGHTS.CURRENT,
balance: options?.balance instanceof bignumber_js_1.default
? options.balance
: new bignumber_js_1.default(options?.balance || "1000000000000000000"), // 1 FIL
spendableBalance: options?.spendableBalance instanceof bignumber_js_1.default
? options.spendableBalance
: new bignumber_js_1.default(options?.spendableBalance || "1000000000000000000"), // 1 FIL
operations: options?.operations || [],
pendingOperations: options?.pendingOperations || [],
currency: options?.currency || mockCurrency,
operationsCount: options?.operationsCount || 0,
subAccounts: options?.subAccounts || [],
swapHistory: [],
balanceHistoryCache: {
HOUR: { latestDate: null, balances: [] },
DAY: { latestDate: null, balances: [] },
WEEK: { latestDate: null, balances: [] },
},
creationDate: options?.creationDate || new Date(),
lastSyncDate: options?.lastSyncDate || new Date(),
nfts: options?.nfts || [],
used: true,
};
return { ...account, ...options };
};
exports.createMockAccount = createMockAccount;
/**
* Create a mock Filecoin transaction for testing
*
* @param options Optional parameters to override default values
* @returns A mock Filecoin transaction
*/
const createMockTransaction = (options) => {
const transaction = {
family: "filecoin",
amount: options?.amount instanceof bignumber_js_1.default
? options.amount
: new bignumber_js_1.default(options?.amount || "100000000000000000"), // 0.1 FIL
recipient: options?.recipient || exports.TEST_ADDRESSES.RECIPIENT_F1,
useAllAmount: options?.useAllAmount || false,
nonce: options?.nonce || 0,
method: options?.method || 0,
version: options?.version || 0,
params: options?.params,
gasLimit: options?.gasLimit || new bignumber_js_1.default(1000000),
gasFeeCap: options?.gasFeeCap || new bignumber_js_1.default("100000"),
gasPremium: options?.gasPremium || new bignumber_js_1.default("100000"),
};
return { ...transaction, ...options };
};
exports.createMockTransaction = createMockTransaction;
/**
* Create a mock Filecoin operation for testing
*
* @param account The account associated with the operation
* @param transaction The transaction details
* @param options Optional parameters to override default values
* @returns A mock Filecoin operation
*/
const createMockOperation = (account, transaction, options) => {
const hash = options?.hash || exports.TEST_TRANSACTION_HASHES.VALID;
const type = options?.type || "OUT";
const fee = transaction.gasLimit.multipliedBy(transaction.gasFeeCap).plus(transaction.gasPremium);
const operation = {
id: options?.id || (0, operation_1.encodeOperationId)(account.id, hash, type),
hash,
type,
senders: options?.senders || [account.freshAddress],
recipients: options?.recipients || [transaction.recipient],
accountId: account.id,
blockHash: options?.blockHash || null,
blockHeight: options?.blockHeight || null,
value: type === "OUT" ? transaction.amount.plus(fee) : transaction.amount,
fee,
date: options?.date || new Date(),
extra: options?.extra || {},
hasFailed: options?.hasFailed || false,
};
return { ...operation, ...options };
};
exports.createMockOperation = createMockOperation;
/**
* Create a mock TokenAccount for testing ERC20 functionality
*
* @param parentAccount The parent Filecoin account
* @param options Optional parameters to override default values
* @returns A mock TokenAccount
*/
const createMockTokenAccount = (parentAccount, options) => {
const tokenAccount = {
type: "TokenAccount",
id: options?.id || `${parentAccount.id}+${exports.TEST_ADDRESSES.ERC20_CONTRACT}`,
parentId: parentAccount.id,
token: options?.token || {
type: "TokenCurrency",
id: `filecoin/erc20/test_token`,
contractAddress: exports.TEST_ADDRESSES.ERC20_CONTRACT,
parentCurrencyId: parentAccount.currency.id,
tokenType: "erc20",
name: "Test Token",
ticker: "TST",
units: [
{
name: "TST",
code: "TST",
magnitude: 18,
},
],
disableCountervalue: false,
},
balance: options?.balance instanceof bignumber_js_1.default
? options.balance
: new bignumber_js_1.default(options?.balance || "1000000000000000000"), // 1 TST
spendableBalance: options?.spendableBalance instanceof bignumber_js_1.default
? options.spendableBalance
: new bignumber_js_1.default(options?.spendableBalance || "1000000000000000000"),
creationDate: options?.creationDate || new Date(),
operationsCount: options?.operationsCount || 0,
operations: options?.operations || [],
pendingOperations: options?.pendingOperations || [],
swapHistory: options?.swapHistory || [],
balanceHistoryCache: options?.balanceHistoryCache || {
HOUR: { latestDate: null, balances: [] },
DAY: { latestDate: null, balances: [] },
WEEK: { latestDate: null, balances: [] },
},
};
return { ...tokenAccount, ...options };
};
exports.createMockTokenAccount = createMockTokenAccount;
/**
* Create mock API response for balance
*
* @param options Optional parameters to override default values
* @returns A mock BalanceResponse
*/
const createMockBalanceResponse = (options) => {
return {
total_balance: options?.total_balance || "1000000000000000000",
spendable_balance: options?.spendable_balance || "1000000000000000000",
locked_balance: options?.locked_balance || "0",
};
};
exports.createMockBalanceResponse = createMockBalanceResponse;
/**
* Create mock API response for transaction
*
* @param options Optional parameters to override default values
* @returns A mock TransactionResponse
*/
const createMockTransactionResponse = (options) => {
return {
hash: options?.hash || exports.TEST_TRANSACTION_HASHES.VALID,
to: options?.to || exports.TEST_ADDRESSES.RECIPIENT_F1,
from: options?.from || exports.TEST_ADDRESSES.F1_ADDRESS,
amount: options?.amount || "100000000000000000",
status: options?.status || "Ok",
type: options?.type || "transfer",
timestamp: options?.timestamp || Math.floor(Date.now() / 1000),
height: options?.height || exports.TEST_BLOCK_HEIGHTS.CURRENT,
fee_data: options?.fee_data || {
MinerFee: {
MinerAddress: "f0123",
Amount: "50000",
},
OverEstimationBurnFee: {
BurnAddress: "f099",
Amount: "25000",
},
BurnFee: {
BurnAddress: "f099",
Amount: "25000",
},
TotalCost: "100000",
},
};
};
exports.createMockTransactionResponse = createMockTransactionResponse;
/**
* Create mock API response for estimated fees
*
* @param options Optional parameters to override default values
* @returns A mock EstimatedFeesResponse
*/
const createMockEstimatedFeesResponse = (options) => {
return {
gas_limit: options?.gas_limit || 1000000,
gas_fee_cap: options?.gas_fee_cap || "100000",
gas_premium: options?.gas_premium || "100000",
nonce: options?.nonce || 0,
};
};
exports.createMockEstimatedFeesResponse = createMockEstimatedFeesResponse;
/**
* Create mock ERC20 transfer for testing token functionality
*
* @param options Optional parameters to override default values
* @returns A mock ERC20Transfer
*/
const createMockERC20Transfer = (options) => {
const transfer = {
id: options?.id || "1",
height: options?.height || exports.TEST_BLOCK_HEIGHTS.CURRENT,
type: options?.type || "erc20_transfer",
status: options?.status || "Ok",
to: options?.to || exports.TEST_ADDRESSES.RECIPIENT_F4,
from: options?.from || exports.TEST_ADDRESSES.F4_ADDRESS,
amount: options?.amount || "1000000000000000000",
contract_address: options?.contract_address || exports.TEST_ADDRESSES.ERC20_CONTRACT,
timestamp: options?.timestamp || Math.floor(Date.now() / 1000),
tx_hash: options?.tx_hash || exports.TEST_TRANSACTION_HASHES.VALID,
};
if (options?.tx_cid !== undefined) {
transfer.tx_cid = options.tx_cid;
}
return transfer;
};
exports.createMockERC20Transfer = createMockERC20Transfer;
//# sourceMappingURL=fixtures.js.map