1
1
import {
2
+ Amount ,
2
3
Chain ,
3
4
Network ,
4
- contracts ,
5
- Amount ,
6
- baseUnits ,
7
5
amountFromBaseUnits ,
6
+ baseUnits ,
7
+ contracts ,
8
8
displayAmount ,
9
9
} from "@wormhole-foundation/sdk-base" ;
10
10
import {
@@ -86,7 +86,7 @@ export class AutomaticTokenBridgeRoute<N extends Network>
86
86
] ;
87
87
}
88
88
89
- // get the liist of destination tokens that may be recieved on the destination chain
89
+ // get the list of destination tokens that may be recieved on the destination chain
90
90
static async supportedDestinationTokens < N extends Network > (
91
91
sourceToken : TokenId ,
92
92
fromChain : ChainContext < N > ,
@@ -120,20 +120,17 @@ export class AutomaticTokenBridgeRoute<N extends Network>
120
120
try {
121
121
const options = params . options ?? this . getDefaultOptions ( ) ;
122
122
123
- const { destination } = this . request ;
124
- let nativeGasPerc = options . nativeGas ?? 0.0 ;
125
-
126
- if ( nativeGasPerc > 1.0 || nativeGasPerc < 0.0 )
123
+ if ( options . nativeGas && ( options . nativeGas > 1.0 || options . nativeGas < 0.0 ) )
127
124
throw new Error ( "Native gas must be between 0.0 and 1.0 (0% and 100%)" ) ;
128
125
129
126
// If destination is native, max out the nativeGas requested
130
- if ( destination && isNative ( destination . id . address ) && nativeGasPerc === 0.0 )
131
- nativeGasPerc = 1.0 ;
127
+ const { destination } = this . request ;
128
+ if ( isNative ( destination . id . address ) && options . nativeGas === 0.0 ) options . nativeGas = 1.0 ;
132
129
130
+ const updatedParams = { ...params , options } ;
133
131
const validatedParams : Vp = {
134
- amount : params . amount ,
135
- options : { ...params . options , nativeGas : nativeGasPerc } ,
136
- normalizedParams : await this . normalizeTransferParams ( params ) ,
132
+ ...updatedParams ,
133
+ normalizedParams : await this . normalizeTransferParams ( updatedParams ) ,
137
134
} ;
138
135
139
136
return { valid : true , params : validatedParams } ;
@@ -142,7 +139,9 @@ export class AutomaticTokenBridgeRoute<N extends Network>
142
139
}
143
140
}
144
141
145
- async normalizeTransferParams ( params : Tp ) : Promise < AutomaticTokenBridgeRoute . NormalizedParams > {
142
+ private async normalizeTransferParams (
143
+ params : Tp ,
144
+ ) : Promise < AutomaticTokenBridgeRoute . NormalizedParams > {
146
145
const amount = this . request . parseAmount ( params . amount ) ;
147
146
148
147
const inputToken = isNative ( this . request . source . id . address )
@@ -159,29 +158,22 @@ export class AutomaticTokenBridgeRoute<N extends Network>
159
158
// Min amount is fee + 5%
160
159
const minAmount = ( fee * 105n ) / 100n ;
161
160
if ( baseUnits ( amount ) < minAmount ) {
162
- throw new Error ( `Minimum amount is ${ displayAmount ( amount ) } ` ) ;
161
+ throw new Error (
162
+ `Minimum amount is ${ displayAmount ( {
163
+ amount : minAmount . toString ( ) ,
164
+ decimals : amount . decimals ,
165
+ } ) } `,
166
+ ) ;
163
167
}
164
168
165
- const transferableAmount = baseUnits ( amount ) - fee ;
166
-
167
- const { destination } = this . request ;
168
- const options = params . options ?? this . getDefaultOptions ( ) ;
169
-
170
- let nativeGasPerc = options . nativeGas ?? 0.0 ;
171
- // If destination is native, max out the nativeGas requested
172
- if ( destination && isNative ( destination . id . address ) && nativeGasPerc === 0.0 )
173
- nativeGasPerc = 1.0 ;
174
- if ( nativeGasPerc > 1.0 || nativeGasPerc < 0.0 ) {
175
- throw new Error ( "Native gas must be between 0.0 and 1.0 (0% and 100%)" ) ;
176
- }
169
+ const redeemableAmount = baseUnits ( amount ) - fee ;
177
170
178
171
// Determine nativeGas
179
172
let nativeGasAmount = 0n ;
180
- if ( nativeGasPerc > 0 ) {
181
- // TODO: currently supporting 2 decimals of the percentage requested
173
+ if ( params . options && params . options . nativeGas > 0 ) {
182
174
const scale = 10000 ;
183
- const scaledGas = BigInt ( options . nativeGas * scale ) ;
184
- nativeGasAmount = ( transferableAmount * scaledGas ) / BigInt ( scale ) ;
175
+ const scaledGas = BigInt ( params . options . nativeGas * scale ) ;
176
+ nativeGasAmount = ( redeemableAmount * scaledGas ) / BigInt ( scale ) ;
185
177
}
186
178
187
179
return {
0 commit comments