@@ -14,7 +14,7 @@ import {
1414 sleep ,
1515} from "@/utils"
1616import { STORAGE_KEYS } from "@/storage"
17- import type { Order , HexString , IGetRequest , IPostRequest } from "@/types"
17+ import type { Order , HexString , IGetRequest , IPostRequest , CancelQuote } from "@/types"
1818import type { IGetRequestMessage } from "@/chain"
1919import type { IProof } from "@/chain"
2020import type { IndexerClient } from "@/client"
@@ -62,10 +62,25 @@ export class OrderCanceller {
6262 * @returns The native token amount required to submit the cancel transaction.
6363 */
6464 async quoteCancelNative ( order : Order , fromDest : boolean = false ) : Promise < bigint > {
65+ const quote = await this . quoteCancelOrder ( order , fromDest )
66+ return quote . nativeValue
67+ }
68+
69+ /**
70+ * Returns both the native token cost and the relayer fee for cancelling an
71+ * order. Frontends can use `relayerFee` to approve the ERC-20 spend before
72+ * submitting the cancel transaction.
73+ *
74+ * @param order - The order to quote.
75+ * @param fromDest - If `true`, quotes the destination-initiated path.
76+ * @returns `{ nativeValue }` — native token amount (wei) to send as `value`;
77+ * `{ relayerFee }` — relayer incentive denominated in the chain's fee token.
78+ */
79+ async quoteCancelOrder ( order : Order , fromDest : boolean = false ) : Promise < CancelQuote > {
6580 if ( fromDest ) {
66- return this . quoteCancelNativeFromDest ( order )
81+ return this . quoteCancelFromDest ( order )
6782 }
68- return this . quoteCancelNativeFromSource ( order )
83+ return this . quoteCancelFromSource ( order )
6984 }
7085
7186 /**
@@ -78,9 +93,12 @@ export class OrderCanceller {
7893 * @param order - The order to quote.
7994 * @returns The native token dispatch fee in wei.
8095 */
81- private async quoteCancelNativeFromSource ( order : Order ) : Promise < bigint > {
82- if ( order . source === order . destination ) return 0n
96+ private async quoteCancelFromSource ( order : Order ) : Promise < CancelQuote > {
97+ if ( order . source === order . destination ) return { nativeValue : 0n , relayerFee : 0n }
8398
99+ const sourceStateMachine = order . source . startsWith ( "0x" )
100+ ? hexToString ( order . source as HexString )
101+ : ( order . source as string )
84102 const height = order . deadline + 1n
85103
86104 const destIntentGateway = this . ctx . dest . configService . getIntentGatewayV2Address (
@@ -97,7 +115,7 @@ export class OrderCanceller {
97115 const context = encodeWithdrawalRequest ( order , order . user as HexString )
98116
99117 const getRequest : IGetRequest = {
100- source : order . source . startsWith ( "0x" ) ? hexToString ( order . source as HexString ) : ( order . source as string ) ,
118+ source : sourceStateMachine ,
101119 dest : order . destination . startsWith ( "0x" )
102120 ? hexToString ( order . destination as HexString )
103121 : ( order . destination as string ) ,
@@ -109,7 +127,11 @@ export class OrderCanceller {
109127 context,
110128 }
111129
112- return await this . ctx . source . quoteNative ( getRequest , 0n )
130+ const feeInSourceFeeToken = await convertGasToFeeToken ( this . ctx , 400_000n , "source" , sourceStateMachine )
131+ const relayerFee = ( feeInSourceFeeToken * 1005n ) / 1000n
132+
133+ const nativeValue = await this . ctx . source . quoteNative ( getRequest , relayerFee )
134+ return { nativeValue, relayerFee }
113135 }
114136
115137 /**
@@ -220,18 +242,18 @@ export class OrderCanceller {
220242 STORAGE_KEYS . getRequest ( orderId ) ,
221243 )
222244 if ( ! getRequest ) {
223- const value = await this . quoteCancelNativeFromSource ( order )
245+ const quote = await this . quoteCancelFromSource ( order )
224246 const data = encodeFunctionData ( {
225247 abi : IntentGatewayV2ABI ,
226248 functionName : "cancelOrder" ,
227- args : [ transformOrderForContract ( order ) , { relayerFee : 0n , height : destIProof . height } ] ,
249+ args : [ transformOrderForContract ( order ) , { relayerFee : quote . relayerFee , height : destIProof . height } ] ,
228250 } ) as HexString
229251
230252 const signedTransaction = yield {
231253 status : "AWAITING_CANCEL_TRANSACTION" as const ,
232254 data,
233255 to : intentGatewayAddress ,
234- value,
256+ value : quote . nativeValue ,
235257 }
236258
237259 const receipt =
@@ -332,8 +354,8 @@ export class OrderCanceller {
332354 * @param order - The order to quote.
333355 * @returns The native token dispatch fee in wei.
334356 */
335- private async quoteCancelNativeFromDest ( order : Order ) : Promise < bigint > {
336- if ( order . source === order . destination ) return 0n
357+ private async quoteCancelFromDest ( order : Order ) : Promise < CancelQuote > {
358+ if ( order . source === order . destination ) return { nativeValue : 0n , relayerFee : 0n }
337359
338360 const destStateMachine = order . destination . startsWith ( "0x" )
339361 ? hexToString ( order . destination as HexString )
@@ -359,7 +381,8 @@ export class OrderCanceller {
359381 timeoutTimestamp : 0n ,
360382 }
361383
362- return await this . ctx . dest . quoteNative ( postRequest , relayerFee )
384+ const nativeValue = await this . ctx . dest . quoteNative ( postRequest , relayerFee )
385+ return { nativeValue, relayerFee }
363386 }
364387
365388 /**
@@ -399,18 +422,18 @@ export class OrderCanceller {
399422 )
400423
401424 if ( ! commitment ) {
402- const value = await this . quoteCancelNativeFromDest ( order )
425+ const quote = await this . quoteCancelFromDest ( order )
403426 const data = encodeFunctionData ( {
404427 abi : IntentGatewayV2ABI ,
405428 functionName : "cancelOrder" ,
406- args : [ transformOrderForContract ( order ) , { relayerFee : 0n , height : 0n } ] ,
429+ args : [ transformOrderForContract ( order ) , { relayerFee : quote . relayerFee , height : 0n } ] ,
407430 } ) as HexString
408431
409432 const signedTransaction = yield {
410433 status : "AWAITING_CANCEL_TRANSACTION" as const ,
411434 data,
412435 to : intentGatewayAddress ,
413- value,
436+ value : quote . nativeValue ,
414437 }
415438
416439 const receipt =
0 commit comments