@coinbase/cdp-sdk 1.4.0 → 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 +8 -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/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/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/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/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,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/client/evm/evm.ts
CHANGED
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
ListTokenBalancesResult,
|
|
28
28
|
SendTransactionOptions,
|
|
29
29
|
TransactionResult,
|
|
30
|
+
GetOrCreateServerAccountOptions,
|
|
30
31
|
} from "./evm.types.js";
|
|
31
32
|
import { toEvmServerAccount } from "../../accounts/evm/toEvmServerAccount.js";
|
|
32
33
|
import { toEvmSmartAccount } from "../../accounts/evm/toEvmSmartAccount.js";
|
|
@@ -38,6 +39,7 @@ import {
|
|
|
38
39
|
waitForUserOperation,
|
|
39
40
|
WaitForUserOperationReturnType,
|
|
40
41
|
} from "../../actions/evm/waitForUserOperation.js";
|
|
42
|
+
import { APIError } from "../../openapi-client/errors.js";
|
|
41
43
|
import { CdpOpenApiClient } from "../../openapi-client/index.js";
|
|
42
44
|
import { Hex } from "../../types/misc.js";
|
|
43
45
|
|
|
@@ -145,7 +147,7 @@ export class EvmClient implements EvmClientInterface {
|
|
|
145
147
|
options.idempotencyKey,
|
|
146
148
|
);
|
|
147
149
|
|
|
148
|
-
return toEvmSmartAccount({
|
|
150
|
+
return toEvmSmartAccount(CdpOpenApiClient, {
|
|
149
151
|
smartAccount,
|
|
150
152
|
owner: options.owner,
|
|
151
153
|
});
|
|
@@ -216,12 +218,53 @@ export class EvmClient implements EvmClientInterface {
|
|
|
216
218
|
async getSmartAccount(options: GetSmartAccountOptions): Promise<SmartAccount> {
|
|
217
219
|
const smartAccount = await CdpOpenApiClient.getEvmSmartAccount(options.address);
|
|
218
220
|
|
|
219
|
-
return toEvmSmartAccount({
|
|
221
|
+
return toEvmSmartAccount(CdpOpenApiClient, {
|
|
220
222
|
smartAccount,
|
|
221
223
|
owner: options.owner,
|
|
222
224
|
});
|
|
223
225
|
}
|
|
224
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Gets a CDP EVM account, or creates one if it doesn't exist.
|
|
229
|
+
*
|
|
230
|
+
* @param {GetOrCreateServerAccountOptions} options - Parameters for getting or creating the account.
|
|
231
|
+
* @param {string} [options.name] - The name of the account to get or create.
|
|
232
|
+
*
|
|
233
|
+
* @returns A promise that resolves to the account.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* const account = await cdp.evm.getOrCreateAccount({
|
|
238
|
+
* name: "MyAccount",
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
async getOrCreateAccount(options: GetOrCreateServerAccountOptions): Promise<ServerAccount> {
|
|
243
|
+
try {
|
|
244
|
+
const account = await this.getAccount(options);
|
|
245
|
+
return account;
|
|
246
|
+
} catch (error) {
|
|
247
|
+
// If it failed because the account doesn't exist, create it
|
|
248
|
+
const doesAccountNotExist = error instanceof APIError && error.statusCode === 404;
|
|
249
|
+
if (doesAccountNotExist) {
|
|
250
|
+
try {
|
|
251
|
+
const account = await this.createAccount(options);
|
|
252
|
+
return account;
|
|
253
|
+
} catch (error) {
|
|
254
|
+
// If it failed because the account already exists, throw an error
|
|
255
|
+
const doesAccountAlreadyExist = error instanceof APIError && error.statusCode === 409;
|
|
256
|
+
if (doesAccountAlreadyExist) {
|
|
257
|
+
const account = await this.getAccount(options);
|
|
258
|
+
return account;
|
|
259
|
+
}
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
225
268
|
/**
|
|
226
269
|
* Gets a user operation for a smart account by user operation hash.
|
|
227
270
|
*
|
package/client/evm/evm.types.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
EvmSmartAccount as SmartAccount,
|
|
5
5
|
} from "../../accounts/types.js";
|
|
6
6
|
import { SendUserOperationReturnType } from "../../actions/evm/sendUserOperation.js";
|
|
7
|
+
import { Actions } from "../../actions/evm/types.js";
|
|
7
8
|
import {
|
|
8
9
|
EvmUserOperationNetwork,
|
|
9
10
|
EvmUserOperationStatus,
|
|
@@ -41,6 +42,7 @@ export type EvmClientInterface = Omit<
|
|
|
41
42
|
createSmartAccount: (options: CreateSmartAccountOptions) => Promise<SmartAccount>;
|
|
42
43
|
getAccount: (options: GetServerAccountOptions) => Promise<ServerAccount>;
|
|
43
44
|
getSmartAccount: (options: GetSmartAccountOptions) => Promise<SmartAccount>;
|
|
45
|
+
getOrCreateAccount: (options: GetOrCreateServerAccountOptions) => Promise<ServerAccount>;
|
|
44
46
|
getUserOperation: (options: GetUserOperationOptions) => Promise<UserOperation>;
|
|
45
47
|
listAccounts: (options: ListServerAccountsOptions) => Promise<ListServerAccountResult>;
|
|
46
48
|
listSmartAccounts: (options: ListSmartAccountsOptions) => Promise<ListSmartAccountResult>;
|
|
@@ -146,6 +148,14 @@ export interface GetSmartAccountOptions {
|
|
|
146
148
|
owner: Account;
|
|
147
149
|
}
|
|
148
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Options for getting an EVM account, or creating one if it doesn't exist.
|
|
153
|
+
*/
|
|
154
|
+
export interface GetOrCreateServerAccountOptions {
|
|
155
|
+
/** The name of the account. */
|
|
156
|
+
name: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
149
159
|
/**
|
|
150
160
|
* Options for listing EVM accounts.
|
|
151
161
|
*/
|
|
@@ -159,7 +169,7 @@ export interface ListServerAccountsOptions {
|
|
|
159
169
|
/**
|
|
160
170
|
* A smart account that only contains the owner address.
|
|
161
171
|
*/
|
|
162
|
-
export interface ReadonlySmartAccount extends Omit<SmartAccount, "owners"> {
|
|
172
|
+
export interface ReadonlySmartAccount extends Omit<SmartAccount, "owners" | keyof Actions> {
|
|
163
173
|
/** The owners of the smart account. */
|
|
164
174
|
owners: Address[];
|
|
165
175
|
}
|
package/client/solana/solana.ts
CHANGED
|
@@ -9,7 +9,9 @@ import {
|
|
|
9
9
|
SignatureResult,
|
|
10
10
|
SignMessageOptions,
|
|
11
11
|
SignTransactionOptions,
|
|
12
|
+
GetOrCreateAccountOptions,
|
|
12
13
|
} from "./solana.types.js";
|
|
14
|
+
import { APIError } from "../../openapi-client/errors.js";
|
|
13
15
|
import { CdpOpenApiClient } from "../../openapi-client/index.js";
|
|
14
16
|
|
|
15
17
|
/**
|
|
@@ -87,6 +89,47 @@ export class SolanaClient implements SolanaClientInterface {
|
|
|
87
89
|
throw new Error("Either address or name must be provided");
|
|
88
90
|
}
|
|
89
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Gets a Solana account by its address.
|
|
94
|
+
*
|
|
95
|
+
* @param {GetOrCreateAccountOptions} options - Parameters for getting or creating the Solana account.
|
|
96
|
+
* @param {string} options.name - The name of the account.
|
|
97
|
+
*
|
|
98
|
+
* @returns A promise that resolves to the account.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const account = await cdp.solana.getOrCreateAccount({
|
|
103
|
+
* name: "MyAccount",
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
async getOrCreateAccount(options: GetOrCreateAccountOptions): Promise<Account> {
|
|
108
|
+
try {
|
|
109
|
+
const account = await this.getAccount(options);
|
|
110
|
+
return account;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
// If it failed because the account doesn't exist, create it
|
|
113
|
+
const doesAccountNotExist = error instanceof APIError && error.statusCode === 404;
|
|
114
|
+
if (doesAccountNotExist) {
|
|
115
|
+
try {
|
|
116
|
+
const account = await this.createAccount(options);
|
|
117
|
+
return account;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
// If it failed because the account already exists, throw an error
|
|
120
|
+
const doesAccountAlreadyExist = error instanceof APIError && error.statusCode === 409;
|
|
121
|
+
if (doesAccountAlreadyExist) {
|
|
122
|
+
const account = await this.getAccount(options);
|
|
123
|
+
return account;
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
90
133
|
/**
|
|
91
134
|
* Lists all Solana accounts.
|
|
92
135
|
*
|
|
@@ -17,6 +17,7 @@ export type SolanaClientInterface = Omit<
|
|
|
17
17
|
> & {
|
|
18
18
|
createAccount: (options: CreateAccountOptions) => Promise<Account>;
|
|
19
19
|
getAccount: (options: GetAccountOptions) => Promise<Account>;
|
|
20
|
+
getOrCreateAccount: (options: GetOrCreateAccountOptions) => Promise<Account>;
|
|
20
21
|
listAccounts: (options: ListAccountsOptions) => Promise<ListAccountsResult>;
|
|
21
22
|
requestFaucet: (options: RequestFaucetOptions) => Promise<SignatureResult>;
|
|
22
23
|
signMessage: (options: SignMessageOptions) => Promise<SignatureResult>;
|
|
@@ -47,6 +48,14 @@ export interface GetAccountOptions {
|
|
|
47
48
|
name?: string;
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Options for getting a Solana account.
|
|
53
|
+
*/
|
|
54
|
+
export interface GetOrCreateAccountOptions {
|
|
55
|
+
/** The name of the account. */
|
|
56
|
+
name: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
50
59
|
/**
|
|
51
60
|
* Options for listing Solana accounts.
|
|
52
61
|
*/
|
package/package.json
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { serializeTransaction } from "viem";
|
|
2
|
+
|
|
3
|
+
import { TransactionRequestEIP1559 } from "../types/misc.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Serializes a transaction for the Coinbase API.
|
|
7
|
+
*
|
|
8
|
+
* @param transaction - The transaction to serialize.
|
|
9
|
+
* @returns The serialized transaction.
|
|
10
|
+
*/
|
|
11
|
+
export function serializeEIP1559Transaction(transaction: TransactionRequestEIP1559) {
|
|
12
|
+
return serializeTransaction({
|
|
13
|
+
...transaction,
|
|
14
|
+
chainId: 1, // ignored by Coinbase API
|
|
15
|
+
type: "eip1559",
|
|
16
|
+
});
|
|
17
|
+
}
|
package/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "1.
|
|
1
|
+
export const version = "1.5.0";
|