Skip to content

Commit

Permalink
simplify health tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanzhelyazkov committed Oct 28, 2024
1 parent 201bd04 commit c8941b4
Showing 1 changed file with 61 additions and 69 deletions.
130 changes: 61 additions & 69 deletions deploy/tests/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,40 +310,34 @@ import { getNamedAccounts } from 'hardhat';
for (const { token, whale, decimals } of Object.values(pools)) {
const tknDepositAmount = toWei(100, decimals);

for (let i = 0; i < 5; i++) {
const { liquidity: prevLiquidity } = await poolCollection.poolData(token);
const { liquidity: prevLiquidity } = await poolCollection.poolData(token);

await depositTKN(token, whale, tknDepositAmount);
await depositTKN(token, whale, tknDepositAmount);

const liquidity = await poolCollection.poolLiquidity(token);
expect(liquidity.stakedBalance).to.equal(prevLiquidity.stakedBalance.add(tknDepositAmount));
}
const liquidity = await poolCollection.poolLiquidity(token);
expect(liquidity.stakedBalance).to.equal(prevLiquidity.stakedBalance.add(tknDepositAmount));
}

// perform a few BNT deposit tests
// perform BNT deposit tests
const bntDepositAmount = toWei(10);

await vbntGovernance
.connect(foundationMultisig)
.grantRole(Roles.TokenGovernance.ROLE_GOVERNOR, daoMultisig.address);
await vbntGovernance.connect(daoMultisig).grantRole(Roles.TokenGovernance.ROLE_MINTER, bntPool.address);

for (let i = 0; i < 5; i++) {
const prevBNBNTAmount = await bnBNT.balanceOf(bntWhale.address);
const prevVBNTTokenAmount = await vbnt.balanceOf(bntWhale.address);
const prevTotalSupply = await bnt.totalSupply();
const prevBNBNTAmount = await bnBNT.balanceOf(bntWhale.address);
const prevVBNTTokenAmount = await vbnt.balanceOf(bntWhale.address);
const prevTotalSupply = await bnt.totalSupply();

await depositBNT(bntDepositAmount);
await depositBNT(bntDepositAmount);

const receivedBNBNTAmount = (await bnBNT.balanceOf(bntWhale.address)).sub(prevBNBNTAmount);
const receivedBNBNTAmount = (await bnBNT.balanceOf(bntWhale.address)).sub(prevBNBNTAmount);

expect(receivedBNBNTAmount).to.be.gt(0);
expect(await vbnt.balanceOf(bntWhale.address)).to.equal(
prevVBNTTokenAmount.add(receivedBNBNTAmount)
);
expect(receivedBNBNTAmount).to.be.gt(0);
expect(await vbnt.balanceOf(bntWhale.address)).to.equal(prevVBNTTokenAmount.add(receivedBNBNTAmount));

expect(await bnt.totalSupply()).to.equal(prevTotalSupply.sub(bntDepositAmount));
}
expect(await bnt.totalSupply()).to.equal(prevTotalSupply.sub(bntDepositAmount));
});
});

Expand Down Expand Up @@ -490,64 +484,62 @@ import { getNamedAccounts } from 'hardhat';

const tradeAmount = toWei(10, decimals);

for (let i = 0; i < 5; i++) {
if (!isNativeToken) {
const tokenContract = await Contracts.ERC20.attach(token);
await tokenContract.connect(whale).approve(network.address, tradeAmount);
}
if (!isNativeToken) {
const tokenContract = await Contracts.ERC20.attach(token);
await tokenContract.connect(whale).approve(network.address, tradeAmount);
}

const prevTokenBalance = await getBalance(tokenWithAddress, whale);
const prevBNTBalance = await bnt.balanceOf(whale.address);

const hop1Params = [
token,
bnt.address,
tradeAmount,
1,
MAX_UINT256,
ZERO_ADDRESS,
{
value: isNativeToken ? tradeAmount : BigNumber.from(0)
}
] as const;
const receivedBNTAmount = await network
.connect(whale)
.callStatic.tradeBySourceAmount(...hop1Params);
const res = await network.connect(whale).tradeBySourceAmount(...hop1Params);

let transactionCost = BigNumber.from(0);
if (isNativeToken) {
transactionCost = await getTransactionCost(res);
const prevTokenBalance = await getBalance(tokenWithAddress, whale);
const prevBNTBalance = await bnt.balanceOf(whale.address);

const hop1Params = [
token,
bnt.address,
tradeAmount,
1,
MAX_UINT256,
ZERO_ADDRESS,
{
value: isNativeToken ? tradeAmount : BigNumber.from(0)
}
] as const;
const receivedBNTAmount = await network
.connect(whale)
.callStatic.tradeBySourceAmount(...hop1Params);
const res = await network.connect(whale).tradeBySourceAmount(...hop1Params);

let transactionCost = BigNumber.from(0);
if (isNativeToken) {
transactionCost = await getTransactionCost(res);
}

const newBNTBalance = await bnt.balanceOf(whale.address);

expect(await getBalance(tokenWithAddress, whale)).to.equal(
prevTokenBalance.sub(tradeAmount).sub(transactionCost)
);
expect(receivedBNTAmount).to.be.gt(0);
expect(newBNTBalance).to.equal(prevBNTBalance.add(receivedBNTAmount));
const newBNTBalance = await bnt.balanceOf(whale.address);

await bnt.connect(whale).approve(network.address, newBNTBalance);
expect(await getBalance(tokenWithAddress, whale)).to.equal(
prevTokenBalance.sub(tradeAmount).sub(transactionCost)
);
expect(receivedBNTAmount).to.be.gt(0);
expect(newBNTBalance).to.equal(prevBNTBalance.add(receivedBNTAmount));

const prevTokenBalance2 = await getBalance(tokenWithAddress, whale);
await bnt.connect(whale).approve(network.address, newBNTBalance);

const hop2Params = [bnt.address, token, newBNTBalance, 1, MAX_UINT256, ZERO_ADDRESS] as const;
const receivedTokenAmount = await network
.connect(whale)
.callStatic.tradeBySourceAmount(...hop2Params);
const res2 = await network.connect(whale).tradeBySourceAmount(...hop2Params);
let transactionCost2 = BigNumber.from(0);
if (isNativeToken) {
transactionCost2 = await getTransactionCost(res2);
}
const prevTokenBalance2 = await getBalance(tokenWithAddress, whale);

expect(receivedTokenAmount).to.be.gt(0);
expect(await getBalance(tokenWithAddress, whale)).to.equal(
prevTokenBalance2.add(receivedTokenAmount).sub(transactionCost2)
);
expect(await bnt.balanceOf(whale.address)).to.be.equal(0);
const hop2Params = [bnt.address, token, newBNTBalance, 1, MAX_UINT256, ZERO_ADDRESS] as const;
const receivedTokenAmount = await network
.connect(whale)
.callStatic.tradeBySourceAmount(...hop2Params);
const res2 = await network.connect(whale).tradeBySourceAmount(...hop2Params);
let transactionCost2 = BigNumber.from(0);
if (isNativeToken) {
transactionCost2 = await getTransactionCost(res2);
}

expect(receivedTokenAmount).to.be.gt(0);
expect(await getBalance(tokenWithAddress, whale)).to.equal(
prevTokenBalance2.add(receivedTokenAmount).sub(transactionCost2)
);
expect(await bnt.balanceOf(whale.address)).to.be.equal(0);
}
});
});
Expand Down

0 comments on commit c8941b4

Please sign in to comment.