@riftresearch/sdk 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/index.d.ts +63 -19
- package/dist/index.js +129 -30
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,10 +61,10 @@ const swap = await executeSwap({
|
|
|
61
61
|
// Call your bitcoin wallet's transfer function here
|
|
62
62
|
},
|
|
63
63
|
})
|
|
64
|
-
console.log(`
|
|
64
|
+
console.log(`Order ID: ${swap.orderId}`)
|
|
65
65
|
|
|
66
66
|
// Check status
|
|
67
|
-
const status = await sdk.getSwapStatus(swap.
|
|
67
|
+
const status = await sdk.getSwapStatus(swap.orderId)
|
|
68
68
|
console.log(`Status: ${status.status}`)
|
|
69
69
|
```
|
|
70
70
|
|
|
@@ -98,7 +98,7 @@ const result = await sdk.createLimitOrder({
|
|
|
98
98
|
validUntil: Math.floor(Date.now() / 1000) + 3600, // optional: order expires in 1 hour
|
|
99
99
|
})
|
|
100
100
|
|
|
101
|
-
console.log(`Order ID: ${result.
|
|
101
|
+
console.log(`Order ID: ${result.orderId}`)
|
|
102
102
|
console.log(`Status: ${result.status}`)
|
|
103
103
|
```
|
|
104
104
|
|
|
@@ -108,7 +108,7 @@ cancellation:
|
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
110
|
const cancel = await sdk.cancelOrder({
|
|
111
|
-
|
|
111
|
+
orderId: result.orderId,
|
|
112
112
|
walletClient,
|
|
113
113
|
})
|
|
114
114
|
|
package/dist/index.d.ts
CHANGED
|
@@ -132,14 +132,57 @@ interface LimitPricing {
|
|
|
132
132
|
buyAmount: U256;
|
|
133
133
|
sellAmount: U256;
|
|
134
134
|
}
|
|
135
|
-
declare const
|
|
136
|
-
type
|
|
137
|
-
interface
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
135
|
+
declare const ANALYTICS_ORDER_STATUSES: readonly ["unfilled", "filled", "cancelled", "settled", "refunded", "failed"];
|
|
136
|
+
type AnalyticsOrderStatus = (typeof ANALYTICS_ORDER_STATUSES)[number];
|
|
137
|
+
interface ExternalDexStatus {
|
|
138
|
+
provider: string | null;
|
|
139
|
+
status: string | null;
|
|
140
|
+
}
|
|
141
|
+
interface TeeOtcStatus {
|
|
142
|
+
status: string | null;
|
|
143
|
+
}
|
|
144
|
+
interface TeeSwapperStatus {
|
|
145
|
+
status: string | null;
|
|
146
|
+
orderStatus: string | null;
|
|
147
|
+
refundState: string | null;
|
|
148
|
+
}
|
|
149
|
+
interface AnalyticsOrderResponse {
|
|
150
|
+
orderId: string;
|
|
151
|
+
routeType: string;
|
|
152
|
+
orderType: string | null;
|
|
153
|
+
provider: string | null;
|
|
154
|
+
path: string[] | null;
|
|
155
|
+
status: AnalyticsOrderStatus;
|
|
156
|
+
senderAddress: Address | null;
|
|
157
|
+
destinationAddress: Address | null;
|
|
158
|
+
refundAddress: Address | null;
|
|
159
|
+
otcSwapId: string | null;
|
|
160
|
+
swapperSwapId: string | null;
|
|
161
|
+
cowOrderUid: string | null;
|
|
162
|
+
sellCurrency: Currency | null;
|
|
163
|
+
buyCurrency: Currency | null;
|
|
164
|
+
quoteMode: "exact_input" | "exact_output" | null;
|
|
165
|
+
quotedSellAmount: U256 | null;
|
|
166
|
+
quotedBuyAmount: U256 | null;
|
|
167
|
+
quotedMinimumBuyAmount: U256 | null;
|
|
168
|
+
quotedMaximumSellAmount: U256 | null;
|
|
169
|
+
executedSellAmount: U256 | null;
|
|
170
|
+
executedBuyAmount: U256 | null;
|
|
171
|
+
executedFeeAmount: U256 | null;
|
|
172
|
+
depositTxHash: TxHash | null;
|
|
173
|
+
payoutTxHash: TxHash | null;
|
|
174
|
+
refundTxHash: TxHash | null;
|
|
175
|
+
rawRouterJson: Record<string, unknown> | null;
|
|
176
|
+
rawExternalDEXOrderJson: Record<string, unknown> | null;
|
|
177
|
+
rawOtcJson: Record<string, unknown> | null;
|
|
178
|
+
rawSwapperJson: Record<string, unknown> | null;
|
|
179
|
+
externalDexStatus: ExternalDexStatus | null;
|
|
180
|
+
teeOtcStatus: TeeOtcStatus | null;
|
|
181
|
+
teeSwapperStatus: TeeSwapperStatus | null;
|
|
182
|
+
createdAt: string;
|
|
183
|
+
updatedAt: string;
|
|
184
|
+
lastSourceUpdateAt: string | null;
|
|
185
|
+
terminalAt: string | null;
|
|
143
186
|
}
|
|
144
187
|
interface ErrorResponse {
|
|
145
188
|
error: string;
|
|
@@ -234,16 +277,16 @@ type ExecutionStep = EvmCallStep | Eip712SignStep | BtcTransferStep;
|
|
|
234
277
|
* Swap response with execution steps that the client must execute.
|
|
235
278
|
*/
|
|
236
279
|
interface SwapResponse {
|
|
237
|
-
/** The Rift
|
|
238
|
-
|
|
280
|
+
/** The Rift order ID */
|
|
281
|
+
orderId: string;
|
|
239
282
|
/** Normalized quote matching /quote response */
|
|
240
283
|
quote: QuoteResponse;
|
|
241
284
|
/** Ordered list of steps the client must execute */
|
|
242
285
|
executionSteps: ExecutionStep[];
|
|
243
286
|
}
|
|
244
287
|
interface CancelOrderResponse {
|
|
245
|
-
/** The public
|
|
246
|
-
|
|
288
|
+
/** The public order ID from the router API. */
|
|
289
|
+
orderId: string;
|
|
247
290
|
/** Whether the downstream cancellation request was accepted. */
|
|
248
291
|
accepted: boolean;
|
|
249
292
|
/** Optional downstream CoW order UID for the cancelled order. */
|
|
@@ -322,7 +365,7 @@ declare function getSupportedModes(from: Currency, to: Currency): SupportedModes
|
|
|
322
365
|
import { Chain as Chain3 } from "viem";
|
|
323
366
|
import { Account, PublicClient, Transport, WalletClient } from "viem";
|
|
324
367
|
import { Chain as Chain2 } from "viem/chains";
|
|
325
|
-
type RiftSwap =
|
|
368
|
+
type RiftSwap = AnalyticsOrderResponse;
|
|
326
369
|
interface QuoteParameters {
|
|
327
370
|
/** The currency to swap from */
|
|
328
371
|
from: Currency;
|
|
@@ -387,8 +430,8 @@ interface GetQuoteResult {
|
|
|
387
430
|
executeSwap: <chain extends Chain2 | undefined = Chain2 | undefined>(context: ExecuteSwapOptions<chain>) => Promise<SwapResult>;
|
|
388
431
|
}
|
|
389
432
|
interface SwapResult {
|
|
390
|
-
|
|
391
|
-
status:
|
|
433
|
+
orderId: string;
|
|
434
|
+
status: AnalyticsOrderStatus;
|
|
392
435
|
rift: RiftSwap;
|
|
393
436
|
}
|
|
394
437
|
type CancelOrderResult = CancelOrderResponse;
|
|
@@ -450,7 +493,7 @@ type ExecuteSwapOptions<chain extends Chain2 | undefined = Chain2 | undefined> =
|
|
|
450
493
|
refundAddress?: string;
|
|
451
494
|
};
|
|
452
495
|
type CancelOrderOptions<chain extends Chain2 | undefined = Chain2 | undefined> = {
|
|
453
|
-
|
|
496
|
+
orderId: string;
|
|
454
497
|
/** Viem WalletClient for signing the router-prepared cancellation typed data */
|
|
455
498
|
walletClient: WalletClient<Transport, chain, Account | undefined>;
|
|
456
499
|
/**
|
|
@@ -529,6 +572,7 @@ declare class RiftSdk {
|
|
|
529
572
|
private buildDexSwapRevertMessage;
|
|
530
573
|
private resolveDexVenueLabel;
|
|
531
574
|
private shouldReportStepResult;
|
|
575
|
+
private deriveCowSwapEthFlowOrderId;
|
|
532
576
|
private getCowSwapApiBase;
|
|
533
577
|
private assertSufficientBalance;
|
|
534
578
|
private getAddress;
|
|
@@ -539,9 +583,9 @@ declare class RiftSdk {
|
|
|
539
583
|
private requireWalletClient;
|
|
540
584
|
private requireSendBitcoin;
|
|
541
585
|
/**
|
|
542
|
-
* Get the current status of
|
|
586
|
+
* Get the current status of an order by its ID.
|
|
543
587
|
*/
|
|
544
|
-
getSwapStatus(
|
|
588
|
+
getSwapStatus(orderId: string): Promise<AnalyticsOrderResponse>;
|
|
545
589
|
/**
|
|
546
590
|
* Cancel a supported limit order through the swap router.
|
|
547
591
|
*
|
|
@@ -552,4 +596,4 @@ declare class RiftSdk {
|
|
|
552
596
|
cancelOrder<chain extends Chain3 | undefined = Chain3 | undefined>(options: CancelOrderOptions<chain>): Promise<CancelOrderResult>;
|
|
553
597
|
}
|
|
554
598
|
declare function createRiftSdk(options: RiftSdkOptions): RiftSdk;
|
|
555
|
-
export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, TokenIdentifier, SwapStatus, SwapRouterApiError, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, QuoteResult, QuoteParameters, NativeToken, LimitPricing, GetQuoteResult, ExecutionStep, ExecutionAction, ExecuteSwapStepType, ExecuteSwapOptions, ExecuteSwapOnExecuteStepCallback, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, CreateLimitOrderOptions, Chain, CancelOrderResult, CancelOrderOptions, BtcTransferStep, BtcTransferKind, BitcoinChain };
|
|
599
|
+
export { getSupportedModes, detectRoute, createRiftSdk, createCurrency, TokenIdentifier, AnalyticsOrderStatus as SwapStatus, SwapRouterApiError, SwapRoute, SwapResult, SwapResponse, SupportedModes, SendBitcoinFn, RiftSwap, RiftSdkOptions, RiftSdk, QuoteResult, QuoteParameters, NativeToken, LimitPricing, GetQuoteResult, ExecutionStep, ExecutionAction, ExecuteSwapStepType, ExecuteSwapOptions, ExecuteSwapOnExecuteStepCallback, EvmChain, EvmCallStep, EvmCallKind, Erc20Token, Currency, Currencies, CreateLimitOrderOptions, Chain, CancelOrderResult, CancelOrderOptions, BtcTransferStep, BtcTransferKind, BitcoinChain };
|
package/dist/index.js
CHANGED
|
@@ -85,6 +85,29 @@ var Currencies = {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
|
+
// ../common/src/cowswap.ts
|
|
89
|
+
var COW_SWAP_ORDER_UID_REGEX = /^0x[0-9a-fA-F]{112}$/;
|
|
90
|
+
var COW_SWAP_ORDER_HASH_REGEX = /^0x[0-9a-fA-F]{64}$/;
|
|
91
|
+
var EVM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;
|
|
92
|
+
var COW_SWAP_ETH_FLOW_UID_VALID_TO = 4294967295;
|
|
93
|
+
function isCowSwapOrderUid(value) {
|
|
94
|
+
return COW_SWAP_ORDER_UID_REGEX.test(value);
|
|
95
|
+
}
|
|
96
|
+
function deriveCowSwapOrderUid(orderHash, owner, validTo) {
|
|
97
|
+
if (!COW_SWAP_ORDER_HASH_REGEX.test(orderHash)) {
|
|
98
|
+
throw new Error(`Invalid CowSwap order hash: ${orderHash}`);
|
|
99
|
+
}
|
|
100
|
+
if (!EVM_ADDRESS_REGEX.test(owner)) {
|
|
101
|
+
throw new Error(`Invalid CowSwap order owner address: ${owner}`);
|
|
102
|
+
}
|
|
103
|
+
if (!Number.isInteger(validTo) || validTo < 0 || validTo > COW_SWAP_ETH_FLOW_UID_VALID_TO) {
|
|
104
|
+
throw new Error(`Invalid CowSwap order validTo: ${validTo}`);
|
|
105
|
+
}
|
|
106
|
+
return `0x${orderHash.slice(2).toLowerCase()}${owner.slice(2).toLowerCase()}${validTo.toString(16).padStart(8, "0")}`;
|
|
107
|
+
}
|
|
108
|
+
function deriveCowSwapEthFlowOrderUid(orderHash, ethFlowContract) {
|
|
109
|
+
return deriveCowSwapOrderUid(orderHash, ethFlowContract, COW_SWAP_ETH_FLOW_UID_VALID_TO);
|
|
110
|
+
}
|
|
88
111
|
// src/errors.ts
|
|
89
112
|
class SwapRouterApiError extends Error {
|
|
90
113
|
status;
|
|
@@ -153,7 +176,11 @@ function getSupportedModes(from, to) {
|
|
|
153
176
|
return { exactInput: true, exactOutput: false };
|
|
154
177
|
}
|
|
155
178
|
// src/sdk.ts
|
|
156
|
-
import {
|
|
179
|
+
import {
|
|
180
|
+
decodeFunctionResult,
|
|
181
|
+
erc20Abi,
|
|
182
|
+
isAddress
|
|
183
|
+
} from "viem";
|
|
157
184
|
|
|
158
185
|
// src/client.ts
|
|
159
186
|
async function request(baseUrl, path, init) {
|
|
@@ -223,24 +250,24 @@ function postJson(baseUrl, path, body) {
|
|
|
223
250
|
function createClient(baseUrl) {
|
|
224
251
|
const normalizedBaseUrl = baseUrl.replace(/\/$/, "");
|
|
225
252
|
const swap = (params) => {
|
|
226
|
-
const
|
|
253
|
+
const orderId = encodeURIComponent(params.orderId);
|
|
227
254
|
return {
|
|
228
|
-
get: () => get(normalizedBaseUrl, `/order/${
|
|
255
|
+
get: () => get(normalizedBaseUrl, `/order/${orderId}`),
|
|
229
256
|
cancel: {
|
|
230
257
|
prepare: {
|
|
231
|
-
post: (body) => postJson(normalizedBaseUrl, `/order/${
|
|
258
|
+
post: (body) => postJson(normalizedBaseUrl, `/order/${orderId}/cancel/prepare`, body)
|
|
232
259
|
},
|
|
233
|
-
post: (body) => postJson(normalizedBaseUrl, `/order/${
|
|
260
|
+
post: (body) => postJson(normalizedBaseUrl, `/order/${orderId}/cancel`, body)
|
|
234
261
|
},
|
|
235
262
|
tx: {
|
|
236
|
-
post: (body) => postJson(normalizedBaseUrl, `/order/${
|
|
263
|
+
post: (body) => postJson(normalizedBaseUrl, `/order/${orderId}/tx`, body)
|
|
237
264
|
},
|
|
238
265
|
"refresh-step": {
|
|
239
|
-
post: (body) => postJson(normalizedBaseUrl, `/order/${
|
|
266
|
+
post: (body) => postJson(normalizedBaseUrl, `/order/${orderId}/refresh-step`, body)
|
|
240
267
|
}
|
|
241
268
|
};
|
|
242
269
|
};
|
|
243
|
-
swap.post = (body) => postJson(normalizedBaseUrl, "/
|
|
270
|
+
swap.post = (body) => postJson(normalizedBaseUrl, "/order/market", body);
|
|
244
271
|
const market = {
|
|
245
272
|
post: (body) => postJson(normalizedBaseUrl, "/order/market", body)
|
|
246
273
|
};
|
|
@@ -266,6 +293,31 @@ var MIN_LIMIT_VALIDITY_LEAD_TIME_SECONDS = 60;
|
|
|
266
293
|
var MAX_LIMIT_VALIDITY_WINDOW_SECONDS = DEFAULT_LIMIT_VALIDITY_WINDOW_SECONDS;
|
|
267
294
|
var MAX_COW_VALID_TO = 4294967295;
|
|
268
295
|
var CANCEL_AUTH_WINDOW_SECONDS = 300;
|
|
296
|
+
var ETHFLOW_ABI = [
|
|
297
|
+
{
|
|
298
|
+
name: "createOrder",
|
|
299
|
+
type: "function",
|
|
300
|
+
stateMutability: "payable",
|
|
301
|
+
inputs: [
|
|
302
|
+
{
|
|
303
|
+
name: "order",
|
|
304
|
+
type: "tuple",
|
|
305
|
+
components: [
|
|
306
|
+
{ name: "buyToken", type: "address" },
|
|
307
|
+
{ name: "receiver", type: "address" },
|
|
308
|
+
{ name: "sellAmount", type: "uint256" },
|
|
309
|
+
{ name: "buyAmount", type: "uint256" },
|
|
310
|
+
{ name: "appData", type: "bytes32" },
|
|
311
|
+
{ name: "feeAmount", type: "uint256" },
|
|
312
|
+
{ name: "validTo", type: "uint32" },
|
|
313
|
+
{ name: "partiallyFillable", type: "bool" },
|
|
314
|
+
{ name: "quoteId", type: "int64" }
|
|
315
|
+
]
|
|
316
|
+
}
|
|
317
|
+
],
|
|
318
|
+
outputs: [{ name: "orderHash", type: "bytes32" }]
|
|
319
|
+
}
|
|
320
|
+
];
|
|
269
321
|
|
|
270
322
|
class RiftSdk {
|
|
271
323
|
riftClient;
|
|
@@ -522,7 +574,7 @@ class RiftSdk {
|
|
|
522
574
|
async executeOrderFlow(params) {
|
|
523
575
|
const swapResponse = await params.createOrder();
|
|
524
576
|
this.logDebug("order created", {
|
|
525
|
-
|
|
577
|
+
orderId: swapResponse.orderId,
|
|
526
578
|
steps: swapResponse.executionSteps.length
|
|
527
579
|
});
|
|
528
580
|
this.assertEvmChainMatchForSteps(swapResponse.executionSteps, params.context);
|
|
@@ -533,7 +585,7 @@ class RiftSdk {
|
|
|
533
585
|
kind: "kind" in step ? step.kind : undefined,
|
|
534
586
|
chainId: "chainId" in step ? step.chainId : undefined
|
|
535
587
|
});
|
|
536
|
-
const result = await this.executeStep(step, params.context, swapResponse.
|
|
588
|
+
const result = await this.executeStep(step, params.context, swapResponse.orderId, params.route);
|
|
537
589
|
this.logDebug("step completed", {
|
|
538
590
|
stepId: step.id,
|
|
539
591
|
txHash: result.txHash,
|
|
@@ -544,36 +596,37 @@ class RiftSdk {
|
|
|
544
596
|
stepId: step.id,
|
|
545
597
|
kind: "kind" in step ? step.kind : undefined
|
|
546
598
|
});
|
|
547
|
-
this.unwrapEdenResult(await this.riftClient.swap({
|
|
599
|
+
this.unwrapEdenResult(await this.riftClient.swap({ orderId: swapResponse.orderId }).tx.post({
|
|
548
600
|
stepId: step.id,
|
|
549
601
|
...result
|
|
550
602
|
}));
|
|
551
603
|
}
|
|
552
604
|
}
|
|
553
605
|
this.logDebug("fetching swap status", {
|
|
554
|
-
|
|
606
|
+
orderId: swapResponse.orderId
|
|
555
607
|
});
|
|
556
|
-
const swap = this.unwrapEdenResult(await this.riftClient.swap({
|
|
608
|
+
const swap = this.unwrapEdenResult(await this.riftClient.swap({ orderId: swapResponse.orderId }).get());
|
|
557
609
|
this.logDebug("swap fetched", {
|
|
558
|
-
|
|
610
|
+
orderId: swapResponse.orderId,
|
|
559
611
|
status: swap.status
|
|
560
612
|
});
|
|
561
613
|
return this.buildSwapResult(swap, {
|
|
562
614
|
chained: params.chained,
|
|
563
|
-
|
|
615
|
+
riftOrderId: swapResponse.orderId
|
|
564
616
|
});
|
|
565
617
|
}
|
|
566
|
-
async executeStep(step, context,
|
|
618
|
+
async executeStep(step, context, orderId, route) {
|
|
567
619
|
switch (step.action) {
|
|
568
620
|
case "evm_call":
|
|
569
|
-
return this.executeEvmCallStep(step, context,
|
|
621
|
+
return this.executeEvmCallStep(step, context, orderId, route);
|
|
570
622
|
case "eip712_sign":
|
|
571
623
|
return this.executeEip712SignStep(step, context);
|
|
572
624
|
case "btc_transfer":
|
|
573
625
|
return this.executeBtcTransferStep(step, context);
|
|
574
626
|
}
|
|
627
|
+
throw new Error(`Unsupported execution step action: ${String(step)}`);
|
|
575
628
|
}
|
|
576
|
-
async executeEvmCallStep(step, context,
|
|
629
|
+
async executeEvmCallStep(step, context, orderId, route) {
|
|
577
630
|
const walletClient = this.requireWalletClient(context);
|
|
578
631
|
const publicClient = this.requirePublicClient(context);
|
|
579
632
|
const account = walletClient.account;
|
|
@@ -606,12 +659,12 @@ class RiftSdk {
|
|
|
606
659
|
throw estimateError;
|
|
607
660
|
}
|
|
608
661
|
this.logDebug("estimateGas failed; attempting refresh-step", {
|
|
609
|
-
|
|
662
|
+
orderId,
|
|
610
663
|
stepId: effectiveStep.id,
|
|
611
664
|
error: estimateError instanceof Error ? estimateError.message : String(estimateError)
|
|
612
665
|
});
|
|
613
666
|
try {
|
|
614
|
-
const refreshed = this.unwrapEdenResult(await this.riftClient.swap({
|
|
667
|
+
const refreshed = this.unwrapEdenResult(await this.riftClient.swap({ orderId })["refresh-step"].post({ stepId: effectiveStep.id }));
|
|
615
668
|
if (!refreshed?.step) {
|
|
616
669
|
throw new Error("estimateGas failed for dex_swap step and refresh-step returned no step");
|
|
617
670
|
}
|
|
@@ -628,7 +681,7 @@ class RiftSdk {
|
|
|
628
681
|
estimatedGas = await publicClient.estimateGas(txRequest);
|
|
629
682
|
} catch (retrySimulationError) {
|
|
630
683
|
this.logDebug("dex_swap simulation failed after refresh-step attempt", {
|
|
631
|
-
|
|
684
|
+
orderId,
|
|
632
685
|
stepId: effectiveStep.id,
|
|
633
686
|
error: retrySimulationError instanceof Error ? retrySimulationError.message : String(retrySimulationError)
|
|
634
687
|
});
|
|
@@ -641,6 +694,7 @@ class RiftSdk {
|
|
|
641
694
|
estimatedGas: estimatedGas.toString(),
|
|
642
695
|
gasLimit: gasLimit.toString()
|
|
643
696
|
});
|
|
697
|
+
const cowswapOrderId = effectiveStep.kind === "cowswap_eth_order" ? await this.deriveCowSwapEthFlowOrderId(effectiveStep, publicClient, account.address) : undefined;
|
|
644
698
|
await context.onExecuteStep?.(effectiveStep.kind === "approval" ? "approval" : "transaction");
|
|
645
699
|
const txHash = await walletClient.sendTransaction({
|
|
646
700
|
...txRequest,
|
|
@@ -655,7 +709,10 @@ class RiftSdk {
|
|
|
655
709
|
}
|
|
656
710
|
throw new Error(`EVM step transaction reverted (${effectiveStep.kind}) with hash ${txHash}`);
|
|
657
711
|
}
|
|
658
|
-
return
|
|
712
|
+
return {
|
|
713
|
+
txHash,
|
|
714
|
+
...cowswapOrderId ? { cowswapOrderId } : {}
|
|
715
|
+
};
|
|
659
716
|
}
|
|
660
717
|
async executeEip712SignStep(step, context) {
|
|
661
718
|
const walletClient = this.requireWalletClient(context);
|
|
@@ -739,6 +796,9 @@ class RiftSdk {
|
|
|
739
796
|
if (!orderId) {
|
|
740
797
|
throw new Error("CowSwap order submission succeeded but returned no order id");
|
|
741
798
|
}
|
|
799
|
+
if (!isCowSwapOrderUid(orderId)) {
|
|
800
|
+
throw new Error(`CowSwap order submission returned a non-UID identifier: ${orderId}`);
|
|
801
|
+
}
|
|
742
802
|
return { cowswapOrderId: orderId };
|
|
743
803
|
}
|
|
744
804
|
async executeBtcTransferStep(step, context) {
|
|
@@ -772,12 +832,12 @@ class RiftSdk {
|
|
|
772
832
|
}
|
|
773
833
|
}
|
|
774
834
|
buildSwapResult(swap, options) {
|
|
775
|
-
const
|
|
776
|
-
if (!
|
|
777
|
-
throw new Error("Missing rift
|
|
835
|
+
const riftOrderId = options?.riftOrderId;
|
|
836
|
+
if (!riftOrderId) {
|
|
837
|
+
throw new Error("Missing rift order id for swap result.");
|
|
778
838
|
}
|
|
779
839
|
return {
|
|
780
|
-
|
|
840
|
+
orderId: riftOrderId,
|
|
781
841
|
status: swap.status,
|
|
782
842
|
rift: swap
|
|
783
843
|
};
|
|
@@ -802,6 +862,45 @@ class RiftSdk {
|
|
|
802
862
|
}
|
|
803
863
|
return false;
|
|
804
864
|
}
|
|
865
|
+
async deriveCowSwapEthFlowOrderId(step, publicClient, accountAddress) {
|
|
866
|
+
this.logDebug("deriving CowSwap EthFlow order uid", {
|
|
867
|
+
stepId: step.id,
|
|
868
|
+
chainId: step.chainId,
|
|
869
|
+
to: step.to
|
|
870
|
+
});
|
|
871
|
+
let responseData;
|
|
872
|
+
try {
|
|
873
|
+
const response = await publicClient.call({
|
|
874
|
+
account: accountAddress,
|
|
875
|
+
to: step.to,
|
|
876
|
+
data: step.calldata,
|
|
877
|
+
value: step.value ? BigInt(step.value) : undefined
|
|
878
|
+
});
|
|
879
|
+
responseData = response.data;
|
|
880
|
+
} catch (error) {
|
|
881
|
+
throw new Error(`Failed to derive CowSwap EthFlow order id before submission: ${error instanceof Error ? error.message : String(error)}`);
|
|
882
|
+
}
|
|
883
|
+
if (!responseData) {
|
|
884
|
+
throw new Error("Failed to derive CowSwap EthFlow order id before submission: empty call result.");
|
|
885
|
+
}
|
|
886
|
+
let orderHash;
|
|
887
|
+
try {
|
|
888
|
+
orderHash = decodeFunctionResult({
|
|
889
|
+
abi: ETHFLOW_ABI,
|
|
890
|
+
functionName: "createOrder",
|
|
891
|
+
data: responseData
|
|
892
|
+
});
|
|
893
|
+
} catch (error) {
|
|
894
|
+
throw new Error(`Failed to decode CowSwap EthFlow order hash: ${error instanceof Error ? error.message : String(error)}`);
|
|
895
|
+
}
|
|
896
|
+
const cowswapOrderId = deriveCowSwapEthFlowOrderUid(orderHash, step.to);
|
|
897
|
+
this.logDebug("derived CowSwap EthFlow order uid", {
|
|
898
|
+
stepId: step.id,
|
|
899
|
+
chainId: step.chainId,
|
|
900
|
+
cowswapOrderId
|
|
901
|
+
});
|
|
902
|
+
return cowswapOrderId;
|
|
903
|
+
}
|
|
805
904
|
getCowSwapApiBase(chainId) {
|
|
806
905
|
if (chainId === 1)
|
|
807
906
|
return "https://api.cow.fi/mainnet";
|
|
@@ -896,8 +995,8 @@ class RiftSdk {
|
|
|
896
995
|
}
|
|
897
996
|
return context.sendBitcoin;
|
|
898
997
|
}
|
|
899
|
-
async getSwapStatus(
|
|
900
|
-
return this.unwrapEdenResult(await this.riftClient.swap({
|
|
998
|
+
async getSwapStatus(orderId) {
|
|
999
|
+
return this.unwrapEdenResult(await this.riftClient.swap({ orderId }).get());
|
|
901
1000
|
}
|
|
902
1001
|
async cancelOrder(options) {
|
|
903
1002
|
const walletClient = options.walletClient;
|
|
@@ -907,7 +1006,7 @@ class RiftSdk {
|
|
|
907
1006
|
}
|
|
908
1007
|
const deadline = options.deadline ?? Math.floor(Date.now() / 1000) + CANCEL_AUTH_WINDOW_SECONDS;
|
|
909
1008
|
this.assertValidCancelDeadline(deadline);
|
|
910
|
-
const preparation = this.unwrapEdenResult(await this.riftClient.swap({
|
|
1009
|
+
const preparation = this.unwrapEdenResult(await this.riftClient.swap({ orderId: options.orderId }).cancel.prepare.post({
|
|
911
1010
|
deadline
|
|
912
1011
|
}));
|
|
913
1012
|
this.assertCancelWalletChain(walletClient, preparation.cancellation.chainId);
|
|
@@ -918,7 +1017,7 @@ class RiftSdk {
|
|
|
918
1017
|
...preparation.cancellation.kind === "tee_swapper" ? { deadline: preparation.cancellation.deadline } : {}
|
|
919
1018
|
}
|
|
920
1019
|
};
|
|
921
|
-
return this.unwrapEdenResult(await this.riftClient.swap({
|
|
1020
|
+
return this.unwrapEdenResult(await this.riftClient.swap({ orderId: options.orderId }).cancel.post(request2));
|
|
922
1021
|
}
|
|
923
1022
|
}
|
|
924
1023
|
function createRiftSdk(options) {
|