@bitte-ai/agent-sdk
Version:
Agent SDK for Bitte Protocol
62 lines (61 loc) • 2.01 kB
JavaScript
import { encodeFunctionData, getAddress, parseAbi, parseEther, toHex, } from "viem";
import { CHAIN_INFO } from "./constants";
import { getChainById } from "./chain";
export 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: parseEther(amount.toString()),
nativeAsset: getNativeAsset(chainId),
};
}
export const unwrapMetaTransaction = (chainId, amount) => {
return {
to: getNativeAsset(chainId).address,
value: "0x0",
data: encodeFunctionData({
abi: parseAbi(["function withdraw(uint wad)"]),
functionName: "withdraw",
args: [amount],
}),
};
};
export const wrapMetaTransaction = (chainId, amount) => {
return {
to: getNativeAsset(chainId).address,
value: toHex(amount),
// methodId for weth.deposit
data: "0xd0e30db0",
};
};
export function getNativeAsset(chainId) {
const wethAddress = CHAIN_INFO[chainId];
if (!wethAddress) {
throw new Error(`Couldn't find wrapped address for chainId=${chainId}`);
}
const chain = getChainById(chainId);
return {
address: getAddress(wethAddress),
symbol: chain.nativeCurrency.symbol,
scanUrl: `${chain.blockExplorers?.default.url}/address/${wethAddress}`,
decimals: chain.nativeCurrency.decimals,
};
}