Skip to content

Commit

Permalink
Make tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymx95 committed Apr 30, 2024
1 parent f5e008a commit 1223365
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion test/trinity/SortedVesselsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract("SortedVessels", async accounts => {
await network.provider.send("evm_revert", [initialSnapshotId])
})

it("contains(): returns true for addresses that have opened vessels", async () => {
it.only("contains(): returns true for addresses that have opened vessels", async () => {
await openVessel({
asset: erc20.address,
ICR: toBN(dec(150, 16)),
Expand Down
13 changes: 8 additions & 5 deletions test/trinity/StabilityPoolTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const mv = testHelpers.MoneyValues
const timeValues = testHelpers.TimeValues

var contracts
var validCollateral
var snapshotId
var initialSnapshotId
var validCollateral
var unsortedCollaterals

const openVessel = async params => th.openVessel(contracts.core, params)
const deploy = async (treasury, distributor, mintingAccounts) => {
Expand All @@ -33,7 +34,7 @@ const deploy = async (treasury, distributor, mintingAccounts) => {
longTimelock = contracts.core.longTimelock

validCollateral = await adminContract.getValidCollateral()

unsortedCollaterals = validCollateral.slice(0)
// getDepositorGains() expects a sorted collateral array
validCollateral = validCollateral.slice(0).sort((a, b) => toBN(a.toLowerCase()).sub(toBN(b.toLowerCase())))

Expand Down Expand Up @@ -1132,8 +1133,9 @@ contract("StabilityPool", async accounts => {
// Expect alice to be entitled to 1000/200000 of the liquidated coll

const aliceExpectedGainERC20 = liquidatedCollERC20.mul(toBN(dec(1000, 18))).div(toBN(dec(200_000, 18)))
const idx = validCollateral.indexOf(erc20.address)
const aliceGainERC20 = (await stabilityPool.getDepositorGains(alice, validCollateral))[1][idx]

const aliceDepositorGains = await stabilityPool.getDepositorGains(alice, validCollateral)
const aliceGainERC20 = aliceDepositorGains[1][aliceDepositorGains[0].indexOf(erc20.address)]
assert.isTrue(aliceExpectedGainERC20.eq(aliceGainERC20))

// Alice withdraws from SP, chooses not to receive gains to avoid transfer/swap costs
Expand All @@ -1150,7 +1152,8 @@ contract("StabilityPool", async accounts => {
await stabilityPool.withdrawFromSP(dec(199_000, 18), validCollateral, { from: whale })
const stability_col_AfterWhaleERC20 = await stabilityPool.getCollateral(erc20.address)

const lastAssetError_Offset= (await stabilityPool.lastAssetError_Offset(idx)).div(toBN(10).pow(toBN(18)))
const collateralId = unsortedCollaterals.indexOf(erc20.address)
const lastAssetError_Offset= (await stabilityPool.lastAssetError_Offset(collateralId)).div(toBN(10).pow(toBN(18)))
assert.closeTo(stability_col_AfterWhaleERC20.sub(aliceExpectedGainERC20), lastAssetError_Offset, toBN(Math.floor(lastAssetError_Offset * 0.001)))
})

Expand Down
22 changes: 14 additions & 8 deletions test/trinity/VesselManagerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var contracts
var snapshotId
var initialSnapshotId
var validCollateral
var unsortedCollaterals

const deploy = async (treasury, distributor, mintingAccounts) => {
contracts = await deploymentHelper.deployTestContracts(treasury, distributor, mintingAccounts)
Expand All @@ -42,7 +43,7 @@ const deploy = async (treasury, distributor, mintingAccounts) => {
longTimelock = contracts.core.longTimelock

validCollateral = await adminContract.getValidCollateral()

unsortedCollaterals = validCollateral.slice(0)
// getDepositorGains() expects a sorted collateral array
validCollateral = validCollateral.slice(0).sort((a, b) => toBN(a.toLowerCase()).sub(toBN(b.toLowerCase())))

Expand Down Expand Up @@ -1169,8 +1170,10 @@ contract("VesselManager", async accounts => {
// Check Bob' SP deposit has absorbed Carol's debt, and he has received her liquidated ETH

const bob_Deposit_Before_Asset = await stabilityPool.getCompoundedDebtTokenDeposits(bob)
const idx = validCollateral.indexOf(erc20.address)
const bob_ETHGain_Before_Asset = (await stabilityPool.getDepositorGains(bob, validCollateral))[1][idx]


const bob_depositor_gains = (await stabilityPool.getDepositorGains(bob, validCollateral))
const bob_ETHGain_Before_Asset = bob_depositor_gains[1][bob_depositor_gains[0].indexOf(erc20.address)]

assert.isAtMost(th.getDifference(bob_Deposit_Before_Asset, B_spDeposit.sub(C_debt_Asset)), 1000000)
assert.isAtMost(th.getDifference(bob_ETHGain_Before_Asset, C_collateral_Asset), 1000)
Expand Down Expand Up @@ -1199,13 +1202,13 @@ contract("VesselManager", async accounts => {
Check Bob' SP deposit has been reduced to 50 TRI, and his ETH gain has increased to 1.5 ETH. */
const alice_Deposit_After_Asset = (await stabilityPool.getCompoundedDebtTokenDeposits(alice)).toString()
const alice_ETHGain_After_Asset = (await stabilityPool.getDepositorGains(alice, validCollateral))[1][
idx
].toString()
const alice_depositor_gains = (await stabilityPool.getDepositorGains(alice, validCollateral))
const alice_ETHGain_After_Asset = alice_depositor_gains[1][alice_depositor_gains[0].indexOf(erc20.address)].toString()

const totalDeposits_Asset = bob_Deposit_Before_Asset.add(A_spDeposit)

const lastAssetError_Offset= (await stabilityPool.lastAssetError_Offset(idx)).div(toBN(10).pow(toBN(18)))
const collateral_Id = unsortedCollaterals.indexOf(erc20.address)
const lastAssetError_Offset= (await stabilityPool.lastAssetError_Offset(collateral_Id)).div(toBN(10).pow(toBN(18)))
const lastDebtTokenLossError_Offset = (await stabilityPool.lastDebtTokenLossError_Offset()).div(toBN(10).pow(toBN(18)))

assert.isAtMost(
Expand All @@ -1224,7 +1227,10 @@ contract("VesselManager", async accounts => {
)

const bob_Deposit_After_Asset = await stabilityPool.getCompoundedDebtTokenDeposits(bob)
const bob_ETHGain_After_Asset = (await stabilityPool.getDepositorGains(bob, validCollateral))[1][idx]

const bob_depositor_gains_After_Asset = (await stabilityPool.getDepositorGains(bob, validCollateral))
const bob_ETHGain_After_Asset = bob_depositor_gains_After_Asset[1][bob_depositor_gains_After_Asset[0].indexOf(erc20.address)]


assert.isAtMost(
th.getDifference(
Expand Down

0 comments on commit 1223365

Please sign in to comment.