@cordystackx/cordy_minikit 3.1.3 → 3.1.5
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/dist/client/consRuntime.d.ts.map +1 -1
- package/dist/client/consRuntime.js +3 -1
- package/dist/client/transfer.d.ts.map +1 -1
- package/dist/client/transfer.js +17 -38
- package/dist/controllers/BridgeEvmWalletModal.d.ts.map +1 -1
- package/dist/controllers/BridgeEvmWalletModal.js +2 -1
- package/dist/controllers/cctpBridge.d.ts +20 -1
- package/dist/controllers/cctpBridge.d.ts.map +1 -1
- package/dist/controllers/cctpBridge.js +308 -41
- package/dist/package.json +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consRuntime.d.ts","sourceRoot":"","sources":["../../client/consRuntime.ts"],"names":[],"mappings":"AAGA,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"consRuntime.d.ts","sourceRoot":"","sources":["../../client/consRuntime.ts"],"names":[],"mappings":"AAGA,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAW/C;AAED,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,QAAQ,GAAG,YAAY,GAAG,SAAS,EAAE,IAAI,SAAiB,GAAG,MAAM,CAEnH;AAED,wBAAsB,qBAAqB,CAAC,IAAI,SAAiB,GAAG,OAAO,CAAC,OAAO,CAAC,CAUnF"}
|
|
@@ -5,7 +5,9 @@ export function setConsPath(path) {
|
|
|
5
5
|
activeConsPath = DEFAULT_CONS_PATH;
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
let normalized = path.trim();
|
|
9
|
+
while (normalized.endsWith("/"))
|
|
10
|
+
normalized = normalized.slice(0, -1);
|
|
9
11
|
if (!normalized.startsWith("/") || normalized.startsWith("//") || /[?#\\]/.test(normalized)) {
|
|
10
12
|
throw new Error('ProvidersClientWrapper: "consPath" must be a same-origin path such as "/services/cons".');
|
|
11
13
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transfer.d.ts","sourceRoot":"","sources":["../../client/transfer.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,iBAAiB,EAAgF,MAAM,iBAAiB,CAAC;AAEvI,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,cAAc,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAK5N,MAAM,MAAM,oBAAoB,GAAG;IACjC,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;IAC5B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,iHAAiH;IACjH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,K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C;CAClB,CAAC;AAeF,wBAAgB,oBAAoB,CAAC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,iBAAiB,CAAA;CAAE,GAAG,IAAI,CA2BpF;AAsaD,+FAA+F;AAC/F,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,IAAI,EAAE,eAAe,EACrB,EAAE,EAAE,eAAe,EACnB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,iBAAiB,CAAC,CAgE5B;AAED,sGAAsG;AACtG,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAwCrE;AAsID,gFAAgF;AAChF,wBAAsB,aAAa,CAAC,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAYlG;AAySD,yEAAyE;AACzE,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,gBAAgB,SAAiC,EACjD,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,eAAe,CAAC,CAW1B;AAED,gFAAgF;AAChF,wBAAgB,eAAe,CAAC,OAAO,EAAE;IAAE,MAAM,EAAE,iBAAiB,CAAA;CAAE,GAAG,IAAI,CAE5E;AAED,2EAA2E;AAC3E,wBAAsB,cAAc,CAClC,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CAa9B"}
|
package/dist/client/transfer.js
CHANGED
|
@@ -5,34 +5,13 @@ import { BrowserProvider, Contract, formatUnits, isAddress, JsonRpcProvider, par
|
|
|
5
5
|
import { getActiveWagmiConfig } from "../config/walletConfig";
|
|
6
6
|
import { readTokenBalance } from "../controllers/Get_Balance";
|
|
7
7
|
import loadStellarBalance from "../controllers/loadStellarBalance";
|
|
8
|
-
import { cctpBridge, cctpResumeStellarToEvm } from "../controllers/cctpBridge";
|
|
8
|
+
import { cctpBridge, cctpResumeStellarToEvm, disconnectCctpEvmWallets, getStellarCctpRecoveryRecords, removeStellarCctpRecoveryRecord } from "../controllers/cctpBridge";
|
|
9
9
|
import tokenAbi from "../config/ERC20_ABI.json";
|
|
10
10
|
import { setCordyRuntimeConfig } from "./runtimeConfig";
|
|
11
11
|
import { waitForBridgeEvmWallet } from "../controllers/bridgeWalletSession";
|
|
12
12
|
import { getConfiguredEvmAssets, requireConfiguredEvmNetwork } from "./runtimeConfig";
|
|
13
13
|
const ACTIVE_WALLET_SESSION_KEY = "cordy_minikit_active_wallet_session";
|
|
14
14
|
const STELLAR_WALLET_STORAGE_KEY = "cordy_minikit_stellar_wallet";
|
|
15
|
-
async function disconnectActiveEvmWallets() {
|
|
16
|
-
const walletConfig = getActiveWagmiConfig();
|
|
17
|
-
const connections = Array.from(walletConfig.state.connections.values());
|
|
18
|
-
if (connections.length === 0)
|
|
19
|
-
return;
|
|
20
|
-
let disconnect;
|
|
21
|
-
try {
|
|
22
|
-
({ disconnect } = await import("wagmi/actions"));
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
for (const connection of connections) {
|
|
28
|
-
try {
|
|
29
|
-
await disconnect(walletConfig, { connector: connection.connector });
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
// A bridge result or its original error should not be replaced by cleanup failure.
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
15
|
// Bridge deployments and tokens are supplied through the app's bridge configuration.
|
|
37
16
|
const BRIDGE_ERC20_ABI = [
|
|
38
17
|
"function allowance(address owner,address spender) view returns (uint256)",
|
|
@@ -459,7 +438,7 @@ async function bridgeStellar(amount, from, to, recipient, options = {}) {
|
|
|
459
438
|
}
|
|
460
439
|
/** Bridge a configured ERC-20 asset between the configured internal and external EVM sides. */
|
|
461
440
|
export async function cordy_bridge(amount, from, to, recipient, options = {}) {
|
|
462
|
-
let
|
|
441
|
+
let disconnectEvmAfterCctp = false;
|
|
463
442
|
try {
|
|
464
443
|
const normalized = normalizedAmount(amount);
|
|
465
444
|
const route = resolveBridgeRoute(configuredAssets?.bridge, options.routeId);
|
|
@@ -471,7 +450,9 @@ export async function cordy_bridge(amount, from, to, recipient, options = {}) {
|
|
|
471
450
|
if (!destinationConfig) {
|
|
472
451
|
throw new Error(`Bridge destination configuration for '${to}' is missing.`);
|
|
473
452
|
}
|
|
474
|
-
|
|
453
|
+
// CCTP may use an EVM wallet on either side. Always release it after the
|
|
454
|
+
// bridge settles, including when a wallet transaction is rejected/cancelled.
|
|
455
|
+
disconnectEvmAfterCctp = route.protocol === "cctp" && ((sourceConfig.networkType === "stellar" && destinationConfig.networkType === "evm") ||
|
|
475
456
|
(sourceConfig.networkType === "evm" && destinationConfig.networkType === "stellar"));
|
|
476
457
|
const needsEvmWallet = sourceConfig.networkType === "evm"
|
|
477
458
|
|| (route.protocol === "cctp" && sourceConfig.networkType === "stellar" && destinationConfig.networkType === "evm");
|
|
@@ -518,20 +499,17 @@ export async function cordy_bridge(amount, from, to, recipient, options = {}) {
|
|
|
518
499
|
};
|
|
519
500
|
}
|
|
520
501
|
finally {
|
|
521
|
-
if (
|
|
522
|
-
await
|
|
502
|
+
if (disconnectEvmAfterCctp)
|
|
503
|
+
await disconnectCctpEvmWallets();
|
|
523
504
|
}
|
|
524
505
|
}
|
|
525
506
|
/** Resume destination delivery for the most recent Stellar-to-EVM CCTP burn saved in this browser. */
|
|
526
507
|
export async function cordy_resumeBridge() {
|
|
527
508
|
let disconnectEvmAfterResume = false;
|
|
528
509
|
try {
|
|
529
|
-
const
|
|
530
|
-
if (!
|
|
510
|
+
const recovery = getStellarCctpRecoveryRecords()[0];
|
|
511
|
+
if (!recovery)
|
|
531
512
|
throw new Error("No recoverable Stellar CCTP burn was found in this browser.");
|
|
532
|
-
const recovery = JSON.parse(raw);
|
|
533
|
-
if (!recovery.burnTransactionHash || !recovery.from || !recovery.to || !recovery.routeId)
|
|
534
|
-
throw new Error("The saved Stellar CCTP recovery record is incomplete.");
|
|
535
513
|
const route = resolveBridgeRoute(configuredAssets?.bridge, recovery.routeId);
|
|
536
514
|
const source = route[recovery.from];
|
|
537
515
|
const destination = route[recovery.to];
|
|
@@ -554,20 +532,21 @@ export async function cordy_resumeBridge() {
|
|
|
554
532
|
destinationNetworkType: "evm",
|
|
555
533
|
});
|
|
556
534
|
}
|
|
557
|
-
const
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
catch { /* Ignore storage cleanup failures after a successful mint. */ }
|
|
562
|
-
return { success: true, from: recovery.from, to: recovery.to, routeId: route.id, routeName: route.name, amount: recovery.amount, asset: recovery.asset, fromNetwork: recovery.fromNetwork, toNetwork: recovery.toNetwork, burnTransactionHash: recovery.burnTransactionHash, transactionHash, status: "submitted" };
|
|
535
|
+
const resumed = await cctpResumeStellarToEvm(recovery.burnTransactionHash, route, recovery.from, recovery.to);
|
|
536
|
+
removeStellarCctpRecoveryRecord(recovery.burnTransactionHash);
|
|
537
|
+
return { success: true, from: recovery.from, to: recovery.to, routeId: route.id, routeName: route.name, amount: recovery.amount, asset: recovery.asset, fromNetwork: recovery.fromNetwork, toNetwork: recovery.toNetwork, burnTransactionHash: recovery.burnTransactionHash, transactionHash: resumed.transactionHash, status: "submitted", ...(resumed.alreadyReceived ? { warning: "This CCTP message was already received on the destination. No additional mint was submitted." } : {}) };
|
|
563
538
|
}
|
|
564
539
|
catch (error) {
|
|
565
540
|
const errorDetail = error instanceof Error ? error.message : String(error);
|
|
541
|
+
if (error instanceof Error && error.name === "CctpMintPendingError") {
|
|
542
|
+
const pendingError = error;
|
|
543
|
+
return { success: false, status: "pending", burnTransactionHash: pendingError.burnTransactionHash, transactionHash: pendingError.mintTransactionHash, error: errorDetail, errorDetail };
|
|
544
|
+
}
|
|
566
545
|
return { success: false, status: "burned", error: errorDetail, errorDetail };
|
|
567
546
|
}
|
|
568
547
|
finally {
|
|
569
548
|
if (disconnectEvmAfterResume)
|
|
570
|
-
await
|
|
549
|
+
await disconnectCctpEvmWallets();
|
|
571
550
|
}
|
|
572
551
|
}
|
|
573
552
|
async function transferEvm(amount, recipient, requestedSymbol) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BridgeEvmWalletModal.d.ts","sourceRoot":"","sources":["../../controllers/BridgeEvmWalletModal.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BridgeEvmWalletModal.d.ts","sourceRoot":"","sources":["../../controllers/BridgeEvmWalletModal.tsx"],"names":[],"mappings":"AAmBA,MAAM,MAAM,yBAAyB,GAAG;IACtC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,WAAW,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5C,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAwEF,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAAC,EAC3C,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,iBAAiB,EAC1B,WAAW,EACX,iBAAiB,EACjB,eAAe,EACf,KAAK,EACL,IAAI,GACL,EAAE,yBAAyB,sCAmI3B"}
|
|
@@ -7,6 +7,7 @@ import { UI_Comp__css } from "../css";
|
|
|
7
7
|
import { isEvmEnvironmentConfigured } from "../config/walletConfig";
|
|
8
8
|
import { getConfiguredEvmAssets, getCordyRuntimeConfig } from "../client/runtimeConfig";
|
|
9
9
|
import { cordy_resumeBridge } from "../client/transfer";
|
|
10
|
+
import { getStellarCctpRecoveryRecords } from "./cctpBridge";
|
|
10
11
|
import { getBridgeWalletSession, rejectBridgeWalletSession, resolveBridgeWalletSession, subscribeBridgeWalletSession, } from "./bridgeWalletSession";
|
|
11
12
|
const ALWAYS_AVAILABLE_CONNECTOR_NAMES = new Set([
|
|
12
13
|
"WalletConnect",
|
|
@@ -96,7 +97,7 @@ export default function BridgeEvmWalletModal({ open: controlledOpen, onClose: co
|
|
|
96
97
|
if (recoveryStarted.current || typeof window === "undefined")
|
|
97
98
|
return;
|
|
98
99
|
try {
|
|
99
|
-
if (
|
|
100
|
+
if (getStellarCctpRecoveryRecords().length === 0)
|
|
100
101
|
return;
|
|
101
102
|
}
|
|
102
103
|
catch {
|
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
import type { CordyBridgeRouteConfig } from "../client/runtimeConfig";
|
|
2
2
|
import type { CordyBridgeResult } from "../client/transfer";
|
|
3
3
|
type CordyBridgeSide = "internal" | "external";
|
|
4
|
-
export declare function
|
|
4
|
+
export declare function disconnectCctpEvmWallets(): Promise<void>;
|
|
5
|
+
export type StellarCctpRecoveryRecord = {
|
|
6
|
+
burnTransactionHash: string;
|
|
7
|
+
from: CordyBridgeSide;
|
|
8
|
+
to: CordyBridgeSide;
|
|
9
|
+
routeId: string;
|
|
10
|
+
routeName?: string;
|
|
11
|
+
amount?: string;
|
|
12
|
+
recipient?: string;
|
|
13
|
+
asset?: string;
|
|
14
|
+
fromNetwork?: string;
|
|
15
|
+
toNetwork?: string;
|
|
16
|
+
mintTransactionHash?: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function getStellarCctpRecoveryRecords(): StellarCctpRecoveryRecord[];
|
|
19
|
+
export declare function removeStellarCctpRecoveryRecord(burnHash: string): void;
|
|
20
|
+
export declare function cctpResumeStellarToEvm(burnHash: string, route: CordyBridgeRouteConfig, from: CordyBridgeSide, to: CordyBridgeSide): Promise<{
|
|
21
|
+
transactionHash?: string;
|
|
22
|
+
alreadyReceived?: boolean;
|
|
23
|
+
}>;
|
|
5
24
|
export declare function cctpBridge(amount: string, from: CordyBridgeSide, to: CordyBridgeSide, recipient: string, route: CordyBridgeRouteConfig): Promise<CordyBridgeResult>;
|
|
6
25
|
export {};
|
|
7
26
|
//# sourceMappingURL=cctpBridge.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cctpBridge.d.ts","sourceRoot":"","sources":["../../controllers/cctpBridge.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,sBAAsB,EACvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAG5D,KAAK,eAAe,GAAG,UAAU,GAAG,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"cctpBridge.d.ts","sourceRoot":"","sources":["../../controllers/cctpBridge.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,sBAAsB,EACvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAG5D,KAAK,eAAe,GAAG,UAAU,GAAG,UAAU,CAAC;AAG/C,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,IAAI,CAAC,CAiB9D;AAuCD,MAAM,MAAM,yBAAyB,GAAG;IACtC,mBAAmB,EAAE,MAAM,CAAC;IAC5B,IAAI,EAAE,eAAe,CAAC;IACtB,EAAE,EAAE,eAAe,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAiBF,wBAAgB,6BAA6B,IAAI,yBAAyB,EAAE,CAE3E;AAQD,wBAAgB,+BAA+B,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAOtE;AAmaD,wBAAsB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,sBAAsB,EAAE,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC;IAAE,eAAe,CAAC,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,CAU1M;AAED,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,eAAe,EACrB,EAAE,EAAE,eAAe,EACnB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,sBAAsB,GAC5B,OAAO,CAAC,iBAAiB,CAAC,CAkH5B"}
|
|
@@ -1,8 +1,29 @@
|
|
|
1
|
-
import { BrowserProvider, Contract, isAddress, parseUnits } from "ethers";
|
|
1
|
+
import { BrowserProvider, Contract, isAddress, isError, parseUnits } from "ethers";
|
|
2
2
|
import { Address, Contract as StellarContract, nativeToScVal, rpc, StrKey, TransactionBuilder, xdr } from "@stellar/stellar-sdk";
|
|
3
3
|
import { getNetworkDetails, requestAccess, signTransaction as freighterSignTransaction } from "@stellar/freighter-api";
|
|
4
4
|
import { getCordyRuntimeConfig } from "../client/runtimeConfig";
|
|
5
5
|
import { getActiveWagmiConfig } from "../config/walletConfig";
|
|
6
|
+
export async function disconnectCctpEvmWallets() {
|
|
7
|
+
const walletConfig = getActiveWagmiConfig();
|
|
8
|
+
const connections = Array.from(walletConfig.state.connections.values());
|
|
9
|
+
if (connections.length === 0)
|
|
10
|
+
return;
|
|
11
|
+
let disconnect;
|
|
12
|
+
try {
|
|
13
|
+
({ disconnect } = await import("wagmi/actions"));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (const connection of connections) {
|
|
19
|
+
try {
|
|
20
|
+
await disconnect(walletConfig, { connector: connection.connector });
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Preserve the bridge result if wallet cleanup fails.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
6
27
|
async function evmStep(step, action) {
|
|
7
28
|
try {
|
|
8
29
|
return await action();
|
|
@@ -30,9 +51,85 @@ const CCTP_MESSENGER_ABI = [
|
|
|
30
51
|
];
|
|
31
52
|
const MESSAGE_TRANSMITTER_ABI = [
|
|
32
53
|
"function receiveMessage(bytes message,bytes attestation)",
|
|
54
|
+
"function usedNonces(bytes32 nonce) view returns (bool)",
|
|
33
55
|
];
|
|
34
56
|
const EMPTY_DESTINATION_CALLER = `0x${"00".repeat(32)}`;
|
|
35
57
|
const STELLAR_CCTP_RECOVERY_KEY = "cordy_minikit_stellar_cctp_recovery";
|
|
58
|
+
const STELLAR_CONFIRMATION_POLL_INTERVAL_MS = 2000;
|
|
59
|
+
const STELLAR_CONFIRMATION_MAX_POLLS = 60;
|
|
60
|
+
const CCTP_MINT_LOCK_TTL_MS = 15 * 60 * 1000;
|
|
61
|
+
const activeCctpMints = new Set();
|
|
62
|
+
function readStellarCctpRecoveryMap() {
|
|
63
|
+
try {
|
|
64
|
+
const stored = window.localStorage.getItem(STELLAR_CCTP_RECOVERY_KEY);
|
|
65
|
+
if (!stored)
|
|
66
|
+
return {};
|
|
67
|
+
const parsed = JSON.parse(stored);
|
|
68
|
+
const legacyRecord = parsed;
|
|
69
|
+
if (typeof legacyRecord.burnTransactionHash === "string") {
|
|
70
|
+
return { [legacyRecord.burnTransactionHash]: legacyRecord };
|
|
71
|
+
}
|
|
72
|
+
return parsed;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export function getStellarCctpRecoveryRecords() {
|
|
79
|
+
return Object.values(readStellarCctpRecoveryMap()).filter((record) => Boolean(record?.burnTransactionHash && record?.routeId));
|
|
80
|
+
}
|
|
81
|
+
function saveStellarCctpRecoveryRecord(record) {
|
|
82
|
+
const records = readStellarCctpRecoveryMap();
|
|
83
|
+
records[record.burnTransactionHash] = record;
|
|
84
|
+
window.localStorage.setItem(STELLAR_CCTP_RECOVERY_KEY, JSON.stringify(records));
|
|
85
|
+
}
|
|
86
|
+
export function removeStellarCctpRecoveryRecord(burnHash) {
|
|
87
|
+
const records = readStellarCctpRecoveryMap();
|
|
88
|
+
delete records[burnHash];
|
|
89
|
+
try {
|
|
90
|
+
if (Object.keys(records).length)
|
|
91
|
+
window.localStorage.setItem(STELLAR_CCTP_RECOVERY_KEY, JSON.stringify(records));
|
|
92
|
+
else
|
|
93
|
+
window.localStorage.removeItem(STELLAR_CCTP_RECOVERY_KEY);
|
|
94
|
+
}
|
|
95
|
+
catch { /* Ignore storage cleanup failures. */ }
|
|
96
|
+
}
|
|
97
|
+
class CctpMintPendingError extends Error {
|
|
98
|
+
constructor(burnTransactionHash, mintTransactionHash) {
|
|
99
|
+
super(`Destination mint recovery for burn ${burnTransactionHash} is already in progress. Check the pending wallet transaction before retrying.`);
|
|
100
|
+
this.burnTransactionHash = burnTransactionHash;
|
|
101
|
+
this.mintTransactionHash = mintTransactionHash;
|
|
102
|
+
this.name = "CctpMintPendingError";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
class EvmTransactionCancelledError extends Error {
|
|
106
|
+
constructor(replacementHash) {
|
|
107
|
+
super("The wallet cancelled the EVM transaction by replacing it with a cancellation transaction.");
|
|
108
|
+
this.replacementHash = replacementHash;
|
|
109
|
+
this.name = "EvmTransactionCancelledError";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function waitForReplaceableEvmTransaction(transaction, startBlock) {
|
|
113
|
+
try {
|
|
114
|
+
return await transaction.replaceableTransaction(startBlock).wait();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (!isError(error, "TRANSACTION_REPLACED"))
|
|
118
|
+
throw error;
|
|
119
|
+
if (error.cancelled)
|
|
120
|
+
throw new EvmTransactionCancelledError(error.receipt?.hash);
|
|
121
|
+
return error.receipt;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
class StellarTransactionPendingError extends Error {
|
|
125
|
+
constructor(transactionHash, detail) {
|
|
126
|
+
super(detail
|
|
127
|
+
? `Stellar transaction ${transactionHash} was submitted, but confirmation could not be checked: ${detail}`
|
|
128
|
+
: `Stellar transaction ${transactionHash} was submitted but is still unconfirmed after ${STELLAR_CONFIRMATION_MAX_POLLS * STELLAR_CONFIRMATION_POLL_INTERVAL_MS / 1000} seconds.`);
|
|
129
|
+
this.transactionHash = transactionHash;
|
|
130
|
+
this.name = "StellarTransactionPendingError";
|
|
131
|
+
}
|
|
132
|
+
}
|
|
36
133
|
async function getConnectedEvmProvider(connection) {
|
|
37
134
|
const connector = connection.connector;
|
|
38
135
|
if (typeof connector.getProvider === "function") {
|
|
@@ -103,11 +200,20 @@ async function submitStellarSorobanTransaction(server, contractId, method, args,
|
|
|
103
200
|
if (result.status === "ERROR")
|
|
104
201
|
throw new Error(`Stellar transaction submission failed: ${JSON.stringify(result)}`);
|
|
105
202
|
onSubmitted?.(result.hash);
|
|
106
|
-
let completed
|
|
107
|
-
|
|
108
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
203
|
+
let completed;
|
|
204
|
+
try {
|
|
109
205
|
completed = await server.getTransaction(result.hash);
|
|
206
|
+
for (let poll = 0; completed.status === "NOT_FOUND" && poll < STELLAR_CONFIRMATION_MAX_POLLS; poll += 1) {
|
|
207
|
+
await new Promise((resolve) => setTimeout(resolve, STELLAR_CONFIRMATION_POLL_INTERVAL_MS));
|
|
208
|
+
completed = await server.getTransaction(result.hash);
|
|
209
|
+
}
|
|
110
210
|
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
213
|
+
throw new StellarTransactionPendingError(result.hash, detail);
|
|
214
|
+
}
|
|
215
|
+
if (completed.status === "NOT_FOUND")
|
|
216
|
+
throw new StellarTransactionPendingError(result.hash);
|
|
111
217
|
if (completed.status !== "SUCCESS")
|
|
112
218
|
throw new Error(`Stellar transaction failed: ${JSON.stringify(completed)}`);
|
|
113
219
|
return result.hash;
|
|
@@ -178,6 +284,38 @@ async function bridgeStellarToEvm(amount, from, to, recipient, route) {
|
|
|
178
284
|
throw new Error("Configure stellarNetwork.sorobanRpcUrl for Stellar CCTP.");
|
|
179
285
|
}
|
|
180
286
|
const destinationWallet = await getDestinationEvmWallet(destination);
|
|
287
|
+
const pendingRecovery = getStellarCctpRecoveryRecords().find((record) => record.routeId === route.id && record.from === from && record.to === to);
|
|
288
|
+
if (pendingRecovery) {
|
|
289
|
+
try {
|
|
290
|
+
const resumed = await finishStellarToEvm(pendingRecovery.burnTransactionHash, source, destination, destinationWallet);
|
|
291
|
+
removeStellarCctpRecoveryRecord(pendingRecovery.burnTransactionHash);
|
|
292
|
+
return {
|
|
293
|
+
success: true,
|
|
294
|
+
asset: pendingRecovery.asset ?? source.assetSymbol,
|
|
295
|
+
from,
|
|
296
|
+
to,
|
|
297
|
+
fromNetwork: pendingRecovery.fromNetwork ?? source.networkName,
|
|
298
|
+
toNetwork: pendingRecovery.toNetwork ?? destination.networkName,
|
|
299
|
+
routeId: route.id,
|
|
300
|
+
routeName: route.name,
|
|
301
|
+
amount: pendingRecovery.amount,
|
|
302
|
+
burnTransactionHash: pendingRecovery.burnTransactionHash,
|
|
303
|
+
transactionHash: resumed.hash,
|
|
304
|
+
status: "submitted",
|
|
305
|
+
...(resumed.alreadyReceived ? { warning: "This CCTP message was already received on the destination; no additional mint was submitted." } : {}),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
const errorDetail = error instanceof Error ? error.message : String(error);
|
|
310
|
+
return {
|
|
311
|
+
success: false,
|
|
312
|
+
...pendingRecovery,
|
|
313
|
+
status: error instanceof CctpMintPendingError ? "pending" : "burned",
|
|
314
|
+
error: errorDetail,
|
|
315
|
+
errorDetail,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
}
|
|
181
319
|
const access = await requestAccess();
|
|
182
320
|
const sourceAddress = access.address;
|
|
183
321
|
if (!sourceAddress)
|
|
@@ -192,40 +330,60 @@ async function bridgeStellarToEvm(amount, from, to, recipient, route) {
|
|
|
192
330
|
const amountIn = parseUnits(amount, 7);
|
|
193
331
|
const owner = new Address(sourceAddress).toScVal();
|
|
194
332
|
const messenger = new Address(source.tokenMessengerMinter).toScVal();
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
333
|
+
let approveHash;
|
|
334
|
+
try {
|
|
335
|
+
approveHash = await submitStellarSorobanTransaction(server, source.tokenAddress, "approve", [
|
|
336
|
+
owner,
|
|
337
|
+
messenger,
|
|
338
|
+
nativeToScVal(amountIn, { type: "i128" }),
|
|
339
|
+
nativeToScVal(latestLedger.sequence + 100000, { type: "u32" }),
|
|
340
|
+
], networkPassphrase, sourceAddress);
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
if (error instanceof StellarTransactionPendingError) {
|
|
344
|
+
return { success: false, asset: source.assetSymbol, from, to, routeId: route.id, routeName: route.name, amount, status: "pending", transactionHash: error.transactionHash, error: error.message, errorDetail: error.message };
|
|
345
|
+
}
|
|
346
|
+
throw error;
|
|
347
|
+
}
|
|
201
348
|
const recoveryForBurn = (burnTransactionHash) => ({ burnTransactionHash, from, to, routeId: route.id, routeName: route.name, amount, recipient, asset: source.assetSymbol, fromNetwork: source.networkName, toNetwork: destination.networkName });
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
349
|
+
let burnHash;
|
|
350
|
+
try {
|
|
351
|
+
burnHash = await submitStellarSorobanTransaction(server, source.tokenMessengerMinter, "deposit_for_burn", [
|
|
352
|
+
owner,
|
|
353
|
+
nativeToScVal(amountIn, { type: "i128" }),
|
|
354
|
+
nativeToScVal(destination.domain, { type: "u32" }),
|
|
355
|
+
evmAddressBytes32(recipient),
|
|
356
|
+
new Address(source.tokenAddress).toScVal(),
|
|
357
|
+
xdr.ScVal.scvBytes(Buffer.alloc(32)),
|
|
358
|
+
nativeToScVal(0n, { type: "i128" }),
|
|
359
|
+
nativeToScVal(2000, { type: "u32" }),
|
|
360
|
+
], networkPassphrase, sourceAddress, (hash) => {
|
|
361
|
+
try {
|
|
362
|
+
saveStellarCctpRecoveryRecord(recoveryForBurn(hash));
|
|
363
|
+
}
|
|
364
|
+
catch { /* Recovery storage may be unavailable in restricted browsers. */ }
|
|
365
|
+
window.dispatchEvent(new CustomEvent("cordy:stellar-cctp-burn-submitted", { detail: { transactionHash: hash } }));
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
if (error instanceof StellarTransactionPendingError) {
|
|
370
|
+
const recovery = recoveryForBurn(error.transactionHash);
|
|
371
|
+
try {
|
|
372
|
+
saveStellarCctpRecoveryRecord(recovery);
|
|
373
|
+
}
|
|
374
|
+
catch { /* Recovery storage may be unavailable in restricted browsers. */ }
|
|
375
|
+
return { success: false, ...recovery, status: "pending", transactionHash: error.transactionHash, error: error.message, errorDetail: error.message };
|
|
214
376
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
});
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
218
379
|
const recovery = recoveryForBurn(burnHash);
|
|
219
380
|
try {
|
|
220
|
-
|
|
381
|
+
saveStellarCctpRecoveryRecord(recovery);
|
|
221
382
|
}
|
|
222
383
|
catch { /* Recovery storage may be unavailable in restricted browsers. */ }
|
|
223
384
|
try {
|
|
224
385
|
const result = await finishStellarToEvm(burnHash, source, destination, destinationWallet);
|
|
225
|
-
|
|
226
|
-
window.localStorage.removeItem(STELLAR_CCTP_RECOVERY_KEY);
|
|
227
|
-
}
|
|
228
|
-
catch { /* Ignore storage cleanup failures. */ }
|
|
386
|
+
removeStellarCctpRecoveryRecord(burnHash);
|
|
229
387
|
return {
|
|
230
388
|
success: true,
|
|
231
389
|
asset: source.assetSymbol,
|
|
@@ -240,24 +398,131 @@ async function bridgeStellarToEvm(amount, from, to, recipient, route) {
|
|
|
240
398
|
status: "submitted",
|
|
241
399
|
approvalTransactionHash: approveHash,
|
|
242
400
|
burnTransactionHash: burnHash,
|
|
401
|
+
...(result.alreadyReceived ? { warning: "This CCTP message was already received on the destination; no additional mint was submitted." } : {}),
|
|
243
402
|
};
|
|
244
403
|
}
|
|
245
404
|
catch (error) {
|
|
246
405
|
const errorDetail = error instanceof Error ? error.message : String(error);
|
|
406
|
+
if (error instanceof CctpMintPendingError) {
|
|
407
|
+
return { success: false, ...recovery, status: "pending", transactionHash: error.mintTransactionHash, error: errorDetail, errorDetail };
|
|
408
|
+
}
|
|
247
409
|
return { success: false, ...recovery, status: "burned", error: `The Stellar burn succeeded (${burnHash}), but destination minting did not finish: ${errorDetail}`, errorDetail };
|
|
248
410
|
}
|
|
249
411
|
}
|
|
250
412
|
async function finishStellarToEvm(burnHash, source, destination, destinationWallet) {
|
|
251
413
|
if (source.domain === undefined || !destination.messageTransmitter)
|
|
252
414
|
throw new Error("The saved CCTP route is missing its source domain or destination transmitter.");
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
415
|
+
if (activeCctpMints.has(burnHash))
|
|
416
|
+
throw new CctpMintPendingError(burnHash);
|
|
417
|
+
activeCctpMints.add(burnHash);
|
|
418
|
+
const lockKey = `${STELLAR_CCTP_RECOVERY_KEY}:mint:${burnHash}`;
|
|
419
|
+
let mintTransactionHash;
|
|
420
|
+
let lockWritten = false;
|
|
421
|
+
let transmitter;
|
|
422
|
+
let nonce;
|
|
423
|
+
try {
|
|
424
|
+
const attestation = await waitForCctpAttestation(source.domain, burnHash);
|
|
425
|
+
const signer = await destinationWallet.provider.getSigner(destinationWallet.connection.accounts[0]);
|
|
426
|
+
transmitter = new Contract(destination.messageTransmitter, MESSAGE_TRANSMITTER_ABI, signer);
|
|
427
|
+
if (!/^0x[\da-fA-F]{88,}$/.test(attestation.message))
|
|
428
|
+
throw new Error("Circle returned a malformed CCTP message; its nonce cannot be checked.");
|
|
429
|
+
nonce = `0x${attestation.message.slice(2 + 24, 2 + 24 + 64)}`;
|
|
430
|
+
if (await transmitter.usedNonces(nonce)) {
|
|
431
|
+
try {
|
|
432
|
+
window.localStorage.removeItem(lockKey);
|
|
433
|
+
}
|
|
434
|
+
catch { /* The on-chain nonce is authoritative. */ }
|
|
435
|
+
return { alreadyReceived: true };
|
|
436
|
+
}
|
|
437
|
+
let previousLock;
|
|
438
|
+
try {
|
|
439
|
+
const rawLock = window.localStorage.getItem(lockKey);
|
|
440
|
+
previousLock = rawLock ? JSON.parse(rawLock) : undefined;
|
|
441
|
+
}
|
|
442
|
+
catch { /* Ignore malformed or inaccessible lock data and continue with the nonce check. */ }
|
|
443
|
+
if (previousLock?.expiresAt && previousLock.expiresAt > Date.now()) {
|
|
444
|
+
throw new CctpMintPendingError(burnHash, previousLock.mintTransactionHash);
|
|
445
|
+
}
|
|
446
|
+
const lock = { burnTransactionHash: burnHash, expiresAt: Date.now() + CCTP_MINT_LOCK_TTL_MS };
|
|
447
|
+
try {
|
|
448
|
+
window.localStorage.setItem(lockKey, JSON.stringify(lock));
|
|
449
|
+
lockWritten = true;
|
|
450
|
+
}
|
|
451
|
+
catch { /* In-memory guard still protects retries in this tab. */ }
|
|
452
|
+
const startBlock = await signer.provider.getBlockNumber();
|
|
453
|
+
const mintTransaction = await transmitter.receiveMessage(attestation.message, attestation.attestation);
|
|
454
|
+
mintTransactionHash = mintTransaction.hash;
|
|
455
|
+
const extendedLock = { ...lock, mintTransactionHash, expiresAt: Date.now() + CCTP_MINT_LOCK_TTL_MS };
|
|
456
|
+
try {
|
|
457
|
+
window.localStorage.setItem(lockKey, JSON.stringify(extendedLock));
|
|
458
|
+
const recovery = readStellarCctpRecoveryMap()[burnHash];
|
|
459
|
+
if (recovery)
|
|
460
|
+
saveStellarCctpRecoveryRecord({ ...recovery, mintTransactionHash });
|
|
461
|
+
}
|
|
462
|
+
catch { /* Keep the persistent pre-submit lock if storage becomes unavailable. */ }
|
|
463
|
+
let receipt;
|
|
464
|
+
try {
|
|
465
|
+
receipt = await waitForReplaceableEvmTransaction(mintTransaction, startBlock);
|
|
466
|
+
}
|
|
467
|
+
catch (error) {
|
|
468
|
+
if (error instanceof EvmTransactionCancelledError) {
|
|
469
|
+
try {
|
|
470
|
+
window.localStorage.removeItem(lockKey);
|
|
471
|
+
}
|
|
472
|
+
catch { /* A cancelled tx cannot consume this CCTP nonce. */ }
|
|
473
|
+
lockWritten = false;
|
|
474
|
+
const recovery = readStellarCctpRecoveryMap()[burnHash];
|
|
475
|
+
if (recovery) {
|
|
476
|
+
const { mintTransactionHash: _ignored, ...withoutMintHash } = recovery;
|
|
477
|
+
try {
|
|
478
|
+
saveStellarCctpRecoveryRecord(withoutMintHash);
|
|
479
|
+
}
|
|
480
|
+
catch { /* Keep the burn record for another resume attempt. */ }
|
|
481
|
+
}
|
|
482
|
+
mintTransactionHash = undefined;
|
|
483
|
+
}
|
|
484
|
+
throw error;
|
|
485
|
+
}
|
|
486
|
+
if (receipt?.hash) {
|
|
487
|
+
mintTransactionHash = receipt.hash;
|
|
488
|
+
try {
|
|
489
|
+
const lock = { burnTransactionHash: burnHash, mintTransactionHash, expiresAt: Date.now() + CCTP_MINT_LOCK_TTL_MS };
|
|
490
|
+
window.localStorage.setItem(lockKey, JSON.stringify(lock));
|
|
491
|
+
const recovery = readStellarCctpRecoveryMap()[burnHash];
|
|
492
|
+
if (recovery)
|
|
493
|
+
saveStellarCctpRecoveryRecord({ ...recovery, mintTransactionHash });
|
|
494
|
+
}
|
|
495
|
+
catch { /* The receipt hash is still returned to the caller below. */ }
|
|
496
|
+
}
|
|
497
|
+
if (!receipt || receipt.status !== 1)
|
|
498
|
+
throw new Error("EVM CCTP mint transaction failed.");
|
|
499
|
+
try {
|
|
500
|
+
window.localStorage.removeItem(lockKey);
|
|
501
|
+
}
|
|
502
|
+
catch { /* Nonce check protects the next retry. */ }
|
|
503
|
+
return { hash: receipt.hash || mintTransactionHash };
|
|
504
|
+
}
|
|
505
|
+
catch (error) {
|
|
506
|
+
if (lockWritten && !mintTransactionHash) {
|
|
507
|
+
try {
|
|
508
|
+
window.localStorage.removeItem(lockKey);
|
|
509
|
+
}
|
|
510
|
+
catch { /* Ignore lock cleanup failures. */ }
|
|
511
|
+
}
|
|
512
|
+
else if (lockWritten && mintTransactionHash) {
|
|
513
|
+
try {
|
|
514
|
+
const receipt = await destinationWallet.provider.getTransactionReceipt(mintTransactionHash);
|
|
515
|
+
if (receipt && (receipt.status === 0 || (transmitter && nonce && await transmitter.usedNonces(nonce)))) {
|
|
516
|
+
window.localStorage.removeItem(lockKey);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
catch { /* Keep the lock while transaction outcome remains unknown. */ }
|
|
520
|
+
}
|
|
521
|
+
throw error;
|
|
522
|
+
}
|
|
523
|
+
finally {
|
|
524
|
+
activeCctpMints.delete(burnHash);
|
|
525
|
+
}
|
|
261
526
|
}
|
|
262
527
|
export async function cctpResumeStellarToEvm(burnHash, route, from, to) {
|
|
263
528
|
const source = route[from];
|
|
@@ -269,7 +534,7 @@ export async function cctpResumeStellarToEvm(burnHash, route, from, to) {
|
|
|
269
534
|
throw new Error("The destination CCTP message transmitter is not configured.");
|
|
270
535
|
const wallet = await getDestinationEvmWallet(destination);
|
|
271
536
|
const result = await finishStellarToEvm(burnHash, source, destination, wallet);
|
|
272
|
-
return result.hash;
|
|
537
|
+
return { transactionHash: result.hash, alreadyReceived: result.alreadyReceived };
|
|
273
538
|
}
|
|
274
539
|
export async function cctpBridge(amount, from, to, recipient, route) {
|
|
275
540
|
const source = route[from];
|
|
@@ -322,8 +587,9 @@ export async function cctpBridge(amount, from, to, recipient, route) {
|
|
|
322
587
|
const allowance = BigInt(allowanceValue.toString());
|
|
323
588
|
let approvalTransactionHash;
|
|
324
589
|
if (allowance < amountIn) {
|
|
590
|
+
const approvalStartBlock = await signer.provider.getBlockNumber();
|
|
325
591
|
const approval = await evmStep(`submit ${source.assetSymbol} approval`, () => token.approve(source.tokenMessenger, amountIn));
|
|
326
|
-
const receipt = await evmStep("wait for the EVM approval receipt", () => approval
|
|
592
|
+
const receipt = await evmStep("wait for the EVM approval receipt", () => waitForReplaceableEvmTransaction(approval, approvalStartBlock));
|
|
327
593
|
if (!receipt || receipt.status !== 1)
|
|
328
594
|
throw new Error("CCTP token approval failed.");
|
|
329
595
|
approvalTransactionHash = receipt.hash || approval.hash;
|
|
@@ -332,6 +598,7 @@ export async function cctpBridge(amount, from, to, recipient, route) {
|
|
|
332
598
|
const destinationDomain = requireCctpArgument(destination.domain, "destination domain");
|
|
333
599
|
const maxFee = 0n;
|
|
334
600
|
const minFinalityThreshold = 2000;
|
|
601
|
+
const burnStartBlock = await signer.provider.getBlockNumber();
|
|
335
602
|
if (destination.networkType === "stellar") {
|
|
336
603
|
const forwarder = stellarContractToBytes32(destination.cctpForwarder);
|
|
337
604
|
const hookData = buildForwarderHook(recipient);
|
|
@@ -341,7 +608,7 @@ export async function cctpBridge(amount, from, to, recipient, route) {
|
|
|
341
608
|
const mintRecipient = toBytes32(recipient);
|
|
342
609
|
transaction = await evmStep("submit the EVM CCTP burn", () => messenger.depositForBurn(amountIn, destinationDomain, mintRecipient, source.tokenAddress, EMPTY_DESTINATION_CALLER, maxFee, minFinalityThreshold));
|
|
343
610
|
}
|
|
344
|
-
const receipt = await evmStep("wait for the EVM burn receipt", () => transaction
|
|
611
|
+
const receipt = await evmStep("wait for the EVM burn receipt", () => waitForReplaceableEvmTransaction(transaction, burnStartBlock));
|
|
345
612
|
if (!receipt || receipt.status !== 1)
|
|
346
613
|
throw new Error("CCTP burn transaction failed.");
|
|
347
614
|
return {
|