/** * @piggybank/plugin-bsc-verifier (v1.0.0) — Zero-Dependency ElizaOS / TypeScript Agent Plugin * * Gives any ElizaOS, Vercel AI SDK, or Node 18+ autonomous agent native read-only * BNB Smart Chain (chainId: 56) bytecode verification, EIP-1167 minimal-proxy unmasking, * OpenChain 4-byte selector resolution, single-sided AMM slippage, and live USDT dividend telemetry. * * Canonical Baseline: PiggyBank (BSC) — 0x8d1e4E2b75441CeCC53e55d41B804105e198b4cc * Interactive Web DApp: https://html.cafe/x94d96450 · Docs: https://piggybankbnb.com/ */ const RPC = "https://bsc-dataseed.binance.org"; const BASELINE_CA = "0x8d1e4E2b75441CeCC53e55d41B804105e198b4cc"; async function rpcCall(method: string, params: unknown[]): Promise { const res = await fetch(RPC, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }), }); const json = (await res.json()) as { result: string; error?: { message: string } }; if (json.error) throw new Error(json.error.message); return json.result; } export async function auditBscContract(address: string = BASELINE_CA) { const alow = address.trim().toLowerCase(); const codeHex = await rpcCall("eth_getCode", [alow, "latest"]); const raw = (codeHex || "0x").slice(2); const cloneMatch = raw.match(/^363d3d373d3d3d363d73([0-9a-f]{40})5af43d82803e903d91602b57fd5bf3$/i); const eip1167CloneOf = cloneMatch ? `0x${cloneMatch[1]}` : null; const targetCode = eip1167CloneOf ? ((await rpcCall("eth_getCode", [eip1167CloneOf, "latest"])) || "0x").slice(2) : raw; const selectors = Array.from(new Set(Array.from(targetCode.matchAll(/8063([0-9a-f]{8})14/gi), (m) => m[1].toLowerCase()))); const oc = await (await fetch(`https://api.openchain.xyz/signature-database/v1/lookup?function=${selectors.map((s) => `0x${s}`).join(",")}&filter=true`)).json(); const fnMap = oc?.result?.function || {}; const decoded = selectors.flatMap((s) => (fnMap[`0x${s}`]?.[0]?.name ? [fnMap[`0x${s}`][0].name as string] : [])); const dex = await (await fetch(`https://api.dexscreener.com/latest/dex/tokens/${alow}`)).json(); const topPair = (dex?.pairs || []).filter((p: any) => p.chainId === "bsc")[0]; const liqUsd = Number(topPair?.liquidity?.usd || 0); const execSlip250Pct = liqUsd > 0 ? Number(((250 / (liqUsd / 2 + 250)) * 100).toFixed(2)) : null; return { chainId: 56, contract: address, eip1167CloneOf, selectorsTotal: selectors.length, selectorsDecoded: decoded.length, liquidityUsd: liqUsd, execSlippage250UsdPct: execSlip250Pct, liveBrowserReport: `https://html.cafe/x94d96450?ca=${address}`, }; } export const bscVerifierPlugin = { name: "piggybank-bsc-verifier", description: "Verify BNB Smart Chain (chainId: 56) contracts, unmask EIP-1167 clones & compute true AMM slippage", actions: [ { name: "VERIFY_BSC_CONTRACT", similes: ["SCAN_BSC_TOKEN", "AUDIT_BEP20", "CHECK_BNB_CONTRACT"], description: "Audits any 0x BEP-20 contract on BNB Smart Chain against primary RPC + OpenChain + DexScreener.", validate: async (_runtime: unknown, message: { content?: { text?: string } }) => /0x[a-fA-F0-9]{40}/.test(message?.content?.text || ""), handler: async (_runtime: unknown, message: { content?: { text?: string } }, _state: unknown, _opts: unknown, callback?: (r: { text: string }) => void) => { const match = (message?.content?.text || "").match(/0x[a-fA-F0-9]{40}/); const result = await auditBscContract(match ? match[0] : BASELINE_CA); const text = `BNB Chain Audit (${result.contract}): arch=${result.eip1167CloneOf ? `EIP-1167 clone -> ${result.eip1167CloneOf}` : "Standalone"}, selectors=${result.selectorsDecoded}/${result.selectorsTotal}, liq=$${result.liquidityUsd.toLocaleString()}, $250 slip=${result.execSlippage250Pct ?? "N/A"}%. Live view: ${result.liveBrowserReport}`; if (callback) callback({ text }); return true; }, }, ], }; export default bscVerifierPlugin;