Skip to content

Commit

Permalink
Merge pull request #178 from keep-network/update-hardhat
Browse files Browse the repository at this point in the history
Update hardhat version and fix unit tests

Due to an error in Hardhat 2.2.0 (NomicFoundation/hardhat#1415)
we decided to update Hardhat version and fix errors in unit and system tests.

The errors in tests were caused by functions like isOpen, takeOffer, earlyClose
being called on auctions that self destructed (the previously used version of Hardhat
seemed to tolerate such calls).
  • Loading branch information
pdyraga authored Aug 24, 2021
2 parents 769d85b + 3b4e6fa commit dc6ee23
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 98 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"eslint-config-keep": "github:keep-network/eslint-config-keep#0c27ade",
"ethereum-waffle": "^3.3.0",
"ethers": "^5.0.32",
"hardhat": "^2.1.1",
"hardhat": "^2.6.1",
"hardhat-deploy": "^0.8.11",
"hardhat-gas-reporter": "^1.0.4",
"prettier": "^2.3.2",
Expand Down
44 changes: 18 additions & 26 deletions test/Auction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
pastEvents,
increaseTime,
impersonateAccount,
isCodeAt,
} = require("./helpers/contract-test-helpers")

const { BigNumber } = ethers
Expand Down Expand Up @@ -150,6 +151,9 @@ describe("Auction", () => {
// add 100sec
await increaseTime(100)

// make sure auction did not self destruct
expect(await isCodeAt(auction.address)).to.be.true

expect(await auction.isOpen()).to.be.equal(true)
})
})
Expand All @@ -164,6 +168,7 @@ describe("Auction", () => {
})

it("should be opened", async () => {
expect(await isCodeAt(auction.address)).to.be.true
expect(await auction.isOpen()).to.be.equal(true)
})

Expand Down Expand Up @@ -275,17 +280,15 @@ describe("Auction", () => {
})

context("when the auction was fully paid off and is closed", () => {
it("should revert on taking offer again", async () => {
it("should destroy the auction", async () => {
// Increase time 1h -> 3600sec
await increaseTime(3600)

// take the entire auction
await auction.connect(bidder1).takeOffer(auctionAmountDesired)

// another bidder is trying to take offer on a closed auction
await expect(
auction.connect(bidder2).takeOffer(BigNumber.from(1))
).to.be.revertedWith("Address: call to non-contract")
// the auction contract address should not store code anymore
expect(await isCodeAt(auction.address)).to.be.false
})
})

Expand Down Expand Up @@ -516,14 +519,18 @@ describe("Auction", () => {
// Increase time so the auction ends
// 3,600 + 82,800 + 1 = 86401sec (auction ended)
await increaseTime(82801)
// when auction ends and is partially filled, it should not self
// destruct
expect(await isCodeAt(auction.address)).to.be.true

// when auction ends and is partially filled, it should stay opened
expect(await auction.isOpen()).to.be.equal(true)
})
}
)

