Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainlink RNG #1778

Merged
merged 14 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions contracts/deploy/00-chainlink-rng.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { HomeChains, isSkipped } from "./utils";
import { getContractOrDeploy } from "./utils/getContractOrDeploy";

const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { deployments, getNamedAccounts, getChainId } = hre;
const { deploy } = deployments;

// fallback to hardhat node signers on local network
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
const chainId = Number(await getChainId()) as unknown as HomeChains; // Checked at runtime by skip()
console.log("deploying to %s with deployer %s", HomeChains[chainId], deployer);

const KEY_HASHES = {
// https://docs.chain.link/vrf/v2-5/supported-networks#arbitrum-mainnet
[HomeChains.ARBITRUM_ONE]: {
2: "0x9e9e46732b32662b9adc6f3abdf6c5e926a666d174a4d6b8e39c4cca76a38897",
30: "0x8472ba59cf7134dfe321f4d61a430c4857e8b19cdd5230b09952a92671c24409",
150: "0xe9f223d7d83ec85c4f78042a4845af3a1c8df7757b4997b815ce4b8d07aca68c",
},
// https://docs.chain.link/vrf/v2-5/supported-networks#arbitrum-sepolia-testnet
[HomeChains.ARBITRUM_SEPOLIA]: {
150: "0x1770bdc7eec7771f7ba4ffd640f34260d7f095b79c92d34a5b2551d6f6cfd2be",
},
[HomeChains.HARDHAT]: {
0: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
};

const SUBSCRIPTION_ID = {
[HomeChains.ARBITRUM_ONE]: "66240499937595191069677958665918759554657443303079118766000192000140992834352",
[HomeChains.ARBITRUM_SEPOLIA]: "38502597312983100069991953687934627561654236680431968938019951490339399569548",
[HomeChains.HARDHAT]: "0x0000000000000000000000000000000000000000000000000000000000000001",
};

function getKeyHash({ gasPrice }: { gasPrice: keyof (typeof KEY_HASHES)[HomeChains.ARBITRUM_ONE] }): string {
if (chainId == HomeChains.HARDHAT) return KEY_HASHES[chainId][0];
if (chainId == HomeChains.ARBITRUM_ONE) return KEY_HASHES[chainId][gasPrice];
if (chainId == HomeChains.ARBITRUM_SEPOLIA) return KEY_HASHES[chainId][150];
throw new Error(`Unknown chainId ${chainId}`);
}
jaybuidl marked this conversation as resolved.
Show resolved Hide resolved

const ChainlinkVRFCoordinator = await getContractOrDeploy(hre, "ChainlinkVRFCoordinator", {
from: deployer,
contract: "ChainlinkVRFCoordinatorV2Mock",
args: [],
log: true,
});

const keyHash = getKeyHash({ gasPrice: 150 });
const subscriptionId = SUBSCRIPTION_ID[chainId];
const requestConfirmations = 200; // between 1 and 200 L2 blocks
const callbackGasLimit = 100000;
jaybuidl marked this conversation as resolved.
Show resolved Hide resolved

await deploy("ChainlinkRNG", {
from: deployer,
args: [
deployer,
deployer, // For testing only, it should be the SortitionModule
ChainlinkVRFCoordinator.target,
keyHash,
subscriptionId,
requestConfirmations,
callbackGasLimit,
],
log: true,
});
};

deployArbitration.tags = ["ChainlinkRNG"];
deployArbitration.skip = async ({ network }) => {
return isSkipped(network, !HomeChains[network.config.chainId ?? 0]);
};

export default deployArbitration;
18 changes: 13 additions & 5 deletions contracts/deploy/00-home-chain-arbitration-neo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { changeCurrencyRate } from "./utils/klerosCoreHelper";
import { HomeChains, isSkipped, isDevnet, PNK, ETH } from "./utils";
import { getContractOrDeploy, getContractOrDeployUpgradable } from "./utils/getContractOrDeploy";
import { deployERC20AndFaucet, deployERC721 } from "./utils/deployTokens";
import { DisputeKitClassic, KlerosCoreNeo } from "../typechain-types";
import { DisputeKitClassic, KlerosCoreNeo, RandomizerRNG } from "../typechain-types";

