Skip to content

Commit

Permalink
SDK tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
asimpk committed Jun 22, 2023
1 parent 09acb95 commit 2b04e97
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 306 deletions.
2 changes: 1 addition & 1 deletion packages/dev-frontend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
REACT_APP_VERSION=18af2f60c636ebe126678a42532b8e7bced2aa2a
REACT_APP_VERSION=09acb9525813dd1e897e1b2cbf3c92c085a36fad
REACT_APP_CHAIN_NAME='Alchemy - Polygon Mumbai'
REACT_APP_CHAIN_ID=80001
REACT_APP_CURRENCY_NAME=MATIC
Expand Down
13 changes: 0 additions & 13 deletions packages/lib-ethers/abi/ActivePool.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,18 +467,5 @@
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "troveRedemptorAddress",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]
167 changes: 82 additions & 85 deletions packages/lib-ethers/test/MultiAssetGasEstimationFeeDecay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Trove,
KUSD_MINIMUM_NET_DEBT,
KUSD_LIQUIDATION_RESERVE,
Decimal
} from "@kumodao/lib-base";


Expand Down Expand Up @@ -37,12 +36,10 @@ describe("EthersKumoGasEstimationFeeDecay", async () => {

let kumo: EthersKumo;
let otherKumos: EthersKumo[];
let redeemedUser: Signer
let someMoreUsers: Signer[]

let mockAssetAddress: string;

const gasLimit = BigNumber.from(2500000);
let gasLimit = BigNumber.from(2500000);

before(async function () {
if (network.name !== "hardhat") {
Expand All @@ -52,110 +49,111 @@ describe("EthersKumoGasEstimationFeeDecay", async () => {

[deployer, funder, user, ...otherUsers] = await ethers.getSigners();
deployment = await deployKumo(deployer);
mockAssetAddress = deployment.addresses['mockAsset1'];
kumo = await connectToDeployment(deployment, user);

expect(kumo).to.be.an.instanceOf(EthersKumo);

[redeemedUser, ...someMoreUsers] = otherUsers.slice(0, 21);
const [redeemedUser, ...someMoreUsers] = otherUsers.slice(0, 21);
[kumo, ...otherKumos] = await connectUsers(deployment, [user, ...someMoreUsers]);
})

mockAssetContracts.forEach(async (mockAssetContract, index) => {
describe(`Gas estimation fee decay Multi Asset ${mockAssetContract.name}`, () => {
before(async function () {
this.timeout("1m");
// Create a "slope" of Troves with similar, but slightly decreasing ICRs
await openTroves(
deployment,
someMoreUsers,
funder,
someMoreUsers.map((_, i) => ({
depositCollateral: 20,
borrowKUSD: KUSD_MINIMUM_NET_DEBT.add(i / 10)
})), mockAssetAddress, gasLimit
);
// Sweep KUSD
await Promise.all(
otherKumos.map(async otherKumo =>
otherKumo.sendKUSD(await user.getAddress(), await otherKumo.getKUSDBalance())
)
);

// Create a "slope" of Troves with similar, but slightly decreasing ICRs
mockAssetAddress = deployment.addresses[mockAssetContract.contract];
const price = await kumo.getPrice(mockAssetAddress);

await openTroves(
deployment,
someMoreUsers,
funder,
someMoreUsers.map((_, i) => ({
depositCollateral: 20,
borrowKUSD: KUSD_MINIMUM_NET_DEBT.add(i / 10)
})), mockAssetAddress, gasLimit
);
// Sweep KUSD
if (index < 1) {
await Promise.all(
otherKumos.map(async otherKumo =>
otherKumo.sendKUSD(await user.getAddress(), await otherKumo.getKUSDBalance())
)
);
}
// Create a "designated victim" Trove that'll be redeemed
const redeemedTroveDebt = await kumo
.getKUSDBalance()
.then(x => x.div(10).add(KUSD_LIQUIDATION_RESERVE));
const redeemedTroveCollateral = redeemedTroveDebt.mulDiv(1.1, price);
const redeemedTrove = new Trove(redeemedTroveCollateral, redeemedTroveDebt);

const redeemedTrove = new Trove(Decimal.from(20.91045), Decimal.from(3801.9));
await openTroves(deployment, [redeemedUser], funder, [Trove.recreate(redeemedTrove)], mockAssetAddress, gasLimit);
// Jump past bootstrap period
await openTroves(deployment, [redeemedUser], funder, [Trove.recreate(redeemedTrove)], mockAssetAddress, gasLimit);

await increaseTime(60 * 60 * 24 * 15);
// Jump past bootstrap period
await increaseTime(60 * 60 * 24 * 15);

// Increase the borrowing rate by redeeming
const { actualKUSDAmount } = await kumo.redeemKUSD(mockAssetAddress, redeemedTrove.netDebt, undefined, { gasLimit });
// Increase the borrowing rate by redeeming
const { actualKUSDAmount } = await kumo.redeemKUSD(mockAssetAddress, redeemedTrove.netDebt);

expect(`${actualKUSDAmount}`).to.equal(`${redeemedTrove.netDebt}`);
expect(`${actualKUSDAmount}`).to.equal(`${redeemedTrove.netDebt}`);

const borrowingRate = await kumo.getFees(mockAssetAddress).then(fees => Number(fees.borrowingRate()));
expect(borrowingRate).to.be.within(0.04, 0.049); // make sure it's high, but not clamped to 5%
});
const borrowingRate = await kumo.getFees(mockAssetAddress).then(fees => Number(fees.borrowingRate()));
expect(borrowingRate).to.be.within(0.04, 0.049); // make sure it's high, but not clamped to 5%

// Always setup same initial balance for user
beforeEach(async () => {
const targetBalance = BigNumber.from(STARTING_BALANCE.hex);
});

await setUpInitialUserBalance(user, funder, gasLimit)
expect(`${await user.getBalance()}`).to.equal(`${targetBalance}`);
});
// Always setup same initial balance for user
beforeEach(async () => {
const targetBalance = BigNumber.from(STARTING_BALANCE.hex);

it(`should predict the gas increase due to fee decay ${mockAssetContract.name}`, async function () {
this.timeout("1m");
await setUpInitialUserBalance(user, funder, gasLimit)
expect(`${await user.getBalance()}`).to.equal(`${targetBalance}`);
});

const [bottomTrove] = await kumo.getTroves(mockAssetAddress, {
first: 1,
sortedBy: "ascendingCollateralRatio"
});
it(`should predict the gas increase due to fee decay`, async function () {
this.timeout("1m");

const borrowingRate = await kumo.getFees(mockAssetAddress).then(fees => fees.borrowingRate());
const [bottomTrove] = await kumo.getTroves(mockAssetAddress, {
first: 1,
sortedBy: "ascendingCollateralRatio"
});

for (const [borrowingFeeDecayToleranceMinutes, roughGasHeadroom] of [
[10, 135000],
[20, 255000],
[30, 340000]
]) {
const tx = await kumo.populate.openTrove(
Trove.recreate(bottomTrove, borrowingRate),
mockAssetAddress,
{
borrowingFeeDecayToleranceMinutes
}
);
expect(tx.gasHeadroom).to.be.within(roughGasHeadroom - 1000, roughGasHeadroom + 1000);
const borrowingRate = await kumo.getFees(mockAssetAddress).then(fees => fees.borrowingRate());

for (const [borrowingFeeDecayToleranceMinutes, roughGasHeadroom] of [
[10, 135000],
[20, 255000],
[30, 340000]
]) {
const tx = await kumo.populate.openTrove(
Trove.recreate(bottomTrove, borrowingRate),
mockAssetAddress,
{
borrowingFeeDecayToleranceMinutes
}
});
);
console.log('tx', tx.gasHeadroom?.toString())
expect(tx.gasHeadroom).to.be.within(roughGasHeadroom - 1000, roughGasHeadroom + 1000);
}
});

it(`should include enough gas for the TX to succeed after pending ${mockAssetContract.name}`, async function () {
this.timeout("1m");
it(`should include enough gas for the TX to succeed after pending`, async function () {
this.timeout("1m");

const [bottomTrove] = await kumo.getTroves(mockAssetAddress, {
first: 1,
sortedBy: "ascendingCollateralRatio"
});
const [bottomTrove] = await kumo.getTroves(mockAssetAddress, {
first: 1,
sortedBy: "ascendingCollateralRatio"
});

const borrowingRate = await kumo.getFees(mockAssetAddress).then(fees => fees.borrowingRate());
const borrowingRate = await kumo.getFees(mockAssetAddress).then(fees => fees.borrowingRate());

const tx = await kumo.populate.openTrove(
Trove.recreate(bottomTrove.multiply(2), borrowingRate),
mockAssetAddress,
{ borrowingFeeDecayToleranceMinutes: 60 },
{ gasLimit }
);
const tx = await kumo.populate.openTrove(
Trove.recreate(bottomTrove.multiply(2), borrowingRate),
mockAssetAddress,
{ borrowingFeeDecayToleranceMinutes: 60 },
{ gasLimit }
);

await increaseTime(60 * 60);
await waitForSuccess(tx.send());
});

await increaseTime(60 * 60);
await waitForSuccess(tx.send());
});
});
})

mockAssetContracts.forEach(async mockAssetContract => {
describe(`Gas estimation fee decay Multi Asset Independent tests ${mockAssetContract.name}`, () => {
Expand Down Expand Up @@ -291,6 +289,5 @@ describe("EthersKumoGasEstimationFeeDecay", async () => {
await waitForSuccess(tx.send());
});
});

})
});
Loading

0 comments on commit 2b04e97

Please sign in to comment.