@coinbase/cdp-sdk 1.4.0 → 1.6.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 +20 -0
- package/README.md +123 -2
- package/_cjs/accounts/evm/toEvmServerAccount.js +26 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_cjs/accounts/evm/toEvmSmartAccount.js +35 -2
- package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
- 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 +72 -0
- package/_cjs/actions/evm/transfer/accountTransferStrategy.js.map +1 -0
- package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js +67 -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 +52 -42
- 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 +26 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_esm/accounts/evm/toEvmSmartAccount.js +35 -2
- package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
- 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 +69 -0
- package/_esm/actions/evm/transfer/accountTransferStrategy.js.map +1 -0
- package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js +64 -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 +52 -42
- 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/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 +3 -2
- package/_types/actions/evm/sendUserOperation.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 +230 -0
- package/_types/actions/evm/types.d.ts.map +1 -0
- package/_types/client/evm/evm.d.ts +22 -3
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +20 -137
- 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 +46 -6
- package/accounts/evm/toEvmSmartAccount.ts +68 -4
- package/accounts/types.ts +20 -16
- 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 +6 -6
- package/actions/evm/transfer/accountTransferStrategy.ts +84 -0
- package/actions/evm/transfer/smartAccountTransferStrategy.ts +72 -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 +244 -0
- package/client/evm/evm.ts +70 -63
- package/client/evm/evm.types.ts +32 -149
- 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
|
@@ -1,9 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type Hex, type TransactionSerializable, serializeTransaction } from "viem";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
listTokenBalances,
|
|
5
|
+
type ListTokenBalancesResult,
|
|
6
|
+
type ListTokenBalancesOptions,
|
|
7
|
+
} from "../../actions/evm/listTokenBalances.js";
|
|
8
|
+
import {
|
|
9
|
+
requestFaucet,
|
|
10
|
+
type RequestFaucetOptions,
|
|
11
|
+
type RequestFaucetResult,
|
|
12
|
+
} from "../../actions/evm/requestFaucet.js";
|
|
13
|
+
import { sendTransaction } from "../../actions/evm/sendTransaction.js";
|
|
14
|
+
import { accountTransferStrategy } from "../../actions/evm/transfer/accountTransferStrategy.js";
|
|
15
|
+
import { transfer } from "../../actions/evm/transfer/transfer.js";
|
|
5
16
|
|
|
17
|
+
import type { SendTransactionOptions } from "../../actions/evm/sendTransaction.js";
|
|
18
|
+
import type { TransferResult } from "../../actions/evm/transfer/types.js";
|
|
19
|
+
import type { CdpOpenApiClientType, EvmAccount } from "../../openapi-client/index.js";
|
|
6
20
|
import type { Address, Hash } from "../../types/misc.js";
|
|
21
|
+
import type { EvmServerAccount } from "../types.js";
|
|
7
22
|
|
|
8
23
|
/**
|
|
9
24
|
* Options for converting a pre-existing EvmAccount to a EvmServerAccount.
|
|
@@ -32,26 +47,51 @@ export function toEvmServerAccount(
|
|
|
32
47
|
const result = await apiClient.signEvmMessage(options.account.address, {
|
|
33
48
|
message: message.toString(),
|
|
34
49
|
});
|
|
35
|
-
return result.signature as
|
|
50
|
+
return result.signature as Hex;
|
|
36
51
|
},
|
|
37
52
|
|
|
38
53
|
async sign(parameters: { hash: Hash }) {
|
|
39
54
|
const result = await apiClient.signEvmHash(options.account.address, {
|
|
40
55
|
hash: parameters.hash,
|
|
41
56
|
});
|
|
42
|
-
return result.signature as
|
|
57
|
+
return result.signature as Hex;
|
|
43
58
|
},
|
|
44
59
|
|
|
45
60
|
async signTransaction(transaction: TransactionSerializable) {
|
|
46
61
|
const result = await apiClient.signEvmTransaction(options.account.address, {
|
|
47
62
|
transaction: serializeTransaction(transaction),
|
|
48
63
|
});
|
|
49
|
-
return result.signedTransaction as
|
|
64
|
+
return result.signedTransaction as Hex;
|
|
50
65
|
},
|
|
51
66
|
|
|
52
67
|
async signTypedData() {
|
|
53
68
|
throw new Error("Not implemented");
|
|
54
69
|
},
|
|
70
|
+
async transfer(transferArgs): Promise<TransferResult> {
|
|
71
|
+
return transfer(apiClient, account, transferArgs, accountTransferStrategy);
|
|
72
|
+
},
|
|
73
|
+
async listTokenBalances(
|
|
74
|
+
options: Omit<ListTokenBalancesOptions, "address">,
|
|
75
|
+
): Promise<ListTokenBalancesResult> {
|
|
76
|
+
return listTokenBalances(apiClient, {
|
|
77
|
+
...options,
|
|
78
|
+
address: this.address,
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
async requestFaucet(
|
|
82
|
+
options: Omit<RequestFaucetOptions, "address">,
|
|
83
|
+
): Promise<RequestFaucetResult> {
|
|
84
|
+
return requestFaucet(apiClient, {
|
|
85
|
+
...options,
|
|
86
|
+
address: this.address,
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
async sendTransaction(options: Omit<SendTransactionOptions, "address">) {
|
|
90
|
+
return sendTransaction(apiClient, {
|
|
91
|
+
...options,
|
|
92
|
+
address: this.address,
|
|
93
|
+
});
|
|
94
|
+
},
|
|
55
95
|
name: options.account.name,
|
|
56
96
|
type: "evm-server",
|
|
57
97
|
};
|
|
@@ -1,7 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
listTokenBalances,
|
|
3
|
+
type ListTokenBalancesOptions,
|
|
4
|
+
type ListTokenBalancesResult,
|
|
5
|
+
} from "../../actions/evm/listTokenBalances.js";
|
|
6
|
+
import {
|
|
7
|
+
RequestFaucetResult,
|
|
8
|
+
RequestFaucetOptions,
|
|
9
|
+
requestFaucet,
|
|
10
|
+
} from "../../actions/evm/requestFaucet.js";
|
|
11
|
+
import {
|
|
12
|
+
type SendUserOperationOptions,
|
|
13
|
+
type SendUserOperationReturnType,
|
|
14
|
+
sendUserOperation,
|
|
15
|
+
} from "../../actions/evm/sendUserOperation.js";
|
|
16
|
+
import { smartAccountTransferStrategy } from "../../actions/evm/transfer/smartAccountTransferStrategy.js";
|
|
17
|
+
import { transfer } from "../../actions/evm/transfer/transfer.js";
|
|
18
|
+
import {
|
|
19
|
+
waitForUserOperation,
|
|
20
|
+
WaitForUserOperationOptions,
|
|
21
|
+
WaitForUserOperationReturnType,
|
|
22
|
+
} from "../../actions/evm/waitForUserOperation.js";
|
|
3
23
|
|
|
24
|
+
import type { TransferResult } from "../../actions/evm/transfer/types.js";
|
|
25
|
+
import type {
|
|
26
|
+
CdpOpenApiClientType,
|
|
27
|
+
EvmSmartAccount as EvmSmartAccountModel,
|
|
28
|
+
} from "../../openapi-client/index.js";
|
|
4
29
|
import type { Address } from "../../types/misc.js";
|
|
30
|
+
import type { EvmAccount, EvmSmartAccount } from "../types.js";
|
|
5
31
|
|
|
6
32
|
/**
|
|
7
33
|
* Options for converting a pre-existing EvmSmartAccount and owner to a EvmSmartAccount
|
|
@@ -19,16 +45,54 @@ export type ToEvmSmartAccountOptions = {
|
|
|
19
45
|
*
|
|
20
46
|
* The owner must be the original owner of the evm smart account.
|
|
21
47
|
*
|
|
48
|
+
* @param {CdpOpenApiClientType} apiClient - The API client.
|
|
22
49
|
* @param {ToEvmSmartAccountOptions} options - Configuration options.
|
|
23
50
|
* @param {EvmSmartAccount} options.smartAccount - The deployed evm smart account.
|
|
24
51
|
* @param {EvmAccount} options.owner - The owner which signs for the smart account.
|
|
25
52
|
* @returns {EvmSmartAccount} A configured EvmSmartAccount instance ready for user operation submission.
|
|
26
|
-
* @throws {Error} If the account is not an original owner of the smart account.
|
|
27
53
|
*/
|
|
28
|
-
export function toEvmSmartAccount(
|
|
54
|
+
export function toEvmSmartAccount(
|
|
55
|
+
apiClient: CdpOpenApiClientType,
|
|
56
|
+
options: ToEvmSmartAccountOptions,
|
|
57
|
+
): EvmSmartAccount {
|
|
29
58
|
const account: EvmSmartAccount = {
|
|
30
59
|
address: options.smartAccount.address as Address,
|
|
31
60
|
owners: [options.owner],
|
|
61
|
+
async transfer(transferArgs): Promise<TransferResult> {
|
|
62
|
+
return transfer(apiClient, account, transferArgs, smartAccountTransferStrategy);
|
|
63
|
+
},
|
|
64
|
+
async listTokenBalances(
|
|
65
|
+
options: Omit<ListTokenBalancesOptions, "address">,
|
|
66
|
+
): Promise<ListTokenBalancesResult> {
|
|
67
|
+
return listTokenBalances(apiClient, {
|
|
68
|
+
...options,
|
|
69
|
+
address: this.address,
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
async sendUserOperation(
|
|
73
|
+
options: Omit<SendUserOperationOptions<unknown[]>, "smartAccount">,
|
|
74
|
+
): Promise<SendUserOperationReturnType> {
|
|
75
|
+
return sendUserOperation(apiClient, {
|
|
76
|
+
...options,
|
|
77
|
+
smartAccount: account,
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
async waitForUserOperation(
|
|
81
|
+
options: Omit<WaitForUserOperationOptions, "smartAccountAddress">,
|
|
82
|
+
): Promise<WaitForUserOperationReturnType> {
|
|
83
|
+
return waitForUserOperation(apiClient, {
|
|
84
|
+
...options,
|
|
85
|
+
smartAccountAddress: account.address,
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
async requestFaucet(
|
|
89
|
+
options: Omit<RequestFaucetOptions, "address">,
|
|
90
|
+
): Promise<RequestFaucetResult> {
|
|
91
|
+
return requestFaucet(apiClient, {
|
|
92
|
+
...options,
|
|
93
|
+
address: account.address,
|
|
94
|
+
});
|
|
95
|
+
},
|
|
32
96
|
name: options.smartAccount.name,
|
|
33
97
|
type: "evm-smart",
|
|
34
98
|
};
|
package/accounts/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SignableMessage, TransactionSerializable, TypedData, TypedDataDefinition } from "viem";
|
|
2
2
|
|
|
3
|
+
import { Actions } from "../actions/evm/types.js";
|
|
3
4
|
import { Prettify } from "../types/utils.js";
|
|
4
5
|
|
|
5
6
|
import type { Hash, Hex, Address } from "../types/misc.js";
|
|
@@ -30,24 +31,27 @@ export type EvmAccount = {
|
|
|
30
31
|
* Server-managed ethereum account
|
|
31
32
|
*/
|
|
32
33
|
export type EvmServerAccount = Prettify<
|
|
33
|
-
EvmAccount &
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
EvmAccount &
|
|
35
|
+
Omit<Actions, "sendUserOperation" | "waitForUserOperation"> & {
|
|
36
|
+
/** Optional name for the server account. */
|
|
37
|
+
name?: string;
|
|
38
|
+
/** Indicates this is a server-managed account. */
|
|
39
|
+
type: "evm-server";
|
|
40
|
+
}
|
|
39
41
|
>;
|
|
40
42
|
|
|
41
43
|
/**
|
|
42
44
|
* Ethereum smart account which supports account abstraction features like user operations, batch transactions, and paymaster.
|
|
43
45
|
*/
|
|
44
|
-
export type EvmSmartAccount =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
export type EvmSmartAccount = Prettify<
|
|
47
|
+
{
|
|
48
|
+
/** The smart account's address. */
|
|
49
|
+
address: Address;
|
|
50
|
+
/** The name of the smart account. */
|
|
51
|
+
name?: string;
|
|
52
|
+
/** Array of accounts that own and can sign for the smart account (currently only supports one owner but will be extended to support multiple owners in the future). */
|
|
53
|
+
owners: EvmAccount[];
|
|
54
|
+
/** Identifier for the smart account type. */
|
|
55
|
+
type: "evm-smart";
|
|
56
|
+
} & Omit<Actions, "sendTransaction">
|
|
57
|
+
>;
|
|
@@ -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
|
+
}
|
|
@@ -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;
|
|
@@ -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,72 @@
|
|
|
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, {
|
|
14
|
+
smartAccount: from,
|
|
15
|
+
network: transferArgs.network,
|
|
16
|
+
calls: [
|
|
17
|
+
{
|
|
18
|
+
to,
|
|
19
|
+
value,
|
|
20
|
+
data: "0x",
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
return result.userOpHash;
|
|
25
|
+
} else {
|
|
26
|
+
const erc20Address = getErc20Address(transferArgs.token, transferArgs.network);
|
|
27
|
+
|
|
28
|
+
const result = await sendUserOperation(apiClient, {
|
|
29
|
+
smartAccount: from,
|
|
30
|
+
network: transferArgs.network,
|
|
31
|
+
calls: [
|
|
32
|
+
{
|
|
33
|
+
to: erc20Address,
|
|
34
|
+
data: encodeFunctionData({
|
|
35
|
+
abi: erc20Abi,
|
|
36
|
+
functionName: "approve",
|
|
37
|
+
args: [to, value],
|
|
38
|
+
}),
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
to: erc20Address,
|
|
42
|
+
data: encodeFunctionData({
|
|
43
|
+
abi: erc20Abi,
|
|
44
|
+
functionName: "transfer",
|
|
45
|
+
args: [to, value],
|
|
46
|
+
}),
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return result.userOpHash;
|
|
52
|
+
}
|
|
53
|
+
})();
|
|
54
|
+
|
|
55
|
+
return userOpHash;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
waitForResult: async ({ apiClient, publicClient, from, hash }) => {
|
|
59
|
+
const result = await waitForUserOperation(apiClient, {
|
|
60
|
+
smartAccountAddress: from.address,
|
|
61
|
+
userOpHash: hash,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (result.status === "complete") {
|
|
65
|
+
return { status: "success", transactionHash: hash };
|
|
66
|
+
} else {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Transaction failed. Check the transaction on the explorer: ${publicClient.chain.blockExplorers?.default.url}/tx/${hash}`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|