@coinbase/cdp-sdk 1.38.3 → 1.38.4

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/_cjs/actions/evm/fund/fund.js +1 -0
  3. package/_cjs/actions/evm/fund/fund.js.map +1 -1
  4. package/_cjs/actions/evm/fund/quoteFund.js +1 -0
  5. package/_cjs/actions/evm/fund/quoteFund.js.map +1 -1
  6. package/_cjs/actions/solana/fund/fund.js +1 -0
  7. package/_cjs/actions/solana/fund/fund.js.map +1 -1
  8. package/_cjs/actions/solana/fund/quoteFund.js +1 -0
  9. package/_cjs/actions/solana/fund/quoteFund.js.map +1 -1
  10. package/_cjs/actions/solana/rpc.js +16 -0
  11. package/_cjs/actions/solana/rpc.js.map +1 -0
  12. package/_cjs/actions/solana/transfer.js +74 -48
  13. package/_cjs/actions/solana/transfer.js.map +1 -1
  14. package/_cjs/actions/solana/utils.js +4 -6
  15. package/_cjs/actions/solana/utils.js.map +1 -1
  16. package/_cjs/actions/waitForFundOperationReceipt.js +1 -0
  17. package/_cjs/actions/waitForFundOperationReceipt.js.map +1 -1
  18. package/_cjs/version.js +1 -1
  19. package/_esm/actions/evm/fund/fund.js +1 -0
  20. package/_esm/actions/evm/fund/fund.js.map +1 -1
  21. package/_esm/actions/evm/fund/quoteFund.js +1 -0
  22. package/_esm/actions/evm/fund/quoteFund.js.map +1 -1
  23. package/_esm/actions/solana/fund/fund.js +1 -0
  24. package/_esm/actions/solana/fund/fund.js.map +1 -1
  25. package/_esm/actions/solana/fund/quoteFund.js +1 -0
  26. package/_esm/actions/solana/fund/quoteFund.js.map +1 -1
  27. package/_esm/actions/solana/rpc.js +13 -0
  28. package/_esm/actions/solana/rpc.js.map +1 -0
  29. package/_esm/actions/solana/transfer.js +72 -48
  30. package/_esm/actions/solana/transfer.js.map +1 -1
  31. package/_esm/actions/solana/utils.js +4 -6
  32. package/_esm/actions/solana/utils.js.map +1 -1
  33. package/_esm/actions/waitForFundOperationReceipt.js +1 -0
  34. package/_esm/actions/waitForFundOperationReceipt.js.map +1 -1
  35. package/_esm/version.js +1 -1
  36. package/_types/actions/evm/fund/fund.d.ts +1 -0
  37. package/_types/actions/evm/fund/fund.d.ts.map +1 -1
  38. package/_types/actions/evm/fund/quoteFund.d.ts +1 -0
  39. package/_types/actions/evm/fund/quoteFund.d.ts.map +1 -1
  40. package/_types/actions/solana/fund/fund.d.ts +1 -0
  41. package/_types/actions/solana/fund/fund.d.ts.map +1 -1
  42. package/_types/actions/solana/fund/quoteFund.d.ts +1 -0
  43. package/_types/actions/solana/fund/quoteFund.d.ts.map +1 -1
  44. package/_types/actions/solana/rpc.d.ts +10 -0
  45. package/_types/actions/solana/rpc.d.ts.map +1 -0
  46. package/_types/actions/solana/transfer.d.ts +37 -1
  47. package/_types/actions/solana/transfer.d.ts.map +1 -1
  48. package/_types/actions/solana/utils.d.ts +4 -6
  49. package/_types/actions/solana/utils.d.ts.map +1 -1
  50. package/_types/actions/waitForFundOperationReceipt.d.ts +1 -0
  51. package/_types/actions/waitForFundOperationReceipt.d.ts.map +1 -1
  52. package/_types/version.d.ts +1 -1
  53. package/actions/evm/fund/fund.ts +1 -0
  54. package/actions/evm/fund/quoteFund.ts +1 -0
  55. package/actions/solana/fund/fund.ts +1 -0
  56. package/actions/solana/fund/quoteFund.ts +1 -0
  57. package/actions/solana/rpc.ts +17 -0
  58. package/actions/solana/transfer.ts +115 -84
  59. package/actions/solana/utils.ts +4 -6
  60. package/actions/waitForFundOperationReceipt.ts +1 -0
  61. package/package.json +5 -6
  62. package/version.ts +1 -1