context("when the auction was fully paid off in partial offers", () => {
it("should revert on taking another offer", async () => {
it("should destroy the auction", async () => {
// Auction amount desired: 1 * 10^18
// Increase time 1h -> 3600sec
await increaseTime(3600)
Expand All @@ -538,9 +545,7 @@ describe("Auction", () => {
.amountOutstanding()
await auction.connect(bidder2).takeOffer(amountOutstanding)

await expect(
auction.connect(bidder2).takeOffer(BigNumber.from(1))
).to.be.revertedWith("Address: call to non-contract")
expect(await isCodeAt(auction.address)).to.be.false
})
})
})
Expand All @@ -558,21 +563,21 @@ describe("Auction", () => {
})

context("when the auction is open and there are no fills", () => {
it("should early close the auction", async () => {
it("should destroy the auction", async () => {
await auction.connect(auctioneerSigner).earlyClose()

expect(await auction.isOpen()).to.be.false
expect(await isCodeAt(auction.address)).to.be.false
})
})

context("when the auction is open and there are partial fills", () => {
it("should early close the auction", async () => {
it("should destroy the auction", async () => {
const partialOfferAmount = auctionAmountDesired.div(BigNumber.from("2"))
await auction.connect(bidder1).takeOffer(partialOfferAmount)

await auction.connect(auctioneerSigner).earlyClose()

expect(await auction.isOpen()).to.be.false
expect(await isCodeAt(auction.address)).to.be.false
})
})

Expand All @@ -583,19 +588,6 @@ describe("Auction", () => {
)
})
})

context("when the auction is closed", () => {
it("should revert", async () => {
// close the auction by taking the whole amount
await auction.connect(bidder1).takeOffer(auctionAmountDesired)

// Will be reverted due to onlyAuctioneer modifier violation as
// Auction storage has been destroyed by harikari and current
// auctioneer is a zero address.
await expect(auction.connect(auctioneerSigner).earlyClose()).to.be
.reverted
})
})
})

describe("amountTransferred", () => {
Expand Down
5 changes: 3 additions & 2 deletions test/Auctioneer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
to1e18,
pastEvents,
increaseTime,
isCodeAt,
} = require("./helpers/contract-test-helpers")

const auctionLength = 86400 // 24h in sec
Expand Down Expand Up @@ -264,10 +265,10 @@ describe("Auctioneer", () => {
})

context("when the auction is still open", () => {
it("should close the auction", async () => {
it("should destroy the auction", async () => {
await auctioneer.connect(bidder).publicEarlyCloseAuction(auctionAddress)

expect(await auction.isOpen()).to.be.false
expect(await isCodeAt(auctionAddress)).to.be.false
})

it("should emit the auction closed event", async () => {
Expand Down
8 changes: 8 additions & 0 deletions test/helpers/contract-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ async function resetFork(blockNumber) {
})
}

// This function checks whether the given address stores contract code. It
// can be used to determine whether a contract stored at the given address has
// self destructed.
async function isCodeAt(address) {
return (await ethers.provider.getCode(address)) != ethers.utils.hexlify("0x")
}

module.exports.to1ePrecision = to1ePrecision
module.exports.to1e18 = to1e18
module.exports.pastEvents = pastEvents
module.exports.lastBlockTime = lastBlockTime
module.exports.increaseTime = increaseTime
module.exports.impersonateAccount = impersonateAccount
module.exports.resetFork = resetFork
module.exports.isCodeAt = isCodeAt

module.exports.ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"
7 changes: 4 additions & 3 deletions test/system/liquidation-multiple-take-offers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
resetFork,
ZERO_ADDRESS,
increaseTime,
isCodeAt,
} = require("../helpers/contract-test-helpers")
const {
underwriterAddress,
Expand Down Expand Up @@ -128,8 +129,8 @@ describeFn("System -- multiple partial fills", () => {
tx = await auction.connect(bidder2).takeOffer(lotSize.mul(60).div(100))
})

it("should close auction", async () => {
expect(await auction.isOpen()).to.be.false
it("should destroy auction", async () => {
expect(await isCodeAt(auction.address)).to.be.false
})

it("should decrease the amount of TBTC for bidders", async () => {
Expand Down Expand Up @@ -177,7 +178,7 @@ describeFn("System -- multiple partial fills", () => {
expect(parseInt(tx.gasLimit)).to.be.lessThan(370000)

const txReceipt = await ethers.provider.getTransactionReceipt(tx.hash)
expect(parseInt(txReceipt.gasUsed)).to.be.lessThan(181000)
expect(parseInt(txReceipt.gasUsed)).to.be.lessThan(280000)
})
})
})
7 changes: 4 additions & 3 deletions test/system/liquidation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
resetFork,
ZERO_ADDRESS,
increaseTime,
isCodeAt,
} = require("../helpers/contract-test-helpers")
const hre = require("hardhat")

Expand Down Expand Up @@ -135,8 +136,8 @@ describeFn("System -- liquidation", () => {
tx = await auction.takeOffer(lotSize)
})

it("should close auction", async () => {
expect(await auction.isOpen()).to.be.false
it("should destroy auction", async () => {
expect(await isCodeAt(auction.address)).to.be.false
})

it("should spend bidder's TBTC", async () => {
Expand Down Expand Up @@ -180,7 +181,7 @@ describeFn("System -- liquidation", () => {
await expect(parseInt(tx.gasLimit)).to.be.lessThan(518000)

const txReceipt = await ethers.provider.getTransactionReceipt(tx.hash)
await expect(parseInt(txReceipt.gasUsed)).to.be.lessThan(255000)
await expect(parseInt(txReceipt.gasUsed)).to.be.lessThan(392000)
})
})
})
3 changes: 2 additions & 1 deletion test/system/surplus-for-auction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
resetFork,
to1ePrecision,
ZERO_ADDRESS,
isCodeAt,
} = require("../helpers/contract-test-helpers")
const Auction = hre.artifacts.readArtifactSync("Auction")
const { initContracts } = require("./init-contracts")
Expand Down Expand Up @@ -159,7 +160,7 @@ describeFn("System -- buying a deposit with surplus", () => {
})

it("should early close cov pool auction for deposit2", async () => {
expect(await auction.isOpen()).to.be.false
expect(await isCodeAt(auction.address)).to.be.false
})

it("should buy deposit3 with surplus TBTC without opening an auction", async () => {
Expand Down
Loading

0 comments on commit dc6ee23

Please sign in to comment.