@@ -4,30 +4,30 @@ import type {
4
4
RoutesResponse ,
5
5
Route ,
6
6
} from "@blockdaemon/blockdaemon-defi-api-typescript-fetch" ;
7
- import { aptosProvider , isAptosTransaction , log } from "../utils/common" ;
8
- import { Account , MoveStructId } from "@aptos-labs/ts-sdk" ;
7
+ import { aptosProvider , isAptosTransaction , log } from "../utils/common" ;
8
+ import { Account , MoveStructId } from "@aptos-labs/ts-sdk" ;
9
9
10
10
const logger = log . getLogger ( "routes-endpoint" ) ;
11
11
12
12
function chooseBestRoute ( routes : Route [ ] , preferredIntegrator : string ) : Route {
13
13
const topThree = [ ...routes ]
14
- . sort ( ( a , b ) => Number ( b . to . amount ) - Number ( a . to . amount ) )
15
- . slice ( 0 , 3 ) ;
14
+ . sort ( ( a , b ) => Number ( b . to . amount ) - Number ( a . to . amount ) )
15
+ . slice ( 0 , 3 ) ;
16
16
17
17
const chosen = topThree . filter (
18
- ( r ) => r . steps ?. [ 0 ] . integrationDetails ?. key === preferredIntegrator ,
18
+ ( r ) => r . steps ?. [ 0 ] . integrationDetails ?. key === preferredIntegrator ,
19
19
) ;
20
20
21
21
return chosen [ 0 ] ;
22
22
}
23
23
24
24
export async function getRoutes (
25
- exchangeAPI : ExchangeApi ,
26
- routeParameters : GetRoutesRequest ,
27
- preferredIntegrator : string = "" ,
25
+ exchangeAPI : ExchangeApi ,
26
+ routeParameters : GetRoutesRequest ,
27
+ preferredIntegrator : string = "" ,
28
28
) : Promise < Route > {
29
29
const routesResponse : RoutesResponse =
30
- await exchangeAPI . getRoutes ( routeParameters ) ;
30
+ await exchangeAPI . getRoutes ( routeParameters ) ;
31
31
logger . info ( "Got valid routes" ) ;
32
32
if ( ! routesResponse . routes || routesResponse . routes . length === 0 ) {
33
33
throw new Error ( "No valid routes returned." ) ;
@@ -37,26 +37,26 @@ export async function getRoutes(
37
37
return routesResponse . routes [ 0 ] ;
38
38
}
39
39
const selectedRoute : Route = chooseBestRoute (
40
- routesResponse . routes ,
41
- preferredIntegrator ,
40
+ routesResponse . routes ,
41
+ preferredIntegrator ,
42
42
) ;
43
43
logger . debug ( "Selected route:" , JSON . stringify ( selectedRoute , null , 2 ) ) ;
44
44
logger . info ( `Selected best route using ${ preferredIntegrator } ` ) ;
45
45
return selectedRoute ;
46
46
}
47
47
48
48
export async function executeSwap (
49
- selectedRoute : Route ,
50
- wallet : { address : string ; privateKey : string } ,
51
- rpcUrl : string ,
49
+ selectedRoute : Route ,
50
+ wallet : { address : string ; privateKey : string } ,
51
+ rpcUrl : string ,
52
52
) : Promise < { hash : string } > {
53
53
const { signAndBroadcastTransaction } = await import ( "./wallet" ) ;
54
54
logger . info ( "Executing swap transaction..." ) ;
55
55
const { transactionRequest } = selectedRoute ;
56
56
const broadcastResult = await signAndBroadcastTransaction (
57
- transactionRequest ,
58
- wallet . privateKey ,
59
- rpcUrl ,
57
+ transactionRequest ,
58
+ wallet . privateKey ,
59
+ rpcUrl ,
60
60
) ;
61
61
if ( ! broadcastResult ?. hash ) {
62
62
throw new Error ( "Failed to broadcast swap transaction" ) ;
@@ -66,16 +66,19 @@ export async function executeSwap(
66
66
}
67
67
68
68
export async function executeSwapAptos (
69
- selectedRoute : Route ,
70
- aptosAccount : Account ,
69
+ selectedRoute : Route ,
70
+ aptosAccount : Account ,
71
71
) : Promise < { hash : string } > {
72
72
logger . info ( "Executing Aptos swap transaction..." ) ;
73
73
74
74
const { transactionRequest } = selectedRoute ;
75
75
76
76
try {
77
77
if ( ! isAptosTransaction ( transactionRequest ) ) {
78
- throw new Error ( 'Expected Aptos transaction but received: ' + transactionRequest . chainType ) ;
78
+ throw new Error (
79
+ "Expected Aptos transaction but received: " +
80
+ transactionRequest . chainType ,
81
+ ) ;
79
82
}
80
83
81
84
const rawTxData = transactionRequest as any ;
@@ -84,7 +87,7 @@ export async function executeSwapAptos(
84
87
sender : rawTxData . sender ,
85
88
function : rawTxData . payload . function ,
86
89
typeArgs : rawTxData . payload . type_arguments ,
87
- args : rawTxData . payload . arguments
90
+ args : rawTxData . payload . arguments ,
88
91
} ) ;
89
92
90
93
// Format the arguments properly
@@ -94,7 +97,7 @@ export async function executeSwapAptos(
94
97
return new Uint8Array ( arg ) ;
95
98
}
96
99
// If argument is a hex string (starting with 0x), convert to bytes
97
- if ( typeof arg === ' string' && arg . startsWith ( '0x' ) ) {
100
+ if ( typeof arg === " string" && arg . startsWith ( "0x" ) ) {
98
101
// Remove '0x' prefix and convert to Uint8Array
99
102
const hexString = arg . slice ( 2 ) ;
100
103
const bytes = new Uint8Array ( hexString . length / 2 ) ;
@@ -104,11 +107,11 @@ export async function executeSwapAptos(
104
107
return bytes ;
105
108
}
106
109
// For boolean values
107
- if ( typeof arg === ' boolean' ) {
110
+ if ( typeof arg === " boolean" ) {
108
111
return arg ;
109
112
}
110
113
// For numeric values
111
- if ( typeof arg === ' number' || ! isNaN ( Number ( arg ) ) ) {
114
+ if ( typeof arg === " number" || ! isNaN ( Number ( arg ) ) ) {
112
115
return Number ( arg ) ;
113
116
}
114
117
return arg ;
@@ -120,13 +123,13 @@ export async function executeSwapAptos(
120
123
data : {
121
124
function : rawTxData . payload . function as MoveStructId ,
122
125
typeArguments : rawTxData . payload . type_arguments ,
123
- functionArguments : formattedArguments
126
+ functionArguments : formattedArguments ,
124
127
} ,
125
128
options : {
126
129
maxGasAmount : Number ( rawTxData . max_gas_amount ) ,
127
130
gasUnitPrice : Number ( rawTxData . gas_unit_price ) ,
128
- expireTimestamp : Number ( rawTxData . expiration_timestamp_secs )
129
- }
131
+ expireTimestamp : Number ( rawTxData . expiration_timestamp_secs ) ,
132
+ } ,
130
133
} ) ;
131
134
132
135
// Sign and submit the transaction
@@ -139,12 +142,16 @@ export async function executeSwapAptos(
139
142
throw new Error ( "No transaction hash returned from submission" ) ;
140
143
}
141
144
142
- const txHash = committedTxn . hash . startsWith ( '0x' ) ? committedTxn . hash : `0x${ committedTxn . hash } ` ;
143
- logger . info ( `Aptos swap transaction submitted successfully. Tx Hash: ${ txHash } ` ) ;
145
+ const txHash = committedTxn . hash . startsWith ( "0x" )
146
+ ? committedTxn . hash
147
+ : `0x${ committedTxn . hash } ` ;
148
+ logger . info (
149
+ `Aptos swap transaction submitted successfully. Tx Hash: ${ txHash } ` ,
150
+ ) ;
144
151
145
152
// Wait for transaction to be confirmed
146
153
await aptosProvider . waitForTransaction ( {
147
- transactionHash : txHash
154
+ transactionHash : txHash ,
148
155
} ) ;
149
156
150
157
logger . info ( `Transaction executed and committed: ${ txHash } ` ) ;
@@ -153,4 +160,4 @@ export async function executeSwapAptos(
153
160
logger . error ( "Failed to execute Aptos transaction:" , error ) ;
154
161
throw new Error ( `Aptos transaction execution failed: ${ error } ` ) ;
155
162
}
156
- }
163
+ }
0 commit comments