@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
164 lines (161 loc) • 7.09 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.wethActionProvider = exports.WethActionProvider = exports.getWethAddress = void 0;
const zod_1 = require("zod");
const actionProvider_1 = require("../actionProvider");
const actionDecorator_1 = require("../actionDecorator");
const schemas_1 = require("./schemas");
const constants_1 = require("./constants");
const constants_2 = require("../erc20/constants");
const viem_1 = require("viem");
const wallet_providers_1 = require("../../wallet-providers");
/**
* Gets the WETH address for the given network.
*
* @param network - The network to get the WETH address for.
* @returns The WETH address for the network, or undefined if not supported.
*/
const getWethAddress = (network) => {
const networkTokens = constants_2.TOKEN_ADDRESSES_BY_SYMBOLS[network.networkId];
return networkTokens && "WETH" in networkTokens ? networkTokens.WETH : undefined;
};
exports.getWethAddress = getWethAddress;
/**
* WethActionProvider is an action provider for WETH.
*/
class WethActionProvider extends actionProvider_1.ActionProvider {
/**
* Constructor for the WethActionProvider.
*/
constructor() {
super("weth", []);
/**
* Checks if the Weth action provider supports the given network.
*
* @param network - The network to check.
* @returns True if the Weth action provider supports the network, false otherwise.
*/
this.supportsNetwork = (network) => {
return (0, exports.getWethAddress)(network) !== undefined;
};
}
/**
* Wraps ETH to WETH.
*
* @param walletProvider - The wallet provider to use for the action.
* @param args - The input arguments for the action.
* @returns A message containing the transaction hash.
*/
async wrapEth(walletProvider, args) {
const network = walletProvider.getNetwork();
const wethAddress = (0, exports.getWethAddress)(network);
if (!wethAddress) {
return `Error: WETH not supported on network ${network.networkId}`;
}
try {
// Convert human-readable ETH amount to wei (ETH has 18 decimals)
const amountInWei = (0, viem_1.parseUnits)(args.amountToWrap, 18);
// Check ETH balance before wrapping
const ethBalance = await walletProvider.getBalance();
if (ethBalance < amountInWei) {
const ethBalanceFormatted = (0, viem_1.formatUnits)(ethBalance, 18);
return `Error: Insufficient ETH balance. Requested to wrap ${args.amountToWrap} ETH, but only ${ethBalanceFormatted} ETH is available.`;
}
const hash = await walletProvider.sendTransaction({
to: wethAddress,
data: (0, viem_1.encodeFunctionData)({
abi: constants_1.WETH_ABI,
functionName: "deposit",
}),
value: amountInWei,
});
await walletProvider.waitForTransactionReceipt(hash);
return `Wrapped ${args.amountToWrap} ETH to WETH. Transaction hash: ${hash}`;
}
catch (error) {
return `Error wrapping ETH: ${error}`;
}
}
/**
* Unwraps WETH to ETH.
*
* @param walletProvider - The wallet provider to use for the action.
* @param args - The input arguments for the action.
* @returns A message containing the transaction hash.
*/
async unwrapEth(walletProvider, args) {
const network = walletProvider.getNetwork();
const wethAddress = (0, exports.getWethAddress)(network);
if (!wethAddress) {
return `Error: WETH not supported on network ${network.networkId}`;
}
try {
// Convert human-readable WETH amount to wei (WETH has 18 decimals)
const amountInWei = (0, viem_1.parseUnits)(args.amountToUnwrap, 18);
// Check WETH balance before unwrapping
const wethBalance = await walletProvider.readContract({
address: wethAddress,
abi: viem_1.erc20Abi,
functionName: "balanceOf",
args: [walletProvider.getAddress()],
});
if (wethBalance < amountInWei) {
const wethBalanceFormatted = (0, viem_1.formatUnits)(wethBalance, 18);
return `Error: Insufficient WETH balance. Requested to unwrap ${args.amountToUnwrap} WETH, but only ${wethBalanceFormatted} WETH is available.`;
}
const hash = await walletProvider.sendTransaction({
to: wethAddress,
data: (0, viem_1.encodeFunctionData)({
abi: constants_1.WETH_ABI,
functionName: "withdraw",
args: [amountInWei],
}),
});
await walletProvider.waitForTransactionReceipt(hash);
return `Unwrapped ${args.amountToUnwrap} WETH to ETH. Transaction hash: ${hash}`;
}
catch (error) {
return `Error unwrapping WETH: ${error}`;
}
}
}
exports.WethActionProvider = WethActionProvider;
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "wrap_eth",
description: `
This tool wraps ETH to WETH.
Inputs:
- Amount of ETH to wrap in human-readable format (e.g., 0.1 for 0.1 ETH).
`,
schema: schemas_1.WrapEthSchema,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
__metadata("design:returntype", Promise)
], WethActionProvider.prototype, "wrapEth", null);
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "unwrap_eth",
description: `
This tool unwraps WETH to ETH.
Inputs:
- Amount of WETH to unwrap in human-readable format (e.g., 0.1 for 0.1 WETH).
`,
schema: schemas_1.UnwrapEthSchema,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
__metadata("design:returntype", Promise)
], WethActionProvider.prototype, "unwrapEth", null);
const wethActionProvider = () => new WethActionProvider();
exports.wethActionProvider = wethActionProvider;