goat-erc4626
Version:
272 lines (267 loc) • 12.4 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
var __typeError = (msg) => {
throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
var __runInitializers = (array, flags, self, value) => {
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
return value;
};
var __decorateElement = (array, flags, name, decorators, target, extra) => {
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
return __privateGet(this, extra);
}, set [name](x) {
return __privateSet(this, extra, x);
} }, name));
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
for (var i = decorators.length - 1; i >= 0; i--) {
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
if (k) {
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
}
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
else if (typeof it !== "object" || it === null) __typeError("Object expected");
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
}
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
};
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
// src/erc4626.plugin.ts
import { PluginBase } from "@goat-sdk/core";
// src/erc4626.service.ts
import { Tool } from "@goat-sdk/core";
// src/abi.ts
import { parseAbi } from "viem";
var ERC4626_ABI = parseAbi([
/*
"function transfer(address to, uint256 amount) external returns (bool)",
"function balanceOf(address account) external view returns (uint256)",
"function totalSupply() external view returns (uint256)",
"function allowance(address owner, address spender) external view returns (uint256)",
"function approve(address spender, uint256 amount) external returns (bool)",
"function transferFrom(address from, address to, uint256 amount) external returns (bool)",
*/
"function addYield(uint256 amount) external",
"function deposit(uint256 assets, address receiver) external returns (uint256)",
"function mint(uint256 shares, address receiver) external returns (uint256)",
"function redeem(uint256 shares, address receiver) external returns (uint256)",
"function withdraw(uint256 assets, address receiver, address owner) external returns (uint256)",
"function convertToShares(uint256 assets) external view returns (uint256)",
"function convertToAssets(uint256 shares) external view returns (uint256)",
"function previewDeposit(uint256 assets) external view returns (uint256)",
"function previewMint(uint256 shares) external view returns (uint256)",
"function previewRedeem(uint256 shares) external view returns (uint256)",
"function previewWithdraw(uint256 assets) external view returns (uint256)",
"function asset() external view returns (address)",
"function decimals() external view returns (uint8)",
"function maxDeposit(address) external view returns (uint256)",
"function maxMint(address) external view returns (uint256)",
"function maxRedeem(address) external view returns (uint256)",
"function maxWithdraw(address) external view returns (uint256)",
"function name() external view returns (string)",
"function owner() external view returns (address)",
"function symbol() external view returns (string)",
"function totalAssets() external view returns (uint256)",
"function totalSupply() external view returns (uint256)",
"function underlyingToken() external view returns (address)"
]);
// src/erc4626.service.ts
var _getTotalSupply_dec, _getTotalAssets_dec, _getUnderlyingToken_dec, _getName_dec, _getSymbol_dec, _getOwner_dec, _getMaxWithdraw_dec, _withdraw_dec, _deposit_dec, _init;
_deposit_dec = [Tool({
description: "Deposit an amount into the ERC4626 vault."
})], _withdraw_dec = [Tool({
//name: "kim_burn",
description: "Withdraw an amount into the ERC4626 vault."
})], _getMaxWithdraw_dec = [Tool({
description: "Get the max withdrawal amount of an ERC4626 vault"
})], _getOwner_dec = [Tool({
description: "Get the owner of an ERC4626 vault."
})], _getSymbol_dec = [Tool({
description: "Get the symbol of an ERC4626 vault."
})], _getName_dec = [Tool({
description: "Get the name of an ERC4626 vault."
})], _getUnderlyingToken_dec = [Tool({
description: "Get the underlying Token of an ERC4626 vault."
})], _getTotalAssets_dec = [Tool({
description: "Get the TotalAssets of an ERC4626 vault."
})], _getTotalSupply_dec = [Tool({
description: "Get the TotalSupply of an ERC4626 vault."
})];
var Erc4626Service = class {
constructor() {
__runInitializers(_init, 5, this);
}
async deposit(walletClient, parameters) {
try {
const hash = await walletClient.sendTransaction({
to: parameters.address,
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "deposit",
args: [parameters.assets, parameters.receiver]
});
return hash.hash;
} catch (error) {
throw new Error(`Failed to deposit into vault: ${error}`);
}
}
async withdraw(walletClient, parameters) {
try {
const hash = await walletClient.sendTransaction({
to: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "withdraw",
args: [parameters.assets, parameters.receiver, parameters.owner]
});
return hash.hash;
} catch (error) {
throw new Error(`Failed to withdraw from vault: ${error}`);
}
}
async getMaxWithdraw(walletClient, parameters) {
try {
const rawBalance = await walletClient.read({
address: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.address, //parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "maxWithdraw",
args: [parameters.address]
});
return String(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch max withdrawal: ${error}`);
}
}
async getOwner(walletClient, parameters) {
try {
const rawBalance = await walletClient.read({
address: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "owner",
args: []
});
return String(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch balance: ${error}`);
}
}
async getSymbol(walletClient, parameters) {
try {
console.log("parameters", parameters);
console.log("parameters.address", parameters.address);
const rawBalance = await walletClient.read({
address: parameters.address,
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "symbol",
args: []
});
return String(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch balance: ${error}`);
}
}
async getName(walletClient, parameters) {
try {
const rawBalance = await walletClient.read({
address: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "name",
args: []
});
return String(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch balance: ${error}`);
}
}
async getUnderlyingToken(walletClient, parameters) {
try {
const rawBalance = await walletClient.read({
address: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "underlyingToken",
args: []
});
return String(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch balance: ${error}`);
}
}
async getTotalAssets(walletClient, parameters) {
try {
const rawBalance = await walletClient.read({
address: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "totalAssets",
args: []
});
return Number(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch balance: ${error}`);
}
}
async getTotalSupply(walletClient, parameters) {
try {
const rawBalance = await walletClient.read({
address: "0x28c52E6c053AD4F2727E2F8de2AD5B81139ca9D4",
//parameters.poolAddress,
abi: ERC4626_ABI,
functionName: "totalSupply",
args: []
});
return Number(rawBalance.value);
} catch (error) {
throw Error(`Failed to fetch balance: ${error}`);
}
}
};
_init = __decoratorStart(null);
__decorateElement(_init, 1, "deposit", _deposit_dec, Erc4626Service);
__decorateElement(_init, 1, "withdraw", _withdraw_dec, Erc4626Service);
__decorateElement(_init, 1, "getMaxWithdraw", _getMaxWithdraw_dec, Erc4626Service);
__decorateElement(_init, 1, "getOwner", _getOwner_dec, Erc4626Service);
__decorateElement(_init, 1, "getSymbol", _getSymbol_dec, Erc4626Service);
__decorateElement(_init, 1, "getName", _getName_dec, Erc4626Service);
__decorateElement(_init, 1, "getUnderlyingToken", _getUnderlyingToken_dec, Erc4626Service);
__decorateElement(_init, 1, "getTotalAssets", _getTotalAssets_dec, Erc4626Service);
__decorateElement(_init, 1, "getTotalSupply", _getTotalSupply_dec, Erc4626Service);
__decoratorMetadata(_init, Erc4626Service);
// src/erc4626.plugin.ts
var ERC4626Plugin = class extends PluginBase {
constructor({ vaults }) {
super("erc4626", [new Erc4626Service()]);
this.supportsChain = (chain) => chain.type === "evm";
}
};
function erc4626({ vaults }) {
return new ERC4626Plugin({ vaults });
}
export {
ERC4626Plugin,
erc4626
};
//# sourceMappingURL=index.js.map