import {ethers} from "ethers" import contracts from "./contracts" import distributor from "../artifacts/contracts/MerkleDistributor.sol/MerkleDistributor.json" const MERKLE_DISTIBUTOR_CONTRACT = 'merkle_distributor' declare global { interface Window { ethereum: ethers.providers.ExternalProvider | ethers.providers.JsonRpcFetchFunc | undefined; } } const abi = () => { return distributor.abi } const bytecode = () => { return distributor.bytecode } const deployedAddress = (network : string | number) => { if (!contracts[`${network}`]) return null return contracts[`${network}`][MERKLE_DISTIBUTOR_CONTRACT] } const getWeb3Provider = (provider = null) : Promise => { if (provider) return Promise.resolve(provider) if (window.ethereum) return Promise.resolve(new ethers.providers.Web3Provider(window.ethereum, "any")) return Promise.reject('no web3 provider defined') } const controllerAddress = (contractKey = MERKLE_DISTIBUTOR_CONTRACT, web3Provider = null) => { return getWeb3Provider(web3Provider) .then(provider => { if ('_isSigner' in provider) return provider.provider.getNetwork() return provider.getNetwork() }) .then(({ chainId }) => { return contractKey.length == 42 ? contractKey : contracts[`${chainId}`][contractKey] }) } const initContract = (contractKey : string, readOnly = true, web3Provider = null) => { return getWeb3Provider(web3Provider) .then(provider => { const signer = readOnly ? provider : ('_isSigner' in provider ? provider : provider.getSigner()) return controllerAddress(contractKey, web3Provider) .then(contract_address => new ethers.Contract(contract_address, distributor.abi, signer)) }) } const owner = (web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.owner()) } const token = (web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.token()) } const tokenId = (web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.tokenId()) } const claimInterface = (web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.claimInterface()) } const ipfsCid = (web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.ipfsCid()) } const currentRound = (web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.currentRound()) } const adminSetNewRound = (newRound : number, newMerkleRoot : string, ipfsCid : string, web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, false, web3Provider) .then(contract => contract.adminSetNewRound(newRound, newMerkleRoot, ipfsCid)) } const isClaimed = (index : number, web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, true, web3Provider) .then(contract => contract.isClaimed(index)) } const claim = (index : number, account : string, amount : number, merkleProof : string, web3Provider = null, contractKey = MERKLE_DISTIBUTOR_CONTRACT) => { return initContract(contractKey, false, web3Provider) .then(contract => contract.claim(index, account, amount, merkleProof)) } export { abi, bytecode, deployedAddress, owner, token, tokenId, claimInterface, ipfsCid, currentRound, adminSetNewRound, isClaimed, claim }