forked from amadeusprotocol/amadeus-typescript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction-flow.ts
More file actions
106 lines (90 loc) · 3.5 KB
/
Copy pathtransaction-flow.ts
File metadata and controls
106 lines (90 loc) · 3.5 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
/**
* Complete Transaction Flow Example
*
* Demonstrates the complete flow of creating and submitting a transaction
*/
import { AmadeusSDK, TransactionBuilder, generateKeypair, toAtomicAma } from '../src/index'
async function transactionFlowExample() {
console.log('=== Complete Transaction Flow Example ===\n')
// Step 1: Initialize SDK
console.log('Step 1: Initializing SDK...')
const sdk = new AmadeusSDK({
baseUrl: 'https://nodes.amadeus.bot/api'
})
console.log('✓ SDK initialized\n')
// Step 2: Generate or use existing keypair
console.log('Step 2: Generating keypair...')
const senderKeypair = generateKeypair()
const recipientKeypair = generateKeypair()
console.log('Sender Public Key:', senderKeypair.publicKey)
console.log('Recipient Public Key:', recipientKeypair.publicKey)
console.log('✓ Keypairs generated\n')
// Step 3: Check sender balance
console.log('Step 3: Checking sender balance...')
try {
const balance = await sdk.wallet.getBalance(senderKeypair.publicKey, 'AMA')
console.log('Current Balance:', balance.balance.float, 'AMA')
console.log('Balance (atomic):', balance.balance.flat)
console.log('✓ Balance checked\n')
} catch (error) {
console.error('Error checking balance:', error)
return
}
// Step 4: Build transaction
console.log('Step 4: Building transaction...')
const transferAmount = 0.000000001 // Minimum transfer amount
const builder = new TransactionBuilder(senderKeypair.privateKey)
const { txHash, txPacked } = builder.transfer({
recipient: recipientKeypair.publicKey,
amount: transferAmount,
symbol: 'AMA'
})
console.log('Transaction Hash:', txHash)
console.log('Amount (human-readable):', transferAmount, 'AMA')
console.log('Amount (atomic units):', toAtomicAma(transferAmount))
console.log('Packed Transaction Size:', txPacked.length, 'bytes')
console.log('✓ Transaction built\n')
// Step 5: Verify transaction structure
console.log('Step 5: Verifying transaction structure...')
console.log('Transaction hash length:', txHash.length)
console.log('Packed transaction is Uint8Array:', txPacked instanceof Uint8Array)
console.log('✓ Transaction structure verified\n')
// Step 6: Submit transaction
console.log('Step 6: Submitting transaction...')
try {
const result = await sdk.transaction.submit(txPacked)
if (result.hash) {
console.log('✓ Transaction submitted successfully!')
console.log('Transaction Hash:', result.hash)
} else {
console.log('Transaction submission result:', result)
}
} catch (error) {
console.error('Error submitting transaction:', error)
console.log('Note: This is expected if the account has no balance')
}
console.log()
// Step 7: Query transaction status
console.log('Step 7: Querying transaction status...')
try {
const tx = await sdk.transaction.get(txHash)
console.log('Transaction found:', tx)
} catch (error) {
console.error('Error querying transaction status:', error)
}
console.log()
// Step 8: Submit transaction and wait for confirmation (preferred method) - Will fail if no balance
// console.log('Step 8: Submitting and waiting for confirmation...')
// try {
// const result = await sdk.transaction.submitAndWait(txPacked)
// console.log('Transaction confirmed:', result)
// } catch (error) {
// console.error('Error (expected if no balance):', error)
// }
console.log('\n=== Transaction Flow Complete ===')
}
// Run if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
transactionFlowExample().catch(console.error)
}
export { transactionFlowExample }