@@ -1,18 +1,28 @@
1
1
  import {
2
- getMint,
3
- getAssociatedTokenAddress,
4
- getAccount,
5
- createAssociatedTokenAccountInstruction,
6
- createTransferCheckedInstruction,
7
- } from "@solana/spl-token";
2
+ pipe,
3
+ createTransactionMessage,
4
+ setTransactionMessageLifetimeUsingBlockhash,
5
+ appendTransactionMessageInstructions,
6
+ Instruction,
7
+ createSolanaRpc,
8
+ address,
9
+ compileTransaction,
10
+ setTransactionMessageFeePayer,
11
+ createNoopSigner,
12
+ getBase64EncodedWireTransaction,
13
+ } from "@solana/kit";
14
+ import { Connection } from "@solana/web3.js";
15
+ import { getTransferSolInstruction } from "@solana-program/system";
8
16
  import {
9
- Connection,
10
- PublicKey,
11
- SystemProgram,
12
- SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
13
- Transaction,
14
- } from "@solana/web3.js";
15
-
17
+ findAssociatedTokenPda,
18
+ getCreateAssociatedTokenInstructionAsync,
19
+ getTransferCheckedInstruction,
20
+ fetchToken,
21
+ fetchMint,
22
+ TOKEN_PROGRAM_ADDRESS,
23
+ } from "@solana-program/token";
24
+
25
+ import { createRpcClient } from "./rpc.js";
16
26
  import { sendTransaction } from "./sendTransaction.js";
