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 web3js with ethersjs #590

Draft
wants to merge 2 commits into
base: confluence
Choose a base branch
from
Draft
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
58 changes: 37 additions & 21 deletions test/helpers/signMsg.js → test/helpers/signMsg.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
export const getLongSigV = signature => {
import {Signer} from "ethers"
import {ethers} from "hardhat"

export const getLongSigV = (signature: string) => {
return parseInt(signature.slice(signature.length - 2, signature.length), 16)
}

export const getEIP2098V = signature => {
export const getEIP2098V = (signature: string) => {
// uint8 v = uint8((uint256(vs) >> 255) + 27);
const sigToBytes = web3.utils.hexToBytes(signature)
const sigToBytes = ethers.utils.arrayify(signature)
const v = (sigToBytes[32] >> 7) + 27
return v
}

export const fixSig = sig => {
export const fixSig = (signature: string) => {
// The recover() in ECDSA.sol from openzeppelin-solidity requires signatures to have a v-value that is 27/28
// ETH clients that implement eth_sign will return a signature with a v-value that is 27/28 or 0/1 (geth returns 27/28 and ganache returns 0/1)
// In order to support all ETH clients that implement eth_sign, we can fix the signature by ensuring that the v-value is 27/28
let v = getLongSigV(sig)
let v = getLongSigV(signature)
if (v < 27) {
v += 27
}

return sig.slice(0, sig.length - 2) + v.toString(16)
return signature.slice(0, signature.length - 2) + v.toString(16)
}

export const web3Sign = async (msg, account) => {
return web3.eth.sign(msg, account)
export const sign = async (msg: string, account: string | Signer) => {
if (typeof account === "string") {
const acc = ethers.provider.getSigner(account)
return acc.signMessage(ethers.utils.arrayify(msg))
} else {
return account.signMessage(ethers.utils.arrayify(msg))
}
}

export default async (msg, account) => {
return fixSig(await web3Sign(msg, account))
export default async (msg: string, account: string) => {
return fixSig(await sign(msg, account))
}

export const flipV = sig => {
let v = parseInt(sig.slice(sig.length - 2, sig.length), 16)
export const flipV = (signature: string) => {
let v = parseInt(
signature.slice(signature.length - 2, signature.length),
16
)
if (v === 27) {
v = 28
} else if (v === 28) {
v = 27
} else {
throw new Error(`unrecognized V value ${v}`)
}
const result = sig.slice(0, sig.length - 2).concat(v.toString(16))
const result = signature
.slice(0, signature.length - 2)
.concat(v.toString(16))
return result
}

// from openzeppelin [https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5b28259dacf47fc208e03611eb3ba8eeaed63cc0/test/utils/cryptography/ECDSA.test.js#L12-L33]
export function to2098Format(signature) {
const long = web3.utils.hexToBytes(signature)
export function to2098Format(signature: string) {
const long = ethers.utils.arrayify(signature)

if (long.length !== 65) {
throw new Error("invalid signature length (expected long format)")
Expand All @@ -54,16 +67,19 @@ export function to2098Format(signature) {
}
const short = long.slice(0, 64)
short[32] |= long[64] % 27 << 7 // set the first bit of the 32nd byte to the v parity bit
return web3.utils.bytesToHex(short)
return ethers.utils.hexlify(short)
}

// from openzeppelin [https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5b28259dacf47fc208e03611eb3ba8eeaed63cc0/test/utils/cryptography/ECDSA.test.js#L12-L33]
export function from2098Format(signature) {
const short = web3.utils.hexToBytes(signature)
export function from2098Format(signature: string) {
const short = ethers.utils.arrayify(signature)
if (short.length !== 64) {
throw new Error("invalid signature length (expected short format)")
}
short.push((short[32] >> 7) + 27)
short[32] &= (1 << 7) - 1 // zero out the first bit of 1 the 32nd byte
return web3.utils.bytesToHex(short)
const long = new Uint8Array(65)
long.set(short, 0)
long[64] = (short[32] >> 7) + 27

long[32] &= (1 << 7) - 1 // zero out the first bit of 1 the 32nd byte
return ethers.utils.hexlify(long)
}
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -889,7 +889,7 @@ describe("TicketBroker", () => {
const recipientRand = 5
const recipientRandHash = web3.utils.soliditySha3(recipientRand)

const sig = await signMsg("junk", sender)
const sig = await signMsg("0xdabbad00", sender)

await expect(
broker.redeemWinningTicket(
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
Loading