@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
126 lines • 4.21 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var contractController_exports = {};
__export(contractController_exports, {
ContractFactory: () => ContractFactory,
clearContractCache: () => clearContractCache,
contractCacheForTesting: () => contractCacheForTesting,
getContractController: () => getContractController,
getContractInfo: () => getContractInfo
});
module.exports = __toCommonJS(contractController_exports);
var import_viem = require("viem");
var import_abi = require("../generated/abi");
var import_addresses = require("../generated/addresses");
var import_client = require("../core/client");
var import_chains = require("../config/chains");
const contractCache = /* @__PURE__ */ new Map();
const contractCacheForTesting = contractCache;
function createCacheKey(contract, chainId) {
return `${contract}:${chainId}`;
}
function getContractController(contract, client = (0, import_client.createClient)()) {
const chainId = client.chain?.id ?? import_chains.vanaMainnet.id;
const cacheKey = createCacheKey(contract, chainId);
let controller = contractCache.get(cacheKey);
if (!controller) {
controller = (0, import_viem.getContract)({
address: (0, import_addresses.getContractAddress)(chainId, contract),
abi: (0, import_abi.getAbi)(contract),
client
});
contractCache.set(cacheKey, controller);
}
return controller;
}
function getContractInfo(contract, chainId = import_chains.vanaMainnet.id) {
return {
address: (0, import_addresses.getContractAddress)(chainId, contract),
abi: (0, import_abi.getAbi)(contract)
};
}
class ContractFactory {
client;
chainId;
constructor(client) {
this.client = client;
try {
this.chainId = client.chain?.id ?? import_chains.vanaMainnet.id;
} catch {
this.chainId = import_chains.vanaMainnet.id;
}
}
/**
* Creates a typed contract instance
*
* @param contract - Contract name (use const assertion for full typing)
* @returns Fully typed contract instance
*/
create(contract) {
return getContractController(contract, this.client);
}
/**
* Gets contract information without creating an instance
*
* @param contract - Contract name
* @returns Contract information with typed ABI
*/
getInfo(contract) {
return getContractInfo(contract, this.chainId);
}
/**
* Lists all available contracts for the current chain
*
* @returns Array of contract names available on this chain
*/
getAvailableContracts() {
const chainAddresses = import_addresses.CONTRACT_ADDRESSES[this.chainId];
if (!chainAddresses) return [];
return Object.keys(chainAddresses);
}
}
function clearContractCache(contract, chainId) {
if (contract && chainId) {
const cacheKey = createCacheKey(contract, chainId);
contractCache.delete(cacheKey);
} else if (contract) {
for (const key of contractCache.keys()) {
if (key.startsWith(`${contract}:`)) {
contractCache.delete(key);
}
}
} else if (chainId) {
for (const key of contractCache.keys()) {
if (key.endsWith(`:${chainId}`)) {
contractCache.delete(key);
}
}
} else {
contractCache.clear();
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ContractFactory,
clearContractCache,
contractCacheForTesting,
getContractController,
getContractInfo
});
//# sourceMappingURL=contractController.cjs.map