forked from amadeusprotocol/amadeus-typescript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
134 lines (115 loc) · 4.41 KB
/
Copy pathbasic-usage.ts
File metadata and controls
134 lines (115 loc) · 4.41 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
/**
* Basic Usage Examples
*
* This file demonstrates basic usage of the Amadeus SDK
*/
import {
AmadeusSDK,
TransactionBuilder,
generateKeypair,
toBase58,
fromBase58,
encode,
decode
} from '../src/index'
async function basicExamples() {
console.log('=== Amadeus SDK Basic Usage Examples ===\n')
// ============================================================================
// 1. Initialize SDK
// ============================================================================
console.log('1. Initializing SDK...')
const sdk = new AmadeusSDK({
baseUrl: 'https://nodes.amadeus.bot/api'
})
console.log('SDK Version:', AmadeusSDK.getVersion())
console.log('SDK initialized!\n')
// ============================================================================
// 2. Generate Keypair
// ============================================================================
console.log('2. Generating keypair...')
const keypair = generateKeypair()
console.log('Public Key:', keypair.publicKey)
console.log('Private Key:', keypair.privateKey.substring(0, 20) + '...')
console.log('Keypair generated!\n')
// ============================================================================
// 3. Query Chain Information
// ============================================================================
console.log('3. Querying chain information...')
try {
const tip = await sdk.chain.getTip()
console.log('Current Chain Height:', tip.entry.height)
console.log('Latest Block Hash:', tip.entry.hash)
const stats = await sdk.chain.getStats()
console.log('Total Entries:', stats.stats.total_entries)
console.log('Total Transactions:', stats.stats.total_transactions)
} catch (error) {
console.error('Error querying chain:', error)
}
console.log()
// ============================================================================
// 4. Query Wallet Balance
// ============================================================================
console.log('4. Querying wallet balance...')
try {
const balance = await sdk.wallet.getBalance(keypair.publicKey, 'AMA')
console.log('AMA Balance:', balance.balance.float)
console.log('Balance (atomic units):', balance.balance.flat)
const allBalances = await sdk.wallet.getAllBalances(keypair.publicKey)
console.log('All Token Balances:', Object.keys(allBalances.balances))
} catch (error) {
console.error('Error querying balance:', error)
}
console.log()
// ============================================================================
// 5. Build Transaction
// ============================================================================
console.log('5. Building transaction...')
const recipient = generateKeypair().publicKey
// Using TransactionBuilder instance
const builder = new TransactionBuilder(keypair.privateKey)
const { txHash, txPacked } = builder.transfer({
recipient,
amount: 0.000000001, // Minimum amount
symbol: 'AMA'
})
console.log('Transaction Hash:', txHash)
console.log('Transaction Packed Length:', txPacked.length, 'bytes')
console.log('Transaction built!\n')
// ============================================================================
// 6. Serialization
// ============================================================================
console.log('6. Testing serialization...')
const data = {
foo: 'bar',
count: 42,
items: [1, 2, 3],
bytes: new Uint8Array([1, 2, 3])
}
const encoded = encode(data)
const decoded = decode(encoded)
console.log('Original data:', data)
console.log('Encoded length:', encoded.length, 'bytes')
console.log('Decoded type:', decoded instanceof Map ? 'Map' : typeof decoded)
console.log('Serialization works!\n')
// ============================================================================
// 7. Base58 Encoding
// ============================================================================
console.log('7. Testing Base58 encoding...')
const bytes = new Uint8Array([1, 2, 3, 255])
const base58 = toBase58(bytes)
const decodedBytes = fromBase58(base58)
console.log('Original bytes:', Array.from(bytes))
console.log('Base58:', base58)
console.log('Decoded bytes:', Array.from(decodedBytes))
console.log(
'Round-trip successful:',
bytes.every((b, i) => b === decodedBytes[i])
)
console.log()
console.log('=== Examples Complete ===')
}
// Run examples if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
basicExamples().catch(console.error)
}
export { basicExamples }