Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete comprehensive cache tests #121

Merged
merged 7 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions contracts/masset/BasketManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,13 @@ contract BasketManager is
uint256 cache = IERC20(bAsset).balanceOf(address(oldIntegration));
// 2.1. Withdraw from the lending market
uint256 lendingBal = oldIntegration.checkBalance(bAsset);
oldIntegration.withdraw(address(this), bAsset, lendingBal, false);
if(lendingBal > 0) {
oldIntegration.withdraw(address(this), bAsset, lendingBal, false);
}
// 2.2. Withdraw from the cache, if any
oldIntegration.withdrawRaw(address(this), bAsset, cache);
if(cache > 0){
oldIntegration.withdrawRaw(address(this), bAsset, cache);
}
uint256 total = lendingBal.add(cache);

// 3. Update the integration address for this bAsset
Expand Down
7 changes: 3 additions & 4 deletions contracts/masset/Masset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ contract Masset is
// 2.2 - Else Deposit X if Cache > %
else {
// This check is in place to ensure that any token with a txFee is rejected
// Audit notes: Assumption made that if no fee is collected here then there is no txfee
require(transferred == _quantity, "Asset not fully transferred");

quantityDeposited = transferred;
Expand Down Expand Up @@ -356,14 +355,14 @@ contract Masset is
Cache memory cache = _getCacheDetails();

// 3. Deposit the input tokens
(uint256 amnountIn, ) =
(uint256 amountIn, ) =
_depositTokens(args.input, inputDetails.bAsset.ratio, inputDetails.integrator, inputDetails.bAsset.isTransferFeeCharged, _quantity, cache.maxCache);
// 3.1. Update the input balance
basketManager.increaseVaultBalance(inputDetails.index, inputDetails.integrator, amnountIn);
basketManager.increaseVaultBalance(inputDetails.index, inputDetails.integrator, amountIn);

// 4. Validate the swap
(bool swapValid, string memory swapValidityReason, uint256 swapOutput, bool applySwapFee) =
forgeValidator.validateSwap(cache.vaultBalanceSum, inputDetails.bAsset, outputDetails.bAsset, amnountIn);
forgeValidator.validateSwap(cache.vaultBalanceSum, inputDetails.bAsset, outputDetails.bAsset, amountIn);
require(swapValid, swapValidityReason);
require(swapOutput > 0, "Must withdraw something");

Expand Down
13 changes: 12 additions & 1 deletion test-utils/machines/massetMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,14 @@ export class MassetMachine {
integratorBalBefore: number | BN,
bAsset: Basset,
): Promise<ActionDetails> {
const hasTxFee = bAsset.isTransferFeeCharged;
if (hasTxFee) {
return {
expectInteraction: true,
amount,
rawBalance: new BN(0),
};
}
const totalSupply = await mAsset.totalSupply();
const surplus = await mAsset.surplus();
const cacheSize = await mAsset.cacheSize();
Expand All @@ -609,7 +617,10 @@ export class MassetMachine {
type === "deposit"
? newSum.sub(maxC.divn(2))
: BN.min(
maxC.divn(2).add(amount).sub(new BN(integratorBalBefore)),
maxC
.divn(2)
.add(amount)
.sub(new BN(integratorBalBefore)),
new BN(bAsset.vaultBalance).sub(new BN(integratorBalBefore)),
),
rawBalance:
Expand Down
2 changes: 1 addition & 1 deletion test-utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const applyRatioMassetToBasset = (input: BN, ratio: BN | string): BN => {
};

// How many mAssets is this bAsset worth
export const applyRatio = (bAssetQ: BN | string, ratio: BN | string): BN => {
export const applyRatio = (bAssetQ: BN | string | number, ratio: BN | string): BN => {
return new BN(bAssetQ).mul(new BN(ratio)).div(ratioScale);
};

Expand Down
50 changes: 45 additions & 5 deletions test/masset/TestBasketManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,9 @@ contract("BasketManager", async (accounts) => {
let newMigration: t.AaveV2IntegrationInstance;
let maliciousIntegration: t.MaliciousAaveIntegrationInstance;
let transferringAsset: t.MockERC20Instance;
before(async () => {
beforeEach(async () => {
await createNewBasketManager();
[, , transferringAsset] = integrationDetails.bAssets;
await transferringAsset.transfer(aaveIntegration.address, new BN(10000));
await aaveIntegration.deposit(transferringAsset.address, new BN(9000), false, {
from: governance,
});
newMigration = await AaveIntegration.new();
await newMigration.initialize(
nexus.address,
Expand Down Expand Up @@ -829,6 +825,10 @@ contract("BasketManager", async (accounts) => {
);
});
it("should fail if the full amount is not transferred and deposited", async () => {
await transferringAsset.transfer(aaveIntegration.address, new BN(10000));
await aaveIntegration.deposit(transferringAsset.address, new BN(9000), false, {
from: governance,
});
await expectRevert(
basketManager.migrateBassets(
[transferringAsset.address],
Expand All @@ -841,6 +841,10 @@ contract("BasketManager", async (accounts) => {
);
});
it("should move all bAssets from a to b", async () => {
await transferringAsset.transfer(aaveIntegration.address, new BN(10000));
await aaveIntegration.deposit(transferringAsset.address, new BN(9000), false, {
from: governance,
});
// get balances before
const bal = await aaveIntegration.checkBalance.call(transferringAsset.address);
expect(bal).bignumber.eq(new BN(9000));
Expand Down Expand Up @@ -875,6 +879,42 @@ contract("BasketManager", async (accounts) => {
newIntegrator: newMigration.address,
});
});
it("should pass if either rawBalance or balance are 0", async () => {
await transferringAsset.transfer(aaveIntegration.address, new BN(10000));
await aaveIntegration.deposit(transferringAsset.address, new BN(10000), false, {
from: governance,
});
// get balances before
const bal = await aaveIntegration.checkBalance.call(transferringAsset.address);
expect(bal).bignumber.eq(new BN(10000));
const rawBal = await transferringAsset.balanceOf(aaveIntegration.address);
expect(rawBal).bignumber.eq(new BN(0));
let integratorAddress = await basketManager.getBassetIntegrator(
transferringAsset.address,
);
expect(integratorAddress).eq(aaveIntegration.address);
// call migrate
const tx = await basketManager.migrateBassets(
[transferringAsset.address],
newMigration.address,
{
from: sa.governor,
},
);
// moves all bAssets from old to new
const migratedBal = await newMigration.checkBalance.call(transferringAsset.address);
expect(migratedBal).bignumber.eq(bal);
const migratedRawBal = await transferringAsset.balanceOf(newMigration.address);
expect(migratedRawBal).bignumber.eq(rawBal);
// updates the integrator address
integratorAddress = await basketManager.getBassetIntegrator(transferringAsset.address);
expect(integratorAddress).eq(newMigration.address);
// emits BassetsMigrated
await expectEvent(tx.receipt, "BassetsMigrated", {
bAssets: [transferringAsset.address],
newIntegrator: newMigration.address,
});
});
});

describe("addBasset()", async () => {
Expand Down
Loading