@coinbase/cdp-sdk 1.3.2 → 1.5.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/CHANGELOG.md +14 -0
- package/README.md +87 -2
- package/_cjs/accounts/evm/toEvmServerAccount.js +5 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_cjs/accounts/evm/toEvmSmartAccount.js +7 -2
- package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
- package/_cjs/actions/evm/transfer/accountTransferStrategy.js +72 -0
- package/_cjs/actions/evm/transfer/accountTransferStrategy.js.map +1 -0
- package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js +65 -0
- package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -0
- package/_cjs/actions/evm/transfer/transfer.js +59 -0
- package/_cjs/actions/evm/transfer/transfer.js.map +1 -0
- package/_cjs/actions/evm/transfer/types.js +3 -0
- package/_cjs/actions/evm/transfer/types.js.map +1 -0
- package/_cjs/actions/evm/transfer/utils.js +39 -0
- package/_cjs/actions/evm/transfer/utils.js.map +1 -0
- package/_cjs/actions/evm/types.js +3 -0
- package/_cjs/actions/evm/types.js.map +1 -0
- package/_cjs/auth/index.js +1 -0
- package/_cjs/auth/index.js.map +1 -1
- package/_cjs/auth/utils/http.js +2 -0
- package/_cjs/auth/utils/http.js.map +1 -1
- package/_cjs/auth/utils/index.js +1 -0
- package/_cjs/auth/utils/index.js.map +1 -1
- package/_cjs/auth/utils/jwt.js +16 -6
- package/_cjs/auth/utils/jwt.js.map +1 -1
- package/_cjs/auth/utils/ws.js +31 -0
- package/_cjs/auth/utils/ws.js.map +1 -0
- package/_cjs/client/evm/evm.js +44 -2
- package/_cjs/client/evm/evm.js.map +1 -1
- package/_cjs/client/solana/solana.js +42 -0
- package/_cjs/client/solana/solana.js.map +1 -1
- package/_cjs/utils/serializeTransaction.js +18 -0
- package/_cjs/utils/serializeTransaction.js.map +1 -0
- package/_cjs/version.js +1 -1
- package/_esm/accounts/evm/toEvmServerAccount.js +5 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_esm/accounts/evm/toEvmSmartAccount.js +7 -2
- package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
- package/_esm/actions/evm/transfer/accountTransferStrategy.js +69 -0
- package/_esm/actions/evm/transfer/accountTransferStrategy.js.map +1 -0
- package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js +62 -0
- package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -0
- package/_esm/actions/evm/transfer/transfer.js +56 -0
- package/_esm/actions/evm/transfer/transfer.js.map +1 -0
- package/_esm/actions/evm/transfer/types.js +2 -0
- package/_esm/actions/evm/transfer/types.js.map +1 -0
- package/_esm/actions/evm/transfer/utils.js +35 -0
- package/_esm/actions/evm/transfer/utils.js.map +1 -0
- package/_esm/actions/evm/types.js +2 -0
- package/_esm/actions/evm/types.js.map +1 -0
- package/_esm/auth/index.js +1 -0
- package/_esm/auth/index.js.map +1 -1
- package/_esm/auth/utils/http.js +2 -1
- package/_esm/auth/utils/http.js.map +1 -1
- package/_esm/auth/utils/index.js +1 -0
- package/_esm/auth/utils/index.js.map +1 -1
- package/_esm/auth/utils/jwt.js +16 -6
- package/_esm/auth/utils/jwt.js.map +1 -1
- package/_esm/auth/utils/ws.js +28 -0
- package/_esm/auth/utils/ws.js.map +1 -0
- package/_esm/client/evm/evm.js +44 -2
- package/_esm/client/evm/evm.js.map +1 -1
- package/_esm/client/solana/solana.js +42 -0
- package/_esm/client/solana/solana.js.map +1 -1
- package/_esm/utils/serializeTransaction.js +15 -0
- package/_esm/utils/serializeTransaction.js.map +1 -0
- package/_esm/version.js +1 -1
- package/_types/accounts/evm/toEvmServerAccount.d.ts +2 -2
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
- package/_types/accounts/evm/toEvmSmartAccount.d.ts +4 -4
- package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -1
- package/_types/accounts/types.d.ts +4 -3
- package/_types/accounts/types.d.ts.map +1 -1
- package/_types/actions/evm/transfer/accountTransferStrategy.d.ts +4 -0
- package/_types/actions/evm/transfer/accountTransferStrategy.d.ts.map +1 -0
- package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts +4 -0
- package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts.map +1 -0
- package/_types/actions/evm/transfer/transfer.d.ts +14 -0
- package/_types/actions/evm/transfer/transfer.d.ts.map +1 -0
- package/_types/actions/evm/transfer/types.d.ts +71 -0
- package/_types/actions/evm/transfer/types.d.ts.map +1 -0
- package/_types/actions/evm/transfer/utils.d.ts +631 -0
- package/_types/actions/evm/transfer/utils.d.ts.map +1 -0
- package/_types/actions/evm/types.d.ts +88 -0
- package/_types/actions/evm/types.d.ts.map +1 -0
- package/_types/auth/index.d.ts +1 -0
- package/_types/auth/index.d.ts.map +1 -1
- package/_types/auth/utils/http.d.ts +12 -0
- package/_types/auth/utils/http.d.ts.map +1 -1
- package/_types/auth/utils/index.d.ts +1 -0
- package/_types/auth/utils/index.d.ts.map +1 -1
- package/_types/auth/utils/jwt.d.ts +13 -7
- package/_types/auth/utils/jwt.d.ts.map +1 -1
- package/_types/auth/utils/ws.d.ts +45 -0
- package/_types/auth/utils/ws.d.ts.map +1 -0
- package/_types/client/evm/evm.d.ts +17 -1
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +10 -1
- package/_types/client/evm/evm.types.d.ts.map +1 -1
- package/_types/client/solana/solana.d.ts +17 -1
- package/_types/client/solana/solana.d.ts.map +1 -1
- package/_types/client/solana/solana.types.d.ts +8 -0
- package/_types/client/solana/solana.types.d.ts.map +1 -1
- package/_types/utils/serializeTransaction.d.ts +9 -0
- package/_types/utils/serializeTransaction.d.ts.map +1 -0
- package/_types/version.d.ts +1 -1
- package/accounts/evm/toEvmServerAccount.ts +12 -6
- package/accounts/evm/toEvmSmartAccount.ts +16 -4
- package/accounts/types.ts +20 -16
- package/actions/evm/transfer/accountTransferStrategy.ts +84 -0
- package/actions/evm/transfer/smartAccountTransferStrategy.ts +70 -0
- package/actions/evm/transfer/transfer.ts +71 -0
- package/actions/evm/transfer/types.ts +81 -0
- package/actions/evm/transfer/utils.ts +39 -0
- package/actions/evm/types.ts +88 -0
- package/auth/README.md +18 -3
- package/auth/index.ts +1 -0
- package/auth/utils/http.ts +7 -1
- package/auth/utils/index.ts +1 -0
- package/auth/utils/jwt.ts +36 -13
- package/auth/utils/ws.ts +76 -0
- package/client/evm/evm.ts +45 -2
- package/client/evm/evm.types.ts +11 -1
- package/client/solana/solana.ts +43 -0
- package/client/solana/solana.types.ts +9 -0
- package/package.json +1 -1
- package/utils/serializeTransaction.ts +17 -0
- package/version.ts +1 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
encodeFunctionData,
|
|
3
|
+
erc20Abi,
|
|
4
|
+
Hex,
|
|
5
|
+
TransactionReceipt,
|
|
6
|
+
WaitForTransactionReceiptTimeoutError,
|
|
7
|
+
} from "viem";
|
|
8
|
+
|
|
9
|
+
import { TransferExecutionStrategy } from "./types.js";
|
|
10
|
+
import { getErc20Address } from "./utils.js";
|
|
11
|
+
import { EvmAccount } from "../../../accounts/types.js";
|
|
12
|
+
import { serializeEIP1559Transaction } from "../../../utils/serializeTransaction.js";
|
|
13
|
+
|
|
14
|
+
export const accountTransferStrategy: TransferExecutionStrategy<EvmAccount> = {
|
|
15
|
+
executeTransfer: async ({ apiClient, from, transferArgs, to, value }) => {
|
|
16
|
+
const transactionHash = await (async () => {
|
|
17
|
+
if (transferArgs.token === "eth") {
|
|
18
|
+
const result = await apiClient.sendEvmTransaction(from.address, {
|
|
19
|
+
transaction: serializeEIP1559Transaction({
|
|
20
|
+
value,
|
|
21
|
+
to,
|
|
22
|
+
}),
|
|
23
|
+
network: transferArgs.network,
|
|
24
|
+
});
|
|
25
|
+
return result.transactionHash as Hex;
|
|
26
|
+
} else {
|
|
27
|
+
const erc20Address = getErc20Address(transferArgs.token, transferArgs.network);
|
|
28
|
+
|
|
29
|
+
await apiClient.sendEvmTransaction(from.address, {
|
|
30
|
+
transaction: serializeEIP1559Transaction({
|
|
31
|
+
to: erc20Address,
|
|
32
|
+
data: encodeFunctionData({
|
|
33
|
+
abi: erc20Abi,
|
|
34
|
+
functionName: "approve",
|
|
35
|
+
args: [to, value],
|
|
36
|
+
}),
|
|
37
|
+
}),
|
|
38
|
+
network: transferArgs.network,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const result = await apiClient.sendEvmTransaction(from.address, {
|
|
42
|
+
transaction: serializeEIP1559Transaction({
|
|
43
|
+
to: erc20Address,
|
|
44
|
+
data: encodeFunctionData({
|
|
45
|
+
abi: erc20Abi,
|
|
46
|
+
functionName: "transfer",
|
|
47
|
+
args: [to, value],
|
|
48
|
+
}),
|
|
49
|
+
}),
|
|
50
|
+
network: transferArgs.network,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return result.transactionHash as Hex;
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
|
|
57
|
+
return transactionHash;
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
waitForResult: async ({ publicClient, hash }) => {
|
|
61
|
+
let receipt: TransactionReceipt;
|
|
62
|
+
try {
|
|
63
|
+
receipt = await publicClient.waitForTransactionReceipt({
|
|
64
|
+
hash,
|
|
65
|
+
});
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (error instanceof WaitForTransactionReceiptTimeoutError) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Transaction timed out. Check the transaction on the explorer: ${publicClient.chain.blockExplorers?.default.url}/tx/${hash}`,
|
|
70
|
+
);
|
|
71
|
+
} else {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (receipt.status === "success") {
|
|
77
|
+
return { status: receipt.status, transactionHash: hash };
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Transaction failed. Check the transaction on the explorer: ${publicClient.chain.blockExplorers?.default.url}/tx/${hash}`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { encodeFunctionData, erc20Abi } from "viem";
|
|
2
|
+
|
|
3
|
+
import { TransferExecutionStrategy } from "./types.js";
|
|
4
|
+
import { getErc20Address } from "./utils.js";
|
|
5
|
+
import { EvmSmartAccount } from "../../../accounts/types.js";
|
|
6
|
+
import { sendUserOperation } from "../sendUserOperation.js";
|
|
7
|
+
import { waitForUserOperation } from "../waitForUserOperation.js";
|
|
8
|
+
|
|
9
|
+
export const smartAccountTransferStrategy: TransferExecutionStrategy<EvmSmartAccount> = {
|
|
10
|
+
executeTransfer: async ({ apiClient, from, transferArgs, to, value }) => {
|
|
11
|
+
const userOpHash = await (async () => {
|
|
12
|
+
if (transferArgs.token === "eth") {
|
|
13
|
+
const result = await sendUserOperation(apiClient, from, {
|
|
14
|
+
network: transferArgs.network,
|
|
15
|
+
calls: [
|
|
16
|
+
{
|
|
17
|
+
to,
|
|
18
|
+
value,
|
|
19
|
+
data: "0x",
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
return result.userOpHash;
|
|
24
|
+
} else {
|
|
25
|
+
const erc20Address = getErc20Address(transferArgs.token, transferArgs.network);
|
|
26
|
+
|
|
27
|
+
const result = await sendUserOperation(apiClient, from, {
|
|
28
|
+
network: transferArgs.network,
|
|
29
|
+
calls: [
|
|
30
|
+
{
|
|
31
|
+
to: erc20Address,
|
|
32
|
+
data: encodeFunctionData({
|
|
33
|
+
abi: erc20Abi,
|
|
34
|
+
functionName: "approve",
|
|
35
|
+
args: [to, value],
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
to: erc20Address,
|
|
40
|
+
data: encodeFunctionData({
|
|
41
|
+
abi: erc20Abi,
|
|
42
|
+
functionName: "transfer",
|
|
43
|
+
args: [to, value],
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return result.userOpHash;
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
|
|
53
|
+
return userOpHash;
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
waitForResult: async ({ apiClient, publicClient, from, hash }) => {
|
|
57
|
+
const result = await waitForUserOperation(apiClient, {
|
|
58
|
+
smartAccountAddress: from.address,
|
|
59
|
+
userOpHash: hash,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (result.status === "complete") {
|
|
63
|
+
return { status: "success", transactionHash: hash };
|
|
64
|
+
} else {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Transaction failed. Check the transaction on the explorer: ${publicClient.chain.blockExplorers?.default.url}/tx/${hash}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createPublicClient, http, erc20Abi, parseUnits, Address, Chain, Transport } from "viem";
|
|
2
|
+
|
|
3
|
+
import { TransferResult, TransferOptions, TransferExecutionStrategy } from "./types.js";
|
|
4
|
+
import { mapNetworkToChain } from "./utils.js";
|
|
5
|
+
import { EvmAccount, EvmSmartAccount } from "../../../accounts/types.js";
|
|
6
|
+
import { CdpOpenApiClientType } from "../../../openapi-client/index.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Transfer an amount of a token from an account to another account.
|
|
10
|
+
*
|
|
11
|
+
* @param apiClient - The client to use to send the transaction.
|
|
12
|
+
* @param from - The account to send the transaction from.
|
|
13
|
+
* @param transferArgs - The options for the transfer.
|
|
14
|
+
* @param transferStrategy - The strategy to use to execute the transfer.
|
|
15
|
+
* @returns The result of the transfer.
|
|
16
|
+
*/
|
|
17
|
+
export async function transfer<T extends EvmAccount | EvmSmartAccount>(
|
|
18
|
+
apiClient: CdpOpenApiClientType,
|
|
19
|
+
from: T,
|
|
20
|
+
transferArgs: TransferOptions,
|
|
21
|
+
transferStrategy: TransferExecutionStrategy<T>,
|
|
22
|
+
): Promise<TransferResult> {
|
|
23
|
+
const publicClient = createPublicClient<Transport, Chain>({
|
|
24
|
+
chain: mapNetworkToChain(transferArgs.network),
|
|
25
|
+
transport: http(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const to =
|
|
29
|
+
typeof transferArgs.to === "string" ? transferArgs.to : (transferArgs.to.address as Address);
|
|
30
|
+
|
|
31
|
+
const value = await (async () => {
|
|
32
|
+
// user supplied a bigint. otherwise, we need to convert the amount to a bigint
|
|
33
|
+
if (typeof transferArgs.amount !== "string") {
|
|
34
|
+
return transferArgs.amount;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const decimals = await (async () => {
|
|
38
|
+
if (transferArgs.token === "eth") {
|
|
39
|
+
return 18;
|
|
40
|
+
} else if (transferArgs.token === "usdc") {
|
|
41
|
+
return 6;
|
|
42
|
+
} else {
|
|
43
|
+
return publicClient.readContract({
|
|
44
|
+
address: transferArgs.token,
|
|
45
|
+
abi: erc20Abi,
|
|
46
|
+
functionName: "decimals",
|
|
47
|
+
args: [],
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
51
|
+
|
|
52
|
+
return parseUnits(transferArgs.amount, decimals);
|
|
53
|
+
})();
|
|
54
|
+
|
|
55
|
+
const hash = await transferStrategy.executeTransfer({
|
|
56
|
+
apiClient,
|
|
57
|
+
from,
|
|
58
|
+
transferArgs,
|
|
59
|
+
to,
|
|
60
|
+
value,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const result = await transferStrategy.waitForResult({
|
|
64
|
+
apiClient,
|
|
65
|
+
publicClient,
|
|
66
|
+
from,
|
|
67
|
+
hash,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Hex, TransactionReceipt, Address, PublicClient, Chain, Transport } from "viem";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CdpOpenApiClientType,
|
|
5
|
+
EvmAccount,
|
|
6
|
+
EvmUserOperationNetwork,
|
|
7
|
+
SendEvmTransactionBodyNetwork,
|
|
8
|
+
} from "../../../openapi-client/index.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The network to transfer the token on.
|
|
12
|
+
*/
|
|
13
|
+
export type Network = SendEvmTransactionBodyNetwork | EvmUserOperationNetwork;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The options for the transfer.
|
|
17
|
+
*/
|
|
18
|
+
export type TransferOptions = {
|
|
19
|
+
/** The account to transfer the token to. */
|
|
20
|
+
to: EvmAccount | Address;
|
|
21
|
+
/**
|
|
22
|
+
* The amount of the token to transfer.
|
|
23
|
+
* If a string is provided, it will be parsed into a bigint based on the token's decimals.
|
|
24
|
+
*/
|
|
25
|
+
amount: bigint | string;
|
|
26
|
+
/** The token to transfer. Can be a contract address or a predefined token name. */
|
|
27
|
+
token: "eth" | "usdc" | Hex;
|
|
28
|
+
/** The network to transfer the token on. */
|
|
29
|
+
network: Network;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The result of the transfer.
|
|
34
|
+
*/
|
|
35
|
+
export type TransferResult = {
|
|
36
|
+
/** The status of the transaction. */
|
|
37
|
+
status: TransactionReceipt["status"];
|
|
38
|
+
/** The transaction hash of the transfer. */
|
|
39
|
+
transactionHash: Hex;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A strategy for executing a transfer.
|
|
44
|
+
*/
|
|
45
|
+
export interface TransferExecutionStrategy<T> {
|
|
46
|
+
/**
|
|
47
|
+
* Executes the transfer.
|
|
48
|
+
*
|
|
49
|
+
* @param args - The arguments for the transfer.
|
|
50
|
+
* @param args.apiClient - The API client to use for the transfer.
|
|
51
|
+
* @param args.from - The account to transfer the token from.
|
|
52
|
+
* @param args.transferArgs - The arguments for the transfer.
|
|
53
|
+
* @param args.to - The account to transfer the token to.
|
|
54
|
+
* @param args.value - The value of the transfer.
|
|
55
|
+
* @returns The transaction hash of the transfer.
|
|
56
|
+
*/
|
|
57
|
+
executeTransfer(args: {
|
|
58
|
+
apiClient: CdpOpenApiClientType;
|
|
59
|
+
from: T;
|
|
60
|
+
transferArgs: TransferOptions;
|
|
61
|
+
to: Address;
|
|
62
|
+
value: bigint;
|
|
63
|
+
}): Promise<Hex>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Waits for the result of the transfer.
|
|
67
|
+
*
|
|
68
|
+
* @param args - The arguments for the transfer.
|
|
69
|
+
* @param args.apiClient - The API client to use for the transfer.
|
|
70
|
+
* @param args.publicClient - The public client to use for the transfer.
|
|
71
|
+
* @param args.from - The account to transfer the token from.
|
|
72
|
+
* @param args.hash - The transaction hash of the transfer.
|
|
73
|
+
* @returns The result of the transfer.
|
|
74
|
+
*/
|
|
75
|
+
waitForResult(args: {
|
|
76
|
+
apiClient: CdpOpenApiClientType;
|
|
77
|
+
publicClient: PublicClient<Transport, Chain>;
|
|
78
|
+
from: T;
|
|
79
|
+
hash: Hex;
|
|
80
|
+
}): Promise<TransferResult>;
|
|
81
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { base, baseSepolia } from "viem/chains";
|
|
2
|
+
|
|
3
|
+
import { Network } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The address of an ERC20 token for a given network.
|
|
7
|
+
*/
|
|
8
|
+
const addressMap = {
|
|
9
|
+
base: {
|
|
10
|
+
usdc: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
11
|
+
},
|
|
12
|
+
"base-sepolia": {
|
|
13
|
+
usdc: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Get the address of an ERC20 token for a given network.
|
|
19
|
+
* If a contract address is provided, it will not be found in the map and will be returned as is.
|
|
20
|
+
*
|
|
21
|
+
* @param token - The token symbol or contract address.
|
|
22
|
+
* @param network - The network to get the address for.
|
|
23
|
+
*
|
|
24
|
+
* @returns The address of the ERC20 token.
|
|
25
|
+
*/
|
|
26
|
+
export function getErc20Address(token: string, network: Network) {
|
|
27
|
+
return addressMap[network][token] ?? token;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Map a network to a viem chain.
|
|
32
|
+
* TODO: Add more networks (or dynamically map viem chains).
|
|
33
|
+
*
|
|
34
|
+
* @param network - The network to map to a chain.
|
|
35
|
+
* @returns The chain for the given network.
|
|
36
|
+
*/
|
|
37
|
+
export function mapNetworkToChain(network: Network) {
|
|
38
|
+
return network === "base" ? base : baseSepolia;
|
|
39
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { TransferOptions, TransferResult } from "./transfer/types.js";
|
|
2
|
+
|
|
3
|
+
export type Actions = {
|
|
4
|
+
/**
|
|
5
|
+
* Transfer an amount of a token from an account to another account.
|
|
6
|
+
*
|
|
7
|
+
* @param options - The options for the transfer.
|
|
8
|
+
* @param options.to - The account or 0x-prefixed address to transfer the token to.
|
|
9
|
+
* @param options.amount - The amount of the token to transfer.
|
|
10
|
+
* @param options.token - The token to transfer.
|
|
11
|
+
* @param options.network - The network to transfer the token on.
|
|
12
|
+
*
|
|
13
|
+
* @returns The result of the transfer.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const { status } = await sender.transfer({
|
|
18
|
+
* to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
|
|
19
|
+
* amount: "0.01",
|
|
20
|
+
* token: "usdc",
|
|
21
|
+
* network: "base-sepolia",
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* **Pass a bigint value**
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const { status } = await sender.transfer({
|
|
29
|
+
* to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
|
|
30
|
+
* amount: 10000n, // equivalent to 0.01 usdc
|
|
31
|
+
* token: "usdc",
|
|
32
|
+
* network: "base-sepolia",
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* **Transfer from a smart account**
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const sender = await cdp.evm.createSmartAccount({
|
|
40
|
+
* owner: await cdp.evm.createAccount({ name: "Owner" }),
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* const { status } = await sender.transfer({
|
|
44
|
+
* to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
|
|
45
|
+
* amount: "0.01",
|
|
46
|
+
* token: "usdc",
|
|
47
|
+
* network: "base-sepolia",
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* **Transfer ETH**
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const { status } = await sender.transfer({
|
|
55
|
+
* to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
|
|
56
|
+
* amount: "0.000001",
|
|
57
|
+
* token: "eth",
|
|
58
|
+
* network: "base-sepolia",
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* **Using a contract address**
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const { status } = await sender.transfer({
|
|
66
|
+
* to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
|
|
67
|
+
* amount: "0.000001",
|
|
68
|
+
* token: "0x4200000000000000000000000000000000000006", // WETH on Base Sepolia
|
|
69
|
+
* network: "base-sepolia",
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* **Transfer to another account**
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const sender = await cdp.evm.createAccount({ name: "Sender" });
|
|
77
|
+
* const receiver = await cdp.evm.createAccount({ name: "Receiver" });
|
|
78
|
+
*
|
|
79
|
+
* const { status } = await sender.transfer({
|
|
80
|
+
* to: receiver,
|
|
81
|
+
* amount: "0.01",
|
|
82
|
+
* token: "usdc",
|
|
83
|
+
* network: "base-sepolia",
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
transfer: (options: TransferOptions) => Promise<TransferResult>;
|
|
88
|
+
};
|
package/auth/README.md
CHANGED
|
@@ -25,6 +25,7 @@ npm install @coinbase/cdp-sdk
|
|
|
25
25
|
```typescript
|
|
26
26
|
import { generateJwt } from "@coinbase/cdp-sdk/auth";
|
|
27
27
|
|
|
28
|
+
// For REST (HTTP) requests
|
|
28
29
|
const jwt = await generateJwt({
|
|
29
30
|
apiKeyId: "YOUR_API_KEY_ID",
|
|
30
31
|
apiKeySecret: "YOUR_API_KEY_SECRET",
|
|
@@ -35,6 +36,18 @@ const jwt = await generateJwt({
|
|
|
35
36
|
});
|
|
36
37
|
|
|
37
38
|
console.log(jwt);
|
|
39
|
+
|
|
40
|
+
// For websocket connections
|
|
41
|
+
const websocketJwt = await generateJwt({
|
|
42
|
+
apiKeyId: "YOUR_API_KEY_ID",
|
|
43
|
+
apiKeySecret: "YOUR_API_KEY_SECRET",
|
|
44
|
+
requestMethod: null,
|
|
45
|
+
requestHost: null,
|
|
46
|
+
requestPath: null,
|
|
47
|
+
expiresIn: 120, // optional (defaults to 120 seconds)
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
console.log(websocketJwt);
|
|
38
51
|
```
|
|
39
52
|
|
|
40
53
|
For information about the above parameters, please refer to the [Authentication parameters](#authentication-parameters) section.
|
|
@@ -138,8 +151,10 @@ The following table provides more context of many of the authentication paramete
|
|
|
138
151
|
| :-- | :-- | :-- |
|
|
139
152
|
| `apiKeyId` | true | The unique identifier for your API key. Supported formats are:<br/>- `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`<br/>- `organizations/{orgId}/apiKeys/{keyId}` |
|
|
140
153
|
| `apiKeySecret` | true | Your API key secret. Supported formats are:<br/>- Edwards key (Ed25519): `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==`<br/>- Elliptic Curve key (ES256): `-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n` |
|
|
141
|
-
| `requestMethod` | true | The HTTP method for the API request you're authenticating (ie, `GET`, `POST`, `PUT`, `DELETE`). |
|
|
142
|
-
| `requestHost` | true | The API host you're calling (ie, `api.cdp.coinbase.com`). |
|
|
143
|
-
| `requestPath` | true | The path of the specific API endpoint you're calling (ie, `/platform/v1/wallets`). |
|
|
154
|
+
| `requestMethod` | true* | The HTTP method for the API request you're authenticating (ie, `GET`, `POST`, `PUT`, `DELETE`). Can be `null` for JWTs intended for websocket connections. |
|
|
155
|
+
| `requestHost` | true* | The API host you're calling (ie, `api.cdp.coinbase.com`). Can be `null` for JWTs intended for websocket connections. |
|
|
156
|
+
| `requestPath` | true* | The path of the specific API endpoint you're calling (ie, `/platform/v1/wallets`). Can be `null` for JWTs intended for websocket connections. |
|
|
144
157
|
| `requestBody` | false | Optional request body data. |
|
|
145
158
|
| `expiresIn` | false | The JWT expiration time in seconds. After this time, the JWT will no longer be valid, and a new one must be generated. Defaults to `120` (ie, 2 minutes) if not specified. |
|
|
159
|
+
|
|
160
|
+
\* Either all three request parameters (`requestMethod`, `requestHost`, and `requestPath`) must be provided for REST API requests, or all three must be `null` for JWTs intended for websocket connections.
|
package/auth/index.ts
CHANGED
package/auth/utils/http.ts
CHANGED
|
@@ -62,6 +62,11 @@ export interface GetAuthHeadersOptions {
|
|
|
62
62
|
* Optional expiration time in seconds (defaults to 120)
|
|
63
63
|
*/
|
|
64
64
|
expiresIn?: number;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Optional audience claim for the JWT
|
|
68
|
+
*/
|
|
69
|
+
audience?: string[];
|
|
65
70
|
}
|
|
66
71
|
|
|
67
72
|
/**
|
|
@@ -83,6 +88,7 @@ export async function getAuthHeaders(
|
|
|
83
88
|
requestHost: options.requestHost,
|
|
84
89
|
requestPath: options.requestPath,
|
|
85
90
|
expiresIn: options.expiresIn,
|
|
91
|
+
audience: options.audience,
|
|
86
92
|
});
|
|
87
93
|
headers["Authorization"] = `Bearer ${jwt}`;
|
|
88
94
|
headers["Content-Type"] = "application/json";
|
|
@@ -131,7 +137,7 @@ function requiresWalletAuth(requestMethod: string, requestPath: string): boolean
|
|
|
131
137
|
* @param sourceVersion - The version of the source making the request
|
|
132
138
|
* @returns Encoded correlation data as a query string
|
|
133
139
|
*/
|
|
134
|
-
function getCorrelationData(source?: string, sourceVersion?: string): string {
|
|
140
|
+
export function getCorrelationData(source?: string, sourceVersion?: string): string {
|
|
135
141
|
const data = {
|
|
136
142
|
sdk_version: version,
|
|
137
143
|
sdk_language: "typescript",
|
package/auth/utils/index.ts
CHANGED
package/auth/utils/jwt.ts
CHANGED
|
@@ -32,24 +32,29 @@ export interface JwtOptions {
|
|
|
32
32
|
apiKeySecret: string;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
* The HTTP method for the request (e.g. 'GET', 'POST')
|
|
35
|
+
* The HTTP method for the request (e.g. 'GET', 'POST'), or null for JWTs intended for websocket connections
|
|
36
36
|
*/
|
|
37
|
-
requestMethod
|
|
37
|
+
requestMethod?: string | null;
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* The host for the request (e.g. 'api.cdp.coinbase.com')
|
|
40
|
+
* The host for the request (e.g. 'api.cdp.coinbase.com'), or null for JWTs intended for websocket connections
|
|
41
41
|
*/
|
|
42
|
-
requestHost
|
|
42
|
+
requestHost?: string | null;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
* The path for the request (e.g. '/platform/v1/wallets')
|
|
45
|
+
* The path for the request (e.g. '/platform/v1/wallets'), or null for JWTs intended for websocket connections
|
|
46
46
|
*/
|
|
47
|
-
requestPath
|
|
47
|
+
requestPath?: string | null;
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* Optional expiration time in seconds (defaults to 120)
|
|
51
51
|
*/
|
|
52
52
|
expiresIn?: number;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Optional audience claim for the JWT
|
|
56
|
+
*/
|
|
57
|
+
audience?: string[];
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
/**
|
|
@@ -88,7 +93,9 @@ export interface WalletJwtOptions {
|
|
|
88
93
|
|
|
89
94
|
/**
|
|
90
95
|
* Generates a JWT (also known as a Bearer token) for authenticating with Coinbase's REST APIs.
|
|
91
|
-
* Supports both EC (ES256) and Ed25519 (EdDSA) keys.
|
|
96
|
+
* Supports both EC (ES256) and Ed25519 (EdDSA) keys. Also supports JWTs meant for
|
|
97
|
+
* websocket connections by allowing requestMethod, requestHost, and requestPath to all be
|
|
98
|
+
* null, in which case the 'uris' claim is omitted from the JWT.
|
|
92
99
|
*
|
|
93
100
|
* @param options - The configuration options for generating the JWT
|
|
94
101
|
* @returns The generated JWT (Bearer token) string
|
|
@@ -102,22 +109,38 @@ export async function generateJwt(options: JwtOptions): Promise<string> {
|
|
|
102
109
|
if (!options.apiKeySecret) {
|
|
103
110
|
throw new Error("Private key is required");
|
|
104
111
|
}
|
|
105
|
-
|
|
106
|
-
|
|
112
|
+
|
|
113
|
+
// Check if we have a REST API request or a websocket connection
|
|
114
|
+
const hasAllRequestParams = Boolean(
|
|
115
|
+
options.requestMethod && options.requestHost && options.requestPath,
|
|
116
|
+
);
|
|
117
|
+
const hasNoRequestParams =
|
|
118
|
+
(options.requestMethod === undefined || options.requestMethod === null) &&
|
|
119
|
+
(options.requestHost === undefined || options.requestHost === null) &&
|
|
120
|
+
(options.requestPath === undefined || options.requestPath === null);
|
|
121
|
+
|
|
122
|
+
// Ensure we either have all request parameters or none (for websocket)
|
|
123
|
+
if (!hasAllRequestParams && !hasNoRequestParams) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
"Either all request details (method, host, path) must be provided, or all must be null for JWTs intended for websocket connections",
|
|
126
|
+
);
|
|
107
127
|
}
|
|
108
128
|
|
|
109
129
|
const now = Math.floor(Date.now() / 1000);
|
|
110
130
|
const expiresIn = options.expiresIn || 120; // Default to 120 seconds if not specified
|
|
111
|
-
const uri = `${options.requestMethod} ${options.requestHost}${options.requestPath}`;
|
|
112
131
|
|
|
113
132
|
// Prepare the JWT payload
|
|
114
|
-
const claims = {
|
|
133
|
+
const claims: JWTPayload = {
|
|
115
134
|
sub: options.apiKeyId,
|
|
116
135
|
iss: "cdp",
|
|
117
|
-
aud: ["cdp_service"],
|
|
118
|
-
uris: [uri],
|
|
136
|
+
aud: options.audience || ["cdp_service"],
|
|
119
137
|
};
|
|
120
138
|
|
|
139
|
+
// Add the uris claim only for REST API requests
|
|
140
|
+
if (hasAllRequestParams) {
|
|
141
|
+
claims.uris = [`${options.requestMethod} ${options.requestHost}${options.requestPath}`];
|
|
142
|
+
}
|
|
143
|
+
|
|
121
144
|
// Generate random nonce for the header
|
|
122
145
|
const randomNonce = nonce();
|
|
123
146
|
|
package/auth/utils/ws.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { getCorrelationData } from "./http.js";
|
|
2
|
+
import { generateJwt } from "./jwt.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for generating WebSocket authentication headers.
|
|
6
|
+
*/
|
|
7
|
+
export interface GetWebSocketAuthHeadersOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The API key ID
|
|
10
|
+
*
|
|
11
|
+
* Examples:
|
|
12
|
+
* 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
13
|
+
* 'organizations/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/apiKeys/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
14
|
+
*/
|
|
15
|
+
apiKeyId: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The API key secret
|
|
19
|
+
*
|
|
20
|
+
* Examples:
|
|
21
|
+
* 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==' (Edwards key (Ed25519))
|
|
22
|
+
* '-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n' (EC key (ES256))
|
|
23
|
+
*/
|
|
24
|
+
apiKeySecret: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The source identifier for the request
|
|
28
|
+
*/
|
|
29
|
+
source?: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The version of the source making the request
|
|
33
|
+
*/
|
|
34
|
+
sourceVersion?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Optional expiration time in seconds (defaults to 120)
|
|
38
|
+
*/
|
|
39
|
+
expiresIn?: number;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Optional audience claim for the JWT
|
|
43
|
+
*/
|
|
44
|
+
audience?: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Gets authentication headers for a WebSocket connection.
|
|
49
|
+
*
|
|
50
|
+
* @param options - The configuration options for generating WebSocket auth headers
|
|
51
|
+
* @returns Object containing the authentication headers
|
|
52
|
+
*/
|
|
53
|
+
export async function getWebSocketAuthHeaders(
|
|
54
|
+
options: GetWebSocketAuthHeadersOptions,
|
|
55
|
+
): Promise<Record<string, string>> {
|
|
56
|
+
const headers: Record<string, string> = {};
|
|
57
|
+
|
|
58
|
+
// Generate and add JWT token without request parameters for WebSocket
|
|
59
|
+
const jwt = await generateJwt({
|
|
60
|
+
apiKeyId: options.apiKeyId,
|
|
61
|
+
apiKeySecret: options.apiKeySecret,
|
|
62
|
+
// All request parameters are null for WebSocket
|
|
63
|
+
requestMethod: null,
|
|
64
|
+
requestHost: null,
|
|
65
|
+
requestPath: null,
|
|
66
|
+
expiresIn: options.expiresIn,
|
|
67
|
+
audience: options.audience,
|
|
68
|
+
});
|
|
69
|
+
headers["Authorization"] = `Bearer ${jwt}`;
|
|
70
|
+
headers["Content-Type"] = "application/json";
|
|
71
|
+
|
|
72
|
+
// Add correlation data
|
|
73
|
+
headers["Correlation-Context"] = getCorrelationData(options.source, options.sourceVersion);
|
|
74
|
+
|
|
75
|
+
return headers;
|
|
76
|
+
}
|