@coinbase/agentkit 0.1.1 → 0.2.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.
Files changed (67) hide show
  1. package/README.md +338 -5
  2. package/dist/action-providers/alchemy/alchemyTokenPricesActionProvider.d.ts +55 -0
  3. package/dist/action-providers/alchemy/alchemyTokenPricesActionProvider.js +173 -0
  4. package/dist/action-providers/alchemy/alchemyTokenPricesActionProvider.test.d.ts +1 -0
  5. package/dist/action-providers/alchemy/alchemyTokenPricesActionProvider.test.js +131 -0
  6. package/dist/action-providers/alchemy/index.d.ts +2 -0
  7. package/dist/action-providers/alchemy/index.js +18 -0
  8. package/dist/action-providers/alchemy/schemas.d.ts +41 -0
  9. package/dist/action-providers/alchemy/schemas.js +34 -0
  10. package/dist/action-providers/basename/basenameActionProvider.d.ts +1 -1
  11. package/dist/action-providers/cdp/cdpApiActionProvider.js +7 -1
  12. package/dist/action-providers/erc20/erc20ActionProvider.d.ts +1 -1
  13. package/dist/action-providers/erc20/schemas.d.ts +2 -2
  14. package/dist/action-providers/erc721/erc721ActionProvider.d.ts +1 -1
  15. package/dist/action-providers/index.d.ts +8 -5
  16. package/dist/action-providers/index.js +8 -5
  17. package/dist/action-providers/moonwell/constants.d.ts +78 -0
  18. package/dist/action-providers/moonwell/constants.js +111 -0
  19. package/dist/action-providers/moonwell/index.d.ts +1 -0
  20. package/dist/action-providers/moonwell/index.js +5 -0
  21. package/dist/action-providers/moonwell/moonwellActionProvider.d.ts +39 -0
  22. package/dist/action-providers/moonwell/moonwellActionProvider.js +249 -0
  23. package/dist/action-providers/moonwell/moonwellActionProvider.test.d.ts +1 -0
  24. package/dist/action-providers/moonwell/moonwellActionProvider.test.js +455 -0
  25. package/dist/action-providers/moonwell/schemas.d.ts +30 -0
  26. package/dist/action-providers/moonwell/schemas.js +39 -0
  27. package/dist/action-providers/morpho/morphoActionProvider.d.ts +1 -1
  28. package/dist/action-providers/morpho/schemas.d.ts +4 -4
  29. package/dist/action-providers/pyth/pythActionProvider.test.d.ts +1 -0
  30. package/dist/action-providers/pyth/pythActionProvider.test.js +113 -0
  31. package/dist/action-providers/spl/index.d.ts +1 -0
  32. package/dist/action-providers/spl/index.js +17 -0
  33. package/dist/action-providers/spl/schemas.d.ts +17 -0
  34. package/dist/action-providers/spl/schemas.js +14 -0
  35. package/dist/action-providers/spl/splActionProvider.d.ts +37 -0
  36. package/dist/action-providers/spl/splActionProvider.js +112 -0
  37. package/dist/action-providers/spl/splActionProvider.test.d.ts +1 -0
  38. package/dist/action-providers/spl/splActionProvider.test.js +206 -0
  39. package/dist/action-providers/wallet/walletActionProvider.d.ts +1 -1
  40. package/dist/action-providers/wallet/walletActionProvider.js +31 -18
  41. package/dist/action-providers/wallet/walletActionProvider.test.js +101 -23
  42. package/dist/action-providers/weth/wethActionProvider.d.ts +1 -1
  43. package/dist/action-providers/wow/wowActionProvider.d.ts +1 -1
  44. package/dist/network/index.d.ts +2 -1
  45. package/dist/network/index.js +2 -1
  46. package/dist/network/network.d.ts +7 -0
  47. package/dist/network/network.js +46 -1
  48. package/dist/network/svm.d.ts +14 -0
  49. package/dist/network/svm.js +33 -0
  50. package/dist/utils.d.ts +11 -0
  51. package/dist/utils.js +14 -0
  52. package/dist/wallet-providers/cdpWalletProvider.d.ts +13 -0
  53. package/dist/wallet-providers/cdpWalletProvider.js +31 -8
  54. package/dist/wallet-providers/index.d.ts +3 -0
  55. package/dist/wallet-providers/index.js +3 -0
  56. package/dist/wallet-providers/privyWalletProvider.d.ts +70 -0
  57. package/dist/wallet-providers/privyWalletProvider.js +139 -0
  58. package/dist/wallet-providers/solanaKeypairWalletProvider.d.ts +143 -0
  59. package/dist/wallet-providers/solanaKeypairWalletProvider.js +280 -0
  60. package/dist/wallet-providers/solanaKeypairWalletProvider.test.d.ts +1 -0
  61. package/dist/wallet-providers/solanaKeypairWalletProvider.test.js +24 -0
  62. package/dist/wallet-providers/svmWalletProvider.d.ts +56 -0
  63. package/dist/wallet-providers/svmWalletProvider.js +13 -0
  64. package/dist/wallet-providers/viemWalletProvider.d.ts +15 -1
  65. package/dist/wallet-providers/viemWalletProvider.js +23 -4
  66. package/dist/wallet-providers/walletProvider.d.ts +1 -1
  67. package/package.json +4 -1
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _PrivyWalletProvider_walletId, _PrivyWalletProvider_authorizationPrivateKey;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PrivyWalletProvider = void 0;
16
+ const server_auth_1 = require("@privy-io/server-auth");
17
+ const viem_1 = require("@privy-io/server-auth/viem");
18
+ const viemWalletProvider_1 = require("./viemWalletProvider");
19
+ const viem_2 = require("viem");
20
+ const network_1 = require("../network/network");
21
+ /**
22
+ * A wallet provider that uses Privy's server wallet API.
23
+ * This provider extends the ViemWalletProvider to provide Privy-specific wallet functionality
24
+ * while maintaining compatibility with the base wallet provider interface.
25
+ */
26
+ class PrivyWalletProvider extends viemWalletProvider_1.ViemWalletProvider {
27
+ /**
28
+ * Private constructor to enforce use of factory method.
29
+ *
30
+ * @param walletClient - The Viem wallet client instance
31
+ * @param config - The configuration options for the Privy wallet
32
+ */
33
+ constructor(walletClient, config) {
34
+ super(walletClient);
35
+ _PrivyWalletProvider_walletId.set(this, void 0);
36
+ _PrivyWalletProvider_authorizationPrivateKey.set(this, void 0);
37
+ __classPrivateFieldSet(this, _PrivyWalletProvider_walletId, config.walletId, "f"); // Now guaranteed to exist
38
+ __classPrivateFieldSet(this, _PrivyWalletProvider_authorizationPrivateKey, config.authorizationPrivateKey, "f");
39
+ }
40
+ /**
41
+ * Creates and configures a new PrivyWalletProvider instance.
42
+ *
43
+ * @param config - The configuration options for the Privy wallet
44
+ * @returns A configured PrivyWalletProvider instance
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const provider = await PrivyWalletProvider.configureWithWallet({
49
+ * appId: "your-app-id",
50
+ * appSecret: "your-app-secret",
51
+ * walletId: "wallet-id",
52
+ * chainId: "84532"
53
+ * });
54
+ * ```
55
+ */
56
+ static async configureWithWallet(config) {
57
+ const privy = new server_auth_1.PrivyClient(config.appId, config.appSecret, {
58
+ walletApi: config.authorizationPrivateKey
59
+ ? {
60
+ authorizationPrivateKey: config.authorizationPrivateKey,
61
+ }
62
+ : undefined,
63
+ });
64
+ let walletId;
65
+ let address;
66
+ if (!config.walletId) {
67
+ if (config.authorizationPrivateKey && !config.authorizationKeyId) {
68
+ throw new Error("authorizationKeyId is required when creating a new wallet with an authorization key, this can be found in your Privy Dashboard");
69
+ }
70
+ if (config.authorizationKeyId && !config.authorizationPrivateKey) {
71
+ throw new Error("authorizationPrivateKey is required when creating a new wallet with an authorizationKeyId. " +
72
+ "If you don't have it, you can create a new one in your Privy Dashboard, or delete the authorization key.");
73
+ }
74
+ try {
75
+ const wallet = await privy.walletApi.create({
76
+ chainType: "ethereum",
77
+ authorizationKeyIds: config.authorizationKeyId ? [config.authorizationKeyId] : undefined,
78
+ });
79
+ walletId = wallet.id;
80
+ address = wallet.address;
81
+ }
82
+ catch (error) {
83
+ console.error(error);
84
+ if (error instanceof Error &&
85
+ error.message.includes("Missing `privy-authorization-signature` header")) {
86
+ // Providing a more informative error message, see context: https://github.com/coinbase/agentkit/pull/242#discussion_r1956428617
87
+ throw new Error("Privy error: you have an authorization key on your account which can create and modify wallets, please delete this key or pass it to the PrivyWalletProvider to create a new wallet");
88
+ }
89
+ throw new Error("Failed to create wallet");
90
+ }
91
+ }
92
+ else {
93
+ walletId = config.walletId;
94
+ const wallet = await privy.walletApi.getWallet({ id: walletId });
95
+ if (!wallet) {
96
+ throw new Error(`Wallet with ID ${walletId} not found`);
97
+ }
98
+ address = wallet.address;
99
+ }
100
+ const account = await (0, viem_1.createViemAccount)({
101
+ walletId,
102
+ address,
103
+ privy,
104
+ });
105
+ const chainId = config.chainId || "84532";
106
+ const chain = (0, network_1.getChain)(chainId);
107
+ if (!chain) {
108
+ throw new Error(`Chain with ID ${chainId} not found`);
109
+ }
110
+ const walletClient = (0, viem_2.createWalletClient)({
111
+ account,
112
+ chain,
113
+ transport: (0, viem_2.http)(),
114
+ });
115
+ return new PrivyWalletProvider(walletClient, { ...config, walletId });
116
+ }
117
+ /**
118
+ * Gets the name of the wallet provider.
119
+ *
120
+ * @returns The string identifier for this wallet provider
121
+ */
122
+ getName() {
123
+ return "privy_wallet_provider";
124
+ }
125
+ /**
126
+ * Exports the wallet data.
127
+ *
128
+ * @returns The wallet data
129
+ */
130
+ exportWallet() {
131
+ return {
132
+ walletId: __classPrivateFieldGet(this, _PrivyWalletProvider_walletId, "f"),
133
+ authorizationPrivateKey: __classPrivateFieldGet(this, _PrivyWalletProvider_authorizationPrivateKey, "f"),
134
+ chainId: this.getNetwork().chainId,
135
+ };
136
+ }
137
+ }
138
+ exports.PrivyWalletProvider = PrivyWalletProvider;
139
+ _PrivyWalletProvider_walletId = new WeakMap(), _PrivyWalletProvider_authorizationPrivateKey = new WeakMap();
@@ -0,0 +1,143 @@
1
+ import { SvmWalletProvider } from "./svmWalletProvider";
2
+ import { Network } from "../network";
3
+ import { Connection, PublicKey, VersionedTransaction, RpcResponseAndContext, SignatureResult, SignatureStatus, SignatureStatusConfig } from "@solana/web3.js";
4
+ import { SOLANA_CLUSTER, SOLANA_NETWORK_ID } from "../network/svm";
5
+ /**
6
+ * SolanaKeypairWalletProvider is a wallet provider that uses a local Solana keypair.
7
+ *
8
+ * @augments SvmWalletProvider
9
+ */
10
+ export declare class SolanaKeypairWalletProvider extends SvmWalletProvider {
11
+ #private;
12
+ /**
13
+ * Creates a new SolanaKeypairWalletProvider
14
+ *
15
+ * @param args - Configuration arguments
16
+ * @param args.keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
17
+ * @param args.rpcUrl - URL of the Solana RPC endpoint
18
+ * @param args.genesisHash - The genesis hash of the network
19
+ */
20
+ constructor({ keypair, rpcUrl, genesisHash, }: {
21
+ keypair: Uint8Array | string;
22
+ rpcUrl: string;
23
+ genesisHash: string;
24
+ });
25
+ /**
26
+ * Get the default RPC URL for a Solana cluster
27
+ *
28
+ * @param cluster - The cluster to get the RPC URL for
29
+ * @returns The RPC URL for the cluster
30
+ */
31
+ static urlForCluster(cluster: SOLANA_CLUSTER): string;
32
+ /**
33
+ * Create a new SolanaKeypairWalletProvider from an SVM networkId and a keypair
34
+ *
35
+ * @param networkId - The SVM networkId
36
+ * @param keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
37
+ * @returns The new SolanaKeypairWalletProvider
38
+ */
39
+ static fromNetwork<T extends SolanaKeypairWalletProvider>(networkId: SOLANA_NETWORK_ID, keypair: Uint8Array | string): Promise<T>;
40
+ /**
41
+ * Create a new SolanaKeypairWalletProvider from an RPC URL and a keypair
42
+ *
43
+ * @param rpcUrl - The URL of the Solana RPC endpoint
44
+ * @param keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
45
+ * @returns The new SolanaKeypairWalletProvider
46
+ */
47
+ static fromRpcUrl<T extends SolanaKeypairWalletProvider>(rpcUrl: string, keypair: Uint8Array | string): Promise<T>;
48
+ /**
49
+ * Create a new SolanaKeypairWalletProvider from a Connection and a keypair
50
+ *
51
+ * @param connection - The Connection to use
52
+ * @param keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
53
+ * @returns The new SolanaKeypairWalletProvider
54
+ */
55
+ static fromConnection<T extends SolanaKeypairWalletProvider>(connection: Connection, keypair: Uint8Array | string): Promise<T>;
56
+ /**
57
+ * Get the connection instance
58
+ *
59
+ * @returns The Solana connection instance
60
+ */
61
+ getConnection(): Connection;
62
+ /**
63
+ * Get the public key of the wallet
64
+ *
65
+ * @returns The wallet's public key
66
+ */
67
+ getPublicKey(): PublicKey;
68
+ /**
69
+ * Get the address of the wallet
70
+ *
71
+ * @returns The base58 encoded address of the wallet
72
+ */
73
+ getAddress(): string;
74
+ /**
75
+ * Get the network
76
+ *
77
+ * @returns The network
78
+ */
79
+ getNetwork(): Network;
80
+ /**
81
+ * Sign a transaction
82
+ *
83
+ * @param transaction - The transaction to sign
84
+ * @returns The signed transaction
85
+ */
86
+ signTransaction(transaction: VersionedTransaction): Promise<VersionedTransaction>;
87
+ /**
88
+ * Send a transaction
89
+ *
90
+ * @param transaction - The transaction to send
91
+ * @returns The signature
92
+ */
93
+ sendTransaction(transaction: VersionedTransaction): Promise<string>;
94
+ /**
95
+ * Sign and send a transaction
96
+ *
97
+ * @param transaction - The transaction to sign and send
98
+ * @returns The signature
99
+ */
100
+ signAndSendTransaction(transaction: VersionedTransaction): Promise<string>;
101
+ /**
102
+ * Get the status of a transaction
103
+ *
104
+ * @param signature - The signature
105
+ * @param options - The options for the status
106
+ * @returns The status
107
+ */
108
+ getSignatureStatus(signature: string, options?: SignatureStatusConfig): Promise<RpcResponseAndContext<SignatureStatus | null>>;
109
+ /**
110
+ * Wait for signature receipt
111
+ *
112
+ * @param signature - The signature
113
+ * @returns The confirmation response
114
+ */
115
+ waitForSignatureResult(signature: string): Promise<RpcResponseAndContext<SignatureResult>>;
116
+ /**
117
+ * Get the name of the wallet provider
118
+ *
119
+ * @returns The name of the wallet provider
120
+ */
121
+ getName(): string;
122
+ /**
123
+ * Get the balance of the wallet
124
+ *
125
+ * @returns The balance of the wallet
126
+ */
127
+ getBalance(): Promise<bigint>;
128
+ /**
129
+ * Transfer SOL from the wallet to another address
130
+ *
131
+ * @param to - The base58 encoded address to transfer the SOL to
132
+ * @param value - The amount of SOL to transfer (as a decimal string, e.g. "0.0001")
133
+ * @returns The signature
134
+ */
135
+ nativeTransfer(to: string, value: string): Promise<string>;
136
+ /**
137
+ * Request SOL tokens from the Solana faucet. This method only works on devnet and testnet networks.
138
+ *
139
+ * @param lamports - The amount of lamports (1 SOL = 1,000,000,000 lamports) to request from the faucet
140
+ * @returns A Promise that resolves to the signature of the airdrop
141
+ */
142
+ requestAirdrop(lamports: number): Promise<string>;
143
+ }
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _SolanaKeypairWalletProvider_keypair, _SolanaKeypairWalletProvider_connection, _SolanaKeypairWalletProvider_genesisHash;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.SolanaKeypairWalletProvider = void 0;
19
+ const svmWalletProvider_1 = require("./svmWalletProvider");
20
+ const web3_js_1 = require("@solana/web3.js");
21
+ const bs58_1 = __importDefault(require("bs58"));
22
+ const svm_1 = require("../network/svm");
23
+ /**
24
+ * SolanaKeypairWalletProvider is a wallet provider that uses a local Solana keypair.
25
+ *
26
+ * @augments SvmWalletProvider
27
+ */
28
+ class SolanaKeypairWalletProvider extends svmWalletProvider_1.SvmWalletProvider {
29
+ /**
30
+ * Creates a new SolanaKeypairWalletProvider
31
+ *
32
+ * @param args - Configuration arguments
33
+ * @param args.keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
34
+ * @param args.rpcUrl - URL of the Solana RPC endpoint
35
+ * @param args.genesisHash - The genesis hash of the network
36
+ */
37
+ constructor({ keypair, rpcUrl, genesisHash, }) {
38
+ super();
39
+ _SolanaKeypairWalletProvider_keypair.set(this, void 0);
40
+ _SolanaKeypairWalletProvider_connection.set(this, void 0);
41
+ _SolanaKeypairWalletProvider_genesisHash.set(this, void 0);
42
+ __classPrivateFieldSet(this, _SolanaKeypairWalletProvider_keypair, typeof keypair === "string"
43
+ ? web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(keypair))
44
+ : web3_js_1.Keypair.fromSecretKey(keypair), "f");
45
+ __classPrivateFieldSet(this, _SolanaKeypairWalletProvider_connection, new web3_js_1.Connection(rpcUrl), "f");
46
+ if (genesisHash in svm_1.SOLANA_NETWORKS) {
47
+ __classPrivateFieldSet(this, _SolanaKeypairWalletProvider_genesisHash, genesisHash, "f");
48
+ }
49
+ else {
50
+ throw new Error(`Unknown network with genesis hash: ${genesisHash}`);
51
+ }
52
+ }
53
+ /**
54
+ * Get the default RPC URL for a Solana cluster
55
+ *
56
+ * @param cluster - The cluster to get the RPC URL for
57
+ * @returns The RPC URL for the cluster
58
+ */
59
+ static urlForCluster(cluster) {
60
+ if (cluster in svm_1.SOLANA_NETWORKS) {
61
+ switch (cluster) {
62
+ case svm_1.SOLANA_MAINNET_GENESIS_BLOCK_HASH:
63
+ return (0, web3_js_1.clusterApiUrl)("mainnet-beta");
64
+ case svm_1.SOLANA_TESTNET_GENESIS_BLOCK_HASH:
65
+ return (0, web3_js_1.clusterApiUrl)("testnet");
66
+ case svm_1.SOLANA_DEVNET_GENESIS_BLOCK_HASH:
67
+ return (0, web3_js_1.clusterApiUrl)("devnet");
68
+ default:
69
+ throw new Error(`Unknown cluster: ${cluster}`);
70
+ }
71
+ }
72
+ else {
73
+ throw new Error(`Unknown cluster: ${cluster}`);
74
+ }
75
+ }
76
+ /**
77
+ * Create a new SolanaKeypairWalletProvider from an SVM networkId and a keypair
78
+ *
79
+ * @param networkId - The SVM networkId
80
+ * @param keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
81
+ * @returns The new SolanaKeypairWalletProvider
82
+ */
83
+ static async fromNetwork(networkId, keypair) {
84
+ let genesisHash;
85
+ switch (networkId) {
86
+ case svm_1.SOLANA_MAINNET_NETWORK_ID:
87
+ genesisHash = svm_1.SOLANA_MAINNET_GENESIS_BLOCK_HASH;
88
+ break;
89
+ case svm_1.SOLANA_DEVNET_NETWORK_ID:
90
+ genesisHash = svm_1.SOLANA_DEVNET_GENESIS_BLOCK_HASH;
91
+ break;
92
+ case svm_1.SOLANA_TESTNET_NETWORK_ID:
93
+ genesisHash = svm_1.SOLANA_TESTNET_GENESIS_BLOCK_HASH;
94
+ break;
95
+ default:
96
+ throw new Error(`${networkId} is not a valid SVM networkId`);
97
+ }
98
+ const rpcUrl = this.urlForCluster(genesisHash);
99
+ return await this.fromRpcUrl(rpcUrl, keypair);
100
+ }
101
+ /**
102
+ * Create a new SolanaKeypairWalletProvider from an RPC URL and a keypair
103
+ *
104
+ * @param rpcUrl - The URL of the Solana RPC endpoint
105
+ * @param keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
106
+ * @returns The new SolanaKeypairWalletProvider
107
+ */
108
+ static async fromRpcUrl(rpcUrl, keypair) {
109
+ const connection = new web3_js_1.Connection(rpcUrl);
110
+ return await this.fromConnection(connection, keypair);
111
+ }
112
+ /**
113
+ * Create a new SolanaKeypairWalletProvider from a Connection and a keypair
114
+ *
115
+ * @param connection - The Connection to use
116
+ * @param keypair - Either a Uint8Array or a base58 encoded string representing a 32-byte secret key
117
+ * @returns The new SolanaKeypairWalletProvider
118
+ */
119
+ static async fromConnection(connection, keypair) {
120
+ const genesisHash = await connection.getGenesisHash();
121
+ return new SolanaKeypairWalletProvider({
122
+ keypair,
123
+ rpcUrl: connection.rpcEndpoint,
124
+ genesisHash: genesisHash,
125
+ });
126
+ }
127
+ /**
128
+ * Get the connection instance
129
+ *
130
+ * @returns The Solana connection instance
131
+ */
132
+ getConnection() {
133
+ return __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f");
134
+ }
135
+ /**
136
+ * Get the public key of the wallet
137
+ *
138
+ * @returns The wallet's public key
139
+ */
140
+ getPublicKey() {
141
+ return __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f").publicKey;
142
+ }
143
+ /**
144
+ * Get the address of the wallet
145
+ *
146
+ * @returns The base58 encoded address of the wallet
147
+ */
148
+ getAddress() {
149
+ return __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f").publicKey.toBase58();
150
+ }
151
+ /**
152
+ * Get the network
153
+ *
154
+ * @returns The network
155
+ */
156
+ getNetwork() {
157
+ return svm_1.SOLANA_NETWORKS[__classPrivateFieldGet(this, _SolanaKeypairWalletProvider_genesisHash, "f")];
158
+ }
159
+ /**
160
+ * Sign a transaction
161
+ *
162
+ * @param transaction - The transaction to sign
163
+ * @returns The signed transaction
164
+ */
165
+ async signTransaction(transaction) {
166
+ transaction.sign([__classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f")]);
167
+ return transaction;
168
+ }
169
+ /**
170
+ * Send a transaction
171
+ *
172
+ * @param transaction - The transaction to send
173
+ * @returns The signature
174
+ */
175
+ async sendTransaction(transaction) {
176
+ const signature = await __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").sendTransaction(transaction);
177
+ await this.waitForSignatureResult(signature);
178
+ return signature;
179
+ }
180
+ /**
181
+ * Sign and send a transaction
182
+ *
183
+ * @param transaction - The transaction to sign and send
184
+ * @returns The signature
185
+ */
186
+ async signAndSendTransaction(transaction) {
187
+ const signedTransaction = await this.signTransaction(transaction);
188
+ return this.sendTransaction(signedTransaction);
189
+ }
190
+ /**
191
+ * Get the status of a transaction
192
+ *
193
+ * @param signature - The signature
194
+ * @param options - The options for the status
195
+ * @returns The status
196
+ */
197
+ async getSignatureStatus(signature, options) {
198
+ return __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").getSignatureStatus(signature, options);
199
+ }
200
+ /**
201
+ * Wait for signature receipt
202
+ *
203
+ * @param signature - The signature
204
+ * @returns The confirmation response
205
+ */
206
+ async waitForSignatureResult(signature) {
207
+ const { blockhash, lastValidBlockHeight } = await __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").getLatestBlockhash();
208
+ return __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").confirmTransaction({
209
+ signature: signature,
210
+ lastValidBlockHeight,
211
+ blockhash,
212
+ });
213
+ }
214
+ /**
215
+ * Get the name of the wallet provider
216
+ *
217
+ * @returns The name of the wallet provider
218
+ */
219
+ getName() {
220
+ return "solana_keypair_wallet_provider";
221
+ }
222
+ /**
223
+ * Get the balance of the wallet
224
+ *
225
+ * @returns The balance of the wallet
226
+ */
227
+ getBalance() {
228
+ return __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").getBalance(__classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f").publicKey).then(balance => BigInt(balance));
229
+ }
230
+ /**
231
+ * Transfer SOL from the wallet to another address
232
+ *
233
+ * @param to - The base58 encoded address to transfer the SOL to
234
+ * @param value - The amount of SOL to transfer (as a decimal string, e.g. "0.0001")
235
+ * @returns The signature
236
+ */
237
+ async nativeTransfer(to, value) {
238
+ const initialBalance = await this.getBalance();
239
+ const solAmount = parseFloat(value);
240
+ const lamports = BigInt(Math.floor(solAmount * web3_js_1.LAMPORTS_PER_SOL));
241
+ // Check if we have enough balance (including estimated fees)
242
+ if (initialBalance < lamports + BigInt(5000)) {
243
+ throw new Error(`Insufficient balance. Have ${Number(initialBalance) / web3_js_1.LAMPORTS_PER_SOL} SOL, need ${solAmount + 0.000005} SOL (including fees)`);
244
+ }
245
+ const toPubkey = new web3_js_1.PublicKey(to);
246
+ const instructions = [
247
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
248
+ microLamports: 10000,
249
+ }),
250
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
251
+ units: 2000,
252
+ }),
253
+ web3_js_1.SystemProgram.transfer({
254
+ fromPubkey: __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f").publicKey,
255
+ toPubkey: toPubkey,
256
+ lamports: lamports,
257
+ }),
258
+ ];
259
+ const tx = new web3_js_1.VersionedTransaction(web3_js_1.MessageV0.compile({
260
+ payerKey: __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f").publicKey,
261
+ instructions: instructions,
262
+ recentBlockhash: (await __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").getLatestBlockhash()).blockhash,
263
+ }));
264
+ tx.sign([__classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f")]);
265
+ const signature = await __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").sendTransaction(tx);
266
+ await this.waitForSignatureResult(signature);
267
+ return signature;
268
+ }
269
+ /**
270
+ * Request SOL tokens from the Solana faucet. This method only works on devnet and testnet networks.
271
+ *
272
+ * @param lamports - The amount of lamports (1 SOL = 1,000,000,000 lamports) to request from the faucet
273
+ * @returns A Promise that resolves to the signature of the airdrop
274
+ */
275
+ async requestAirdrop(lamports) {
276
+ return await __classPrivateFieldGet(this, _SolanaKeypairWalletProvider_connection, "f").requestAirdrop(__classPrivateFieldGet(this, _SolanaKeypairWalletProvider_keypair, "f").publicKey, lamports);
277
+ }
278
+ }
279
+ exports.SolanaKeypairWalletProvider = SolanaKeypairWalletProvider;
280
+ _SolanaKeypairWalletProvider_keypair = new WeakMap(), _SolanaKeypairWalletProvider_connection = new WeakMap(), _SolanaKeypairWalletProvider_genesisHash = new WeakMap();
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const web3_js_1 = require("@solana/web3.js");
4
+ const solanaKeypairWalletProvider_1 = require("./solanaKeypairWalletProvider");
5
+ const svm_1 = require("../network/svm");
6
+ describe("Solana Keypair Wallet", () => {
7
+ it("should initialize correctly via convenience getters", async () => {
8
+ const keypair = web3_js_1.Keypair.generate();
9
+ let wallet = await solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider.fromRpcUrl((0, web3_js_1.clusterApiUrl)("devnet"), keypair.secretKey);
10
+ expect(wallet.getNetwork()).toEqual(svm_1.SOLANA_NETWORKS[svm_1.SOLANA_DEVNET_GENESIS_BLOCK_HASH]);
11
+ wallet = await solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider.fromRpcUrl((0, web3_js_1.clusterApiUrl)("testnet"), keypair.secretKey);
12
+ expect(wallet.getNetwork()).toEqual(svm_1.SOLANA_NETWORKS[svm_1.SOLANA_TESTNET_GENESIS_BLOCK_HASH]);
13
+ wallet = await solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider.fromRpcUrl((0, web3_js_1.clusterApiUrl)("mainnet-beta"), keypair.secretKey);
14
+ expect(wallet.getNetwork()).toEqual(svm_1.SOLANA_NETWORKS[svm_1.SOLANA_MAINNET_GENESIS_BLOCK_HASH]);
15
+ expect(wallet.getAddress()).toEqual(keypair.publicKey.toBase58());
16
+ });
17
+ it("should error when the network genesis hash is unknown", async () => {
18
+ expect(() => new solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider({
19
+ keypair: web3_js_1.Keypair.generate().secretKey,
20
+ rpcUrl: (0, web3_js_1.clusterApiUrl)("mainnet-beta"),
21
+ genesisHash: "0x123",
22
+ })).toThrowError("Unknown network with genesis hash");
23
+ });
24
+ });
@@ -0,0 +1,56 @@
1
+ import { WalletProvider } from "./walletProvider";
2
+ import { Connection, PublicKey, RpcResponseAndContext, SignatureStatus, SignatureStatusConfig, VersionedTransaction, SignatureResult } from "@solana/web3.js";
3
+ /**
4
+ * SvmWalletProvider is the abstract base class for all Solana wallet providers (non browsers).
5
+ *
6
+ * @abstract
7
+ */
8
+ export declare abstract class SvmWalletProvider extends WalletProvider {
9
+ /**
10
+ * Get the connection instance.
11
+ *
12
+ * @returns The Solana connection instance.
13
+ */
14
+ abstract getConnection(): Connection;
15
+ /**
16
+ * Get the public key of the wallet.
17
+ *
18
+ * @returns The wallet's public key.
19
+ */
20
+ abstract getPublicKey(): PublicKey;
21
+ /**
22
+ * Sign a transaction.
23
+ *
24
+ * @param transaction - The transaction to sign.
25
+ * @returns The signed transaction.
26
+ */
27
+ abstract signTransaction(transaction: VersionedTransaction): Promise<VersionedTransaction>;
28
+ /**
29
+ * Send a transaction.
30
+ *
31
+ * @param transaction - The transaction to send.
32
+ * @returns The signature.
33
+ */
34
+ abstract sendTransaction(transaction: VersionedTransaction): Promise<string>;
35
+ /**
36
+ * Sign and send a transaction.
37
+ *
38
+ * @param transaction - The transaction to sign and send.
39
+ * @returns The signature.
40
+ */
41
+ abstract signAndSendTransaction(transaction: VersionedTransaction): Promise<string>;
42
+ /**
43
+ * Get the status of a transaction.
44
+ *
45
+ * @param signature - The signature.
46
+ * @returns The status.
47
+ */
48
+ abstract getSignatureStatus(signature: string, options?: SignatureStatusConfig): Promise<RpcResponseAndContext<SignatureStatus | null>>;
49
+ /**
50
+ * Wait for signature receipt.
51
+ *
52
+ * @param signature - The signature
53
+ * @returns The confirmation response
54
+ */
55
+ abstract waitForSignatureResult(signature: string): Promise<RpcResponseAndContext<SignatureResult>>;
56
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.SvmWalletProvider = void 0;
5
+ const walletProvider_1 = require("./walletProvider");
6
+ /**
7
+ * SvmWalletProvider is the abstract base class for all Solana wallet providers (non browsers).
8
+ *
9
+ * @abstract
10
+ */
11
+ class SvmWalletProvider extends walletProvider_1.WalletProvider {
12
+ }
13
+ exports.SvmWalletProvider = SvmWalletProvider;