17
27
  import {
18
28
  getConnectedNetwork,
@@ -42,7 +52,7 @@ export interface TransferOptions {
42
52
  */
43
53
  token: "sol" | "usdc" | string;
44
54
  /**
45
- * The network to use which will be used to create a Connection, otherwise a Connection can be provided.
55
+ * The network to use which will be used to create an RPC client, otherwise an RPC client can be provided.
46
56
  */
47
57
  network: Network | Connection;
48
58
  }
@@ -61,16 +71,18 @@ export async function transfer(
61
71
  ): Promise<SignatureResult> {
62
72
  const connection = getOrCreateConnection({ networkOrConnection: options.network });
63
73
  const connectedNetwork = await getConnectedNetwork(connection);
74
+ const rpc = createRpcClient(connectedNetwork);
64
75
 
65
- const tx =
76
+ const base64Transaction =
66
77
  options.token === "sol"
67
- ? await getNativeTransfer({
78
+ ? await getNativeTransferBase64Transaction({
79
+ rpc,
68
80
  from: options.from,
69
81
  to: options.to,
70
82
  amount: options.amount,
71
83
  })
72
- : await getSplTransfer({
73
- connection,
84
+ : await getSplTransferBase64Transaction({
85
+ rpc,
74
86
  from: options.from,
75
87
  to: options.to,
76
88
  mintAddress:
@@ -78,59 +90,63 @@ export async function transfer(
78
90
  amount: options.amount,
79
91
  });
80
92
 
81
- const serializedTx = Buffer.from(tx.serialize({ requireAllSignatures: false })).toString(
82
- "base64",
83
- );
84
-
85
93
  const signature = await sendTransaction(apiClient, {
86
94
  network: connectedNetwork === "mainnet" ? "solana" : "solana-devnet",
87
- transaction: serializedTx,
95
+ transaction: base64Transaction,
88
96
  });
89
97
 
90
98
  return signature;
91
99
  }
92
100
 
93
- type GetNativeTransferOptions = Omit<TransferOptions, "token" | "network">;
101
+ type GetNativeTransferOptions = Omit<TransferOptions, "token" | "network"> & {
102
+ rpc: ReturnType<typeof createSolanaRpc>;
103
+ };
94
104
 
95
105
  /**
96
- * Gets the transaction for a native SOL transfer
106
+ * Gets the transaction for a SOL transfer
97
107
  *
98
- * @param options - The options for the native SOL transfer
108
+ * @param options - The options for the SOL transfer
99
109
  *
110
+ * @param options.rpc - The Solana RPC client
100
111
  * @param options.from - The source address
101
112
  * @param options.to - The destination address
102
- * @param options.amount - The amount to transfer
113
+ * @param options.amount - The amount in lamports to transfer
103
114
  *
104
- * @returns The native SOL transfer transaction
115
+ * @returns The SOL transfer transaction
105
116
  */
106
- async function getNativeTransfer({
117
+ export async function getNativeTransferBase64Transaction({
118
+ rpc,
107
119
  from,
108
120
  to,
109
121
  amount,
110
- }: GetNativeTransferOptions): Promise<Transaction> {
111
- const transaction = new Transaction();
112
- transaction.add(
113
- SystemProgram.transfer({
114
- fromPubkey: new PublicKey(from),
115
- toPubkey: new PublicKey(to),
116
- lamports: amount,
122
+ }: GetNativeTransferOptions): Promise<string> {
123
+ const fromAddr = address(from);
124
+ const toAddr = address(to);
125
+
126
+ const instructions: Instruction[] = [
127
+ getTransferSolInstruction({
128
+ source: createNoopSigner(fromAddr),
129
+ destination: toAddr,
130
+ amount,
117
131
  }),
132
+ ];
133
+
134
+ const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
135
+
136
+ const txMsg = pipe(
137
+ createTransactionMessage({ version: 0 }),
138
+ tx => setTransactionMessageFeePayer(fromAddr, tx),
139
+ tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
140
+ tx => appendTransactionMessageInstructions(instructions, tx),
118
141
  );
119
- transaction.recentBlockhash = SYSVAR_RECENT_BLOCKHASHES_PUBKEY.toBase58();
120
- transaction.feePayer = new PublicKey(from);
121
142
 
122
- return transaction;
143
+ const compiledTransaction = compileTransaction(txMsg);
144
+ return getBase64EncodedWireTransaction(compiledTransaction);
123
145
  }
124
146
 
125
- type GetSplTransferOptions = Omit<TransferOptions, "token" | "network"> & {
126
- /**
127
- * The mint address of the token
128
- */
147
+ type GetSplTokenTransferOptions = Omit<TransferOptions, "network" | "token"> & {
129
148
  mintAddress: string;
130
- /**
131
- * The connection to the Solana network.
132
- */
133
- connection: Connection;
149
+ rpc: ReturnType<typeof createSolanaRpc>;
134
150
  };
135
151
 
136
152
  /**
@@ -138,63 +154,78 @@ type GetSplTransferOptions = Omit<TransferOptions, "token" | "network"> & {
138
154
  *
139
155
  * @param options - The options for the SPL token transfer
140
156
  *
141
- * @param options.connection - The Solana connection
157
+ * @param options.rpc - The Solana RPC client
142
158
  * @param options.from - The source address
143
159
  * @param options.to - The destination address
144
160
  * @param options.mintAddress - The mint address of the token
145
- * @param options.amount - The amount to transfer
161
+ * @param options.amount - The amount in units of the token to transfer
146
162
  *
147
163
  * @returns The SPL token transfer transaction
148
164
  */
149
- async function getSplTransfer({
150
- connection,
165
+ export async function getSplTransferBase64Transaction({
166
+ rpc,
151
167
  from,
152
168
  to,
153
169
  mintAddress,
154
170
  amount,
155
- }: GetSplTransferOptions): Promise<Transaction> {
156
- const fromPubkey = new PublicKey(from);
157
- const toPubkey = new PublicKey(to);
158
- const mintPubkey = new PublicKey(mintAddress);
171
+ }: GetSplTokenTransferOptions): Promise<string> {
172
+ const fromAddr = address(from);
173
+ const toAddr = address(to);
174
+ const mintAddr = address(mintAddress);
159
175
 
160
- let mintInfo: Awaited<ReturnType<typeof getMint>>;
161
- try {
162
- mintInfo = await getMint(connection, mintPubkey);
163
- } catch (error) {
164
- throw new Error(`Failed to fetch mint info for mint address ${mintAddress}. Error: ${error}`);
165
- }
166
-
167
- const sourceAta = await getAssociatedTokenAddress(mintPubkey, fromPubkey);
168
- const destinationAta = await getAssociatedTokenAddress(mintPubkey, toPubkey);
176
+ const mintInfo = await fetchMint(rpc, mintAddr);
169
177
 
170
- const transaction = new Transaction();
178
+ const [sourceAta] = await findAssociatedTokenPda({
179
+ mint: mintAddr,
180
+ owner: fromAddr,
181
+ tokenProgram: TOKEN_PROGRAM_ADDRESS,
182
+ });
183
+ const [destAta] = await findAssociatedTokenPda({
184
+ mint: mintAddr,
185
+ owner: toAddr,
186
+ tokenProgram: TOKEN_PROGRAM_ADDRESS,
187
+ });
171
188
 
172
- const sourceAccount = await getAccount(connection, sourceAta);
173
- if (sourceAccount.amount < amount) {
174
- throw new Error(`Insufficient token balance. Have ${sourceAccount.amount}, need ${amount}`);
189
+ const sourceAcct = await fetchToken(rpc, sourceAta);
190
+ if (sourceAcct.data.amount < amount) {
191
+ throw new Error(`Insufficient token balance: have ${sourceAcct.data.amount}, need ${amount}`);
175
192
  }
176
193
 
177
- // Check if destination account exists, if not create it
194
+ const instructions: Instruction[] = [];
195
+
196
+ // If destination ATA does not exist, add create instruction
178
197
  try {
179
- await getAccount(connection, destinationAta);
198
+ await fetchToken(rpc, destAta);
180
199
  } catch {
181
- transaction.add(
182
- createAssociatedTokenAccountInstruction(fromPubkey, destinationAta, toPubkey, mintPubkey),
183
- );
200
+ const createDestIx = await getCreateAssociatedTokenInstructionAsync({
201
+ payer: createNoopSigner(fromAddr),
202
+ owner: toAddr,
203
+ ata: destAta,
204
+ mint: mintAddr,
205
+ });
206
+ instructions.push(createDestIx);
184
207
  }
185
208
 
186
- transaction.add(
187
- createTransferCheckedInstruction(
188
- sourceAta,
189
- mintPubkey,
190
- destinationAta,
191
- fromPubkey,
209
+ instructions.push(
210
+ getTransferCheckedInstruction({
211
+ source: sourceAta,
212
+ mint: mintAddr,
213
+ destination: destAta,
214
+ authority: fromAddr,
192
215
  amount,
193
- mintInfo.decimals,
194
- ),
216
+ decimals: mintInfo.data.decimals,
217
+ }),
218
+ );
219
+
220
+ const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
221
+
222
+ const txMsg = pipe(
223
+ createTransactionMessage({ version: 0 }),
224
+ tx => setTransactionMessageFeePayer(fromAddr, tx),
225
+ tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
226
+ tx => appendTransactionMessageInstructions(instructions, tx),
195
227
  );
196
- transaction.recentBlockhash = SYSVAR_RECENT_BLOCKHASHES_PUBKEY.toBase58();
197
- transaction.feePayer = new PublicKey(from);
198
228
 
199
- return transaction;
229
+ const compiledTransaction = compileTransaction(txMsg);
230
+ return getBase64EncodedWireTransaction(compiledTransaction);
200
231
  }
@@ -14,7 +14,7 @@ type GetOrCreateConnectionOptions = {
14
14
  };
15
15
 
16
16
  /**
17
- * Get a connection to the Solana network
17
+ * Get a connection for the Solana network
18
18
  *
19
19
  * @param options - The options for the connection
20
20
  *
@@ -37,12 +37,10 @@ export function getOrCreateConnection({
37
37
  }
38
38
 
39
39
  /**
40
- * Get the network of the connected Solana node
40
+ * Legacy function for compatibility during migration
41
41
  *
42
- * @param connection - The connection to the Solana network
43
- * @throws {Error} If the network is not mainnet or devnet
44
- *
45
- * @returns The network of the connected Solana node
42
+ * @param connection - The Solana Connection instance
43
+ * @returns The network type (mainnet or devnet)
46
44
  */
47
45
  export async function getConnectedNetwork(connection: Connection): Promise<Network> {
48
46
  const genesisHash = await connection.getGenesisHash();
@@ -53,6 +53,7 @@ export type WaitForFundOperationResult = FailedFundOperation | CompletedFundOper
53
53
  /**
54
54
  * Waits for a fund operation to complete or fail.
55
55
  *
56
+ * @deprecated This method will be removed in a future version. Consider using our Onramp API instead. See https://docs.cdp.coinbase.com/api-reference/v2/rest-api/onramp/create-an-onramp-order.
56
57
  * @example
57
58
  * ```ts
58
59
  * import { waitForFundOperation } from "@coinbase/cdp-sdk";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coinbase/cdp-sdk",
3
- "version": "1.38.3",
3
+ "version": "1.38.4",
4
4
  "description": "SDK for interacting with the Coinbase Developer Platform Wallet API",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
@@ -18,7 +18,9 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@solana/spl-token": "^0.4.13",
21
+ "@solana/kit": "^3.0.3",
22
+ "@solana-program/token": "^0.6.0",
23
+ "@solana-program/system": "^0.8.0",
22
24
  "@solana/web3.js": "^1.98.1",
23
25
  "abitype": "1.0.6",
24
26
  "axios": "^1.12.2",
@@ -43,8 +45,5 @@
43
45
  "cdp sdk",
44
46
  "sdk",
45
47
  "api"
46
- ],
47
- "overrides": {
48
- "bigint-buffer": "npm:bigint-buffer-fixed@^1.1.6"
49
- }
48
+ ]
50
49
  }
package/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "1.38.3";
1
+ export const version = "1.38.4";