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

Replace web3->ethers and JS->TS in RPC class #589

Open
wants to merge 1 commit into
base: confluence
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions test/gas-report/poolsize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import RPC from "../../utils/rpc"
import {web3, ethers} from "hardhat"
import {ethers} from "hardhat"
import setupIntegrationTest from "../helpers/setupIntegrationTest"

import chai from "chai"
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("transcoder pool size gas report", () => {
}

before(async () => {
rpc = new RPC(web3)
rpc = new RPC(ethers.provider)

const fixture = await setupIntegrationTest()
controller = await ethers.getContractAt(
Expand Down
2 changes: 1 addition & 1 deletion test/gas-report/redeemTicket.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("redeem ticket gas report", () => {
let signers

before(async () => {
rpc = new RPC(web3)
rpc = new RPC(ethers.provider)
signers = await ethers.getSigners()
transcoder = signers[0]
broadcaster = signers[1]
Expand Down
2 changes: 1 addition & 1 deletion test/integration/PoolUpdatesWithHints.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("PoolUpdatesWithHints", () => {
transcoders = signers.slice(0, 10)
delegator = signers[11]
newTranscoder = signers[12]
rpc = new RPC(web3)
rpc = new RPC(ethers.provider)

const fixture = await setupIntegrationTest()
controller = await ethers.getContractAt(
Expand Down
4 changes: 2 additions & 2 deletions test/unit/BondingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import {constants} from "../../utils/constants"
import math from "../helpers/math"
import {assert} from "chai"
import {ethers, web3} from "hardhat"
import {ethers} from "hardhat"
const BigNumber = ethers.BigNumber
import chai from "chai"
import {solidity} from "ethereum-waffle"
Expand Down Expand Up @@ -49,7 +49,7 @@ describe("BondingManager", () => {
let signers
before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()

const llFac = await ethers.getContractFactory("SortedDoublyLL")
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Fixture from "./helpers/Fixture"
import {contractId} from "../../utils/helpers"
import {web3, ethers} from "hardhat"
import {ethers} from "hardhat"

import chai, {expect, assert} from "chai"
import {solidity} from "ethereum-waffle"
Expand All @@ -14,7 +14,7 @@ describe("Controller", () => {

before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()
controller = fixture.controller
commitHash = fixture.commitHash
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Governor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Governor", () => {

before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()
const govFac = await ethers.getContractFactory("Governor")
governor = await govFac.deploy()
Expand Down
2 changes: 1 addition & 1 deletion test/unit/LivepeerTokenFaucet.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("LivepeerTokenFaucet", () => {
let signers

before(async () => {
rpc = new RPC(web3)
rpc = new RPC(ethers.provider)
signers = await ethers.getSigners()
const tokenFac = await ethers.getContractFactory("LivepeerToken")
const faucetFac = await ethers.getContractFactory("LivepeerTokenFaucet")
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ManagerProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("ManagerProxy", () => {

before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()
await fixture.deployAndRegister(
await ethers.getContractFactory("ManagerProxyTargetMockV1"),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/MerkleSnapshot.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {keccak256, bufferToHex} from "ethereumjs-util"
import MerkleTree from "../../utils/merkleTree"
import Fixture from "./helpers/Fixture"
import {web3, ethers} from "hardhat"
import {ethers} from "hardhat"
import chai, {expect, assert} from "chai"
import {solidity} from "ethereum-waffle"
chai.use(solidity)
Expand All @@ -13,7 +13,7 @@ describe("MerkleSnapshot", () => {

before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()
const merkleFac = await ethers.getContractFactory("MerkleSnapshot")
merkleSnapshot = await merkleFac.deploy(fixture.controller.address)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Minter.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("Minter", () => {
})

before(async () => {
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()

minter = await fixture.deployAndRegister(
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Poll", () => {

before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
})

beforeEach(async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/PollCreator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fixture from "./helpers/Fixture"
import {web3, ethers} from "hardhat"
import {ethers} from "hardhat"

import {expect, use} from "chai"
import {solidity} from "ethereum-waffle"
Expand All @@ -22,7 +22,7 @@ describe("PollCreator", () => {
before(async () => {
;[, mockBondingManagerEOA] = await ethers.getSigners()

fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)

bondingManagerMock = await smock.fake(
"contracts/polling/PollCreator.sol:IBondingManager",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/RoundsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("RoundsManager", () => {

before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()

roundsManager = await fixture.deployAndRegister(
Expand Down
4 changes: 2 additions & 2 deletions test/unit/ServiceRegistry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fixture from "./helpers/Fixture"
import {web3, ethers} from "hardhat"
import {ethers} from "hardhat"

import chai, {expect, assert} from "chai"
import {solidity} from "ethereum-waffle"
Expand All @@ -12,7 +12,7 @@ describe("ServiceRegistry", () => {
let controller
before(async () => {
signers = await ethers.getSigners()
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
// Use dummy Controller in these unit tests
// We are testing the logic of ServiceRegistry directly so we do not
// interact with the contract via a proxy
Expand Down
2 changes: 1 addition & 1 deletion test/unit/TicketBroker.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("TicketBroker", () => {
sender = signers[0].address
recipient = signers[1].address
funder = signers[3]
fixture = new Fixture(web3)
fixture = new Fixture(ethers.provider)
await fixture.deploy()

broker = await (
Expand Down
4 changes: 2 additions & 2 deletions test/unit/helpers/Fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {contractId} from "../../../utils/helpers"
import {ethers} from "hardhat"

export default class Fixture {
constructor(web3) {
this.rpc = new RPC(web3)
constructor(provider) {
this.rpc = new RPC(provider)
this.commitHash = "0x3031323334353637383930313233343536373839"
}

Expand Down
94 changes: 0 additions & 94 deletions utils/rpc.js

This file was deleted.

79 changes: 79 additions & 0 deletions utils/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {ethers} from "ethers"

export default class RPC {
constructor(public provider: ethers.providers.JsonRpcProvider) {}

sendAsync(method: string, arg: any[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we explicitly mark it as 'async' and also specify a return type? (I guess by using a generic since it is used in functions that return Promise<void> or Promise<someType>

try {
return this.provider.send(method, arg)
} catch (error: any) {
throw error
}
}

// Change block time using TestRPC call evm_setTimestamp
// https://github.com/numerai/contract/blob/master/test/numeraire.js
Comment on lines +14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments are now outdated and can be removed

increaseTime(time: number) {
return this.sendAsync("evm_increaseTime", [time])
}

mine() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark it as async and since it seems in the codebase we are not expecting any return type i guess you can just use Promise<void>

return this.sendAsync("evm_mine", [])
}

async snapshot() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify return type

const id = await this.sendAsync("evm_snapshot", [])
return id
}

revert(snapshotId: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the comment for the 'mine' method

return this.sendAsync("evm_revert", [snapshotId])
}

async wait(blocks = 1, seconds = 20) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

const currentBlock = await this.provider.getBlockNumber()
const targetBlock = currentBlock + blocks
await this.waitUntilBlock(targetBlock, seconds)
}

async getBlockNumberAsync() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add return type

return this.provider.getBlockNumber()
}

async waitUntilBlock(targetBlock: number, seconds = 20) {
let currentBlock = await this.provider.getBlockNumber()

while (currentBlock < targetBlock) {
await this.increaseTime(seconds)
await this.mine()
currentBlock++
}
}

async waitUntilNextBlockMultiple(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify return type

blockMultiple: number,
multiples = 1,
seconds = 20
) {
const currentBlock = await this.provider.getBlockNumber()
const additionalBlocks = (multiples - 1) * blockMultiple
await this.waitUntilBlock(
this.nextBlockMultiple(currentBlock, blockMultiple) +
additionalBlocks
)
}

nextBlockMultiple(currentBlockNum: number, blockMultiple: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify return type

if (blockMultiple === 0) {
return currentBlockNum
}

const remainder = currentBlockNum % blockMultiple

if (remainder === 0) {
return currentBlockNum
}

return currentBlockNum + blockMultiple - remainder
}
}