-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
152 lines (131 loc) · 4.13 KB
/
index.ts
File metadata and controls
152 lines (131 loc) · 4.13 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
import { config } from 'dotenv'
import algosdk from 'algosdk'
import { RouterClient } from '@txnlab/haystack-router'
// Load environment variables
config()
interface SwapConfig {
apiKey: string
mnemonic: string
fromAssetId: number
toAssetId: number
amount: bigint
slippage: number
}
/**
* Custom transaction signer using algosdk.Account
* This is an alternative to use-wallet for Node.js environments
*/
async function createAccountSigner(mnemonic: string) {
const account = algosdk.mnemonicToSecretKey(mnemonic)
return async (
txnGroup: algosdk.Transaction[],
indexesToSign: number[],
): Promise<Uint8Array[]> => {
return indexesToSign.map((index) => {
// Sign the transaction at the specified index
return algosdk.signTransaction(txnGroup[index], account.sk).blob
})
}
}
/**
* Parse and validate environment variables
*/
function parseConfig(): SwapConfig {
const apiKey = process.env.HAYSTACK_ROUTER_API_KEY
const mnemonic = process.env.ACCOUNT_MNEMONIC
const fromAssetId = parseInt(process.env.FROM_ASSET_ID || '0', 10)
const toAssetId = parseInt(process.env.TO_ASSET_ID || '31566704', 10)
const amount = BigInt(process.env.AMOUNT || '1000000')
const slippage = parseFloat(process.env.SLIPPAGE || '1')
if (!apiKey) {
throw new Error('HAYSTACK_ROUTER_API_KEY is required in .env file')
}
if (!mnemonic) {
throw new Error('ACCOUNT_MNEMONIC is required in .env file')
}
return {
apiKey,
mnemonic,
fromAssetId,
toAssetId,
amount,
slippage,
}
}
/**
* Format asset amount for display
*/
function formatAmount(amount: bigint, decimals = 6): string {
const divisor = BigInt(10 ** decimals)
const whole = amount / divisor
const fraction = amount % divisor
return `${whole}.${fraction.toString().padStart(decimals, '0')}`
}
/**
* Main swap execution function
*/
async function executeSwap() {
console.log('🚀 Haystack Router CLI Swap Tool\n')
try {
// Parse configuration
const config = parseConfig()
console.log('📋 Configuration:')
console.log(` From Asset: ${config.fromAssetId}`)
console.log(` To Asset: ${config.toAssetId}`)
console.log(
` Amount: ${formatAmount(config.amount)} (${config.amount} microunits)`,
)
console.log(` Slippage: ${config.slippage}%\n`)
// Initialize Haystack Router client
const router = new RouterClient({
apiKey: config.apiKey,
autoOptIn: true,
})
// Get account from mnemonic
const account = algosdk.mnemonicToSecretKey(config.mnemonic)
console.log(`💼 Using account: ${account.addr}\n`)
// Create custom signer
const signer = await createAccountSigner(config.mnemonic)
// Fetch quote
console.log('📊 Fetching quote...')
const quote = await router.newQuote({
fromASAID: config.fromAssetId,
toASAID: config.toAssetId,
amount: config.amount,
address: account.addr.toString(),
})
console.log('✅ Quote received:')
console.log(` Expected output: ${formatAmount(quote.quote)} tokens`)
console.log(
` Price impact: ${quote.userPriceImpact?.toFixed(4) ?? 'N/A'}%`,
)
console.log(` USD In: $${quote.usdIn.toFixed(2)}`)
console.log(` USD Out: $${quote.usdOut.toFixed(2)}`)
console.log(` Route paths: ${quote.route.length}`)
console.log(` Flattened route:`, quote.flattenedRoute)
console.log()
// Execute swap
console.log('🔄 Executing swap...')
const swap = await router.newSwap({
quote,
address: account.addr.toString(),
signer,
slippage: config.slippage,
})
const result = await swap.execute()
console.log('✨ Swap completed successfully!')
console.log(` Confirmed in round: ${result.confirmedRound}`)
console.log(` Transaction IDs:`)
result.txIds.forEach((txId, index) => {
console.log(` ${index + 1}. ${txId}`)
})
console.log()
console.log(`🔗 View on allo: https://allo.info/tx/${result.txIds[0]}`)
} catch (error) {
console.error('❌ Error:', error instanceof Error ? error.message : error)
process.exit(1)
}
}
// Run the swap
executeSwap()