@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
244 lines (243 loc) • 10.7 kB
JavaScript
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
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");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _CdpEvmWalletProvider_instances, _CdpEvmWalletProvider_publicClient, _CdpEvmWalletProvider_serverAccount, _CdpEvmWalletProvider_cdp, _CdpEvmWalletProvider_network, _CdpEvmWalletProvider_getCdpSdkNetwork;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdpEvmWalletProvider = void 0;
const cdp_sdk_1 = require("@coinbase/cdp-sdk");
const viem_1 = require("viem");
const network_1 = require("../network");
const evmWalletProvider_1 = require("./evmWalletProvider");
/**
* A wallet provider that uses the Coinbase SDK.
*/
class CdpEvmWalletProvider extends evmWalletProvider_1.EvmWalletProvider {
/**
* Constructs a new CdpEvmWalletProvider.
*
* @param config - The configuration options for the CdpEvmWalletProvider.
*/
constructor(config) {
super();
_CdpEvmWalletProvider_instances.add(this);
_CdpEvmWalletProvider_publicClient.set(this, void 0);
_CdpEvmWalletProvider_serverAccount.set(this, void 0);
_CdpEvmWalletProvider_cdp.set(this, void 0);
_CdpEvmWalletProvider_network.set(this, void 0);
__classPrivateFieldSet(this, _CdpEvmWalletProvider_serverAccount, config.serverAccount, "f");
__classPrivateFieldSet(this, _CdpEvmWalletProvider_cdp, config.cdp, "f");
__classPrivateFieldSet(this, _CdpEvmWalletProvider_publicClient, config.publicClient, "f");
__classPrivateFieldSet(this, _CdpEvmWalletProvider_network, config.network, "f");
}
/**
* Configures a new CdpEvmWalletProvider with a wallet.
*
* @param config - Optional configuration parameters
* @returns A Promise that resolves to a new CdpEvmWalletProvider instance
* @throws Error if required environment variables are missing or wallet initialization fails
*/
static async configureWithWallet(config = {}) {
const apiKeyId = config.apiKeyId || process.env.CDP_API_KEY_ID;
const apiKeySecret = config.apiKeySecret || process.env.CDP_API_KEY_SECRET;
const walletSecret = config.walletSecret || process.env.CDP_WALLET_SECRET;
const idempotencyKey = config.idempotencyKey || process.env.IDEMPOTENCY_KEY;
if (!apiKeyId || !apiKeySecret || !walletSecret) {
throw new Error("Missing required environment variables. CDP_API_KEY_ID, CDP_API_KEY_SECRET, CDP_WALLET_SECRET are required.");
}
const networkId = config.networkId || process.env.NETWORK_ID || "base-sepolia";
const network = {
protocolFamily: "evm",
chainId: network_1.NETWORK_ID_TO_CHAIN_ID[networkId],
networkId: networkId,
};
const cdpClient = new cdp_sdk_1.CdpClient({
apiKeyId,
apiKeySecret,
walletSecret,
});
const serverAccount = await (config.address
? cdpClient.evm.getAccount({ address: config.address })
: cdpClient.evm.createAccount({ idempotencyKey }));
const publicClient = (0, viem_1.createPublicClient)({
chain: network_1.NETWORK_ID_TO_VIEM_CHAIN[networkId],
transport: (0, viem_1.http)(),
});
return new CdpEvmWalletProvider({
publicClient,
cdp: cdpClient,
serverAccount,
network,
});
}
/**
* Exports the wallet.
*
* @returns The wallet's data.
*/
async exportWallet() {
return {
name: __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").name,
address: __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").address,
};
}
/**
* Signs a message.
*
* @param message - The message to sign.
* @returns The signed message.
*/
async signMessage(message) {
return __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").signMessage({ message });
}
/**
* Signs a typed data object.
*
* @param typedData - The typed data object to sign.
* @returns The signed typed data object.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async signTypedData(typedData) {
return __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").signTypedData(typedData);
}
/**
* Signs a transaction.
*
* @param transaction - The transaction to sign.
* @returns The signed transaction.
*/
async signTransaction(transaction) {
const serializedTx = (0, viem_1.serializeTransaction)(transaction);
const signedTx = await __classPrivateFieldGet(this, _CdpEvmWalletProvider_cdp, "f").evm.signTransaction({
address: __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").address,
transaction: serializedTx,
});
return signedTx.signature;
}
/**
* Sends a transaction.
*
* @param transaction - The transaction to send.
* @returns The hash of the transaction.
*/
async sendTransaction(transaction) {
const txWithGasParams = {
...transaction,
chainId: __classPrivateFieldGet(this, _CdpEvmWalletProvider_network, "f").chainId,
};
if (!txWithGasParams.maxFeePerGas && !txWithGasParams.gasPrice) {
const feeData = await __classPrivateFieldGet(this, _CdpEvmWalletProvider_publicClient, "f").estimateFeesPerGas();
txWithGasParams.maxFeePerGas = feeData.maxFeePerGas;
txWithGasParams.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
}
if (!txWithGasParams.gas) {
try {
txWithGasParams.gas = await __classPrivateFieldGet(this, _CdpEvmWalletProvider_publicClient, "f").estimateGas({
account: __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").address,
...txWithGasParams,
});
}
catch (error) {
console.warn("Failed to estimate gas, continuing without gas estimation", error);
}
}
const result = await __classPrivateFieldGet(this, _CdpEvmWalletProvider_cdp, "f").evm.sendTransaction({
address: __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").address,
transaction: (0, viem_1.serializeTransaction)(txWithGasParams),
network: __classPrivateFieldGet(this, _CdpEvmWalletProvider_instances, "m", _CdpEvmWalletProvider_getCdpSdkNetwork).call(this),
});
return result.transactionHash;
}
/**
* Gets the address of the wallet.
*
* @returns The address of the wallet.
*/
getAddress() {
return __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").address;
}
/**
* Gets the network of the wallet.
*
* @returns The network of the wallet.
*/
getNetwork() {
return __classPrivateFieldGet(this, _CdpEvmWalletProvider_network, "f");
}
/**
* Gets the name of the wallet provider.
*
* @returns The name of the wallet provider.
*/
getName() {
return "cdp_evm_wallet_provider";
}
/**
* Gets the CDP client.
*
* @returns The CDP client.
*/
getClient() {
return __classPrivateFieldGet(this, _CdpEvmWalletProvider_cdp, "f");
}
/**
* Gets the balance of the wallet.
*
* @returns The balance of the wallet in wei
*/
async getBalance() {
return await __classPrivateFieldGet(this, _CdpEvmWalletProvider_publicClient, "f").getBalance({ address: __classPrivateFieldGet(this, _CdpEvmWalletProvider_serverAccount, "f").address });
}
/**
* Waits for a transaction receipt.
*
* @param txHash - The hash of the transaction to wait for.
* @returns The transaction receipt.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async waitForTransactionReceipt(txHash) {
return await __classPrivateFieldGet(this, _CdpEvmWalletProvider_publicClient, "f").waitForTransactionReceipt({ hash: txHash });
}
/**
* Reads a contract.
*
* @param params - The parameters to read the contract.
* @returns The response from the contract.
*/
async readContract(params) {
return __classPrivateFieldGet(this, _CdpEvmWalletProvider_publicClient, "f").readContract(params);
}
/**
* Transfer the native asset of the network.
*
* @param to - The destination address.
* @param value - The amount to transfer in Wei.
* @returns The transaction hash.
*/
async nativeTransfer(to, value) {
return this.sendTransaction({
to: to,
value: BigInt(value),
data: "0x",
});
}
}
exports.CdpEvmWalletProvider = CdpEvmWalletProvider;
_CdpEvmWalletProvider_publicClient = new WeakMap(), _CdpEvmWalletProvider_serverAccount = new WeakMap(), _CdpEvmWalletProvider_cdp = new WeakMap(), _CdpEvmWalletProvider_network = new WeakMap(), _CdpEvmWalletProvider_instances = new WeakSet(), _CdpEvmWalletProvider_getCdpSdkNetwork = function _CdpEvmWalletProvider_getCdpSdkNetwork() {
switch (__classPrivateFieldGet(this, _CdpEvmWalletProvider_network, "f").networkId) {
case "base-sepolia":
return "base-sepolia";
case "base-mainnet":
return "base";
default:
throw new Error(`Unsupported network: ${__classPrivateFieldGet(this, _CdpEvmWalletProvider_network, "f").networkId}`);
}
};