@bitte-ai/agent-sdk
Version:
Agent SDK for Bitte Protocol
69 lines (68 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapMetaTransaction = exports.unwrapMetaTransaction = void 0;
exports.validateWethInput = validateWethInput;
exports.getNativeAsset = getNativeAsset;
const viem_1 = require("viem");
const constants_1 = require("./constants");
const chain_1 = require("./chain");
function validateWethInput(params) {
const chainIdStr = params.get("chainId");
const amountStr = params.get("amount");
// Ensure required fields
if (!chainIdStr) {
throw new Error("Missing required parameter: chainId");
}
if (!amountStr) {
throw new Error("Missing required parameter: amount");
}
// Validate chainId
const chainId = parseInt(chainIdStr);
if (isNaN(chainId)) {
throw new Error("Invalid chainId, must be a number");
}
// Validate amount
const amount = parseFloat(amountStr);
if (isNaN(amount) || amount <= 0) {
throw new Error("Invalid amount, must be a positive float");
}
return {
chainId,
amount: (0, viem_1.parseEther)(amount.toString()),
nativeAsset: getNativeAsset(chainId),
};
}
const unwrapMetaTransaction = (chainId, amount) => {
return {
to: getNativeAsset(chainId).address,
value: "0x0",
data: (0, viem_1.encodeFunctionData)({
abi: (0, viem_1.parseAbi)(["function withdraw(uint wad)"]),
functionName: "withdraw",
args: [amount],
}),
};
};
exports.unwrapMetaTransaction = unwrapMetaTransaction;
const wrapMetaTransaction = (chainId, amount) => {
return {
to: getNativeAsset(chainId).address,
value: (0, viem_1.toHex)(amount),
// methodId for weth.deposit
data: "0xd0e30db0",
};
};
exports.wrapMetaTransaction = wrapMetaTransaction;
function getNativeAsset(chainId) {
const wethAddress = constants_1.CHAIN_INFO[chainId];
if (!wethAddress) {
throw new Error(`Couldn't find wrapped address for chainId=${chainId}`);
}
const chain = (0, chain_1.getChainById)(chainId);
return {
address: (0, viem_1.getAddress)(wethAddress),
symbol: chain.nativeCurrency.symbol,
scanUrl: `${chain.blockExplorers?.default.url}/address/${wethAddress}`,
decimals: chain.nativeCurrency.decimals,
};
}