UNPKG

@syncswap/sdk

Version:

SyncSwap TypeScript SDK for building DeFi applications

83 lines 3.33 kB
import { CaseInsensitiveMap } from "../utils/caseInsensitiveMap.js"; import ContractLoader from "./contractLoader.js"; class ContractRegistry { static getNetworkName() { return this.network; } static setNetwork(network) { this.network = network; } static setProviderOrSigner(providerOrSigner) { this.providerOrSigner = providerOrSigner; } static getProviderOrSigner() { return this.providerOrSigner; } static resetContracts() { //console.log('[ContractRegistry] reset contracts'); this.contractByAddress = CaseInsensitiveMap.create(); this.contractByName = CaseInsensitiveMap.create(); this.addressByName = CaseInsensitiveMap.create(); } static async register(provider, network, type, name, address) { //console.log(`[ContractRegistry] Register contract ${name} to type ${type.name}.`); address = address.toLowerCase(); if (this.contractByAddress.has(address)) { //console.warn('[ContractRegistry] Found duplicate contract', type, name, address); return; } this.addressByName.set(name, address); this.addressByName.set(name.toLowerCase(), address); const contract = await ContractLoader.loadContract(provider, type, address); this.contractByAddress.set(address, { network: network, contract: contract, }); this.contractByName.set(name, { network: network, contract: contract, }); this.contractByName.set(name.toLowerCase(), { network: network, contract: contract, }); this.contractByName.set(type.name, { network: network, contract: contract, }); } static getAddressByName(name) { const address = this.addressByName.get(name.toLowerCase()); //if (!address) { // throw Error(`Contract ${name} not exists on getting address by name.`); //} // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return address; } static getContractByName(name) { const contract = this.contractByName.get(name.toLowerCase()); if (!contract || !this.providerOrSigner) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return undefined; //throw Error(`Contract ${name} not exists on getting contract by name.`); } return contract.contract.connect(this.providerOrSigner); } static hasContract(name) { return this.contractByName.get(name) !== undefined; } static getContractByAddress(signerOrProvider, address) { const contract = this.contractByAddress.get(address); if (!contract) { throw Error(`Contract ${address} not exists on getting contract by address.`); } return contract.contract.connect(signerOrProvider); } } ContractRegistry.contractByAddress = CaseInsensitiveMap.create(); ContractRegistry.contractByName = CaseInsensitiveMap.create(); ContractRegistry.addressByName = CaseInsensitiveMap.create(); ContractRegistry.network = ""; ContractRegistry.providerOrSigner = undefined; export default ContractRegistry; //# sourceMappingURL=contractRegistry.js.map