@aptos-labs/cross-chain-core 4.23.2 → 4.24.1

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/index.js CHANGED
@@ -18,14 +18,18 @@ var __copyProps = (to, from, except, desc) => {
18
18
  return to;
19
19
  };
20
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
26
  mod
23
27
  ));
24
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
29
 
26
30
  // src/index.ts
27
- var src_exports = {};
28
- __export(src_exports, {
31
+ var index_exports = {};
32
+ __export(index_exports, {
29
33
  AptosLocalSigner: () => AptosLocalSigner,
30
34
  AptosMainnetChain: () => AptosMainnetChain,
31
35
  AptosMainnetUSDCToken: () => AptosMainnetUSDCToken,
@@ -43,7 +47,7 @@ __export(src_exports, {
43
47
  testnetChains: () => testnetChains,
44
48
  testnetTokens: () => testnetTokens
45
49
  });
46
- module.exports = __toCommonJS(src_exports);
50
+ module.exports = __toCommonJS(index_exports);
47
51
 
48
52
  // src/CrossChainCore.ts
49
53
  var import_ts_sdk4 = require("@aptos-labs/ts-sdk");
@@ -93,6 +97,7 @@ var AptosLocalSigner = class {
93
97
  claimedTransactionHashes() {
94
98
  return this._claimedTransactionHashes;
95
99
  }
100
+ /* other methods... */
96
101
  async signAndSend(txs) {
97
102
  const txHashes = [];
98
103
  for (const tx of txs) {
@@ -161,13 +166,12 @@ var import_sdk_solana = require("@wormhole-foundation/sdk-solana");
161
166
  var import_web32 = require("@solana/web3.js");
162
167
  var import_derived_wallet_solana = require("@aptos-labs/derived-wallet-solana");
163
168
  async function signAndSendTransaction2(request, wallet, options, crossChainCore) {
164
- var _a, _b, _c, _d;
165
169
  if (!wallet || !(wallet instanceof import_derived_wallet_solana.SolanaDerivedWallet)) {
166
170
  throw new Error("Invalid wallet type or missing Solana wallet").message;
167
171
  }
168
- const commitment = (_a = options == null ? void 0 : options.commitment) != null ? _a : "finalized";
172
+ const commitment = options?.commitment ?? "finalized";
169
173
  const connection = new import_web32.Connection(
170
- (_d = (_c = (_b = crossChainCore == null ? void 0 : crossChainCore._dappConfig) == null ? void 0 : _b.solanaConfig) == null ? void 0 : _c.rpc) != null ? _d : "https://api.devnet.solana.com"
174
+ crossChainCore?._dappConfig?.solanaConfig?.rpc ?? "https://api.devnet.solana.com"
171
175
  );
172
176
  const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(commitment);
173
177
  const transaction = request.transaction.transaction;
@@ -186,13 +190,13 @@ async function signAndSendTransaction2(request, wallet, options, crossChainCore)
186
190
  throw new Error("Wallet does not support signing transactions").message;
187
191
  }
188
192
  const tx = await wallet.solanaWallet.signTransaction(unsignedTx);
189
- if (!tx)
190
- throw new Error("Failed to sign transaction").message;
193
+ if (!tx) throw new Error("Failed to sign transaction").message;
191
194
  const serializedTx = tx.serialize();
192
195
  const sendOptions = {
193
196
  skipPreflight: true,
194
197
  maxRetries: 0,
195
198
  preFlightCommitment: commitment
199
+ // See PR and linked issue for why setting this matters: https://github.com/anza-xyz/agave/pull/483
196
200
  };
197
201
  signature = await connection.sendRawTransaction(serializedTx, sendOptions);
198
202
  confirmTransactionPromise = connection.confirmTransaction(
@@ -232,6 +236,7 @@ async function signAndSendTransaction2(request, wallet, options, crossChainCore)
232
236
  errorMessage = `Transaction failed: ${JSON.stringify(
233
237
  confirmedTx.value.err,
234
238
  (_key, value) => typeof value === "bigint" ? value.toString() : value
239
+ // Handle bigint props
235
240
  )}`;
236
241
  } catch (e) {
237
242
  errorMessage = `Transaction failed: Unknown error`;
@@ -262,42 +267,41 @@ async function setPriorityFeeInstructions(connection, blockhash, lastValidBlockH
262
267
  return unsignedTx;
263
268
  }
264
269
  async function createPriorityFeeInstructions(connection, transaction, crossChainCore) {
265
- var _a, _b, _c;
266
270
  let unitsUsed = 2e5;
267
271
  let simulationAttempts = 0;
268
- simulationLoop:
269
- while (true) {
270
- const response = await connection.simulateTransaction(
271
- transaction
272
- );
273
- if (response.value.err) {
274
- if (checkKnownSimulationError(response.value)) {
275
- if (simulationAttempts < 5) {
276
- simulationAttempts++;
277
- await sleep(1e3);
278
- continue simulationLoop;
279
- }
280
- } else if (simulationAttempts < 3) {
272
+ simulationLoop: while (true) {
273
+ const response = await connection.simulateTransaction(
274
+ transaction
275
+ );
276
+ if (response.value.err) {
277
+ if (checkKnownSimulationError(response.value)) {
278
+ if (simulationAttempts < 5) {
281
279
  simulationAttempts++;
282
280
  await sleep(1e3);
283
281
  continue simulationLoop;
284
282
  }
285
- throw new Error(
286
- `Simulation failed: ${JSON.stringify(response.value.err)}
283
+ } else if (simulationAttempts < 3) {
284
+ simulationAttempts++;
285
+ await sleep(1e3);
286
+ continue simulationLoop;
287
+ }
288
+ throw new Error(
289
+ `Simulation failed: ${JSON.stringify(response.value.err)}
287
290
  Logs:
288
291
  ${(response.value.logs || []).join("\n ")}`
289
- ).message;
290
- } else {
291
- if (response.value.unitsConsumed) {
292
- unitsUsed = response.value.unitsConsumed;
293
- }
294
- break;
292
+ ).message;
293
+ } else {
294
+ if (response.value.unitsConsumed) {
295
+ unitsUsed = response.value.unitsConsumed;
295
296
  }
297
+ break;
296
298
  }
299
+ }
297
300
  const unitBudget = Math.floor(unitsUsed * 1.2);
298
301
  const instructions = [];
299
302
  instructions.push(
300
303
  import_web3.ComputeBudgetProgram.setComputeUnitLimit({
304
+ // Set compute budget to 120% of the units used in the simulated transaction
301
305
  units: unitBudget
302
306
  })
303
307
  );
@@ -306,7 +310,7 @@ ${(response.value.logs || []).join("\n ")}`
306
310
  percentileMultiple = 1,
307
311
  min = 1e5,
308
312
  max = 1e8
309
- } = (_c = (_b = (_a = crossChainCore == null ? void 0 : crossChainCore._dappConfig) == null ? void 0 : _a.solanaConfig) == null ? void 0 : _b.priorityFeeConfig) != null ? _c : {};
313
+ } = crossChainCore?._dappConfig?.solanaConfig?.priorityFeeConfig ?? {};
310
314
  const calculateFee = async (rpcProvider2) => {
311
315
  if (rpcProvider2 === "triton") {
312
316
  try {
@@ -349,7 +353,10 @@ ${(response.value.logs || []).join("\n ")}`
349
353
  };
350
354
  const rpcProvider = determineRpcProvider(connection.rpcEndpoint);
351
355
  const { fee, methodUsed } = await calculateFee(rpcProvider);
352
- const maxFeeInSol = fee / 1e6 / import_web3.LAMPORTS_PER_SOL * unitBudget;
356
+ const maxFeeInSol = fee / // convert microlamports to lamports
357
+ 1e6 / // convert lamports to SOL
358
+ import_web3.LAMPORTS_PER_SOL * // multiply by maximum compute units used
359
+ unitBudget;
353
360
  console.table({
354
361
  "RPC Provider": rpcProvider,
355
362
  "Method used": methodUsed,
@@ -441,7 +448,7 @@ async function signAndSendTransaction3(request, wallet, chainName, options) {
441
448
  const signer = await provider.getSigner();
442
449
  const response = await signer.sendTransaction(request.transaction);
443
450
  const receipt = await response.wait();
444
- return (receipt == null ? void 0 : receipt.hash) || "";
451
+ return receipt?.hash || "";
445
452
  }
446
453
 
447
454
  // src/providers/wormhole/signers/Signer.ts
@@ -508,8 +515,7 @@ var WormholeProvider = class {
508
515
  return this._wormholeContext;
509
516
  }
510
517
  async setWormholeContext(sourceChain) {
511
- var _a;
512
- const dappNetwork = (_a = this.crossChainCore._dappConfig) == null ? void 0 : _a.aptosNetwork;
518
+ const dappNetwork = this.crossChainCore._dappConfig?.aptosNetwork;
513
519
  if (dappNetwork === import_ts_sdk2.Network.DEVNET) {
514
520
  throw new Error("Devnet is not supported on Wormhole");
515
521
  }
@@ -541,6 +547,7 @@ var WormholeProvider = class {
541
547
  );
542
548
  const resolver = this._wormholeContext.resolver([
543
549
  import_sdk.routes.CCTPRoute
550
+ // manual CCTP
544
551
  ]);
545
552
  const route = await resolver.findRoutes(request);
546
553
  const cctpRoute = route[0];
@@ -573,7 +580,6 @@ var WormholeProvider = class {
573
580
  return quote;
574
581
  }
575
582
  async submitCCTPTransfer(input) {
576
- var _a;
577
583
  const { sourceChain, wallet, destinationAddress } = input;
578
584
  if (!this._wormholeContext) {
579
585
  await this.setWormholeContext(sourceChain);
@@ -584,7 +590,7 @@ var WormholeProvider = class {
584
590
  let signerAddress;
585
591
  const chainContext = this.getChainConfig(sourceChain).context;
586
592
  if (chainContext === "Solana") {
587
- signerAddress = ((_a = wallet.solanaWallet.publicKey) == null ? void 0 : _a.toBase58()) || "";
593
+ signerAddress = wallet.solanaWallet.publicKey?.toBase58() || "";
588
594
  } else {
589
595
  [signerAddress] = await wallet.eip1193Provider.request({
590
596
  method: "eth_requestAccounts"
@@ -625,7 +631,9 @@ var WormholeProvider = class {
625
631
  "Aptos",
626
632
  {},
627
633
  mainSigner,
634
+ // the account that signs the "claim" transaction
628
635
  sponsorAccount ? sponsorAccount : void 0
636
+ // the fee payer account
629
637
  );
630
638
  if (import_sdk.routes.isManual(this.wormholeRoute)) {
631
639
  const circleAttestationReceipt = await this.wormholeRoute.complete(signer, receipt);
@@ -653,9 +661,13 @@ var WormholeProvider = class {
653
661
  }
654
662
  return { destinationChainTxnId: "" };
655
663
  }
664
+ /**
665
+ * Initiates a transfer of USDC funds from the source chain wallet to the destination chain wallet
666
+ * @param args
667
+ * @returns
668
+ */
656
669
  async initiateCCTPTransfer(input) {
657
- var _a;
658
- if (((_a = this.crossChainCore._dappConfig) == null ? void 0 : _a.aptosNetwork) === import_ts_sdk2.Network.DEVNET) {
670
+ if (this.crossChainCore._dappConfig?.aptosNetwork === import_ts_sdk2.Network.DEVNET) {
659
671
  throw new Error("Devnet is not supported on Wormhole");
660
672
  }
661
673
  if (input.amount) {
@@ -734,6 +746,21 @@ var testnetChains = {
734
746
  defaultRpc: "https://api.devnet.solana.com",
735
747
  wrappedGasToken: "So11111111111111111111111111111111111111112"
736
748
  }
749
+ // Sui: {
750
+ // key: "Sui",
751
+ // id: 21,
752
+ // context: Context.SUI,
753
+ // finalityThreshold: 0,
754
+ // displayName: "Sui",
755
+ // explorerUrl: "https://suiscan.xyz/testnet/",
756
+ // explorerName: "Suiscan",
757
+ // gasToken: "SUI",
758
+ // chainId: 0,
759
+ // icon: "Sui",
760
+ // maxBlockSearch: 0,
761
+ // symbol: "SUI",
762
+ // sdkName: "Sui",
763
+ // },
737
764
  };
738
765
  var AptosTestnetChain = {
739
766
  key: "Aptos",
@@ -771,6 +798,16 @@ var testnetTokens = {
771
798
  icon: "USDC",
772
799
  decimals: 6
773
800
  }
801
+ // Sui: {
802
+ // symbol: "USDC",
803
+ // tokenId: {
804
+ // chain: "Sui",
805
+ // address:
806
+ // "0xa1ec7fc00a6f40db9693ad1415d0c193ad3906494428cf252621037bd7117e29::usdc::USDC",
807
+ // },
808
+ // icon: "USDC",
809
+ // decimals: 6,
810
+ // },
774
811
  };
775
812
  var AptosTestnetUSDCToken = {
776
813
  symbol: "USDC",
@@ -814,6 +851,21 @@ var mainnetChains = {
814
851
  symbol: "SOL",
815
852
  defaultRpc: "https://solana-mainnet.rpc.extrnode.com"
816
853
  }
854
+ // Sui: {
855
+ // key: "Sui",
856
+ // id: 21,
857
+ // context: Context.SUI,
858
+ // finalityThreshold: 0,
859
+ // displayName: "Sui",
860
+ // sdkName: "Sui",
861
+ // explorerUrl: "https://suiscan.xyz/",
862
+ // explorerName: "Suiscan",
863
+ // gasToken: "SUI",
864
+ // chainId: 0,
865
+ // icon: "Sui",
866
+ // maxBlockSearch: 0,
867
+ // symbol: "SUI",
868
+ // },
817
869
  };
818
870
  var AptosMainnetChain = {
819
871
  key: "Aptos",
@@ -850,6 +902,16 @@ var mainnetTokens = {
850
902
  icon: "USDC",
851
903
  decimals: 6
852
904
  }
905
+ // Sui: {
906
+ // symbol: "USDC",
907
+ // decimals: 6,
908
+ // tokenId: {
909
+ // chain: "Sui",
910
+ // address:
911
+ // "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
912
+ // },
913
+ // icon: "USDC",
914
+ // },
853
915
  };
854
916
  var AptosMainnetUSDCToken = {
855
917
  symbol: "USDC",
@@ -866,7 +928,6 @@ var import_ts_sdk3 = require("@aptos-labs/ts-sdk");
866
928
  var import_web33 = require("@solana/web3.js");
867
929
  var import_ethers2 = require("ethers");
868
930
  var getSolanaWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
869
- var _a;
870
931
  const address = new import_web33.PublicKey(walletAddress);
871
932
  const tokenAddress = aptosNetwork === import_ts_sdk3.Network.MAINNET ? mainnetTokens["Solana"].tokenId.address : testnetTokens["Solana"].tokenId.address;
872
933
  const connection = new import_web33.Connection(rpc);
@@ -875,8 +936,7 @@ var getSolanaWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
875
936
  });
876
937
  const checkAddress = splToken.value.length > 0 ? splToken.value[0].pubkey : address;
877
938
  const balance = await connection.getTokenAccountBalance(checkAddress);
878
- console.log("balance", balance);
879
- return (_a = balance.value.uiAmountString) != null ? _a : (Number(balance.value.amount) / 10 ** balance.value.decimals).toString();
939
+ return balance.value.uiAmountString ?? (Number(balance.value.amount) / 10 ** balance.value.decimals).toString();
880
940
  };
881
941
  var getEthereumWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
882
942
  const token = aptosNetwork === import_ts_sdk3.Network.MAINNET ? mainnetTokens["Ethereum"] : testnetTokens["Sepolia"];
@@ -914,9 +974,8 @@ var CrossChainCore = class {
914
974
  this.CHAINS = testnetChains;
915
975
  this.TOKENS = testnetTokens;
916
976
  this.APTOS_TOKEN = AptosTestnetUSDCToken;
917
- var _a;
918
977
  this._dappConfig = args.dappConfig;
919
- if (((_a = args.dappConfig) == null ? void 0 : _a.aptosNetwork) === import_ts_sdk4.Network.MAINNET) {
978
+ if (args.dappConfig?.aptosNetwork === import_ts_sdk4.Network.MAINNET) {
920
979
  this.CHAINS = mainnetChains;
921
980
  this.TOKENS = mainnetTokens;
922
981
  this.APTOS_TOKEN = AptosMainnetUSDCToken;
@@ -935,7 +994,6 @@ var CrossChainCore = class {
935
994
  }
936
995
  }
937
996
  async getWalletUSDCBalance(walletAddress, sourceChain) {
938
- var _a, _b, _c;
939
997
  if (sourceChain === "Aptos") {
940
998
  return await getAptosWalletUSDCBalance(
941
999
  walletAddress,
@@ -950,13 +1008,14 @@ var CrossChainCore = class {
950
1008
  return await getSolanaWalletUSDCBalance(
951
1009
  walletAddress,
952
1010
  this._dappConfig.aptosNetwork,
953
- (_c = (_b = (_a = this._dappConfig) == null ? void 0 : _a.solanaConfig) == null ? void 0 : _b.rpc) != null ? _c : this.CHAINS[sourceChain].defaultRpc
1011
+ this._dappConfig?.solanaConfig?.rpc ?? this.CHAINS[sourceChain].defaultRpc
954
1012
  );
955
1013
  case "Ethereum":
956
1014
  case "Sepolia":
957
1015
  return await getEthereumWalletUSDCBalance(
958
1016
  walletAddress,
959
1017
  this._dappConfig.aptosNetwork,
1018
+ // TODO: maybe let the user config it
960
1019
  this.CHAINS[sourceChain].defaultRpc
961
1020
  );
962
1021
  default: