UNPKG

@celo/contractkit

Version:

Celo's ContractKit to interact with Celo network

202 lines 9.53 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Web3ContractCache = exports.ContractFactories = void 0; const Accounts_1 = require("@celo/abis/web3/Accounts"); const Attestations_1 = require("@celo/abis/web3/Attestations"); const CeloUnreleasedTreasury_1 = require("@celo/abis/web3/CeloUnreleasedTreasury"); const Election_1 = require("@celo/abis/web3/Election"); const EpochManager_1 = require("@celo/abis/web3/EpochManager"); const EpochManagerEnabler_1 = require("@celo/abis/web3/EpochManagerEnabler"); const EpochRewards_1 = require("@celo/abis/web3/EpochRewards"); const Escrow_1 = require("@celo/abis/web3/Escrow"); const FederatedAttestations_1 = require("@celo/abis/web3/FederatedAttestations"); const FeeCurrencyDirectory_1 = require("@celo/abis/web3/FeeCurrencyDirectory"); const FeeHandler_1 = require("@celo/abis/web3/FeeHandler"); const Freezer_1 = require("@celo/abis/web3/Freezer"); const GoldToken_1 = require("@celo/abis/web3/GoldToken"); const Governance_1 = require("@celo/abis/web3/Governance"); const GovernanceSlasher_1 = require("@celo/abis/web3/GovernanceSlasher"); const IERC20_1 = require("@celo/abis/web3/IERC20"); const LockedGold_1 = require("@celo/abis/web3/LockedGold"); const Reserve_1 = require("@celo/abis/web3/mento/Reserve"); const StableToken_1 = require("@celo/abis/web3/mento/StableToken"); const MentoFeeHandlerSeller_1 = require("@celo/abis/web3/MentoFeeHandlerSeller"); const MultiSig_1 = require("@celo/abis/web3/MultiSig"); const OdisPayments_1 = require("@celo/abis/web3/OdisPayments"); const Proxy_1 = require("@celo/abis/web3/Proxy"); const Registry_1 = require("@celo/abis/web3/Registry"); const ScoreManager_1 = require("@celo/abis/web3/ScoreManager"); const SortedOracles_1 = require("@celo/abis/web3/SortedOracles"); const UniswapFeeHandlerSeller_1 = require("@celo/abis/web3/UniswapFeeHandlerSeller"); const Validators_1 = require("@celo/abis/web3/Validators"); const debug_1 = __importDefault(require("debug")); const base_1 = require("./base"); const celo_tokens_1 = require("./celo-tokens"); const debug = (0, debug_1.default)('kit:web3-contract-cache'); exports.ContractFactories = { [base_1.CeloContract.Accounts]: Accounts_1.newAccounts, [base_1.CeloContract.Attestations]: Attestations_1.newAttestations, [base_1.CeloContract.CeloUnreleasedTreasury]: CeloUnreleasedTreasury_1.newCeloUnreleasedTreasury, [base_1.CeloContract.Election]: Election_1.newElection, [base_1.CeloContract.EpochManager]: EpochManager_1.newEpochManager, [base_1.CeloContract.EpochManagerEnabler]: EpochManagerEnabler_1.newEpochManagerEnabler, [base_1.CeloContract.EpochRewards]: EpochRewards_1.newEpochRewards, [base_1.CeloContract.ERC20]: IERC20_1.newIERC20, [base_1.CeloContract.Escrow]: Escrow_1.newEscrow, [base_1.CeloContract.FederatedAttestations]: FederatedAttestations_1.newFederatedAttestations, [base_1.CeloContract.FeeCurrencyDirectory]: FeeCurrencyDirectory_1.newFeeCurrencyDirectory, [base_1.CeloContract.Freezer]: Freezer_1.newFreezer, [base_1.CeloContract.FeeHandler]: FeeHandler_1.newFeeHandler, [base_1.CeloContract.MentoFeeHandlerSeller]: MentoFeeHandlerSeller_1.newMentoFeeHandlerSeller, [base_1.CeloContract.UniswapFeeHandlerSeller]: UniswapFeeHandlerSeller_1.newUniswapFeeHandlerSeller, [base_1.CeloContract.CeloToken]: GoldToken_1.newGoldToken, [base_1.CeloContract.GoldToken]: GoldToken_1.newGoldToken, [base_1.CeloContract.Governance]: Governance_1.newGovernance, [base_1.CeloContract.GovernanceSlasher]: GovernanceSlasher_1.newGovernanceSlasher, [base_1.CeloContract.LockedCelo]: LockedGold_1.newLockedGold, [base_1.CeloContract.LockedGold]: LockedGold_1.newLockedGold, [base_1.CeloContract.MultiSig]: MultiSig_1.newMultiSig, [base_1.CeloContract.OdisPayments]: OdisPayments_1.newOdisPayments, [base_1.CeloContract.Registry]: Registry_1.newRegistry, [base_1.CeloContract.Reserve]: Reserve_1.newReserve, [base_1.CeloContract.ScoreManager]: ScoreManager_1.newScoreManager, [base_1.CeloContract.SortedOracles]: SortedOracles_1.newSortedOracles, [base_1.CeloContract.StableToken]: StableToken_1.newStableToken, [base_1.CeloContract.StableTokenEUR]: StableToken_1.newStableToken, [base_1.CeloContract.StableTokenBRL]: StableToken_1.newStableToken, [base_1.CeloContract.Validators]: Validators_1.newValidators, }; const StableToContract = { [celo_tokens_1.StableToken.cEUR]: base_1.CeloContract.StableTokenEUR, [celo_tokens_1.StableToken.cUSD]: base_1.CeloContract.StableToken, [celo_tokens_1.StableToken.cREAL]: base_1.CeloContract.StableTokenBRL, }; /** * Native Web3 contracts factory and cache. * * Exposes accessors to all `CeloContract` web3 contracts. * * Mostly a private cache, kit users would normally use * a contract wrapper */ class Web3ContractCache { /** core contract's address registry */ constructor(registry) { this.registry = registry; this.cacheMap = {}; } getAccounts() { return this.getContract(base_1.CeloContract.Accounts); } getAttestations() { return this.getContract(base_1.CeloContract.Attestations); } getCeloUnreleasedTreasury() { return this.getContract(base_1.CeloContract.CeloUnreleasedTreasury); } getElection() { return this.getContract(base_1.CeloContract.Election); } getEpochManager() { return this.getContract(base_1.CeloContract.EpochManager); } getEpochManagerEnabler() { return this.getContract(base_1.CeloContract.EpochManagerEnabler); } getEpochRewards() { return this.getContract(base_1.CeloContract.EpochRewards); } getErc20(address) { return this.getContract(base_1.CeloContract.ERC20, address); } getEscrow() { return this.getContract(base_1.CeloContract.Escrow); } getFederatedAttestations() { return this.getContract(base_1.CeloContract.FederatedAttestations); } getFreezer() { return this.getContract(base_1.CeloContract.Freezer); } getFeeHandler() { return this.getContract(base_1.CeloContract.FeeHandler); } /* @deprecated use getLockedCelo */ getGoldToken() { return this.getContract(base_1.CeloContract.CeloToken); } getCeloToken() { return this.getContract(base_1.CeloContract.CeloToken); } getGovernance() { return this.getContract(base_1.CeloContract.Governance); } /* @deprecated use getLockedCelo */ getLockedGold() { return this.getContract(base_1.CeloContract.LockedGold); } getLockedCelo() { return this.getContract(base_1.CeloContract.LockedCelo); } getMultiSig(address) { return this.getContract(base_1.CeloContract.MultiSig, address); } getOdisPayments() { return this.getContract(base_1.CeloContract.OdisPayments); } getRegistry() { return this.getContract(base_1.CeloContract.Registry); } getReserve() { return this.getContract(base_1.CeloContract.Reserve); } getScoreManager() { return this.getContract(base_1.CeloContract.ScoreManager); } getSortedOracles() { return this.getContract(base_1.CeloContract.SortedOracles); } getStableToken(stableToken = celo_tokens_1.StableToken.cUSD) { return this.getContract(StableToContract[stableToken]); } getValidators() { return this.getContract(base_1.CeloContract.Validators); } /** * Get native web3 contract wrapper */ getContract(contract, address) { return __awaiter(this, void 0, void 0, function* () { if (this.cacheMap[contract] == null || address !== undefined) { // core contract in the registry if (!address) { address = yield this.registry.addressFor(contract); } debug('Initiating contract %s', contract); debug('is it included?', base_1.ProxyContracts.includes(contract)); debug('is it included?', base_1.ProxyContracts.toString()); const createFn = base_1.ProxyContracts.includes(contract) ? Proxy_1.newProxy : exports.ContractFactories[contract]; this.cacheMap[contract] = createFn(this.registry.connection.web3, address); } // we know it's defined (thus the !) return this.cacheMap[contract]; }); } invalidateContract(contract) { this.cacheMap[contract] = undefined; } } exports.Web3ContractCache = Web3ContractCache; //# sourceMappingURL=web3-contract-cache.js.map