Skip to content

Commit

Permalink
test: Fix hardhat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Mar 20, 2024
1 parent 2909ce6 commit d498695
Show file tree
Hide file tree
Showing 15 changed files with 2,503 additions and 2,494 deletions.
10 changes: 5 additions & 5 deletions contracts/test/AccessControlTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ contract(
// Attempt call from alice
try {
const tx1 = await borrowerOperations.moveETHGainToTrove(
bob,
th.addressToTroveId(bob),
1,
{ from: bob }
);
Expand All @@ -87,7 +87,7 @@ contract(
it("applyPendingRewards(): reverts when called by an account that is not BorrowerOperations", async () => {
// Attempt call from alice
try {
const txAlice = await troveManager.applyPendingRewards(bob, {
const txAlice = await troveManager.applyPendingRewards(th.addressToTroveId(bob), {
from: alice,
});
} catch (err) {
Expand Down Expand Up @@ -131,11 +131,11 @@ contract(
}
});

// addTroveOwnerToArray
it("addTroveOwnerToArray(): reverts when called by an account that is not BorrowerOperations", async () => {
// addTroveIdToArray
it("addTroveIdToArray(): reverts when called by an account that is not BorrowerOperations", async () => {
// Attempt call from alice
try {
const txAlice = await troveManager.addTroveOwnerToArray(bob, {
const txAlice = await troveManager.addTroveIdToArray(bob, {
from: alice,
});
} catch (err) {
Expand Down
957 changes: 491 additions & 466 deletions contracts/test/BorrowerOperationsTest.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/test/CollSurplusPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ contract("CollSurplusPool", async (accounts) => {

it("CollSurplusPool: claimColl(): Reverts if caller is not Borrower Operations", async () => {
await th.assertRevert(
collSurplusPool.claimColl(A, { from: A }),
collSurplusPool.claimColl(A, th.addressToTroveId(A), { from: A }),
"CollSurplusPool: Caller is not Borrower Operations"
);
});

it("CollSurplusPool: claimColl(): Reverts if nothing to claim", async () => {
await th.assertRevert(
borrowerOperations.claimCollateral({ from: A }),
borrowerOperations.claimCollateral(th.addressToTroveId(A), { from: A }),
"CollSurplusPool: No collateral available to claim"
);
});
Expand Down
134 changes: 67 additions & 67 deletions contracts/test/GasCompensationTest.js

Large diffs are not rendered by default.

115 changes: 56 additions & 59 deletions contracts/test/HintHelpers_getApproxHintTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ let latestRandomSeed = 31337;
const TroveManagerTester = artifacts.require("TroveManagerTester");
const BoldToken = artifacts.require("BoldToken");

const INITIAL_PRICE = dec(100, 18);

contract("HintHelpers", async (accounts) => {
const [owner] = accounts;

Expand Down Expand Up @@ -114,23 +116,23 @@ contract("HintHelpers", async (accounts) => {

numAccounts = 10;

await priceFeed.setPrice(dec(100, 18));
await priceFeed.setPrice(INITIAL_PRICE);
await makeTrovesInSequence(accounts, numAccounts);
// await makeTrovesInParallel(accounts, numAccounts)
});

it("setup: makes accounts with nominal ICRs increasing by 1% consecutively", async () => {
// check first 10 accounts
const ICR_0 = await troveManager.getNominalICR(accounts[0]);
const ICR_1 = await troveManager.getNominalICR(accounts[1]);
const ICR_2 = await troveManager.getNominalICR(accounts[2]);
const ICR_3 = await troveManager.getNominalICR(accounts[3]);
const ICR_4 = await troveManager.getNominalICR(accounts[4]);
const ICR_5 = await troveManager.getNominalICR(accounts[5]);
const ICR_6 = await troveManager.getNominalICR(accounts[6]);
const ICR_7 = await troveManager.getNominalICR(accounts[7]);
const ICR_8 = await troveManager.getNominalICR(accounts[8]);
const ICR_9 = await troveManager.getNominalICR(accounts[9]);
const ICR_0 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[0]), INITIAL_PRICE);
const ICR_1 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[1]), INITIAL_PRICE);
const ICR_2 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[2]), INITIAL_PRICE);
const ICR_3 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[3]), INITIAL_PRICE);
const ICR_4 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[4]), INITIAL_PRICE);
const ICR_5 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[5]), INITIAL_PRICE);
const ICR_6 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[6]), INITIAL_PRICE);
const ICR_7 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[7]), INITIAL_PRICE);
const ICR_8 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[8]), INITIAL_PRICE);
const ICR_9 = await troveManager.getCurrentICR(th.addressToTroveId(accounts[9]), INITIAL_PRICE);

assert.isTrue(ICR_0.eq(toBN(dec(200, 16))));
assert.isTrue(ICR_1.eq(toBN(dec(201, 16))));
Expand All @@ -155,78 +157,78 @@ contract("HintHelpers", async (accounts) => {
const CR_250 = "2500000000000000000";
const CRPercent_250 = Number(web3.utils.fromWei(CR_250, "ether")) * 100;

let hintAddress;
let hintId;

// const hintAddress_250 = await functionCaller.troveManager_getApproxHint(CR_250, sqrtLength * 10)
({ hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint(
// const hintId_250 = await functionCaller.troveManager_getApproxHint(CR_250, sqrtLength * 10)
({ hintId, latestRandomSeed } = await hintHelpers.getApproxHint(
CR_250,
sqrtLength * 10,
latestRandomSeed
));
const ICR_hintAddress_250 = await troveManager.getNominalICR(hintAddress);
const ICRPercent_hintAddress_250 =
Number(web3.utils.fromWei(ICR_hintAddress_250, "ether")) * 100;
const ICR_hintId_250 = await troveManager.getCurrentICR(hintId, INITIAL_PRICE);
const ICRPercent_hintId_250 =
Number(web3.utils.fromWei(ICR_hintId_250, "ether")) * 100;

// check the hint position is at most sqrtLength positions away from the correct position
ICR_Difference_250 = ICRPercent_hintAddress_250 - CRPercent_250;
ICR_Difference_250 = ICRPercent_hintId_250 - CRPercent_250;
assert.isBelow(ICR_Difference_250, sqrtLength);

// CR = 287%
const CR_287 = "2870000000000000000";
const CRPercent_287 = Number(web3.utils.fromWei(CR_287, "ether")) * 100;

// const hintAddress_287 = await functionCaller.troveManager_getApproxHint(CR_287, sqrtLength * 10)
({ hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint(
// const hintId_287 = await functionCaller.troveManager_getApproxHint(CR_287, sqrtLength * 10)
({ hintId, latestRandomSeed } = await hintHelpers.getApproxHint(
CR_287,
sqrtLength * 10,
latestRandomSeed
));
const ICR_hintAddress_287 = await troveManager.getNominalICR(hintAddress);
const ICRPercent_hintAddress_287 =
Number(web3.utils.fromWei(ICR_hintAddress_287, "ether")) * 100;
const ICR_hintId_287 = await troveManager.getCurrentICR(hintId, INITIAL_PRICE);
const ICRPercent_hintId_287 =
Number(web3.utils.fromWei(ICR_hintId_287, "ether")) * 100;

// check the hint position is at most sqrtLength positions away from the correct position
ICR_Difference_287 = ICRPercent_hintAddress_287 - CRPercent_287;
ICR_Difference_287 = ICRPercent_hintId_287 - CRPercent_287;
assert.isBelow(ICR_Difference_287, sqrtLength);

// CR = 213%
const CR_213 = "2130000000000000000";
const CRPercent_213 = Number(web3.utils.fromWei(CR_213, "ether")) * 100;

// const hintAddress_213 = await functionCaller.troveManager_getApproxHint(CR_213, sqrtLength * 10)
({ hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint(
// const hintId_213 = await functionCaller.troveManager_getApproxHint(CR_213, sqrtLength * 10)
({ hintId, latestRandomSeed } = await hintHelpers.getApproxHint(
CR_213,
sqrtLength * 10,
latestRandomSeed
));
const ICR_hintAddress_213 = await troveManager.getNominalICR(hintAddress);
const ICRPercent_hintAddress_213 =
Number(web3.utils.fromWei(ICR_hintAddress_213, "ether")) * 100;
const ICR_hintId_213 = await troveManager.getCurrentICR(hintId, INITIAL_PRICE);
const ICRPercent_hintId_213 =
Number(web3.utils.fromWei(ICR_hintId_213, "ether")) * 100;

// check the hint position is at most sqrtLength positions away from the correct position
ICR_Difference_213 = ICRPercent_hintAddress_213 - CRPercent_213;
ICR_Difference_213 = ICRPercent_hintId_213 - CRPercent_213;
assert.isBelow(ICR_Difference_213, sqrtLength);

// CR = 201%
const CR_201 = "2010000000000000000";
const CRPercent_201 = Number(web3.utils.fromWei(CR_201, "ether")) * 100;

// const hintAddress_201 = await functionCaller.troveManager_getApproxHint(CR_201, sqrtLength * 10)
({ hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint(
// const hintId_201 = await functionCaller.troveManager_getApproxHint(CR_201, sqrtLength * 10)
({ hintId, latestRandomSeed } = await hintHelpers.getApproxHint(
CR_201,
sqrtLength * 10,
latestRandomSeed
));
const ICR_hintAddress_201 = await troveManager.getNominalICR(hintAddress);
const ICRPercent_hintAddress_201 =
Number(web3.utils.fromWei(ICR_hintAddress_201, "ether")) * 100;
const ICR_hintId_201 = await troveManager.getCurrentICR(hintId, INITIAL_PRICE);
const ICRPercent_hintId_201 =
Number(web3.utils.fromWei(ICR_hintId_201, "ether")) * 100;

// check the hint position is at most sqrtLength positions away from the correct position
ICR_Difference_201 = ICRPercent_hintAddress_201 - CRPercent_201;
ICR_Difference_201 = ICRPercent_hintId_201 - CRPercent_201;
assert.isBelow(ICR_Difference_201, sqrtLength);
});

/* Pass 100 random collateral ratios to getApproxHint(). For each, check whether the returned hint address is within
/* Pass 100 random collateral ratios to getApproxHint(). For each, check whether the returned hint id is within
sqrt(length) positions of where a Trove with that CR should be inserted. */
// it("getApproxHint(): for 100 random CRs, returns the address of a Trove within sqrt(length) positions of the correct insert position", async () => {
// const sqrtLength = Math.ceil(Math.sqrt(numAccounts))
Expand All @@ -241,7 +243,7 @@ contract("HintHelpers", async (accounts) => {
// const ICR = web3.utils.toWei((ICR_Percent * 10).toString(), 'finney')

// const hintAddress = await hintHelpers.getApproxHint(ICR, sqrtLength * 10)
// const ICR_hintAddress = await troveManager.getNominalICR(hintAddress)
// const ICR_hintAddress = await troveManager.getCurrentICR(hintAddres, INITIAL_PRICES)
// const ICRPercent_hintAddress = Number(web3.utils.fromWei(ICR_hintAddress, 'ether')) * 100

// // check the hint position is at most sqrtLength positions away from the correct position
Expand All @@ -257,26 +259,26 @@ contract("HintHelpers", async (accounts) => {
const CR_Max =
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";

let hintAddress;
let hintId;

// const hintAddress_Max = await functionCaller.troveManager_getApproxHint(CR_Max, sqrtLength * 10)
({ hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint(
// const hintId_Max = await functionCaller.troveManager_getApproxHint(CR_Max, sqrtLength * 10)
({ hintId, latestRandomSeed } = await hintHelpers.getApproxHint(
CR_Max,
sqrtLength * 10,
latestRandomSeed
));

const ICR_hintAddress_Max = await troveManager.getNominalICR(hintAddress);
const ICRPercent_hintAddress_Max =
Number(web3.utils.fromWei(ICR_hintAddress_Max, "ether")) * 100;
const ICR_hintId_Max = await troveManager.getCurrentICR(hintId, INITIAL_PRICE);
const ICRPercent_hintId_Max =
Number(web3.utils.fromWei(ICR_hintId_Max, "ether")) * 100;

const firstTrove = await sortedTroves.getFirst();
const ICR_FirstTrove = await troveManager.getNominalICR(firstTrove);
const ICR_FirstTrove = await troveManager.getCurrentICR(firstTrove, INITIAL_PRICE);
const ICRPercent_FirstTrove =
Number(web3.utils.fromWei(ICR_FirstTrove, "ether")) * 100;

// check the hint position is at most sqrtLength positions away from the correct position
ICR_Difference_Max = ICRPercent_hintAddress_Max - ICRPercent_FirstTrove;
ICR_Difference_Max = ICRPercent_hintId_Max - ICRPercent_FirstTrove;
assert.isBelow(ICR_Difference_Max, sqrtLength);
});

Expand All @@ -286,33 +288,28 @@ contract("HintHelpers", async (accounts) => {
// CR = MCR
const CR_Min = "1100000000000000000";

let hintAddress;
let hintId;

// const hintAddress_Min = await functionCaller.troveManager_getApproxHint(CR_Min, sqrtLength * 10)
({ hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint(
// const hintId_Min = await functionCaller.troveManager_getApproxHint(CR_Min, sqrtLength * 10)
({ hintId, latestRandomSeed } = await hintHelpers.getApproxHint(
CR_Min,
sqrtLength * 10,
latestRandomSeed
));
const ICR_hintAddress_Min = await troveManager.getNominalICR(hintAddress);
const ICRPercent_hintAddress_Min =
Number(web3.utils.fromWei(ICR_hintAddress_Min, "ether")) * 100;
const ICR_hintId_Min = await troveManager.getCurrentICR(hintId, INITIAL_PRICE);
const ICRPercent_hintId_Min =
Number(web3.utils.fromWei(ICR_hintId_Min, "ether")) * 100;

const lastTrove = await sortedTroves.getLast();
const ICR_LastTrove = await troveManager.getNominalICR(lastTrove);
const ICR_LastTrove = await troveManager.getCurrentICR(lastTrove, INITIAL_PRICE);
const ICRPercent_LastTrove =
Number(web3.utils.fromWei(ICR_LastTrove, "ether")) * 100;

// check the hint position is at most sqrtLength positions away from the correct position
const ICR_Difference_Min =
ICRPercent_hintAddress_Min - ICRPercent_LastTrove;
ICRPercent_hintId_Min - ICRPercent_LastTrove;
assert.isBelow(ICR_Difference_Min, sqrtLength);
});

it("computeNominalCR()", async () => {
const NICR = await hintHelpers.computeNominalCR(dec(3, 18), dec(200, 18));
assert.equal(NICR.toString(), dec(150, 16));
});
});

// Gas usage: See gas costs spreadsheet. Cost per trial = 10k-ish.
Loading

0 comments on commit d498695

Please sign in to comment.