@agentek/tools
Version:
Blockchain tools for AI agents
57 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCaip10 = parseCaip10;
exports.getGovernorBySlug = getGovernorBySlug;
const constants_js_1 = require("./constants.js");
const viem_1 = require("viem");
function parseCaip10(caip10) {
const parts = caip10.split(":");
if (parts.length !== 3) {
throw new Error("Invalid CAIP‑10 format");
}
const [namespace, chainRef, accountAddress] = parts;
if (namespace !== "eip155") {
throw new Error("Only eip155 is supported in this app");
}
const chainId = Number(chainRef);
if (isNaN(chainId)) {
throw new Error("Chain reference is not a valid number");
}
if (!accountAddress) {
throw new Error("Missing address in CAIP‑10 format");
}
const address = (0, viem_1.getAddress)(accountAddress);
return { namespace, chainId, address };
}
async function getGovernorBySlug(slug, tallyApiKey) {
// Handle arbitrum slug special case
if (slug === "arbitrum") {
slug = "arbitrum-core";
}
const getGovernorQuery = `
query GetGovernorBySlug($slug: String!) {
governor(input: { slug: $slug }) {
id
name
type
}
}
`;
const governorResponse = await fetch(constants_js_1.TALLY_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Api-Key": tallyApiKey,
},
body: JSON.stringify({
query: getGovernorQuery,
variables: { slug },
}),
});
const governorResult = await governorResponse.json();
if (governorResult.errors || !governorResult.data?.governor?.id) {
throw new Error("DAO not supported");
}
return governorResult.data.governor;
}
//# sourceMappingURL=utils.js.map