-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbalance-check-test.ts
More file actions
137 lines (123 loc) · 4.65 KB
/
balance-check-test.ts
File metadata and controls
137 lines (123 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import Icpay from '../src/index';
// Test the enhanced balance functionality
async function testEnhancedBalanceFeatures() {
const icpay = new Icpay({
secretKey: 'your-secret-key',
environment: 'development',
apiUrl: 'http://localhost:6201',
icHost: 'http://localhost:8080'
});
try {
console.log('=== Testing Enhanced Balance Features ===\n');
// 1. Test getting verified ledgers with price information
console.log('1. Testing getVerifiedLedgers with prices...');
const verifiedLedgers = await icpay.getVerifiedLedgers();
console.log('Verified Ledgers:', verifiedLedgers.map(ledger => ({
symbol: ledger.symbol,
canisterId: ledger.canisterId,
currentPrice: ledger.currentPrice,
lastPriceUpdate: ledger.lastPriceUpdate
})));
// 2. Test getting all ledgers with prices
console.log('\n2. Testing getAllLedgersWithPrices...');
const allLedgersWithPrices = await icpay.getAllLedgersWithPrices();
console.log('All Ledgers with Prices:', allLedgersWithPrices.map(ledger => ({
symbol: ledger.symbol,
canisterId: ledger.canisterId,
currentPrice: ledger.currentPrice
})));
// 3. Test price calculation
console.log('\n3. Testing price calculation...');
const icpLedgerId = 'ryjl3-tyaaa-aaaaa-aaaba-cai'; // ICP ledger
try {
const priceCalculation = await icpay.calculateTokenAmountFromUSD({
usdAmount: 100,
ledgerCanisterId: icpLedgerId
});
console.log('Price Calculation Result:', {
usdAmount: priceCalculation.usdAmount,
ledgerSymbol: priceCalculation.ledgerSymbol,
currentPrice: priceCalculation.currentPrice,
tokenAmountHuman: priceCalculation.tokenAmountHuman,
tokenAmountDecimals: priceCalculation.tokenAmountDecimals,
priceTimestamp: priceCalculation.priceTimestamp
});
} catch (error) {
console.log('Price calculation failed (expected if no price data):', error.message);
}
// 4. Test getting ledger info
console.log('\n4. Testing getLedgerInfo...');
try {
const ledgerInfo = await icpay.getLedgerInfo(icpLedgerId);
console.log('Ledger Info:', {
name: ledgerInfo.name,
symbol: ledgerInfo.symbol,
canisterId: ledgerInfo.canisterId,
currentPrice: ledgerInfo.currentPrice,
lastPriceUpdate: ledgerInfo.lastPriceUpdate
});
} catch (error) {
console.log('Ledger info failed:', error.message);
}
// 5. Test getting account wallet balances (from API)
console.log('\n5. Testing getAccountWalletBalances...');
try {
const accountBalances = await icpay.protected.getAccountWalletBalances();
console.log('Account Wallet Balances:', {
totalBalancesUSD: accountBalances.totalBalancesUSD,
lastUpdated: accountBalances.lastUpdated,
balances: accountBalances.balances.map(balance => ({
symbol: balance.ledgerSymbol,
balance: balance.formattedBalance,
currentPrice: balance.currentPrice
}))
});
} catch (error) {
console.log('Account wallet balances failed:', error.message);
}
// 6. Test sendFundsUsd functionality
console.log('\n6. Testing sendFundsUsd...');
try {
const usdTransaction = await icpay.sendFundsUsd({
usdAmount: 5.61, // $5.61 USD
ledgerCanisterId: icpLedgerId,
metadata: { description: 'Test USD transaction' }
});
console.log('USD Transaction Result:', {
transactionId: usdTransaction.transactionId,
status: usdTransaction.status,
amount: usdTransaction.amount,
timestamp: usdTransaction.timestamp
});
} catch (error) {
console.log('sendFundsUsd failed (expected if no wallet connected):', error.message);
}
// 7. Test transaction history (without filters)
console.log('\n7. Testing getTransactionHistory...');
try {
const history = await icpay.getTransactionHistory({
limit: 5,
offset: 0
});
console.log('Transaction History:', {
total: history.total,
hasMore: history.hasMore,
transactions: history.transactions.map(tx => ({
id: tx.id,
status: tx.status,
amount: tx.amount,
currency: tx.currency,
ledgerSymbol: tx.ledgerSymbol
}))
});
} catch (error) {
console.log('Transaction history failed:', error.message);
}
console.log('\n=== Enhanced Balance Features Test Completed ===');
} catch (error) {
console.error('Error in enhanced balance test:', error);
}
}
// Run the test
testEnhancedBalanceFeatures().catch(console.error);
export default testEnhancedBalanceFeatures;