@coin-voyage/crypto 2.4.0 → 2.4.3-beta.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.
@@ -1,27 +1,59 @@
1
- import { getEvmPaymentData } from "@coin-voyage/shared/payment";
2
1
  import { estimateFeesPerGas } from "viem/actions";
3
- import { useConfig, useSendTransaction, useWriteContract } from "wagmi";
2
+ import { useChainId, useConfig, useSendTransaction, useSwitchChain, useWriteContract } from "wagmi";
4
3
  export function useEVMTransaction() {
5
4
  const { writeContractAsync } = useWriteContract();
6
5
  const { sendTransactionAsync } = useSendTransaction();
7
6
  const config = useConfig();
7
+ const walletChainId = useChainId();
8
+ const { switchChainAsync } = useSwitchChain();
9
+ const trySwitchChain = async (sourceChainId) => {
10
+ if (walletChainId === sourceChainId)
11
+ return;
12
+ let switchedId;
13
+ try {
14
+ const switched = await switchChainAsync({ chainId: sourceChainId });
15
+ switchedId = switched?.id;
16
+ }
17
+ catch (e) {
18
+ console.error("Failed to switch chain", e);
19
+ throw new Error("Failed to switch chain", { cause: e });
20
+ }
21
+ if (switchedId !== sourceChainId) {
22
+ console.error("Failed to switch chain", { expected: sourceChainId, actual: switchedId });
23
+ throw new Error("Failed to switch chain");
24
+ }
25
+ };
26
+ const feesPerGas = async (options) => {
27
+ try {
28
+ const client = config.getClient({ chainId: options.chainId });
29
+ return await estimateFeesPerGas(client, options);
30
+ }
31
+ catch (e) {
32
+ console.error("Failed to estimate fees per gas", e);
33
+ throw new Error("Failed to estimate fees per gas", { cause: e });
34
+ }
35
+ };
8
36
  const execute = async (params) => {
9
- const client = config.getClient();
10
- const gasEstimates = await estimateFeesPerGas(client, {
11
- chain: client.chain,
12
- type: "eip1559",
13
- });
14
- const evmPaymentData = getEvmPaymentData(params.paymentData);
37
+ const evmPaymentData = params.paymentData?.evm;
15
38
  if (evmPaymentData) {
39
+ await trySwitchChain(evmPaymentData.chainId);
40
+ if (!evmPaymentData.maxFeePerGas && !evmPaymentData.maxPriorityFeePerGas) {
41
+ const gasEstimates = await feesPerGas({
42
+ chainId: evmPaymentData.chainId,
43
+ type: "eip1559",
44
+ });
45
+ evmPaymentData.maxFeePerGas = gasEstimates.maxFeePerGas?.toString();
46
+ evmPaymentData.maxPriorityFeePerGas = gasEstimates.maxPriorityFeePerGas?.toString();
47
+ }
16
48
  const tx = await sendTransactionAsync({
17
49
  to: evmPaymentData.to,
18
50
  data: evmPaymentData.data,
19
51
  value: BigInt(evmPaymentData.value),
20
52
  chainId: evmPaymentData.chainId,
21
- maxFeePerGas: evmPaymentData.maxFeePerGas ? BigInt(evmPaymentData.maxFeePerGas) : gasEstimates.maxFeePerGas,
53
+ maxFeePerGas: evmPaymentData.maxFeePerGas ? BigInt(evmPaymentData.maxFeePerGas) : undefined,
22
54
  maxPriorityFeePerGas: evmPaymentData.maxPriorityFeePerGas
23
55
  ? BigInt(evmPaymentData.maxPriorityFeePerGas)
24
- : gasEstimates.maxPriorityFeePerGas,
56
+ : undefined,
25
57
  });
26
58
  return tx;
27
59
  }
@@ -29,15 +61,26 @@ export function useEVMTransaction() {
29
61
  if (!toAddress || amount == undefined) {
30
62
  throw new Error("Missing EVM transfer target or amount");
31
63
  }
64
+ if (chainId) {
65
+ await trySwitchChain(chainId);
66
+ }
67
+ const gasEstimates = await feesPerGas({
68
+ chainId,
69
+ type: "eip1559",
70
+ });
32
71
  const value = BigInt(amount);
33
72
  const to = toAddress;
73
+ const maxFeePerGas = gasEstimates.maxFeePerGas ? BigInt(gasEstimates.maxFeePerGas) : undefined;
74
+ const maxPriorityFeePerGas = gasEstimates.maxPriorityFeePerGas
75
+ ? BigInt(gasEstimates.maxPriorityFeePerGas)
76
+ : undefined;
34
77
  if (!token) {
35
78
  const tx = await sendTransactionAsync({
36
79
  to,
37
80
  value,
38
81
  chainId,
39
- maxFeePerGas: gasEstimates.maxFeePerGas,
40
- maxPriorityFeePerGas: gasEstimates.maxPriorityFeePerGas,
82
+ maxFeePerGas,
83
+ maxPriorityFeePerGas,
41
84
  });
42
85
  return tx;
43
86
  }
@@ -58,8 +101,8 @@ export function useEVMTransaction() {
58
101
  address: token.address,
59
102
  functionName: "transfer",
60
103
  chainId,
61
- maxFeePerGas: gasEstimates.maxFeePerGas,
62
- maxPriorityFeePerGas: gasEstimates.maxPriorityFeePerGas,
104
+ maxFeePerGas,
105
+ maxPriorityFeePerGas,
63
106
  args: [to, value],
64
107
  });
65
108
  return tx;
@@ -1,4 +1,3 @@
1
- import { getSolanaPaymentData } from "@coin-voyage/shared/payment";
2
1
  import { hex } from "@scure/base";
3
2
  import { createTransferCheckedInstruction, getAssociatedTokenAddress } from "@solana/spl-token";
4
3
  import { useConnection, useWallet } from "@solana/wallet-adapter-react";
@@ -17,7 +16,7 @@ export function useSolanaTransaction() {
17
16
  throw new Error("Wallet not connected. Please reconnect your wallet and try again.");
18
17
  }
19
18
  }
20
- const preparedPayment = getSolanaPaymentData(params.paymentData);
19
+ const preparedPayment = params.paymentData?.solana;
21
20
  if (preparedPayment) {
22
21
  const { context: { slot: minContextSlot }, value: { lastValidBlockHeight }, } = await connection.getLatestBlockhashAndContext();
23
22
  const transaction = deserializePreparedSolanaTransaction(preparedPayment.transaction);
@@ -1,4 +1,3 @@
1
- import { getSuiPaymentData } from "@coin-voyage/shared/payment";
2
1
  import { useReportTransactionEffects, useSignTransaction, useSuiClient } from "@mysten/dapp-kit";
3
2
  import { Transaction } from "@mysten/sui/transactions";
4
3
  import { SUI_PACKAGE_IDS } from "./constants";
@@ -7,7 +6,7 @@ export function useSUITransaction() {
7
6
  const { mutateAsync: signTransaction } = useSignTransaction();
8
7
  const { mutate: reportTransactionEffects } = useReportTransactionEffects();
9
8
  const execute = async (params) => {
10
- const preparedPayment = getSuiPaymentData(params.paymentData);
9
+ const preparedPayment = params.paymentData?.sui;
11
10
  if (preparedPayment) {
12
11
  const transaction = typeof preparedPayment.transaction === "string"
13
12
  ? preparedPayment.transaction
@@ -1,10 +1,9 @@
1
1
  import { useAccount } from "@bigmi/react";
2
- import { getBitcoinPaymentData } from "@coin-voyage/shared/payment";
3
2
  import { sendBtc } from "./send-btc";
4
3
  export function useUTXOTransaction() {
5
4
  const { account, connector } = useAccount();
6
5
  const execute = async (params) => {
7
- const bitcoinPaymentData = getBitcoinPaymentData(params.paymentData);
6
+ const bitcoinPaymentData = params.paymentData?.bitcoin;
8
7
  if (bitcoinPaymentData) {
9
8
  throw new Error("Prepared Bitcoin transactions are not supported in wallet flow yet.");
10
9
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coin-voyage/crypto",
3
3
  "description": "Crypto utilities for Coin Voyage",
4
- "version": "2.4.0",
4
+ "version": "2.4.3-beta.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "author": "Lars <lars@coinvoyage.io>",
@@ -106,7 +106,7 @@
106
106
  "@solana/wallet-adapter-walletconnect": "0.1.21",
107
107
  "@solana/wallet-adapter-base": "0.9.27",
108
108
  "@solana/wallet-adapter-coinbase": "0.1.23",
109
- "@coin-voyage/shared": "2.4.0"
109
+ "@coin-voyage/shared": "2.4.3-beta.0"
110
110
  },
111
111
  "devDependencies": {
112
112
  "@types/elliptic": "6.4.18"