Skip to content

Commit

Permalink
fix: Fix hardhat tests after non zero min interest rate
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Jul 28, 2024
1 parent e456c4e commit 0f746f5
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 255 deletions.
1 change: 1 addition & 0 deletions contracts/src/Dependencies/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ uint256 constant SP_YIELD_SPLIT = 72 * _1pct; // 72%
contract Constants {
uint256 public constant _ETH_GAS_COMPENSATION = ETH_GAS_COMPENSATION;
uint256 public constant _MIN_DEBT = MIN_DEBT;
uint256 public constant _MIN_ANNUAL_INTEREST_RATE = MIN_ANNUAL_INTEREST_RATE;
}
75 changes: 39 additions & 36 deletions contracts/test/GasCompensationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ contract("Gas compensation tests", async (accounts) => {
before(async () => {
const WETH = await ERC20.new("WETH", "WETH");
troveManagerTester = await TroveManagerTester.new(
toBN(dec(150, 16)),
toBN(dec(110, 16)),
toBN(dec(110, 16)),
toBN(dec(10, 16)),
Expand Down Expand Up @@ -248,63 +249,62 @@ contract("Gas compensation tests", async (accounts) => {
ICR: toBN("1818181818181818181"),
extraParams: { from: alice },
});
const alice_ICR = (
await troveManager.getCurrentICR(aliceTroveId, price)
).toString();
const alice_ICR = await troveManager.getCurrentICR(aliceTroveId, price);
// Expect aliceICR = (1 * 200) / (110) = 181.81%
assert.isAtMost(th.getDifference(alice_ICR, "1818181818181818181"), 1000);
assert.isAtMost(th.getDifference(alice_ICR.toString(), "1818181818181818181"), 1e15);
assert.isTrue(alice_ICR.lt(th.toBN("1818181818181818181")))

// B opens with 0.5 ETH, 50 Bold
const { troveId: bobTroveId } = await openTrove({ ICR: toBN(dec(2, 18)), extraParams: { from: bob } });
const bob_ICR = (await troveManager.getCurrentICR(bobTroveId, price)).toString();
const bob_ICR = await troveManager.getCurrentICR(bobTroveId, price);
// Expect Bob's ICR = (0.5 * 200) / 50 = 200%
assert.isAtMost(th.getDifference(bob_ICR, dec(2, 18)), 1000);
assert.isAtMost(th.getDifference(bob_ICR.toString(), dec(2, 18)), 1e15);
assert.isTrue(alice_ICR.lt(th.toBN(dec(2, 18))));

// F opens with 1 ETH, 100 Bold
const { troveId: flynTroveId } = await openTrove({
ICR: toBN(dec(2, 18)),
extraBoldAmount: dec(100, 18),
extraParams: { from: flyn },
});
const flyn_ICR = (await troveManager.getCurrentICR(flynTroveId, price)).toString();
const flyn_ICR = await troveManager.getCurrentICR(flynTroveId, price);
// Expect Flyn's ICR = (1 * 200) / 100 = 200%
assert.isAtMost(th.getDifference(flyn_ICR, dec(2, 18)), 1000);
assert.isAtMost(th.getDifference(flyn_ICR.toString(), dec(2, 18)), 1e15);
assert.isTrue(flyn_ICR.lt(th.toBN(dec(2, 18))));

// C opens with 2.5 ETH, 160 Bold
const { troveId: carolTroveId } = await openTrove({ ICR: toBN(dec(3125, 15)), extraParams: { from: carol } });
const carol_ICR = (
await troveManager.getCurrentICR(carolTroveId, price)
).toString();
const carol_ICR = await troveManager.getCurrentICR(carolTroveId, price);
// Expect Carol's ICR = (2.5 * 200) / (160) = 312.50%
assert.isAtMost(th.getDifference(carol_ICR, "3125000000000000000"), 1000);
assert.isAtMost(th.getDifference(carol_ICR.toString(), "3125000000000000000"), 1e15);
assert.isTrue(carol_ICR.lt(th.toBN("3125000000000000000")));

// D opens with 1 ETH, 0 Bold
const { troveId: dennisTroveId } = await openTrove({ ICR: toBN(dec(4, 18)), extraParams: { from: dennis } });
const dennis_ICR = (
await troveManager.getCurrentICR(dennisTroveId, price)
).toString();
const dennis_ICR = await troveManager.getCurrentICR(dennisTroveId, price);
// Expect Dennis's ICR = (1 * 200) / (50) = 400.00%
assert.isAtMost(th.getDifference(dennis_ICR, dec(4, 18)), 1000);
assert.isAtMost(th.getDifference(dennis_ICR.toString(), dec(4, 18)), 1e15);
assert.isTrue(dennis_ICR.lt(th.toBN(dec(4, 18))));

// E opens with 4405.45 ETH, 32598.35 Bold
const { troveId: erinTroveId } = await openTrove({
ICR: toBN("27028668628933700000"),
extraParams: { from: erin },
});
const erin_ICR = (await troveManager.getCurrentICR(erinTroveId, price)).toString();
const erin_ICR = await troveManager.getCurrentICR(erinTroveId, price);
// Expect Erin's ICR = (4405.45 * 200) / (32598.35) = 2702.87%
assert.isAtMost(th.getDifference(erin_ICR, "27028668628933700000"), 100000);
assert.isAtMost(th.getDifference(erin_ICR.toString(), "27028668628933700000"), 3e15);
assert.isTrue(erin_ICR.lt(th.toBN("27028668628933700000")));

// H opens with 1 ETH, 180 Bold
const { troveId: harrietTroveId } = await openTrove({
ICR: toBN("1111111111111111111"),
extraParams: { from: harriet },
});
const harriet_ICR = (
await troveManager.getCurrentICR(harrietTroveId, price)
).toString();
const harriet_ICR = await troveManager.getCurrentICR(harrietTroveId, price);
// Expect Harriet's ICR = (1 * 200) / (180) = 111.11%
assert.isAtMost(th.getDifference(harriet_ICR, "1111111111111111111"), 1000);
assert.isAtMost(th.getDifference(harriet_ICR.toString(), "1111111111111111111"), 1e15);
assert.isTrue(harriet_ICR.lt(th.toBN("1111111111111111111")));
});

// Test compensation amounts and liquidation amounts
Expand Down Expand Up @@ -462,14 +462,17 @@ contract("Gas compensation tests", async (accounts) => {

// Check ETH in SP has not changed due to the lquidation of C
const ETHinSP_C = await stabilityPool.getCollBalance();
assert.equal(
ETHinSP_C.toString(),
aliceColl
.sub(_0pt5percent_aliceColl)
.add(bobColl)
.sub(_0pt5percent_bobColl)
.add(carolColl)
.sub(_0pt5percent_carolColl),
assert.isAtMost(
th.getDifference(
ETHinSP_C,
aliceColl
.sub(_0pt5percent_aliceColl)
.add(bobColl)
.sub(_0pt5percent_bobColl)
.add(carolColl)
.sub(_0pt5percent_carolColl)
),
1e12
); // (1+2+3 ETH) * 0.995
});

Expand Down Expand Up @@ -821,7 +824,7 @@ contract("Gas compensation tests", async (accounts) => {

assert.isAtMost(
th.getDifference(expectedLiquidatedDebt_A, loggedDebt_A),
1000,
1e13,
);
assert.isAtMost(
th.getDifference(expectedLiquidatedColl_A, loggedColl_A),
Expand Down Expand Up @@ -856,7 +859,7 @@ contract("Gas compensation tests", async (accounts) => {

assert.isAtMost(
th.getDifference(expectedLiquidatedDebt_B, loggedDebt_B),
1000,
1e13,
);
assert.isAtMost(
th.getDifference(expectedLiquidatedColl_B, loggedColl_B),
Expand Down Expand Up @@ -943,7 +946,7 @@ contract("Gas compensation tests", async (accounts) => {

assert.isAtMost(
th.getDifference(expectedLiquidatedDebt_A, loggedDebt_A),
1000,
1e13,
);
assert.isAtMost(
th.getDifference(expectedLiquidatedColl_A, loggedColl_A),
Expand Down Expand Up @@ -987,7 +990,7 @@ contract("Gas compensation tests", async (accounts) => {

assert.isAtMost(
th.getDifference(expectedLiquidatedDebt_B, loggedDebt_B),
1000,
2e13,
);
assert.isAtMost(
th.getDifference(expectedLiquidatedColl_B, loggedColl_B),
Expand Down Expand Up @@ -1062,7 +1065,7 @@ contract("Gas compensation tests", async (accounts) => {

assert.isAtMost(
th.getDifference(expectedLiquidatedDebt_A, loggedDebt_A),
1000,
1e13,
);
assert.isAtMost(
th.getDifference(expectedLiquidatedColl_A, loggedColl_A),
Expand Down Expand Up @@ -1102,7 +1105,7 @@ contract("Gas compensation tests", async (accounts) => {

assert.isAtMost(
th.getDifference(expectedLiquidatedDebt_B, loggedDebt_B),
1000,
2e13,
);
assert.isAtMost(
th.getDifference(expectedLiquidatedColl_B, loggedColl_B),
Expand Down
39 changes: 21 additions & 18 deletions contracts/test/SP_P_TruncationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const TroveManagerTester = artifacts.require("TroveManagerTester");
const BoldToken = artifacts.require("BoldToken");

const GAS_PRICE = 10000000;
let MIN_ANNUAL_INTEREST_RATE;

contract("StabilityPool Scale Factor issue tests", async (accounts) => {
const fundedAccounts = accounts.slice(0, 11);
Expand Down Expand Up @@ -49,30 +50,32 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {
sortedTroves = contracts.sortedTroves;
troveManager = contracts.troveManager;
borrowerOperations = contracts.borrowerOperations;

MIN_ANNUAL_INTEREST_RATE = await contracts.constants._MIN_ANNUAL_INTEREST_RATE();
});

describe("Scale Factor issue tests", async () => {
it("1. Liquidation succeeds after 2 high-fraction liquidations", async () => {
// Whale opens Trove with 100k ETH and sends 50k Bold to A
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, 0, {
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, MIN_ANNUAL_INTEREST_RATE, {
from: whale,
value: dec(100000, "ether"),
});
await boldToken.transfer(A, dec(50000, 18), { from: whale });

// Open 3 Troves with 2000 Bold debt
for (const account of [A, B, C]) {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, 0, {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, MIN_ANNUAL_INTEREST_RATE, {
from: account,
value: dec(15, "ether"),
});

// th.logBN("Trove debt", await th.getTroveEntireDebtByAddress(contracts, account));

assert.isTrue(
(await th.getTroveEntireDebtByAddress(contracts, account)).eq(
th.toBN(dec(2000, 18)),
),
assert.isAtMost(th.getDifference(
await th.getTroveEntireDebtByAddress(contracts, account),
th.toBN(dec(2000, 18))),
1000
);
}

Expand Down Expand Up @@ -135,15 +138,15 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {

it("2. New deposits can be made after 2 high fraction liquidations", async () => {
// Whale opens Trove with 100k ETH and sends 50k Bold to A
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, 0, {
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, MIN_ANNUAL_INTEREST_RATE, {
from: whale,
value: dec(100000, "ether"),
});
await boldToken.transfer(A, dec(50000, 18), { from: whale });

// Open 3 Troves with 2000 Bold debt
for (const account of [A, B, C]) {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, 0, {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, MIN_ANNUAL_INTEREST_RATE, {
from: account,
value: dec(15, "ether"),
});
Expand Down Expand Up @@ -224,15 +227,15 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {

it("3. Liquidation succeeds when P == 1e9 liquidation has newProductFactor == 1e9", async () => {
// Whale opens Trove with 100k ETH and sends 50k Bold to A
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, 0, {
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, MIN_ANNUAL_INTEREST_RATE, {
from: whale,
value: dec(100000, "ether"),
});
await boldToken.transfer(A, dec(50000, 18), { from: whale });

// Open 3 Troves with 2000 Bold debt
for (const account of [A, B, C]) {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, 0, {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, MIN_ANNUAL_INTEREST_RATE, {
from: account,
value: dec(15, "ether"),
});
Expand Down Expand Up @@ -327,15 +330,15 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {

it("4. Liquidation succeeds when P == 1e9 and liquidation has newProductFactor > 1e9", async () => {
// Whale opens Trove with 100k ETH and sends 50k Bold to A
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, 0, {
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, MIN_ANNUAL_INTEREST_RATE, {
from: whale,
value: dec(100000, "ether"),
});
await boldToken.transfer(A, dec(50000, 18), { from: whale });

// Open 3 Troves with 2000 Bold debt
for (const account of [A, B, C]) {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, 0, {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, MIN_ANNUAL_INTEREST_RATE, {
from: account,
value: dec(15, "ether"),
});
Expand Down Expand Up @@ -432,15 +435,15 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {

it("5. Depositor have correct depleted stake after deposit at P == 1e9 and scale changing liq (with newProductFactor == 1e9)", async () => {
// Whale opens Trove with 100k ETH and sends 50k Bold to A
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, 0, {
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, MIN_ANNUAL_INTEREST_RATE, {
from: whale,
value: dec(100000, "ether"),
});
await boldToken.transfer(A, dec(50000, 18), { from: whale });

// Open 3 Troves with 2000 Bold debt
for (const account of [A, B, C]) {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, 0, {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, MIN_ANNUAL_INTEREST_RATE, {
from: account,
value: dec(15, "ether"),
});
Expand Down Expand Up @@ -544,15 +547,15 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {

it("6. Depositor have correct depleted stake after deposit at P == 1e9 and scale changing liq (with newProductFactor > 1e9)", async () => {
// Whale opens Trove with 100k ETH and sends 50k Bold to A
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, 0, {
await th.openTroveWrapper(contracts, await getOpenTroveBoldAmount(dec(100000, 18)), whale, whale, MIN_ANNUAL_INTEREST_RATE, {
from: whale,
value: dec(100000, "ether"),
});
await boldToken.transfer(A, dec(50000, 18), { from: whale });

// Open 3 Troves with 2000 Bold debt
for (const account of [A, B, C]) {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, 0, {
await th.openTroveWrapper(contracts, await getBoldAmountForDesiredDebt(2000), account, account, MIN_ANNUAL_INTEREST_RATE, {
from: account,
value: dec(15, "ether"),
});
Expand Down Expand Up @@ -660,7 +663,7 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {
await getOpenTroveBoldAmount(dec(100000, 18)),
whale,
whale,
0,
MIN_ANNUAL_INTEREST_RATE,
{ from: whale, value: dec(100000, "ether") }
);
assert.isTrue((await th.getTroveEntireDebtByAddress(contracts, whale)).eq(th.toBN(dec(100000, 18))));
Expand All @@ -673,7 +676,7 @@ contract("StabilityPool Scale Factor issue tests", async (accounts) => {
await getOpenTroveBoldAmount(dec(2000, 18)),
account,
account,
0,
MIN_ANNUAL_INTEREST_RATE,
{ from: account, value: dec(15, "ether") }
);
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/test/SortedTrovesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ contract("SortedTroves", async (accounts) => {

it("contains(): returns true for addresses that have opened troves", async () => {
const { troveId: aliceTroveId } = await openTrove({
ICR: toBN(dec(150, 16)),
ICR: toBN(dec(1501, 15)),
extraParams: { from: alice },
});
const { troveId: bobTroveId } = await openTrove({ ICR: toBN(dec(20, 18)), extraParams: { from: bob } });
Expand All @@ -118,7 +118,7 @@ contract("SortedTroves", async (accounts) => {

it("contains(): returns false for addresses that have not opened troves", async () => {
await openTrove({
ICR: toBN(dec(150, 16)),
ICR: toBN(dec(1501, 15)),
extraParams: { from: alice },
});
await openTrove({ ICR: toBN(dec(20, 18)), extraParams: { from: bob } });
Expand Down Expand Up @@ -238,7 +238,7 @@ contract("SortedTroves", async (accounts) => {
// true when list size is 1 and the trove the only one in system
it("contains(): true when list size is 1 and the trove the only one in system", async () => {
const { troveId: aliceTroveId } = await openTrove({
ICR: toBN(dec(150, 16)),
ICR: toBN(dec(151, 16)),
extraParams: { from: alice },
});

Expand All @@ -248,7 +248,7 @@ contract("SortedTroves", async (accounts) => {
// false when list size is 1 and trove is not in the system
it("contains(): false when list size is 1 and trove is not in the system", async () => {
await openTrove({
ICR: toBN(dec(150, 16)),
ICR: toBN(dec(151, 16)),
extraParams: { from: alice },
});

Expand Down
Loading

0 comments on commit 0f746f5

Please sign in to comment.