const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { ethers, deployments, getNamedAccounts, getChainId } = hre;
Expand Down Expand Up @@ -36,9 +36,9 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
});

const rng = await deployUpgradable(deployments, "RandomizerRNG", {
const rng = await deploy("RandomizerRNG", {
from: deployer,
args: [randomizerOracle.target, deployer],
args: [deployer, ZeroAddress, randomizerOracle.target], // The SortitionModule is configured later
log: true,
});

Expand Down Expand Up @@ -85,7 +85,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
deployer,
deployer,
pnk.target,
ZeroAddress,
ZeroAddress, // KlerosCore is configured later
disputeKit.address,
false,
[minStake, alpha, feeForJuror, jurorsForCourtJump],
Expand All @@ -97,14 +97,22 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
}); // nonce+2 (implementation), nonce+3 (proxy)

// execute DisputeKitClassic.changeCore() only if necessary
// disputeKit.changeCore() only if necessary
const disputeKitContract = (await hre.ethers.getContract("DisputeKitClassicNeo")) as DisputeKitClassic;
const currentCore = await disputeKitContract.core();
if (currentCore !== klerosCore.address) {
console.log(`disputeKit.changeCore(${klerosCore.address})`);
await disputeKitContract.changeCore(klerosCore.address);
}

// rng.changeSortitionModule() only if necessary
const rngContract = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG;
const currentSortitionModule = await rngContract.sortitionModule();
if (currentSortitionModule !== sortitionModule.address) {
console.log(`rng.changeSortitionModule(${sortitionModule.address})`);
await rngContract.changeSortitionModule(sortitionModule.address);
}

const core = (await hre.ethers.getContract("KlerosCoreNeo")) as KlerosCoreNeo;
try {
await changeCurrencyRate(core, await weth.getAddress(), true, 1, 1);
Expand Down
6 changes: 3 additions & 3 deletions contracts/deploy/00-home-chain-arbitration-university.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { deployUpgradable } from "./utils/deployUpgradable";
import { changeCurrencyRate } from "./utils/klerosCoreHelper";
import { ETH, HomeChains, PNK, isSkipped } from "./utils";
import { deployERC20AndFaucet } from "./utils/deployTokens";
import { DisputeKitClassic, KlerosCore, KlerosCoreUniversity } from "../typechain-types";
import { DisputeKitClassic, KlerosCoreUniversity } from "../typechain-types";
import { getContractOrDeploy, getContractOrDeployUpgradable } from "./utils/getContractOrDeploy";

const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
Expand Down Expand Up @@ -53,7 +53,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
deployer, // governor
deployer, // instructor
pnk.target,
ZeroAddress,
ZeroAddress, // KlerosCore is configured later
disputeKit.address,
false,
[minStake, alpha, feeForJuror, jurorsForCourtJump],
Expand All @@ -63,7 +63,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
}); // nonce+2 (implementation), nonce+3 (proxy)

// changeCore() only if necessary
// disputeKit.changeCore() only if necessary
const disputeKitContract = (await ethers.getContract("DisputeKitClassicUniversity")) as DisputeKitClassic;
const currentCore = await disputeKitContract.core();
if (currentCore !== klerosCore.address) {
Expand Down
18 changes: 13 additions & 5 deletions contracts/deploy/00-home-chain-arbitration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { changeCurrencyRate } from "./utils/klerosCoreHelper";
import { HomeChains, isSkipped, isDevnet, isMainnet, PNK, ETH } from "./utils";
import { getContractOrDeploy, getContractOrDeployUpgradable } from "./utils/getContractOrDeploy";
import { deployERC20AndFaucet } from "./utils/deployTokens";
import { DisputeKitClassic, KlerosCore } from "../typechain-types";
import { DisputeKitClassic, KlerosCore, RandomizerRNG } from "../typechain-types";

const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { ethers, deployments, getNamedAccounts, getChainId } = hre;
Expand Down Expand Up @@ -36,9 +36,9 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
});

const randomizerRng = await getContractOrDeployUpgradable(hre, "RandomizerRNG", {
const randomizerRng = await getContractOrDeploy(hre, "RandomizerRNG", {
from: deployer,
args: [randomizerOracle.target, deployer],
args: [deployer, ZeroAddress, randomizerOracle.target], // The SortitionModule is configured later
log: true,
});

Expand Down Expand Up @@ -83,7 +83,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
deployer,
deployer,
pnk.target,
ZeroAddress,
ZeroAddress, // KlerosCore is configured later
disputeKit.address,
false,
[minStake, alpha, feeForJuror, jurorsForCourtJump],
Expand All @@ -94,14 +94,22 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
}); // nonce+2 (implementation), nonce+3 (proxy)

// changeCore() only if necessary
// disputeKit.changeCore() only if necessary
const disputeKitContract = (await ethers.getContract("DisputeKitClassic")) as DisputeKitClassic;
const currentCore = await disputeKitContract.core();
if (currentCore !== klerosCore.address) {
console.log(`disputeKit.changeCore(${klerosCore.address})`);
await disputeKitContract.changeCore(klerosCore.address);
}

// rng.changeSortitionModule() only if necessary
const rngContract = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG;
const currentSortitionModule = await rngContract.sortitionModule();
if (currentSortitionModule !== sortitionModule.address) {
console.log(`rng.changeSortitionModule(${sortitionModule.address})`);
await rngContract.changeSortitionModule(sortitionModule.address);
}

const core = (await hre.ethers.getContract("KlerosCore")) as KlerosCore;
try {
await changeCurrencyRate(core, await pnk.getAddress(), true, 12225583, 12);
Expand Down
11 changes: 6 additions & 5 deletions contracts/deploy/00-rng.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { SortitionModule } from "../typechain-types";
import { HomeChains, isSkipped } from "./utils";
import { HomeChains, isMainnet, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";
import { getContractOrDeploy } from "./utils/getContractOrDeploy";

Expand All @@ -15,16 +15,18 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const chainId = Number(await getChainId());
console.log("deploying to %s with deployer %s", HomeChains[chainId], deployer);

const sortitionModule = (await ethers.getContract("SortitionModuleNeo")) as SortitionModule;

const randomizerOracle = await getContractOrDeploy(hre, "RandomizerOracle", {
from: deployer,
contract: "RandomizerMock",
args: [],
log: true,
});

const rng1 = await deployUpgradable(deployments, "RandomizerRNG", {
const rng1 = await deploy("RandomizerRNG", {
from: deployer,
args: [randomizerOracle.address, deployer],
args: [deployer, sortitionModule.target, randomizerOracle.address],
log: true,
});

Expand All @@ -34,13 +36,12 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
});

const sortitionModule = (await ethers.getContract("SortitionModuleNeo")) as SortitionModule;
await sortitionModule.changeRandomNumberGenerator(rng2.address, RNG_LOOKAHEAD);
};

deployArbitration.tags = ["RNG"];
deployArbitration.skip = async ({ network }) => {
return isSkipped(network, !HomeChains[network.config.chainId ?? 0]);
return isSkipped(network, isMainnet(network));
};

export default deployArbitration;
18 changes: 18 additions & 0 deletions contracts/deployments/arbitrum/ChainlinkVRFCoordinator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"address": "0xf97f4df75117a78c1A5a0DBb814Af92458539FB4",
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"stateMutability": "payable",
"type": "fallback"
},
{
"stateMutability": "payable",
"type": "receive"
}
]
}
Loading