Skip to content

Commit feab1cc

Browse files
committed
fix(sdk): thread relayerFee into cancel order calldata
1 parent 56416d0 commit feab1cc

4 files changed

Lines changed: 61 additions & 18 deletions

File tree

sdk/packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hyperbridge/sdk",
3-
"version": "1.8.2",
3+
"version": "1.8.4",
44
"description": "The hyperclient SDK provides utilities for querying proofs and statuses for cross-chain requests from HyperBridge.",
55
"type": "module",
66
"types": "./dist/node/index.d.ts",

sdk/packages/sdk/src/protocols/intents/IntentGateway.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createSessionKeyStorage, createCancellationStorage, createUsedUserOpsStorage } from "@/storage"
22
import { Swap } from "@/utils/swap"
33
import type { TransactionReceipt } from "viem"
4-
import type { Order, HexString } from "@/types"
4+
import type { Order, HexString, CancelQuote } from "@/types"
55
import type {
66
PackedUserOperation,
77
SubmitBidOptions,
@@ -280,6 +280,21 @@ export class IntentGateway {
280280
return this.orderCanceller.quoteCancelNative(order, fromDest)
281281
}
282282

283+
/**
284+
* Returns both the native token cost and the relayer fee for cancelling an
285+
* order. Use `relayerFee` to approve the ERC-20 spend before submitting.
286+
*
287+
* Delegates to {@link OrderCanceller.quoteCancelOrder}.
288+
*
289+
* @param order - The order to quote cancellation for.
290+
* @param fromDest - If `true`, quotes the destination-initiated cancellation fee.
291+
* @returns `{ nativeValue }` — native token amount (wei) to send as `value`;
292+
* `{ relayerFee }` — relayer incentive denominated in the chain's fee token.
293+
*/
294+
async quoteCancelOrder(order: Order, fromDest: boolean = false): Promise<CancelQuote> {
295+
return this.orderCanceller.quoteCancelOrder(order, fromDest)
296+
}
297+
283298
/**
284299
* Async generator that cancels an order and streams status events until
285300
* cancellation is complete.

sdk/packages/sdk/src/protocols/intents/OrderCanceller.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
sleep,
1515
} from "@/utils"
1616
import { STORAGE_KEYS } from "@/storage"
17-
import type { Order, HexString, IGetRequest, IPostRequest } from "@/types"
17+
import type { Order, HexString, IGetRequest, IPostRequest, CancelQuote } from "@/types"
1818
import type { IGetRequestMessage } from "@/chain"
1919
import type { IProof } from "@/chain"
2020
import 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 =

sdk/packages/sdk/src/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ export enum TeleportStatus {
197197
REFUNDED = "REFUNDED",
198198
}
199199

200+
export interface CancelQuote {
201+
nativeValue: bigint
202+
relayerFee: bigint
203+
}
204+
200205
export interface TokenGatewayAssetTeleportedResponse {
201206
tokenGatewayAssetTeleportedV2s: {
202207
nodes: Array<{

0 commit comments

Comments
 (0)