@coinbase/cdp-sdk 1.5.0 → 1.7.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 +25 -0
- package/README.md +101 -3
- package/_cjs/accounts/evm/toEvmServerAccount.js +21 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_cjs/accounts/evm/toEvmSmartAccount.js +35 -0
- package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
- package/_cjs/accounts/{types.js.map → evm/types.js.map} +1 -1
- package/_cjs/accounts/solana/toSolanaAccount.js +41 -0
- package/_cjs/accounts/solana/toSolanaAccount.js.map +1 -0
- package/_cjs/accounts/solana/types.js +3 -0
- package/_cjs/accounts/solana/types.js.map +1 -0
- package/_cjs/actions/evm/getUserOperation.js +36 -0
- package/_cjs/actions/evm/getUserOperation.js.map +1 -0
- package/_cjs/actions/evm/listTokenBalances.js +33 -0
- package/_cjs/actions/evm/listTokenBalances.js.map +1 -0
- package/_cjs/actions/evm/requestFaucet.js +18 -0
- package/_cjs/actions/evm/requestFaucet.js.map +1 -0
- package/_cjs/actions/evm/sendTransaction.js +29 -0
- package/_cjs/actions/evm/sendTransaction.js.map +1 -0
- package/_cjs/actions/evm/sendUserOperation.js +5 -6
- package/_cjs/actions/evm/sendUserOperation.js.map +1 -1
- package/_cjs/actions/evm/transfer/accountTransferStrategy.js +11 -7
- package/_cjs/actions/evm/transfer/accountTransferStrategy.js.map +1 -1
- package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js +13 -8
- package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -1
- package/_cjs/actions/evm/transfer/transfer.js +7 -3
- package/_cjs/actions/evm/transfer/transfer.js.map +1 -1
- package/_cjs/actions/solana/requestFaucet.js +29 -0
- package/_cjs/actions/solana/requestFaucet.js.map +1 -0
- package/_cjs/actions/solana/signMessage.js +29 -0
- package/_cjs/actions/solana/signMessage.js.map +1 -0
- package/_cjs/actions/solana/signTransaction.js +46 -0
- package/_cjs/actions/solana/signTransaction.js.map +1 -0
- package/_cjs/actions/solana/types.js +3 -0
- package/_cjs/actions/solana/types.js.map +1 -0
- package/_cjs/client/evm/evm.js +10 -52
- package/_cjs/client/evm/evm.js.map +1 -1
- package/_cjs/client/solana/solana.js +22 -17
- package/_cjs/client/solana/solana.js.map +1 -1
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +59 -1
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
- package/_cjs/openapi-client/generated/index.msw.js +3 -2
- package/_cjs/openapi-client/generated/index.msw.js.map +1 -1
- package/_cjs/openapi-client/generated/policy-engine/policy-engine.js +56 -0
- package/_cjs/openapi-client/generated/policy-engine/policy-engine.js.map +1 -0
- package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js +284 -0
- package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -0
- package/_cjs/openapi-client/index.js +1 -0
- package/_cjs/openapi-client/index.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/accounts/evm/toEvmServerAccount.js +21 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_esm/accounts/evm/toEvmSmartAccount.js +35 -0
- package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
- package/_esm/accounts/{types.js.map → evm/types.js.map} +1 -1
- package/_esm/accounts/solana/toSolanaAccount.js +38 -0
- package/_esm/accounts/solana/toSolanaAccount.js.map +1 -0
- package/_esm/accounts/solana/types.js +2 -0
- package/_esm/accounts/solana/types.js.map +1 -0
- package/_esm/actions/evm/getUserOperation.js +33 -0
- package/_esm/actions/evm/getUserOperation.js.map +1 -0
- package/_esm/actions/evm/listTokenBalances.js +30 -0
- package/_esm/actions/evm/listTokenBalances.js.map +1 -0
- package/_esm/actions/evm/requestFaucet.js +15 -0
- package/_esm/actions/evm/requestFaucet.js.map +1 -0
- package/_esm/actions/evm/sendTransaction.js +26 -0
- package/_esm/actions/evm/sendTransaction.js.map +1 -0
- package/_esm/actions/evm/sendUserOperation.js +5 -6
- package/_esm/actions/evm/sendUserOperation.js.map +1 -1
- package/_esm/actions/evm/transfer/accountTransferStrategy.js +11 -7
- package/_esm/actions/evm/transfer/accountTransferStrategy.js.map +1 -1
- package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js +13 -8
- package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -1
- package/_esm/actions/evm/transfer/transfer.js +7 -3
- package/_esm/actions/evm/transfer/transfer.js.map +1 -1
- package/_esm/actions/solana/requestFaucet.js +26 -0
- package/_esm/actions/solana/requestFaucet.js.map +1 -0
- package/_esm/actions/solana/signMessage.js +26 -0
- package/_esm/actions/solana/signMessage.js.map +1 -0
- package/_esm/actions/solana/signTransaction.js +43 -0
- package/_esm/actions/solana/signTransaction.js.map +1 -0
- package/_esm/actions/solana/types.js +2 -0
- package/_esm/actions/solana/types.js.map +1 -0
- package/_esm/client/evm/evm.js +10 -52
- package/_esm/client/evm/evm.js.map +1 -1
- package/_esm/client/solana/solana.js +22 -17
- package/_esm/client/solana/solana.js.map +1 -1
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +58 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
- package/_esm/openapi-client/generated/index.msw.js +3 -2
- package/_esm/openapi-client/generated/index.msw.js.map +1 -1
- package/_esm/openapi-client/generated/policy-engine/policy-engine.js +48 -0
- package/_esm/openapi-client/generated/policy-engine/policy-engine.js.map +1 -0
- package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js +259 -0
- package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -0
- package/_esm/openapi-client/index.js +1 -0
- package/_esm/openapi-client/index.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/accounts/evm/toEvmServerAccount.d.ts +1 -1
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
- package/_types/accounts/evm/toEvmSmartAccount.d.ts +1 -1
- package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -1
- package/_types/accounts/{types.d.ts → evm/types.d.ts} +5 -5
- package/_types/accounts/evm/types.d.ts.map +1 -0
- package/_types/accounts/solana/toSolanaAccount.d.ts +20 -0
- package/_types/accounts/solana/toSolanaAccount.d.ts.map +1 -0
- package/_types/accounts/solana/types.d.ts +12 -0
- package/_types/accounts/solana/types.d.ts.map +1 -0
- package/_types/actions/evm/getUserOperation.d.ts +22 -0
- package/_types/actions/evm/getUserOperation.d.ts.map +1 -0
- package/_types/actions/evm/listTokenBalances.d.ts +76 -0
- package/_types/actions/evm/listTokenBalances.d.ts.map +1 -0
- package/_types/actions/evm/requestFaucet.d.ts +34 -0
- package/_types/actions/evm/requestFaucet.d.ts.map +1 -0
- package/_types/actions/evm/sendTransaction.d.ts +42 -0
- package/_types/actions/evm/sendTransaction.d.ts.map +1 -0
- package/_types/actions/evm/sendUserOperation.d.ts +4 -3
- package/_types/actions/evm/sendUserOperation.d.ts.map +1 -1
- package/_types/actions/evm/transfer/accountTransferStrategy.d.ts +1 -1
- package/_types/actions/evm/transfer/accountTransferStrategy.d.ts.map +1 -1
- package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts +1 -1
- package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts.map +1 -1
- package/_types/actions/evm/transfer/transfer.d.ts +3 -3
- package/_types/actions/evm/transfer/transfer.d.ts.map +1 -1
- package/_types/actions/evm/transfer/types.d.ts +41 -7
- package/_types/actions/evm/transfer/types.d.ts.map +1 -1
- package/_types/actions/evm/types.d.ts +168 -3
- package/_types/actions/evm/types.d.ts.map +1 -1
- package/_types/actions/solana/requestFaucet.d.ts +23 -0
- package/_types/actions/solana/requestFaucet.d.ts.map +1 -0
- package/_types/actions/solana/signMessage.d.ts +23 -0
- package/_types/actions/solana/signMessage.d.ts.map +1 -0
- package/_types/actions/solana/signTransaction.d.ts +38 -0
- package/_types/actions/solana/signTransaction.d.ts.map +1 -0
- package/_types/actions/solana/types.d.ts +80 -0
- package/_types/actions/solana/types.d.ts.map +1 -0
- package/_types/client/evm/evm.d.ts +6 -3
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +12 -138
- package/_types/client/evm/evm.types.d.ts.map +1 -1
- package/_types/client/solana/solana.d.ts +5 -4
- package/_types/client/solana/solana.d.ts.map +1 -1
- package/_types/client/solana/solana.types.d.ts +8 -11
- package/_types/client/solana/solana.types.d.ts.map +1 -1
- package/_types/index.d.ts +1 -1
- package/_types/index.d.ts.map +1 -1
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +218 -0
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
- package/_types/openapi-client/generated/index.msw.d.ts +3 -2
- package/_types/openapi-client/generated/index.msw.d.ts.map +1 -1
- package/_types/openapi-client/generated/policy-engine/policy-engine.d.ts +43 -0
- package/_types/openapi-client/generated/policy-engine/policy-engine.d.ts.map +1 -0
- package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts +25 -0
- package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts.map +1 -0
- package/_types/openapi-client/index.d.ts +1 -0
- package/_types/openapi-client/index.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/accounts/evm/toEvmServerAccount.ts +35 -1
- package/accounts/evm/toEvmSmartAccount.ts +63 -1
- package/accounts/{types.ts → evm/types.ts} +5 -5
- package/accounts/solana/toSolanaAccount.ts +59 -0
- package/accounts/solana/types.ts +13 -0
- package/actions/evm/getUserOperation.ts +40 -0
- package/actions/evm/listTokenBalances.ts +107 -0
- package/actions/evm/requestFaucet.ts +46 -0
- package/actions/evm/sendTransaction.ts +73 -0
- package/actions/evm/sendUserOperation.ts +7 -7
- package/actions/evm/transfer/accountTransferStrategy.ts +12 -8
- package/actions/evm/transfer/smartAccountTransferStrategy.ts +14 -9
- package/actions/evm/transfer/transfer.ts +17 -6
- package/actions/evm/transfer/types.ts +55 -12
- package/actions/evm/types.ts +191 -3
- package/actions/solana/requestFaucet.ts +35 -0
- package/actions/solana/signMessage.ts +36 -0
- package/actions/solana/signTransaction.ts +53 -0
- package/actions/solana/types.ts +87 -0
- package/client/evm/evm.ts +28 -77
- package/client/evm/evm.types.ts +25 -151
- package/client/solana/solana.ts +32 -37
- package/client/solana/solana.types.ts +10 -15
- package/index.ts +1 -1
- package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +274 -0
- package/openapi-client/generated/index.msw.ts +3 -2
- package/openapi-client/generated/policy-engine/policy-engine.msw.ts +404 -0
- package/openapi-client/generated/policy-engine/policy-engine.ts +95 -0
- package/openapi-client/index.ts +1 -0
- package/package.json +1 -1
- package/version.ts +1 -1
- package/_types/accounts/types.d.ts.map +0 -1
- /package/_cjs/accounts/{types.js → evm/types.js} +0 -0
- /package/_esm/accounts/{types.js → evm/types.js} +0 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AccountActions } from "../../actions/solana/types.js";
|
|
2
|
+
import { SolanaAccount as OpenAPISolanaAccount } from "../../openapi-client/index.js";
|
|
3
|
+
import { Prettify } from "../../types/utils.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A base Solana account.
|
|
7
|
+
*/
|
|
8
|
+
export type Account = OpenAPISolanaAccount;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A Solana account with actions.
|
|
12
|
+
*/
|
|
13
|
+
export type SolanaAccount = Prettify<OpenAPISolanaAccount & AccountActions>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { GetUserOperationOptions, UserOperation } from "../../client/evm/evm.types.js";
|
|
2
|
+
import { CdpOpenApiClientType } from "../../openapi-client/index.js";
|
|
3
|
+
import { Address, Hex } from "../../types/misc.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Gets a user operation for a smart account by user operation hash.
|
|
7
|
+
*
|
|
8
|
+
* @param {CdpOpenApiClientType} client - The client to use to get the user operation.
|
|
9
|
+
* @param {GetUserOperationOptions} options - Parameters for getting the user operation.
|
|
10
|
+
* @param {SmartAccount} options.smartAccount - The smart account signing the user operation.
|
|
11
|
+
* @param {string} options.userOpHash - The user operation hash.
|
|
12
|
+
*
|
|
13
|
+
* @returns A promise that resolves to the user operation.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const userOp = await getUserOperation(client, {
|
|
18
|
+
* smartAccount,
|
|
19
|
+
* userOpHash: "0x1234567890123456789012345678901234567890123456789012345678901234",
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export async function getUserOperation(
|
|
24
|
+
client: CdpOpenApiClientType,
|
|
25
|
+
options: GetUserOperationOptions,
|
|
26
|
+
): Promise<UserOperation> {
|
|
27
|
+
const userOp = await client.getUserOperation(options.smartAccount.address, options.userOpHash);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
calls: userOp.calls.map(call => ({
|
|
31
|
+
to: call.to as Address,
|
|
32
|
+
value: BigInt(call.value),
|
|
33
|
+
data: call.data as Hex,
|
|
34
|
+
})),
|
|
35
|
+
network: userOp.network,
|
|
36
|
+
status: userOp.status,
|
|
37
|
+
transactionHash: userOp.transactionHash as Hex | undefined,
|
|
38
|
+
userOpHash: userOp.userOpHash as Hex,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Address } from "viem";
|
|
2
|
+
|
|
3
|
+
import { CdpOpenApiClientType, ListEvmTokenBalancesNetwork } from "../../openapi-client/index.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A token on an EVM network, which is either an ERC-20 or a native token (i.e. ETH).
|
|
7
|
+
*/
|
|
8
|
+
export interface EvmToken {
|
|
9
|
+
/**
|
|
10
|
+
* The contract address of the token. For Ether, the contract address is 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
|
|
11
|
+
* per EIP-7528. For ERC-20 tokens, this is the contract address where the token is deployed.
|
|
12
|
+
*/
|
|
13
|
+
contractAddress: Address;
|
|
14
|
+
/** The network the token is on. */
|
|
15
|
+
network: ListEvmTokenBalancesNetwork;
|
|
16
|
+
/**
|
|
17
|
+
* The symbol of the token, which is optional and non-unique. Note: This field
|
|
18
|
+
* may not be present for most tokens while the API is still under development.
|
|
19
|
+
*/
|
|
20
|
+
symbol?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The name of the token, which is optional and non-unique. Note: This field
|
|
23
|
+
* may not be present for most tokens while the API is still under development.
|
|
24
|
+
*/
|
|
25
|
+
name?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A token amount on an EVM network.
|
|
30
|
+
*/
|
|
31
|
+
export interface EvmTokenAmount {
|
|
32
|
+
/** The amount of the token in the smallest indivisible unit of the token. */
|
|
33
|
+
amount: bigint;
|
|
34
|
+
/** The number of decimals in the token. */
|
|
35
|
+
decimals: bigint;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An EVM token balance.
|
|
39
|
+
*/
|
|
40
|
+
export interface EvmTokenBalance {
|
|
41
|
+
/** The token. */
|
|
42
|
+
token: EvmToken;
|
|
43
|
+
/** The amount of the token. */
|
|
44
|
+
amount: EvmTokenAmount;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Options for listing EVM token balances.
|
|
49
|
+
*/
|
|
50
|
+
export interface ListTokenBalancesOptions {
|
|
51
|
+
/** The address of the account. */
|
|
52
|
+
address: Address;
|
|
53
|
+
/** The network. */
|
|
54
|
+
network: ListEvmTokenBalancesNetwork;
|
|
55
|
+
/** The page size to paginate through the token balances. */
|
|
56
|
+
pageSize?: number;
|
|
57
|
+
/** The page token to paginate through the token balances. */
|
|
58
|
+
pageToken?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The result of listing EVM token balances.
|
|
63
|
+
*/
|
|
64
|
+
export interface ListTokenBalancesResult {
|
|
65
|
+
/** The token balances. */
|
|
66
|
+
balances: EvmTokenBalance[];
|
|
67
|
+
/**
|
|
68
|
+
* The next page token to paginate through the token balances.
|
|
69
|
+
* If undefined, there are no more token balances to paginate through.
|
|
70
|
+
*/
|
|
71
|
+
nextPageToken?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* List the token balances for an EVM account.
|
|
76
|
+
*
|
|
77
|
+
* @param client - The client to use to list the token balances.
|
|
78
|
+
* @param options - The options for listing the token balances.
|
|
79
|
+
* @returns The result of listing the token balances.
|
|
80
|
+
*/
|
|
81
|
+
export async function listTokenBalances(
|
|
82
|
+
client: CdpOpenApiClientType,
|
|
83
|
+
options: ListTokenBalancesOptions,
|
|
84
|
+
): Promise<ListTokenBalancesResult> {
|
|
85
|
+
const response = await client.listEvmTokenBalances(options.network, options.address, {
|
|
86
|
+
pageSize: options.pageSize,
|
|
87
|
+
pageToken: options.pageToken,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const balances = response.balances.map(balance => {
|
|
91
|
+
return {
|
|
92
|
+
token: {
|
|
93
|
+
network: balance.token.network,
|
|
94
|
+
contractAddress: balance.token.contractAddress as Address,
|
|
95
|
+
},
|
|
96
|
+
amount: {
|
|
97
|
+
amount: BigInt(balance.amount.amount),
|
|
98
|
+
decimals: BigInt(balance.amount.decimals),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
balances,
|
|
105
|
+
nextPageToken: response.nextPageToken,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type CdpOpenApiClientType } from "../../openapi-client/index.js";
|
|
2
|
+
import { Hex } from "../../types/misc.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for requesting funds from an EVM faucet.
|
|
6
|
+
*/
|
|
7
|
+
export interface RequestFaucetOptions {
|
|
8
|
+
/** The address of the account. */
|
|
9
|
+
address: string;
|
|
10
|
+
/** The network to request funds from. */
|
|
11
|
+
network: "base-sepolia" | "ethereum-sepolia";
|
|
12
|
+
/** The token to request funds for. */
|
|
13
|
+
token: "eth" | "usdc" | "eurc" | "cbbtc";
|
|
14
|
+
/** The idempotency key. */
|
|
15
|
+
idempotencyKey?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The result of requesting funds from an EVM faucet.
|
|
20
|
+
*/
|
|
21
|
+
export interface RequestFaucetResult {
|
|
22
|
+
/** The transaction hash. */
|
|
23
|
+
transactionHash: Hex;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Requests funds from an EVM faucet.
|
|
28
|
+
*
|
|
29
|
+
* @param apiClient - The API client.
|
|
30
|
+
* @param options - The options for requesting funds from the EVM faucet.
|
|
31
|
+
*
|
|
32
|
+
* @returns A promise that resolves to the transaction hash.
|
|
33
|
+
*/
|
|
34
|
+
export async function requestFaucet(
|
|
35
|
+
apiClient: CdpOpenApiClientType,
|
|
36
|
+
options: RequestFaucetOptions,
|
|
37
|
+
) {
|
|
38
|
+
const { transactionHash } = await apiClient.requestEvmFaucet(
|
|
39
|
+
{ address: options.address, network: options.network, token: options.token },
|
|
40
|
+
options.idempotencyKey,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
transactionHash: transactionHash as Hex,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { serializeTransaction, TransactionRequestEIP1559 } from "viem";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
CdpOpenApiClientType,
|
|
5
|
+
SendEvmTransactionBodyNetwork,
|
|
6
|
+
} from "../../openapi-client/index.js";
|
|
7
|
+
import type { Address, Hex } from "../../types/misc.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options for sending an EVM transaction.
|
|
11
|
+
*/
|
|
12
|
+
export interface SendTransactionOptions {
|
|
13
|
+
/** The address of the account. */
|
|
14
|
+
address: Address;
|
|
15
|
+
/**
|
|
16
|
+
* The transaction to send. The chainId is ignored in favor of the `network` field.
|
|
17
|
+
*
|
|
18
|
+
* This can be either:
|
|
19
|
+
* - An RLP-encoded transaction to sign and send, as a 0x-prefixed hex string, or
|
|
20
|
+
* - An EIP-1559 transaction request object.
|
|
21
|
+
*/
|
|
22
|
+
transaction: Hex | TransactionRequestEIP1559;
|
|
23
|
+
/**
|
|
24
|
+
* The network to send the transaction to.
|
|
25
|
+
* The chainId in the `transaction` field is ignored in favor of this field.
|
|
26
|
+
*/
|
|
27
|
+
network: SendEvmTransactionBodyNetwork;
|
|
28
|
+
/** The idempotency key. */
|
|
29
|
+
idempotencyKey?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Result of a transaction
|
|
34
|
+
*/
|
|
35
|
+
export interface TransactionResult {
|
|
36
|
+
/** The hash of the transaction. */
|
|
37
|
+
transactionHash: Hex;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Sends an EVM transaction.
|
|
42
|
+
*
|
|
43
|
+
* @param apiClient - The API client.
|
|
44
|
+
* @param options - The options for sending the transaction.
|
|
45
|
+
*
|
|
46
|
+
* @returns The result of the transaction.
|
|
47
|
+
*/
|
|
48
|
+
export async function sendTransaction(
|
|
49
|
+
apiClient: CdpOpenApiClientType,
|
|
50
|
+
options: SendTransactionOptions,
|
|
51
|
+
): Promise<TransactionResult> {
|
|
52
|
+
const { address, network, idempotencyKey } = options;
|
|
53
|
+
let transaction = options.transaction;
|
|
54
|
+
|
|
55
|
+
if (typeof transaction !== "string") {
|
|
56
|
+
transaction = serializeTransaction({
|
|
57
|
+
...transaction,
|
|
58
|
+
// chainId is ignored in favor of network
|
|
59
|
+
chainId: 1,
|
|
60
|
+
type: "eip1559",
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const result = await apiClient.sendEvmTransaction(
|
|
65
|
+
address,
|
|
66
|
+
{ transaction, network },
|
|
67
|
+
idempotencyKey,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
transactionHash: result.transactionHash as Hex,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
CdpOpenApiClientType,
|
|
7
7
|
} from "../../openapi-client/index.js";
|
|
8
8
|
|
|
9
|
-
import type { EvmSmartAccount } from "../../accounts/types.js";
|
|
9
|
+
import type { EvmSmartAccount } from "../../accounts/evm/types.js";
|
|
10
10
|
import type { Calls } from "../../types/calls.js";
|
|
11
11
|
import type { Address, Hex } from "../../types/misc.js";
|
|
12
12
|
|
|
@@ -16,6 +16,8 @@ import type { Address, Hex } from "../../types/misc.js";
|
|
|
16
16
|
* @template T - Array type for the calls parameter.
|
|
17
17
|
*/
|
|
18
18
|
export type SendUserOperationOptions<T extends readonly unknown[]> = {
|
|
19
|
+
/** The smart account. */
|
|
20
|
+
smartAccount: EvmSmartAccount;
|
|
19
21
|
/**
|
|
20
22
|
* Array of contract calls to execute in the user operation.
|
|
21
23
|
* Each call can either be:
|
|
@@ -97,13 +99,11 @@ export type SendUserOperationReturnType = {
|
|
|
97
99
|
* ```
|
|
98
100
|
*
|
|
99
101
|
* @param {CdpOpenApiClientType} client - The client to use to send the user operation.
|
|
100
|
-
* @param {EvmSmartAccount} smartAccount - The smart account to send the user operation from.
|
|
101
102
|
* @param {SendUserOperationOptions<T>} options - The options for the user operation.
|
|
102
103
|
* @returns {Promise<SendUserOperationReturnType>} The result of the user operation.
|
|
103
104
|
*/
|
|
104
105
|
export async function sendUserOperation<T extends readonly unknown[]>(
|
|
105
106
|
client: CdpOpenApiClientType,
|
|
106
|
-
smartAccount: EvmSmartAccount,
|
|
107
107
|
options: SendUserOperationOptions<T>,
|
|
108
108
|
): Promise<SendUserOperationReturnType> {
|
|
109
109
|
const { calls, network, paymasterUrl } = options;
|
|
@@ -134,20 +134,20 @@ export async function sendUserOperation<T extends readonly unknown[]>(
|
|
|
134
134
|
};
|
|
135
135
|
});
|
|
136
136
|
|
|
137
|
-
const createOpResponse = await client.prepareUserOperation(smartAccount.address, {
|
|
137
|
+
const createOpResponse = await client.prepareUserOperation(options.smartAccount.address, {
|
|
138
138
|
network,
|
|
139
139
|
calls: encodedCalls,
|
|
140
140
|
paymasterUrl,
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
-
const owner = smartAccount.owners[0];
|
|
143
|
+
const owner = options.smartAccount.owners[0];
|
|
144
144
|
|
|
145
145
|
const signature = await owner.sign({
|
|
146
146
|
hash: createOpResponse.userOpHash as Hex,
|
|
147
147
|
});
|
|
148
148
|
|
|
149
149
|
const broadcastResponse = await client.sendUserOperation(
|
|
150
|
-
smartAccount.address,
|
|
150
|
+
options.smartAccount.address,
|
|
151
151
|
createOpResponse.userOpHash as Hex,
|
|
152
152
|
{
|
|
153
153
|
signature,
|
|
@@ -156,7 +156,7 @@ export async function sendUserOperation<T extends readonly unknown[]>(
|
|
|
156
156
|
);
|
|
157
157
|
|
|
158
158
|
return {
|
|
159
|
-
smartAccountAddress: smartAccount.address,
|
|
159
|
+
smartAccountAddress: options.smartAccount.address,
|
|
160
160
|
status: broadcastResponse.status,
|
|
161
161
|
userOpHash: createOpResponse.userOpHash,
|
|
162
162
|
} as SendUserOperationReturnType;
|
|
@@ -8,23 +8,23 @@ import {
|
|
|
8
8
|
|
|
9
9
|
import { TransferExecutionStrategy } from "./types.js";
|
|
10
10
|
import { getErc20Address } from "./utils.js";
|
|
11
|
-
import { EvmAccount } from "../../../accounts/types.js";
|
|
11
|
+
import { EvmAccount } from "../../../accounts/evm/types.js";
|
|
12
12
|
import { serializeEIP1559Transaction } from "../../../utils/serializeTransaction.js";
|
|
13
13
|
|
|
14
14
|
export const accountTransferStrategy: TransferExecutionStrategy<EvmAccount> = {
|
|
15
|
-
executeTransfer: async ({ apiClient, from,
|
|
15
|
+
executeTransfer: async ({ apiClient, from, to, value, token, network }) => {
|
|
16
16
|
const transactionHash = await (async () => {
|
|
17
|
-
if (
|
|
17
|
+
if (token === "eth") {
|
|
18
18
|
const result = await apiClient.sendEvmTransaction(from.address, {
|
|
19
19
|
transaction: serializeEIP1559Transaction({
|
|
20
20
|
value,
|
|
21
21
|
to,
|
|
22
22
|
}),
|
|
23
|
-
network
|
|
23
|
+
network,
|
|
24
24
|
});
|
|
25
25
|
return result.transactionHash as Hex;
|
|
26
26
|
} else {
|
|
27
|
-
const erc20Address = getErc20Address(
|
|
27
|
+
const erc20Address = getErc20Address(token, network);
|
|
28
28
|
|
|
29
29
|
await apiClient.sendEvmTransaction(from.address, {
|
|
30
30
|
transaction: serializeEIP1559Transaction({
|
|
@@ -35,7 +35,7 @@ export const accountTransferStrategy: TransferExecutionStrategy<EvmAccount> = {
|
|
|
35
35
|
args: [to, value],
|
|
36
36
|
}),
|
|
37
37
|
}),
|
|
38
|
-
network
|
|
38
|
+
network,
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
const result = await apiClient.sendEvmTransaction(from.address, {
|
|
@@ -47,7 +47,7 @@ export const accountTransferStrategy: TransferExecutionStrategy<EvmAccount> = {
|
|
|
47
47
|
args: [to, value],
|
|
48
48
|
}),
|
|
49
49
|
}),
|
|
50
|
-
network
|
|
50
|
+
network,
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
return result.transactionHash as Hex;
|
|
@@ -57,11 +57,15 @@ export const accountTransferStrategy: TransferExecutionStrategy<EvmAccount> = {
|
|
|
57
57
|
return transactionHash;
|
|
58
58
|
},
|
|
59
59
|
|
|
60
|
-
waitForResult: async ({ publicClient, hash }) => {
|
|
60
|
+
waitForResult: async ({ publicClient, hash, waitOptions }) => {
|
|
61
61
|
let receipt: TransactionReceipt;
|
|
62
62
|
try {
|
|
63
63
|
receipt = await publicClient.waitForTransactionReceipt({
|
|
64
64
|
hash,
|
|
65
|
+
pollingInterval: waitOptions?.intervalSeconds
|
|
66
|
+
? waitOptions.intervalSeconds * 1000
|
|
67
|
+
: undefined,
|
|
68
|
+
timeout: waitOptions?.timeoutSeconds ? waitOptions.timeoutSeconds * 1000 : undefined,
|
|
65
69
|
});
|
|
66
70
|
} catch (error) {
|
|
67
71
|
if (error instanceof WaitForTransactionReceiptTimeoutError) {
|
|
@@ -2,16 +2,18 @@ import { encodeFunctionData, erc20Abi } from "viem";
|
|
|
2
2
|
|
|
3
3
|
import { TransferExecutionStrategy } from "./types.js";
|
|
4
4
|
import { getErc20Address } from "./utils.js";
|
|
5
|
-
import { EvmSmartAccount } from "../../../accounts/types.js";
|
|
5
|
+
import { EvmSmartAccount } from "../../../accounts/evm/types.js";
|
|
6
6
|
import { sendUserOperation } from "../sendUserOperation.js";
|
|
7
7
|
import { waitForUserOperation } from "../waitForUserOperation.js";
|
|
8
8
|
|
|
9
9
|
export const smartAccountTransferStrategy: TransferExecutionStrategy<EvmSmartAccount> = {
|
|
10
|
-
executeTransfer: async ({ apiClient, from,
|
|
10
|
+
executeTransfer: async ({ apiClient, from, to, value, token, network, paymasterUrl }) => {
|
|
11
11
|
const userOpHash = await (async () => {
|
|
12
|
-
if (
|
|
13
|
-
const result = await sendUserOperation(apiClient,
|
|
14
|
-
|
|
12
|
+
if (token === "eth") {
|
|
13
|
+
const result = await sendUserOperation(apiClient, {
|
|
14
|
+
smartAccount: from,
|
|
15
|
+
paymasterUrl,
|
|
16
|
+
network,
|
|
15
17
|
calls: [
|
|
16
18
|
{
|
|
17
19
|
to,
|
|
@@ -22,10 +24,12 @@ export const smartAccountTransferStrategy: TransferExecutionStrategy<EvmSmartAcc
|
|
|
22
24
|
});
|
|
23
25
|
return result.userOpHash;
|
|
24
26
|
} else {
|
|
25
|
-
const erc20Address = getErc20Address(
|
|
27
|
+
const erc20Address = getErc20Address(token, network);
|
|
26
28
|
|
|
27
|
-
const result = await sendUserOperation(apiClient,
|
|
28
|
-
|
|
29
|
+
const result = await sendUserOperation(apiClient, {
|
|
30
|
+
smartAccount: from,
|
|
31
|
+
paymasterUrl,
|
|
32
|
+
network,
|
|
29
33
|
calls: [
|
|
30
34
|
{
|
|
31
35
|
to: erc20Address,
|
|
@@ -53,10 +57,11 @@ export const smartAccountTransferStrategy: TransferExecutionStrategy<EvmSmartAcc
|
|
|
53
57
|
return userOpHash;
|
|
54
58
|
},
|
|
55
59
|
|
|
56
|
-
waitForResult: async ({ apiClient, publicClient, from, hash }) => {
|
|
60
|
+
waitForResult: async ({ apiClient, publicClient, from, hash, waitOptions }) => {
|
|
57
61
|
const result = await waitForUserOperation(apiClient, {
|
|
58
62
|
smartAccountAddress: from.address,
|
|
59
63
|
userOpHash: hash,
|
|
64
|
+
waitOptions,
|
|
60
65
|
});
|
|
61
66
|
|
|
62
67
|
if (result.status === "complete") {
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { createPublicClient, http, erc20Abi, parseUnits, Address, Chain, Transport } from "viem";
|
|
2
2
|
|
|
3
|
-
import { TransferResult, TransferOptions, TransferExecutionStrategy } from "./types.js";
|
|
4
3
|
import { mapNetworkToChain } from "./utils.js";
|
|
5
|
-
import { EvmAccount, EvmSmartAccount } from "../../../accounts/types.js";
|
|
4
|
+
import { EvmAccount, EvmSmartAccount } from "../../../accounts/evm/types.js";
|
|
6
5
|
import { CdpOpenApiClientType } from "../../../openapi-client/index.js";
|
|
7
6
|
|
|
7
|
+
import type {
|
|
8
|
+
TransferResult,
|
|
9
|
+
TransferExecutionStrategy,
|
|
10
|
+
SmartAccountTransferOptions,
|
|
11
|
+
AccountTransferOptions,
|
|
12
|
+
} from "./types.js";
|
|
13
|
+
|
|
8
14
|
/**
|
|
9
15
|
* Transfer an amount of a token from an account to another account.
|
|
10
16
|
*
|
|
@@ -17,7 +23,7 @@ import { CdpOpenApiClientType } from "../../../openapi-client/index.js";
|
|
|
17
23
|
export async function transfer<T extends EvmAccount | EvmSmartAccount>(
|
|
18
24
|
apiClient: CdpOpenApiClientType,
|
|
19
25
|
from: T,
|
|
20
|
-
transferArgs:
|
|
26
|
+
transferArgs: T extends EvmSmartAccount ? SmartAccountTransferOptions : AccountTransferOptions,
|
|
21
27
|
transferStrategy: TransferExecutionStrategy<T>,
|
|
22
28
|
): Promise<TransferResult> {
|
|
23
29
|
const publicClient = createPublicClient<Transport, Chain>({
|
|
@@ -52,19 +58,24 @@ export async function transfer<T extends EvmAccount | EvmSmartAccount>(
|
|
|
52
58
|
return parseUnits(transferArgs.amount, decimals);
|
|
53
59
|
})();
|
|
54
60
|
|
|
55
|
-
const
|
|
61
|
+
const transfer = {
|
|
56
62
|
apiClient,
|
|
57
63
|
from,
|
|
58
|
-
transferArgs,
|
|
59
64
|
to,
|
|
60
65
|
value,
|
|
61
|
-
|
|
66
|
+
token: transferArgs.token,
|
|
67
|
+
network: transferArgs.network,
|
|
68
|
+
paymasterUrl: "paymasterUrl" in transferArgs ? transferArgs.paymasterUrl : undefined,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const hash = await transferStrategy.executeTransfer(transfer);
|
|
62
72
|
|
|
63
73
|
const result = await transferStrategy.waitForResult({
|
|
64
74
|
apiClient,
|
|
65
75
|
publicClient,
|
|
66
76
|
from,
|
|
67
77
|
hash,
|
|
78
|
+
waitOptions: transferArgs.waitOptions,
|
|
68
79
|
});
|
|
69
80
|
|
|
70
81
|
return result;
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Hex,
|
|
3
|
+
TransactionReceipt,
|
|
4
|
+
Address,
|
|
5
|
+
PublicClient,
|
|
6
|
+
Chain,
|
|
7
|
+
Transport,
|
|
8
|
+
WaitForTransactionReceiptParameters,
|
|
9
|
+
} from "viem";
|
|
2
10
|
|
|
3
11
|
import {
|
|
4
12
|
CdpOpenApiClientType,
|
|
5
|
-
EvmAccount,
|
|
6
13
|
EvmUserOperationNetwork,
|
|
7
14
|
SendEvmTransactionBodyNetwork,
|
|
8
15
|
} from "../../../openapi-client/index.js";
|
|
9
16
|
|
|
17
|
+
import type { EvmAccount, EvmSmartAccount } from "../../../accounts/evm/types.js";
|
|
18
|
+
import type { WaitForUserOperationOptions } from "../waitForUserOperation.js";
|
|
19
|
+
|
|
10
20
|
/**
|
|
11
21
|
* The network to transfer the token on.
|
|
12
22
|
*/
|
|
@@ -15,7 +25,7 @@ export type Network = SendEvmTransactionBodyNetwork | EvmUserOperationNetwork;
|
|
|
15
25
|
/**
|
|
16
26
|
* The options for the transfer.
|
|
17
27
|
*/
|
|
18
|
-
|
|
28
|
+
type TransferOptions = {
|
|
19
29
|
/** The account to transfer the token to. */
|
|
20
30
|
to: EvmAccount | Address;
|
|
21
31
|
/**
|
|
@@ -29,6 +39,33 @@ export type TransferOptions = {
|
|
|
29
39
|
network: Network;
|
|
30
40
|
};
|
|
31
41
|
|
|
42
|
+
/**
|
|
43
|
+
* The options for the transfer using an account.
|
|
44
|
+
*/
|
|
45
|
+
export type AccountTransferOptions = TransferOptions & {
|
|
46
|
+
/** The options for waiting for the result of the transfer. */
|
|
47
|
+
waitOptions?: {
|
|
48
|
+
/**
|
|
49
|
+
* Polling frequency (in seconds). Defaults to 4 seconds.
|
|
50
|
+
*/
|
|
51
|
+
intervalSeconds?: WaitForTransactionReceiptParameters["pollingInterval"];
|
|
52
|
+
/**
|
|
53
|
+
* Optional timeout (in seconds) to wait before stopping polling. Defaults to 180 seconds.
|
|
54
|
+
*/
|
|
55
|
+
timeoutSeconds?: WaitForTransactionReceiptParameters["timeout"];
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The options for the transfer using a smart account.
|
|
61
|
+
*/
|
|
62
|
+
export type SmartAccountTransferOptions = TransferOptions & {
|
|
63
|
+
/** The paymaster URL to use for the transfer. */
|
|
64
|
+
paymasterUrl?: string;
|
|
65
|
+
/** The options for waiting for the result of the transfer. */
|
|
66
|
+
waitOptions?: WaitForUserOperationOptions["waitOptions"];
|
|
67
|
+
};
|
|
68
|
+
|
|
32
69
|
/**
|
|
33
70
|
* The result of the transfer.
|
|
34
71
|
*/
|
|
@@ -42,25 +79,29 @@ export type TransferResult = {
|
|
|
42
79
|
/**
|
|
43
80
|
* A strategy for executing a transfer.
|
|
44
81
|
*/
|
|
45
|
-
export interface TransferExecutionStrategy<T> {
|
|
82
|
+
export interface TransferExecutionStrategy<T extends EvmAccount | EvmSmartAccount> {
|
|
46
83
|
/**
|
|
47
84
|
* Executes the transfer.
|
|
48
85
|
*
|
|
49
86
|
* @param args - The arguments for the transfer.
|
|
50
87
|
* @param args.apiClient - The API client to use for the transfer.
|
|
51
88
|
* @param args.from - The account to transfer the token from.
|
|
52
|
-
* @param args.transferArgs - The arguments for the transfer.
|
|
53
89
|
* @param args.to - The account to transfer the token to.
|
|
54
90
|
* @param args.value - The value of the transfer.
|
|
91
|
+
* @param args.token - The token to transfer.
|
|
92
|
+
* @param args.network - The network to transfer the token on.
|
|
55
93
|
* @returns The transaction hash of the transfer.
|
|
56
94
|
*/
|
|
57
|
-
executeTransfer(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
95
|
+
executeTransfer(
|
|
96
|
+
args: {
|
|
97
|
+
apiClient: CdpOpenApiClientType;
|
|
98
|
+
from: T;
|
|
99
|
+
to: Address;
|
|
100
|
+
value: bigint;
|
|
101
|
+
token: TransferOptions["token"];
|
|
102
|
+
network: TransferOptions["network"];
|
|
103
|
+
} & (T extends EvmSmartAccount ? { paymasterUrl?: string } : object),
|
|
104
|
+
): Promise<Hex>;
|
|
64
105
|
|
|
65
106
|
/**
|
|
66
107
|
* Waits for the result of the transfer.
|
|
@@ -70,6 +111,7 @@ export interface TransferExecutionStrategy<T> {
|
|
|
70
111
|
* @param args.publicClient - The public client to use for the transfer.
|
|
71
112
|
* @param args.from - The account to transfer the token from.
|
|
72
113
|
* @param args.hash - The transaction hash of the transfer.
|
|
114
|
+
* @param args.waitOptions - The options for waiting for the result of the transfer.
|
|
73
115
|
* @returns The result of the transfer.
|
|
74
116
|
*/
|
|
75
117
|
waitForResult(args: {
|
|
@@ -77,5 +119,6 @@ export interface TransferExecutionStrategy<T> {
|
|
|
77
119
|
publicClient: PublicClient<Transport, Chain>;
|
|
78
120
|
from: T;
|
|
79
121
|
hash: Hex;
|
|
122
|
+
waitOptions?: WaitForUserOperationOptions["waitOptions"];
|
|
80
123
|
}): Promise<TransferResult>;
|
|
81
124
|
}
|