@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.
- package/CHANGELOG.md +6 -0
- package/_cjs/actions/evm/fund/fund.js +1 -0
- package/_cjs/actions/evm/fund/fund.js.map +1 -1
- package/_cjs/actions/evm/fund/quoteFund.js +1 -0
- package/_cjs/actions/evm/fund/quoteFund.js.map +1 -1
- package/_cjs/actions/solana/fund/fund.js +1 -0
- package/_cjs/actions/solana/fund/fund.js.map +1 -1
- package/_cjs/actions/solana/fund/quoteFund.js +1 -0
- package/_cjs/actions/solana/fund/quoteFund.js.map +1 -1
- package/_cjs/actions/solana/rpc.js +16 -0
- package/_cjs/actions/solana/rpc.js.map +1 -0
- package/_cjs/actions/solana/transfer.js +74 -48
- package/_cjs/actions/solana/transfer.js.map +1 -1
- package/_cjs/actions/solana/utils.js +4 -6
- package/_cjs/actions/solana/utils.js.map +1 -1
- package/_cjs/actions/waitForFundOperationReceipt.js +1 -0
- package/_cjs/actions/waitForFundOperationReceipt.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/actions/evm/fund/fund.js +1 -0
- package/_esm/actions/evm/fund/fund.js.map +1 -1
- package/_esm/actions/evm/fund/quoteFund.js +1 -0
- package/_esm/actions/evm/fund/quoteFund.js.map +1 -1
- package/_esm/actions/solana/fund/fund.js +1 -0
- package/_esm/actions/solana/fund/fund.js.map +1 -1
- package/_esm/actions/solana/fund/quoteFund.js +1 -0
- package/_esm/actions/solana/fund/quoteFund.js.map +1 -1
- package/_esm/actions/solana/rpc.js +13 -0
- package/_esm/actions/solana/rpc.js.map +1 -0
- package/_esm/actions/solana/transfer.js +72 -48
- package/_esm/actions/solana/transfer.js.map +1 -1
- package/_esm/actions/solana/utils.js +4 -6
- package/_esm/actions/solana/utils.js.map +1 -1
- package/_esm/actions/waitForFundOperationReceipt.js +1 -0
- package/_esm/actions/waitForFundOperationReceipt.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/actions/evm/fund/fund.d.ts +1 -0
- package/_types/actions/evm/fund/fund.d.ts.map +1 -1
- package/_types/actions/evm/fund/quoteFund.d.ts +1 -0
- package/_types/actions/evm/fund/quoteFund.d.ts.map +1 -1
- package/_types/actions/solana/fund/fund.d.ts +1 -0
- package/_types/actions/solana/fund/fund.d.ts.map +1 -1
- package/_types/actions/solana/fund/quoteFund.d.ts +1 -0
- package/_types/actions/solana/fund/quoteFund.d.ts.map +1 -1
- package/_types/actions/solana/rpc.d.ts +10 -0
- package/_types/actions/solana/rpc.d.ts.map +1 -0
- package/_types/actions/solana/transfer.d.ts +37 -1
- package/_types/actions/solana/transfer.d.ts.map +1 -1
- package/_types/actions/solana/utils.d.ts +4 -6
- package/_types/actions/solana/utils.d.ts.map +1 -1
- package/_types/actions/waitForFundOperationReceipt.d.ts +1 -0
- package/_types/actions/waitForFundOperationReceipt.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/actions/evm/fund/fund.ts +1 -0
- package/actions/evm/fund/quoteFund.ts +1 -0
- package/actions/solana/fund/fund.ts +1 -0
- package/actions/solana/fund/quoteFund.ts +1 -0
- package/actions/solana/rpc.ts +17 -0
- package/actions/solana/transfer.ts +115 -84
- package/actions/solana/utils.ts +4 -6
- package/actions/waitForFundOperationReceipt.ts +1 -0
- package/package.json +5 -6
- package/version.ts +1 -1
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
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
|
|
76
|
+
const base64Transaction =
|
|
66
77
|
options.token === "sol"
|
|
67
|
-
? await
|
|
78
|
+
? await getNativeTransferBase64Transaction({
|
|
79
|
+
rpc,
|
|
68
80
|
from: options.from,
|
|
69
81
|
to: options.to,
|
|
70
82
|
amount: options.amount,
|
|
71
83
|
})
|
|
72
|
-
: await
|
|
73
|
-
|
|
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:
|
|
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
|
|
106
|
+
* Gets the transaction for a SOL transfer
|
|
97
107
|
*
|
|
98
|
-
* @param options - The options for the
|
|
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
|
|
115
|
+
* @returns The SOL transfer transaction
|
|
105
116
|
*/
|
|
106
|
-
async function
|
|
117
|
+
export async function getNativeTransferBase64Transaction({
|
|
118
|
+
rpc,
|
|
107
119
|
from,
|
|
108
120
|
to,
|
|
109
121
|
amount,
|
|
110
|
-
}: GetNativeTransferOptions): Promise<
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
143
|
+
const compiledTransaction = compileTransaction(txMsg);
|
|
144
|
+
return getBase64EncodedWireTransaction(compiledTransaction);
|
|
123
145
|
}
|
|
124
146
|
|
|
125
|
-
type
|
|
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.
|
|
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
|
|
150
|
-
|
|
165
|
+
export async function getSplTransferBase64Transaction({
|
|
166
|
+
rpc,
|
|
151
167
|
from,
|
|
152
168
|
to,
|
|
153
169
|
mintAddress,
|
|
154
170
|
amount,
|
|
155
|
-
}:
|
|
156
|
-
const
|
|
157
|
-
const
|
|
158
|
-
const
|
|
171
|
+
}: GetSplTokenTransferOptions): Promise<string> {
|
|
172
|
+
const fromAddr = address(from);
|
|
173
|
+
const toAddr = address(to);
|
|
174
|
+
const mintAddr = address(mintAddress);
|
|
159
175
|
|
|
160
|
-
|
|
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
|
|
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
|
|
173
|
-
if (
|
|
174
|
-
throw new Error(`Insufficient token balance
|
|
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
|
-
|
|
194
|
+
const instructions: Instruction[] = [];
|
|
195
|
+
|
|
196
|
+
// If destination ATA does not exist, add create instruction
|
|
178
197
|
try {
|
|
179
|
-
await
|
|
198
|
+
await fetchToken(rpc, destAta);
|
|
180
199
|
} catch {
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
sourceAta,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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
|
-
|
|
229
|
+
const compiledTransaction = compileTransaction(txMsg);
|
|
230
|
+
return getBase64EncodedWireTransaction(compiledTransaction);
|
|
200
231
|
}
|
package/actions/solana/utils.ts
CHANGED
|
@@ -14,7 +14,7 @@ type GetOrCreateConnectionOptions = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Get a connection
|
|
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
|
-
*
|
|
40
|
+
* Legacy function for compatibility during migration
|
|
41
41
|
*
|
|
42
|
-
* @param connection - The
|
|
43
|
-
* @
|
|
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
|
+
"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/
|
|
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.
|
|
1
|
+
export const version = "1.38.4";
|