@bitte-ai/agent-sdk
Version:
Agent SDK for Bitte Protocol
98 lines (97 loc) • 4 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClientForChain = getClientForChain;
exports.getAlchemyClient = getAlchemyClient;
const viem_1 = require("viem");
const chains = __importStar(require("viem/chains"));
const CHAINS_BY_CHAIN_ID = Object.fromEntries(Object.values(chains).map((chain) => [chain.id, chain]));
const getChainById = (chainId) => {
return CHAINS_BY_CHAIN_ID[chainId];
};
function getClientForChain(chainId, alchemyKey) {
if (alchemyKey) {
const alchemyClient = getAlchemyClient(chainId, alchemyKey);
if (alchemyClient) {
return alchemyClient;
}
}
const chain = getChainById(chainId);
if (!chain) {
throw new Error(`Chain with ID ${chainId} not found`);
}
return (0, viem_1.createPublicClient)({
chain,
transport: (0, viem_1.http)(chain.rpcUrls.default.http[0]),
});
}
// Alchemy RPC endpoints for different chains
const ALCHEMY_RPC_ENDPOINTS = {
1: "https://eth-mainnet.g.alchemy.com/v2",
10: "https://opt-mainnet.g.alchemy.com/v2",
56: "https://bsc-mainnet.g.alchemy.com/v2",
100: "https://gnosis-mainnet.g.alchemy.com/v2",
130: "https://unichain-mainnet.g.alchemy.com/v2",
137: "https://polygon-mainnet.g.alchemy.com/v2",
146: "https://sonic-mainnet.g.alchemy.com/v2",
232: "https://lens-mainnet.g.alchemy.com/v2",
250: "https://fantom-mainnet.g.alchemy.com/v2",
252: "https://frax-mainnet.g.alchemy.com/v2",
592: "https://astar-mainnet.g.alchemy.com/v2",
1868: "https://soneium-mainnet.g.alchemy.com/v2",
8453: "https://base-mainnet.g.alchemy.com/v2",
42161: "https://arb-mainnet.g.alchemy.com/v2",
42220: "https://celo-mainnet.g.alchemy.com/v2",
43114: "https://avax-mainnet.g.alchemy.com/v2",
59144: "https://linea-mainnet.g.alchemy.com/v2",
80094: "https://berachain-mainnet.g.alchemy.com/v2",
81457: "https://blast-mainnet.g.alchemy.com/v2",
7777777: "https://zora-mainnet.g.alchemy.com/v2",
// Testnets
10200: "https://gnosis-chiado.g.alchemy.com/v2/",
84532: "https://base-sepolia.g.alchemy.com/v2/",
11155111: "https://eth-sepolia.g.alchemy.com/v2/",
11155420: "https://opt-sepolia.g.alchemy.com/v2/",
};
function getAlchemyClient(chainId, alchemyKey) {
const alchemyRpcBase = ALCHEMY_RPC_ENDPOINTS[chainId];
if (alchemyRpcBase) {
return (0, viem_1.createPublicClient)({
chain: getChainById(chainId),
transport: (0, viem_1.http)(`${alchemyRpcBase}/${alchemyKey}`),
});
}
console.warn("No Alchemy Base URL available");
}