|
| 1 | +import { HardhatRuntimeEnvironment } from "hardhat/types"; |
| 2 | +import { DeployFunction } from "hardhat-deploy/types"; |
| 3 | +import { BigNumber, BigNumberish } from "ethers"; |
| 4 | +import { getContractAddress } from "./utils/getContractAddress"; |
| 5 | +import { deployUpgradable } from "./utils/deployUpgradable"; |
| 6 | +import { HomeChains, isSkipped } from "./utils"; |
| 7 | +import { deployERC20AndFaucet } from "./utils/deployERC20AndFaucet"; |
| 8 | +import { DisputeKitClassic, KlerosCore } from "../typechain-types"; |
| 9 | +import { getContractOrDeployUpgradable } from "./utils/getContractOrDeploy"; |
| 10 | + |
| 11 | +const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => { |
| 12 | + const { ethers, deployments, getNamedAccounts, getChainId } = hre; |
| 13 | + const { deploy } = deployments; |
| 14 | + const { AddressZero } = hre.ethers.constants; |
| 15 | + |
| 16 | + // fallback to hardhat node signers on local network |
| 17 | + const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address; |
| 18 | + const chainId = Number(await getChainId()); |
| 19 | + console.log("deploying to %s with deployer %s", HomeChains[chainId], deployer); |
| 20 | + |
| 21 | + const pnk = await deployERC20AndFaucet(hre, deployer, "PNK"); |
| 22 | + const dai = await deployERC20AndFaucet(hre, deployer, "DAI"); |
| 23 | + const weth = await deployERC20AndFaucet(hre, deployer, "WETH"); |
| 24 | + |
| 25 | + const disputeKit = await deployUpgradable(deployments, "DisputeKitClassicUniversity", { |
| 26 | + from: deployer, |
| 27 | + contract: "DisputeKitClassic", |
| 28 | + args: [deployer, AddressZero], |
| 29 | + log: true, |
| 30 | + }); |
| 31 | + |
| 32 | + let klerosCoreAddress = await deployments.getOrNull("KlerosCoreUniversity").then((deployment) => deployment?.address); |
| 33 | + if (!klerosCoreAddress) { |
| 34 | + const nonce = await ethers.provider.getTransactionCount(deployer); |
| 35 | + klerosCoreAddress = getContractAddress(deployer, nonce + 3); // deployed on the 4th tx (nonce+3): SortitionModule Impl tx, SortitionModule Proxy tx, KlerosCore Impl tx, KlerosCore Proxy tx |
| 36 | + console.log("calculated future KlerosCoreUniversity address for nonce %d: %s", nonce + 3, klerosCoreAddress); |
| 37 | + } |
| 38 | + const sortitionModule = await deployUpgradable(deployments, "SortitionModuleUniversity", { |
| 39 | + from: deployer, |
| 40 | + args: [deployer, klerosCoreAddress], |
| 41 | + log: true, |
| 42 | + }); // nonce (implementation), nonce+1 (proxy) |
| 43 | + |
| 44 | + const minStake = BigNumber.from(10).pow(20).mul(2); |
| 45 | + const alpha = 10000; |
| 46 | + const feeForJuror = BigNumber.from(10).pow(17); |
| 47 | + const klerosCore = await deployUpgradable(deployments, "KlerosCoreUniversity", { |
| 48 | + from: deployer, |
| 49 | + args: [ |
| 50 | + deployer, // governor |
| 51 | + deployer, // instructor |
| 52 | + pnk.address, |
| 53 | + AddressZero, |
| 54 | + disputeKit.address, |
| 55 | + false, |
| 56 | + [minStake, alpha, feeForJuror, 256], // minStake, alpha, feeForJuror, jurorsForCourtJump |
| 57 | + [0, 0, 0, 10], // evidencePeriod, commitPeriod, votePeriod, appealPeriod |
| 58 | + sortitionModule.address, |
| 59 | + ], |
| 60 | + log: true, |
| 61 | + }); // nonce+2 (implementation), nonce+3 (proxy) |
| 62 | + |
| 63 | + // changeCore() only if necessary |
| 64 | + const disputeKitContract = (await ethers.getContract("DisputeKitClassicUniversity")) as DisputeKitClassic; |
| 65 | + const currentCore = await disputeKitContract.core(); |
| 66 | + if (currentCore !== klerosCore.address) { |
| 67 | + console.log("changing DisputeKitClassicUniversity.core to %s", klerosCore.address); |
| 68 | + await disputeKitContract.changeCore(klerosCore.address); |
| 69 | + } |
| 70 | + |
| 71 | + const changeCurrencyRate = async ( |
| 72 | + erc20: string, |
| 73 | + accepted: boolean, |
| 74 | + rateInEth: BigNumberish, |
| 75 | + rateDecimals: BigNumberish |
| 76 | + ) => { |
| 77 | + const core = (await ethers.getContract("KlerosCoreUniversity")) as KlerosCore; |
| 78 | + const pnkRate = await core.currencyRates(erc20); |
| 79 | + if (pnkRate.feePaymentAccepted !== accepted) { |
| 80 | + console.log(`core.changeAcceptedFeeTokens(${erc20}, ${accepted})`); |
| 81 | + await core.changeAcceptedFeeTokens(erc20, accepted); |
| 82 | + } |
| 83 | + if (!pnkRate.rateInEth.eq(rateInEth) || pnkRate.rateDecimals !== rateDecimals) { |
| 84 | + console.log(`core.changeCurrencyRates(${erc20}, ${rateInEth}, ${rateDecimals})`); |
| 85 | + await core.changeCurrencyRates(erc20, rateInEth, rateDecimals); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + try { |
| 90 | + await changeCurrencyRate(pnk.address, true, 12225583, 12); |
| 91 | + await changeCurrencyRate(dai.address, true, 60327783, 11); |
| 92 | + await changeCurrencyRate(weth.address, true, 1, 1); |
| 93 | + } catch (e) { |
| 94 | + console.error("failed to change currency rates:", e); |
| 95 | + } |
| 96 | + |
| 97 | + const disputeTemplateRegistry = await getContractOrDeployUpgradable(hre, "DisputeTemplateRegistry", { |
| 98 | + from: deployer, |
| 99 | + args: [deployer], |
| 100 | + log: true, |
| 101 | + }); |
| 102 | + |
| 103 | + await deploy("DisputeResolverUniversity", { |
| 104 | + from: deployer, |
| 105 | + contract: "DisputeResolver", |
| 106 | + args: [klerosCore.address, disputeTemplateRegistry.address], |
| 107 | + log: true, |
| 108 | + }); |
| 109 | +}; |
| 110 | + |
| 111 | +deployArbitration.tags = ["ArbitrationUniversity"]; |
| 112 | +deployArbitration.skip = async ({ network }) => { |
| 113 | + return isSkipped(network, !HomeChains[network.config.chainId ?? 0]); |
| 114 | +}; |
| 115 | + |
| 116 | +export default deployArbitration; |
0 commit comments