UNPKG

@syncswap/sdk

Version:

SyncSwap TypeScript SDK for building DeFi applications

70 lines 2.99 kB
import { CaseInsensitiveMap } from "../utils/caseInsensitiveMap.js"; import ContractLoader from "./contractLoader.js"; import { stateStore } from "../statestore/statestore.js"; class ContractRegistry { static resetContracts() { //console.log('[ContractRegistry] reset contracts'); this.contractByAddress = CaseInsensitiveMap.create(); this.contractByName = CaseInsensitiveMap.create(); this.addressByName = CaseInsensitiveMap.create(); } static async register(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(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 || !stateStore().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(stateStore().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(); export default ContractRegistry; //# sourceMappingURL=contractRegistry.js.map