Skip to content

Commit

Permalink
Merge pull request #790 from liquity/wollooo-fixed-test_2
Browse files Browse the repository at this point in the history
contracts: Fix test gas issues
  • Loading branch information
bingen authored Mar 29, 2022
2 parents 4bc42e7 + 6327ff3 commit 0c61ed8
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 218 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/brownie-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ networks:
default_contract_owner: true
cmd_settings:
port: 8545
gas_limit: 8000000
gas_limit: 9000000
accounts: 1000
chain_id: 1337
network_id: 1588949648
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ module.exports = {
hardhat: {
accounts: accountsList,
gas: 10000000, // tx gas limit
blockGasLimit: 15000000,
blockGasLimit: 15000000,
gasPrice: 20000000000,
hardfork: "london"
initialBaseFeePerGas: 0,
},
mainnet: {
url: alchemyUrl(),
Expand Down
14 changes: 13 additions & 1 deletion packages/contracts/test/BorrowerOperationsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,18 @@ contract('BorrowerOperations', async accounts => {
await assertRevert(repayTxAPromise, "BorrowerOps: Trove's net debt must be greater than minimum")
})

it("adjustTrove(): Reverts if repaid amount is greater than current debt", async () => {
const { totalDebt } = await openTrove({ extraLUSDAmount: toBN(dec(10000, 18)), ICR: toBN(dec(10, 18)), extraParams: { from: alice } })
LUSD_GAS_COMPENSATION = await borrowerOperations.LUSD_GAS_COMPENSATION()
const repayAmount = totalDebt.sub(LUSD_GAS_COMPENSATION).add(toBN(1))
await openTrove({ extraLUSDAmount: repayAmount, ICR: toBN(dec(150, 16)), extraParams: { from: bob } })

await lusdToken.transfer(alice, repayAmount, { from: bob })

await assertRevert(borrowerOperations.adjustTrove(th._100pct, 0, repayAmount, false, alice, alice, { from: alice }),
"SafeMath: subtraction overflow")
})

it("repayLUSD(): reverts when calling address does not have active trove", async () => {
await openTrove({ extraLUSDAmount: toBN(dec(10000, 18)), ICR: toBN(dec(2, 18)), extraParams: { from: alice } })
await openTrove({ extraLUSDAmount: toBN(dec(10000, 18)), ICR: toBN(dec(2, 18)), extraParams: { from: bob } })
Expand Down Expand Up @@ -2389,7 +2401,7 @@ contract('BorrowerOperations', async accounts => {
assert.isTrue(totalStakesAfter.eq(totalStakesBefore.add(toBN(dec(1, 18)))))
})

it("adjustTrove(): updates borrower's stake and totalStakes with a coll decrease", async () => {
it("adjustTrove(): updates borrower's stake and totalStakes with a coll decrease", async () => {
await openTrove({ extraLUSDAmount: toBN(dec(10000, 18)), ICR: toBN(dec(10, 18)), extraParams: { from: whale } })

await openTrove({ extraLUSDAmount: toBN(dec(10000, 18)), ICR: toBN(dec(10, 18)), extraParams: { from: alice } })
Expand Down
80 changes: 41 additions & 39 deletions packages/contracts/test/GasCompensationTest.js

Large diffs are not rendered by default.

62 changes: 32 additions & 30 deletions packages/contracts/test/LQTYStakingFeeRewardsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const assertRevert = th.assertRevert
const toBN = th.toBN
const ZERO = th.toBN('0')

const GAS_PRICE = 10000000

/* NOTE: These tests do not test for specific ETH and LUSD gain values. They only test that the
* gains are non-zero, occur when they should, and are in correct proportion to the user's stake.
*
Expand Down Expand Up @@ -94,7 +96,7 @@ contract('LQTYStaking revenue share tests', async accounts => {
await th.fastForwardTime(timeValues.SECONDS_IN_ONE_YEAR, web3.currentProvider)

// multisig transfers LQTY to staker A
await lqtyToken.transfer(A, dec(100, 18), {from: multisig})
await lqtyToken.transfer(A, dec(100, 18), {from: multisig, gasPrice: GAS_PRICE})

// console.log(`A lqty bal: ${await lqtyToken.balanceOf(A)}`)

Expand All @@ -108,7 +110,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand Down Expand Up @@ -137,15 +139,15 @@ contract('LQTYStaking revenue share tests', async accounts => {
await th.fastForwardTime(timeValues.SECONDS_IN_ONE_YEAR, web3.currentProvider)

// multisig transfers LQTY to staker A
await lqtyToken.transfer(A, dec(100, 18), {from: multisig})
await lqtyToken.transfer(A, dec(100, 18), {from: multisig, gasPrice: GAS_PRICE})

// Check ETH fee per unit staked is zero
const F_ETH_Before = await lqtyStaking.F_ETH()
assert.equal(F_ETH_Before, '0')

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand Down Expand Up @@ -182,7 +184,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), gasPrice= GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand Down Expand Up @@ -226,7 +228,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand Down Expand Up @@ -266,7 +268,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand All @@ -277,7 +279,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const C_BalBeforeREdemption = await lusdToken.balanceOf(C)
// C redeems
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18))
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const C_BalAfterRedemption = await lusdToken.balanceOf(C)
assert.isTrue(C_BalAfterRedemption.lt(C_BalBeforeREdemption))
Expand Down Expand Up @@ -307,13 +309,13 @@ contract('LQTYStaking revenue share tests', async accounts => {
const A_LUSDBalance_Before = toBN(await lusdToken.balanceOf(A))

// A un-stakes
await lqtyStaking.unstake(dec(100, 18), {from: A, gasPrice: 0})
const GAS_Used = th.gasUsed(await lqtyStaking.unstake(dec(100, 18), {from: A, gasPrice: GAS_PRICE }))

const A_ETHBalance_After = toBN(await web3.eth.getBalance(A))
const A_LUSDBalance_After = toBN(await lusdToken.balanceOf(A))


const A_ETHGain = A_ETHBalance_After.sub(A_ETHBalance_Before)
const A_ETHGain = A_ETHBalance_After.sub(A_ETHBalance_Before).add(toBN(GAS_Used * GAS_PRICE))
const A_LUSDGain = A_LUSDBalance_After.sub(A_LUSDBalance_Before)

assert.isAtMost(th.getDifference(expectedTotalETHGain, A_ETHGain), 1000)
Expand All @@ -339,7 +341,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand All @@ -350,7 +352,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const C_BalBeforeREdemption = await lusdToken.balanceOf(C)
// C redeems
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18))
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const C_BalAfterRedemption = await lusdToken.balanceOf(C)
assert.isTrue(C_BalAfterRedemption.lt(C_BalBeforeREdemption))
Expand Down Expand Up @@ -380,12 +382,12 @@ contract('LQTYStaking revenue share tests', async accounts => {
const A_LUSDBalance_Before = toBN(await lusdToken.balanceOf(A))

// A tops up
await lqtyStaking.stake(dec(50, 18), {from: A, gasPrice: 0})
const GAS_Used = th.gasUsed(await lqtyStaking.stake(dec(50, 18), {from: A, gasPrice: GAS_PRICE }))

const A_ETHBalance_After = toBN(await web3.eth.getBalance(A))
const A_LUSDBalance_After = toBN(await lusdToken.balanceOf(A))

const A_ETHGain = A_ETHBalance_After.sub(A_ETHBalance_Before)
const A_ETHGain = A_ETHBalance_After.sub(A_ETHBalance_Before).add(toBN(GAS_Used * GAS_PRICE))
const A_LUSDGain = A_LUSDBalance_After.sub(A_LUSDBalance_Before)

assert.isAtMost(th.getDifference(expectedTotalETHGain, A_ETHGain), 1000)
Expand All @@ -411,7 +413,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand All @@ -422,7 +424,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const C_BalBeforeREdemption = await lusdToken.balanceOf(C)
// C redeems
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18))
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const C_BalAfterRedemption = await lusdToken.balanceOf(C)
assert.isTrue(C_BalAfterRedemption.lt(C_BalBeforeREdemption))
Expand Down Expand Up @@ -457,7 +459,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const B_BalBeforeREdemption = await lusdToken.balanceOf(B)
// B redeems
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18))
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const B_BalAfterRedemption = await lusdToken.balanceOf(B)
assert.isTrue(B_BalAfterRedemption.lt(B_BalBeforeREdemption))
Expand All @@ -468,7 +470,7 @@ contract('LQTYStaking revenue share tests', async accounts => {

const C_BalBeforeREdemption = await lusdToken.balanceOf(C)
// C redeems
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18))
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(100, 18), gasPrice = GAS_PRICE)

const C_BalAfterRedemption = await lusdToken.balanceOf(C)
assert.isTrue(C_BalAfterRedemption.lt(C_BalBeforeREdemption))
Expand Down Expand Up @@ -530,12 +532,12 @@ contract('LQTYStaking revenue share tests', async accounts => {
assert.equal(await lqtyStaking.totalLQTYStaked(), dec(600, 18))

// F redeems
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(F, contracts, dec(45, 18))
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(F, contracts, dec(45, 18), gasPrice = GAS_PRICE)
const emittedETHFee_1 = toBN((await th.getEmittedRedemptionValues(redemptionTx_1))[3])
assert.isTrue(emittedETHFee_1.gt(toBN('0')))

// G redeems
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(G, contracts, dec(197, 18))
const redemptionTx_2 = await th.redeemCollateralAndGetTxObject(G, contracts, dec(197, 18), gasPrice = GAS_PRICE)
const emittedETHFee_2 = toBN((await th.getEmittedRedemptionValues(redemptionTx_2))[3])
assert.isTrue(emittedETHFee_2.gt(toBN('0')))

Expand All @@ -559,7 +561,7 @@ contract('LQTYStaking revenue share tests', async accounts => {
assert.equal(await lqtyStaking.totalLQTYStaked(), dec(650, 18))

// G redeems
const redemptionTx_3 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(197, 18))
const redemptionTx_3 = await th.redeemCollateralAndGetTxObject(C, contracts, dec(197, 18), gasPrice = GAS_PRICE)
const emittedETHFee_3 = toBN((await th.getEmittedRedemptionValues(redemptionTx_3))[3])
assert.isTrue(emittedETHFee_3.gt(toBN('0')))

Expand Down Expand Up @@ -623,10 +625,10 @@ contract('LQTYStaking revenue share tests', async accounts => {
const D_LUSDBalance_Before = toBN(await lusdToken.balanceOf(D))

// A-D un-stake
const unstake_A = await lqtyStaking.unstake(dec(100, 18), {from: A, gasPrice: 0})
const unstake_B = await lqtyStaking.unstake(dec(200, 18), {from: B, gasPrice: 0})
const unstake_C = await lqtyStaking.unstake(dec(400, 18), {from: C, gasPrice: 0})
const unstake_D = await lqtyStaking.unstake(dec(50, 18), {from: D, gasPrice: 0})
const A_GAS_Used = th.gasUsed(await lqtyStaking.unstake(dec(100, 18), {from: A, gasPrice: GAS_PRICE }))
const B_GAS_Used = th.gasUsed(await lqtyStaking.unstake(dec(200, 18), {from: B, gasPrice: GAS_PRICE }))
const C_GAS_Used = th.gasUsed(await lqtyStaking.unstake(dec(400, 18), {from: C, gasPrice: GAS_PRICE }))
const D_GAS_Used = th.gasUsed(await lqtyStaking.unstake(dec(50, 18), {from: D, gasPrice: GAS_PRICE }))

// Confirm all depositors could withdraw

Expand All @@ -645,13 +647,13 @@ contract('LQTYStaking revenue share tests', async accounts => {
const D_LUSDBalance_After = toBN(await lusdToken.balanceOf(D))

// Get ETH and LUSD gains
const A_ETHGain = A_ETHBalance_After.sub(A_ETHBalance_Before)
const A_ETHGain = A_ETHBalance_After.sub(A_ETHBalance_Before).add(toBN(A_GAS_Used * GAS_PRICE))
const A_LUSDGain = A_LUSDBalance_After.sub(A_LUSDBalance_Before)
const B_ETHGain = B_ETHBalance_After.sub(B_ETHBalance_Before)
const B_ETHGain = B_ETHBalance_After.sub(B_ETHBalance_Before).add(toBN(B_GAS_Used * GAS_PRICE))
const B_LUSDGain = B_LUSDBalance_After.sub(B_LUSDBalance_Before)
const C_ETHGain = C_ETHBalance_After.sub(C_ETHBalance_Before)
const C_ETHGain = C_ETHBalance_After.sub(C_ETHBalance_Before).add(toBN(C_GAS_Used * GAS_PRICE))
const C_LUSDGain = C_LUSDBalance_After.sub(C_LUSDBalance_Before)
const D_ETHGain = D_ETHBalance_After.sub(D_ETHBalance_Before)
const D_ETHGain = D_ETHBalance_After.sub(D_ETHBalance_Before).add(toBN(D_GAS_Used * GAS_PRICE))
const D_LUSDGain = D_LUSDBalance_After.sub(D_LUSDBalance_Before)

// Check gains match expected amounts
Expand Down Expand Up @@ -688,7 +690,7 @@ contract('LQTYStaking revenue share tests', async accounts => {


// B makes a redemption, creating ETH gain for proxy
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(45, 18))
const redemptionTx_1 = await th.redeemCollateralAndGetTxObject(B, contracts, dec(45, 18), gasPrice = GAS_PRICE)

const proxy_ETHGain = await lqtyStaking.getPendingETHGain(nonPayable.address)
assert.isTrue(proxy_ETHGain.gt(toBN('0')))
Expand Down
24 changes: 14 additions & 10 deletions packages/contracts/test/ProxyBorrowerWrappersScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const timeValues = testHelpers.TimeValues
const ZERO_ADDRESS = th.ZERO_ADDRESS
const assertRevert = th.assertRevert

const GAS_PRICE = 10000000


const {
buildUserProxies,
BorrowerOperationsProxy,
Expand Down Expand Up @@ -97,16 +100,17 @@ contract('BorrowerWrappers', async accounts => {
const proxyAddress = borrowerWrappers.getProxyAddressFromUser(alice)

// send some ETH to proxy
await web3.eth.sendTransaction({ from: owner, to: proxyAddress, value: amount })
await web3.eth.sendTransaction({ from: owner, to: proxyAddress, value: amount, gasPrice: GAS_PRICE })
assert.equal(await web3.eth.getBalance(proxyAddress), amount.toString())

const balanceBefore = toBN(await web3.eth.getBalance(alice))

// recover ETH
await borrowerWrappers.transferETH(alice, amount, { from: alice, gasPrice: 0 })
const gas_Used = th.gasUsed(await borrowerWrappers.transferETH(alice, amount, { from: alice, gasPrice: GAS_PRICE }))

const balanceAfter = toBN(await web3.eth.getBalance(alice))

assert.equal(balanceAfter.sub(balanceBefore), amount.toString())
const expectedBalance = toBN(balanceBefore.sub(toBN(gas_Used * GAS_PRICE)))
assert.equal(balanceAfter.sub(expectedBalance), amount.toString())
})

it('non proxy owner cannot recover ETH', async () => {
Expand Down Expand Up @@ -173,7 +177,7 @@ contract('BorrowerWrappers', async accounts => {
await th.fastForwardTime(timeValues.SECONDS_IN_ONE_WEEK * 2, web3.currentProvider)

// whale redeems 150 LUSD
await th.redeemCollateral(whale, contracts, redeemAmount)
await th.redeemCollateral(whale, contracts, redeemAmount, GAS_PRICE)
assert.equal(await web3.eth.getBalance(proxyAddress), '0')

// surplus: 5 - 150/200
Expand Down Expand Up @@ -205,7 +209,7 @@ contract('BorrowerWrappers', async accounts => {
await th.fastForwardTime(timeValues.SECONDS_IN_ONE_WEEK * 2, web3.currentProvider)

// whale redeems 150 LUSD
await th.redeemCollateral(whale, contracts, redeemAmount)
await th.redeemCollateral(whale, contracts, redeemAmount, GAS_PRICE)
assert.equal(await web3.eth.getBalance(proxyAddress), '0')

// surplus: 5 - 150/200
Expand Down Expand Up @@ -370,7 +374,7 @@ contract('BorrowerWrappers', async accounts => {

// whale redeems 100 LUSD
const redeemedAmount = toBN(dec(100, 18))
await th.redeemCollateral(whale, contracts, redeemedAmount)
await th.redeemCollateral(whale, contracts, redeemedAmount, GAS_PRICE)

// Bob tries to claims staking gains in behalf of Alice
const proxy = borrowerWrappers.getProxyFromUser(alice)
Expand Down Expand Up @@ -411,7 +415,7 @@ contract('BorrowerWrappers', async accounts => {

// whale redeems 100 LUSD
const redeemedAmount = toBN(dec(100, 18))
await th.redeemCollateral(whale, contracts, redeemedAmount)
await th.redeemCollateral(whale, contracts, redeemedAmount, GAS_PRICE)

const ethBalanceBefore = await web3.eth.getBalance(borrowerOperations.getProxyAddressFromUser(alice))
const troveCollBefore = await troveManager.getTroveColl(alice)
Expand Down Expand Up @@ -481,7 +485,7 @@ contract('BorrowerWrappers', async accounts => {

// whale redeems 100 LUSD
const redeemedAmount = toBN(dec(100, 18))
await th.redeemCollateral(whale, contracts, redeemedAmount)
await th.redeemCollateral(whale, contracts, redeemedAmount, GAS_PRICE)

// Alice ETH gain is ((150/2000) * (redemption fee over redeemedAmount) / price)
const redemptionFee = await troveManager.getRedemptionFeeWithDecay(redeemedAmount)
Expand Down Expand Up @@ -642,7 +646,7 @@ contract('BorrowerWrappers', async accounts => {

// whale redeems 100 LUSD
const redeemedAmount = toBN(dec(100, 18))
await th.redeemCollateral(whale, contracts, redeemedAmount)
await th.redeemCollateral(whale, contracts, redeemedAmount, GAS_PRICE)

// Alice ETH gain is ((150/2000) * (redemption fee over redeemedAmount) / price)
const redemptionFee = await troveManager.getRedemptionFeeWithDecay(redeemedAmount)
Expand Down
Loading

0 comments on commit 0c61ed8

Please sign in to comment.