@aptos-labs/cross-chain-core 4.23.1 → 4.24.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/dist/index.mjs CHANGED
@@ -56,6 +56,7 @@ var AptosLocalSigner = class {
56
56
  claimedTransactionHashes() {
57
57
  return this._claimedTransactionHashes;
58
58
  }
59
+ /* other methods... */
59
60
  async signAndSend(txs) {
60
61
  const txHashes = [];
61
62
  for (const tx of txs) {
@@ -130,13 +131,12 @@ import {
130
131
  import { Connection } from "@solana/web3.js";
131
132
  import { SolanaDerivedWallet } from "@aptos-labs/derived-wallet-solana";
132
133
  async function signAndSendTransaction2(request, wallet, options, crossChainCore) {
133
- var _a, _b, _c, _d;
134
134
  if (!wallet || !(wallet instanceof SolanaDerivedWallet)) {
135
135
  throw new Error("Invalid wallet type or missing Solana wallet").message;
136
136
  }
137
- const commitment = (_a = options == null ? void 0 : options.commitment) != null ? _a : "finalized";
137
+ const commitment = options?.commitment ?? "finalized";
138
138
  const connection = new Connection(
139
- (_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"
139
+ crossChainCore?._dappConfig?.solanaConfig?.rpc ?? "https://api.devnet.solana.com"
140
140
  );
141
141
  const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(commitment);
142
142
  const transaction = request.transaction.transaction;
@@ -155,13 +155,13 @@ async function signAndSendTransaction2(request, wallet, options, crossChainCore)
155
155
  throw new Error("Wallet does not support signing transactions").message;
156
156
  }
157
157
  const tx = await wallet.solanaWallet.signTransaction(unsignedTx);
158
- if (!tx)
159
- throw new Error("Failed to sign transaction").message;
158
+ if (!tx) throw new Error("Failed to sign transaction").message;
160
159
  const serializedTx = tx.serialize();
161
160
  const sendOptions = {
162
161
  skipPreflight: true,
163
162
  maxRetries: 0,
164
163
  preFlightCommitment: commitment
164
+ // See PR and linked issue for why setting this matters: https://github.com/anza-xyz/agave/pull/483
165
165
  };
166
166
  signature = await connection.sendRawTransaction(serializedTx, sendOptions);
167
167
  confirmTransactionPromise = connection.confirmTransaction(
@@ -201,6 +201,7 @@ async function signAndSendTransaction2(request, wallet, options, crossChainCore)
201
201
  errorMessage = `Transaction failed: ${JSON.stringify(
202
202
  confirmedTx.value.err,
203
203
  (_key, value) => typeof value === "bigint" ? value.toString() : value
204
+ // Handle bigint props
204
205
  )}`;
205
206
  } catch (e) {
206
207
  errorMessage = `Transaction failed: Unknown error`;
@@ -231,42 +232,41 @@ async function setPriorityFeeInstructions(connection, blockhash, lastValidBlockH
231
232
  return unsignedTx;
232
233
  }
233
234
  async function createPriorityFeeInstructions(connection, transaction, crossChainCore) {
234
- var _a, _b, _c;
235
235
  let unitsUsed = 2e5;
236
236
  let simulationAttempts = 0;
237
- simulationLoop:
238
- while (true) {
239
- const response = await connection.simulateTransaction(
240
- transaction
241
- );
242
- if (response.value.err) {
243
- if (checkKnownSimulationError(response.value)) {
244
- if (simulationAttempts < 5) {
245
- simulationAttempts++;
246
- await sleep(1e3);
247
- continue simulationLoop;
248
- }
249
- } else if (simulationAttempts < 3) {
237
+ simulationLoop: while (true) {
238
+ const response = await connection.simulateTransaction(
239
+ transaction
240
+ );
241
+ if (response.value.err) {
242
+ if (checkKnownSimulationError(response.value)) {
243
+ if (simulationAttempts < 5) {
250
244
  simulationAttempts++;
251
245
  await sleep(1e3);
252
246
  continue simulationLoop;
253
247
  }
254
- throw new Error(
255
- `Simulation failed: ${JSON.stringify(response.value.err)}
248
+ } else if (simulationAttempts < 3) {
249
+ simulationAttempts++;
250
+ await sleep(1e3);
251
+ continue simulationLoop;
252
+ }
253
+ throw new Error(
254
+ `Simulation failed: ${JSON.stringify(response.value.err)}
256
255
  Logs:
257
256
  ${(response.value.logs || []).join("\n ")}`
258
- ).message;
259
- } else {
260
- if (response.value.unitsConsumed) {
261
- unitsUsed = response.value.unitsConsumed;
262
- }
263
- break;
257
+ ).message;
258
+ } else {
259
+ if (response.value.unitsConsumed) {
260
+ unitsUsed = response.value.unitsConsumed;
264
261
  }
262
+ break;
265
263
  }
264
+ }
266
265
  const unitBudget = Math.floor(unitsUsed * 1.2);
267
266
  const instructions = [];
268
267
  instructions.push(
269
268
  ComputeBudgetProgram.setComputeUnitLimit({
269
+ // Set compute budget to 120% of the units used in the simulated transaction
270
270
  units: unitBudget
271
271
  })
272
272
  );
@@ -275,7 +275,7 @@ ${(response.value.logs || []).join("\n ")}`
275
275
  percentileMultiple = 1,
276
276
  min = 1e5,
277
277
  max = 1e8
278
- } = (_c = (_b = (_a = crossChainCore == null ? void 0 : crossChainCore._dappConfig) == null ? void 0 : _a.solanaConfig) == null ? void 0 : _b.priorityFeeConfig) != null ? _c : {};
278
+ } = crossChainCore?._dappConfig?.solanaConfig?.priorityFeeConfig ?? {};
279
279
  const calculateFee = async (rpcProvider2) => {
280
280
  if (rpcProvider2 === "triton") {
281
281
  try {
@@ -318,7 +318,10 @@ ${(response.value.logs || []).join("\n ")}`
318
318
  };
319
319
  const rpcProvider = determineRpcProvider(connection.rpcEndpoint);
320
320
  const { fee, methodUsed } = await calculateFee(rpcProvider);
321
- const maxFeeInSol = fee / 1e6 / LAMPORTS_PER_SOL * unitBudget;
321
+ const maxFeeInSol = fee / // convert microlamports to lamports
322
+ 1e6 / // convert lamports to SOL
323
+ LAMPORTS_PER_SOL * // multiply by maximum compute units used
324
+ unitBudget;
322
325
  console.table({
323
326
  "RPC Provider": rpcProvider,
324
327
  "Method used": methodUsed,
@@ -410,7 +413,7 @@ async function signAndSendTransaction3(request, wallet, chainName, options) {
410
413
  const signer = await provider.getSigner();
411
414
  const response = await signer.sendTransaction(request.transaction);
412
415
  const receipt = await response.wait();
413
- return (receipt == null ? void 0 : receipt.hash) || "";
416
+ return receipt?.hash || "";
414
417
  }
415
418
 
416
419
  // src/providers/wormhole/signers/Signer.ts
@@ -477,8 +480,7 @@ var WormholeProvider = class {
477
480
  return this._wormholeContext;
478
481
  }
479
482
  async setWormholeContext(sourceChain) {
480
- var _a;
481
- const dappNetwork = (_a = this.crossChainCore._dappConfig) == null ? void 0 : _a.aptosNetwork;
483
+ const dappNetwork = this.crossChainCore._dappConfig?.aptosNetwork;
482
484
  if (dappNetwork === Network.DEVNET) {
483
485
  throw new Error("Devnet is not supported on Wormhole");
484
486
  }
@@ -510,6 +512,7 @@ var WormholeProvider = class {
510
512
  );
511
513
  const resolver = this._wormholeContext.resolver([
512
514
  routes.CCTPRoute
515
+ // manual CCTP
513
516
  ]);
514
517
  const route = await resolver.findRoutes(request);
515
518
  const cctpRoute = route[0];
@@ -542,7 +545,6 @@ var WormholeProvider = class {
542
545
  return quote;
543
546
  }
544
547
  async submitCCTPTransfer(input) {
545
- var _a;
546
548
  const { sourceChain, wallet, destinationAddress } = input;
547
549
  if (!this._wormholeContext) {
548
550
  await this.setWormholeContext(sourceChain);
@@ -553,7 +555,7 @@ var WormholeProvider = class {
553
555
  let signerAddress;
554
556
  const chainContext = this.getChainConfig(sourceChain).context;
555
557
  if (chainContext === "Solana") {
556
- signerAddress = ((_a = wallet.solanaWallet.publicKey) == null ? void 0 : _a.toBase58()) || "";
558
+ signerAddress = wallet.solanaWallet.publicKey?.toBase58() || "";
557
559
  } else {
558
560
  [signerAddress] = await wallet.eip1193Provider.request({
559
561
  method: "eth_requestAccounts"
@@ -594,7 +596,9 @@ var WormholeProvider = class {
594
596
  "Aptos",
595
597
  {},
596
598
  mainSigner,
599
+ // the account that signs the "claim" transaction
597
600
  sponsorAccount ? sponsorAccount : void 0
601
+ // the fee payer account
598
602
  );
599
603
  if (routes.isManual(this.wormholeRoute)) {
600
604
  const circleAttestationReceipt = await this.wormholeRoute.complete(signer, receipt);
@@ -622,9 +626,13 @@ var WormholeProvider = class {
622
626
  }
623
627
  return { destinationChainTxnId: "" };
624
628
  }
629
+ /**
630
+ * Initiates a transfer of USDC funds from the source chain wallet to the destination chain wallet
631
+ * @param args
632
+ * @returns
633
+ */
625
634
  async initiateCCTPTransfer(input) {
626
- var _a;
627
- if (((_a = this.crossChainCore._dappConfig) == null ? void 0 : _a.aptosNetwork) === Network.DEVNET) {
635
+ if (this.crossChainCore._dappConfig?.aptosNetwork === Network.DEVNET) {
628
636
  throw new Error("Devnet is not supported on Wormhole");
629
637
  }
630
638
  if (input.amount) {
@@ -703,6 +711,21 @@ var testnetChains = {
703
711
  defaultRpc: "https://api.devnet.solana.com",
704
712
  wrappedGasToken: "So11111111111111111111111111111111111111112"
705
713
  }
714
+ // Sui: {
715
+ // key: "Sui",
716
+ // id: 21,
717
+ // context: Context.SUI,
718
+ // finalityThreshold: 0,
719
+ // displayName: "Sui",
720
+ // explorerUrl: "https://suiscan.xyz/testnet/",
721
+ // explorerName: "Suiscan",
722
+ // gasToken: "SUI",
723
+ // chainId: 0,
724
+ // icon: "Sui",
725
+ // maxBlockSearch: 0,
726
+ // symbol: "SUI",
727
+ // sdkName: "Sui",
728
+ // },
706
729
  };
707
730
  var AptosTestnetChain = {
708
731
  key: "Aptos",
@@ -740,6 +763,16 @@ var testnetTokens = {
740
763
  icon: "USDC",
741
764
  decimals: 6
742
765
  }
766
+ // Sui: {
767
+ // symbol: "USDC",
768
+ // tokenId: {
769
+ // chain: "Sui",
770
+ // address:
771
+ // "0xa1ec7fc00a6f40db9693ad1415d0c193ad3906494428cf252621037bd7117e29::usdc::USDC",
772
+ // },
773
+ // icon: "USDC",
774
+ // decimals: 6,
775
+ // },
743
776
  };
744
777
  var AptosTestnetUSDCToken = {
745
778
  symbol: "USDC",
@@ -783,6 +816,21 @@ var mainnetChains = {
783
816
  symbol: "SOL",
784
817
  defaultRpc: "https://solana-mainnet.rpc.extrnode.com"
785
818
  }
819
+ // Sui: {
820
+ // key: "Sui",
821
+ // id: 21,
822
+ // context: Context.SUI,
823
+ // finalityThreshold: 0,
824
+ // displayName: "Sui",
825
+ // sdkName: "Sui",
826
+ // explorerUrl: "https://suiscan.xyz/",
827
+ // explorerName: "Suiscan",
828
+ // gasToken: "SUI",
829
+ // chainId: 0,
830
+ // icon: "Sui",
831
+ // maxBlockSearch: 0,
832
+ // symbol: "SUI",
833
+ // },
786
834
  };
787
835
  var AptosMainnetChain = {
788
836
  key: "Aptos",
@@ -819,6 +867,16 @@ var mainnetTokens = {
819
867
  icon: "USDC",
820
868
  decimals: 6
821
869
  }
870
+ // Sui: {
871
+ // symbol: "USDC",
872
+ // decimals: 6,
873
+ // tokenId: {
874
+ // chain: "Sui",
875
+ // address:
876
+ // "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
877
+ // },
878
+ // icon: "USDC",
879
+ // },
822
880
  };
823
881
  var AptosMainnetUSDCToken = {
824
882
  symbol: "USDC",
@@ -835,7 +893,6 @@ import { Aptos as Aptos2, AptosConfig as AptosConfig2, Network as Network2 } fro
835
893
  import { Connection as Connection2, PublicKey } from "@solana/web3.js";
836
894
  import { ethers as ethers2, JsonRpcProvider } from "ethers";
837
895
  var getSolanaWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
838
- var _a;
839
896
  const address = new PublicKey(walletAddress);
840
897
  const tokenAddress = aptosNetwork === Network2.MAINNET ? mainnetTokens["Solana"].tokenId.address : testnetTokens["Solana"].tokenId.address;
841
898
  const connection = new Connection2(rpc);
@@ -844,8 +901,7 @@ var getSolanaWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
844
901
  });
845
902
  const checkAddress = splToken.value.length > 0 ? splToken.value[0].pubkey : address;
846
903
  const balance = await connection.getTokenAccountBalance(checkAddress);
847
- console.log("balance", balance);
848
- return (_a = balance.value.uiAmountString) != null ? _a : (Number(balance.value.amount) / 10 ** balance.value.decimals).toString();
904
+ return balance.value.uiAmountString ?? (Number(balance.value.amount) / 10 ** balance.value.decimals).toString();
849
905
  };
850
906
  var getEthereumWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
851
907
  const token = aptosNetwork === Network2.MAINNET ? mainnetTokens["Ethereum"] : testnetTokens["Sepolia"];
@@ -883,9 +939,8 @@ var CrossChainCore = class {
883
939
  this.CHAINS = testnetChains;
884
940
  this.TOKENS = testnetTokens;
885
941
  this.APTOS_TOKEN = AptosTestnetUSDCToken;
886
- var _a;
887
942
  this._dappConfig = args.dappConfig;
888
- if (((_a = args.dappConfig) == null ? void 0 : _a.aptosNetwork) === Network3.MAINNET) {
943
+ if (args.dappConfig?.aptosNetwork === Network3.MAINNET) {
889
944
  this.CHAINS = mainnetChains;
890
945
  this.TOKENS = mainnetTokens;
891
946
  this.APTOS_TOKEN = AptosMainnetUSDCToken;
@@ -904,7 +959,6 @@ var CrossChainCore = class {
904
959
  }
905
960
  }
906
961
  async getWalletUSDCBalance(walletAddress, sourceChain) {
907
- var _a, _b, _c;
908
962
  if (sourceChain === "Aptos") {
909
963
  return await getAptosWalletUSDCBalance(
910
964
  walletAddress,
@@ -919,13 +973,14 @@ var CrossChainCore = class {
919
973
  return await getSolanaWalletUSDCBalance(
920
974
  walletAddress,
921
975
  this._dappConfig.aptosNetwork,
922
- (_c = (_b = (_a = this._dappConfig) == null ? void 0 : _a.solanaConfig) == null ? void 0 : _b.rpc) != null ? _c : this.CHAINS[sourceChain].defaultRpc
976
+ this._dappConfig?.solanaConfig?.rpc ?? this.CHAINS[sourceChain].defaultRpc
923
977
  );
924
978
  case "Ethereum":
925
979
  case "Sepolia":
926
980
  return await getEthereumWalletUSDCBalance(
927
981
  walletAddress,
928
982
  this._dappConfig.aptosNetwork,
983
+ // TODO: maybe let the user config it
929
984
  this.CHAINS[sourceChain].defaultRpc
930
985
  );
931
986
  default: