From 1de7e625ef85e04e73b97646fc0bcb0111220b7b Mon Sep 17 00:00:00 2001 From: Intizar Date: Mon, 3 Jun 2024 17:24:43 +0900 Subject: [PATCH] add localhost options --- hardhat.config.ts | 10 ++++--- scripts/test.ts | 69 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/utils.ts | 2 ++ 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 scripts/test.ts diff --git a/hardhat.config.ts b/hardhat.config.ts index 5e54357..b2be353 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -37,7 +37,9 @@ const config: HardhatUserConfig = { networks: { localhost: { gas: 1_400_000, - gasPrice: 250_000_000_000 + gasPrice: 250_000_000_000, + ...commonConfig, + url: process.env.PROVIDER || 'http://127.0.0.1:8545' }, baobab: { url: process.env.PROVIDER || 'https://api.baobab.klaytn.net:8651', @@ -57,11 +59,13 @@ const config: HardhatUserConfig = { }, prepayment: { baobab: '0x8d3A1663d10eEb0bC9C9e537e1BBeA69383194e7', - cypress: '0xc2C88492Cf7e5240C3EB49353539E75336960600' + cypress: '0xc2C88492Cf7e5240C3EB49353539E75336960600', + localhost: '0x5FbDB2315678afecb367f032d93F642f64180aa3' }, vrfCoordinator: { baobab: '0xDA8c0A00A372503aa6EC80f9b29Cc97C454bE499', - cypress: '0x3F247f70DC083A2907B8E76635986fd09AA80EFb' + cypress: '0x3F247f70DC083A2907B8E76635986fd09AA80EFb', + localhost: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512' } } } diff --git a/scripts/test.ts b/scripts/test.ts new file mode 100644 index 0000000..39d70ca --- /dev/null +++ b/scripts/test.ts @@ -0,0 +1,69 @@ +import { ethers } from 'hardhat' +import { getKeyHash } from './utils' +import { Prepayment__factory } from '@bisonai/orakl-contracts' +import hre from 'hardhat' +import { readFileSync } from 'fs' +import { join } from 'path' + +const ACC_ID = process.env.ACC_ID + +async function createContracts(count: number) { + const { getNamedAccounts } = hre + const { vrfCoordinator, prepayment: prepaymentAddress } = await getNamedAccounts() + const prepayment = await ethers.getContractAt(Prepayment__factory.abi, prepaymentAddress) + + const VRFConsumerFactory = await ethers.getContractFactory('VRFConsumer') + + const contracts = [] + for (let i = 0; i < count; i++) { + const userContract = await VRFConsumerFactory.deploy(vrfCoordinator) + await userContract.deployed() + contracts.push(userContract.address) + + await (await prepayment.addConsumer(ACC_ID, userContract.address)).wait() + } + + return contracts +} + +async function requestRandomWords(contracts: string[]) { + const path = join(__dirname, '../deployments/localhost/VRFConsumer.json') + const VRFConsumerABI = JSON.parse(readFileSync(path, 'utf-8')).abi + for (const contractAddr of contracts) { + const keyHash = getKeyHash() + const callbackGasLimit = 500_000 + const numWords = 1 + + const contract = await ethers.getContractAt(VRFConsumerABI, contractAddr) + await contract.requestRandomWords(keyHash, ACC_ID, callbackGasLimit, numWords) + } +} + +async function readRandomwords(contracts: string[]) { + const path = join(__dirname, '../deployments/localhost/VRFConsumer.json') + const VRFConsumerABI = JSON.parse(readFileSync(path, 'utf-8')).abi + for (const contractAddr of contracts) { + const contract = await ethers.getContractAt(VRFConsumerABI, contractAddr) + const randomWord = await contract.sRandomWord() + console.log(`${contract.address} randomWord ${randomWord.toString()}`) + } +} + +async function main() { + // const contracts = await createContracts(5) + // console.log(contracts) + const contracts = [ + '0x8A791620dd6260079BF849Dc5567aDC3F2FdC318', + '0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e', + '0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82', + '0x0B306BF915C4d645ff596e518fAf3F9669b97016', + '0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE' + ] + // await requestRandomWords(contracts) + await readRandomwords(contracts) +} + +main().catch((error) => { + console.error(error) + process.exitCode = 1 +}) diff --git a/scripts/utils.ts b/scripts/utils.ts index 443e848..76cf5dd 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -4,6 +4,8 @@ export function getKeyHash() { return '0xd9af33106d664a53cb9946df5cd81a30695f5b72224ee64e798b278af812779c' } else if (networkName == 'cypress') { return '0x6cff5233743b3c0321a19ae11ab38ae0ddc7ddfe1e91b162fa8bb657488fb157' + } else if (networkName == 'localhost') { + return '0xeaec8ebbc75ec18c1d761e4e11d2eab84392a55786264a3aa7385ab4532db1e4' } else { throw new Error(`Key Hash is not defined for network: ${networkName}`) }