UNPKG

@syncswap/sdk

Version:

SyncSwap TypeScript SDK for building DeFi applications

88 lines 3.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const caseInsensitiveMap_1 = require("../utils/caseInsensitiveMap"); const contractLoader_1 = __importDefault(require("./contractLoader")); 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_1.CaseInsensitiveMap.create(); this.contractByName = caseInsensitiveMap_1.CaseInsensitiveMap.create(); this.addressByName = caseInsensitiveMap_1.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_1.default.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_1.CaseInsensitiveMap.create(); ContractRegistry.contractByName = caseInsensitiveMap_1.CaseInsensitiveMap.create(); ContractRegistry.addressByName = caseInsensitiveMap_1.CaseInsensitiveMap.create(); ContractRegistry.network = ""; ContractRegistry.providerOrSigner = undefined; exports.default = ContractRegistry; //# sourceMappingURL=contractRegistry.js.map