Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Sep 6, 2024
1 parent 1faee2a commit 061ec70
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
12 changes: 9 additions & 3 deletions test/unit/Pool/getPoolValueInEth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const setup = require('./setup');
const utils = require('../utils');

const { toBytes8 } = require('../utils').helpers;
const {
helpers: { toBytes8 },
constants: {
Assets: { ETH },
},
} = utils;

const { BigNumber } = ethers;
const { parseEther } = ethers.utils;
Expand Down Expand Up @@ -77,9 +83,9 @@ describe('getPoolValueInEth', function () {
const oldPoolValue = await pool.getPoolValueInEth();

await pool.connect(governance).updateAddressParameters(toBytes8('SWP_OP'), defaultSender.address);
await pool.setSwapValue(parseEther('1'));
await pool.setSwapAssetAmount(ETH, parseEther('1'));

const swapValue = await pool.swapValue();
const swapValue = await pool.assetsInSwapOperator(ETH);
expect(swapValue.toString()).to.eq(parseEther('1').toString());

const newPoolValue = await pool.getPoolValueInEth();
Expand Down
17 changes: 7 additions & 10 deletions test/unit/SwapOperator/closeOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ const {
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { domain: makeDomain, computeOrderUid } = require('@cowprotocol/contracts');
const {
setEtherBalance,
setNextBlockTime,
revertToSnapshot,
takeSnapshot,
increaseTime,
mineNextBlock,
} = require('../../utils/evm');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const setup = require('./setup');
const utils = require('../utils');

const { setEtherBalance, setNextBlockTime, revertToSnapshot, takeSnapshot, increaseTime, mineNextBlock } = utils.evm;
const { ETH } = utils.constants.Assets;

const {
utils: { parseEther, hexZeroPad },
Expand Down Expand Up @@ -462,12 +459,12 @@ describe('closeOrder', function () {
contracts: { swapOperator, pool },
contractOrder,
} = await loadFixture(closeOrderSetup);
const oldSwapValue = await pool.swapValue();
const oldSwapValue = await pool.assetsInSwapOperator(ETH);
expect(oldSwapValue).to.be.gt(0);

await swapOperator.closeOrder(contractOrder);

const newSwapValue = await pool.swapValue();
const newSwapValue = await pool.assetsInSwapOperator(ETH);
expect(newSwapValue).to.eq(0);
});
});
31 changes: 16 additions & 15 deletions test/unit/SwapOperator/placeOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ const {
daiMaxAmount,
} = require('./helpers');
const setup = require('./setup');
const { setEtherBalance, setNextBlockTime } = require('../../utils/evm');
const utils = require('../utils');

const { setEtherBalance, setNextBlockTime } = utils.evm;
const { ETH } = utils.constants.Assets;

const { parseEther, hexZeroPad, hexlify, randomBytes } = ethers.utils;
const ETH_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';

function createContractOrder(domain, order, overrides = {}) {
order = { ...order, ...overrides };
Expand Down Expand Up @@ -260,7 +262,7 @@ describe('placeOrder', function () {
const { contracts, order, domain } = await loadFixture(placeSellWethOrderSetup);
const { swapOperator } = contracts;

const badOrder = createContractOrder(domain, order, { sellToken: ETH_ADDRESS });
const badOrder = createContractOrder(domain, order, { sellToken: ETH });
const placeOrder = swapOperator.placeOrder(badOrder.contractOrder, badOrder.orderUID);
await expect(placeOrder).to.be.revertedWithCustomError(swapOperator, 'InvalidTokenAddress').withArgs('sellToken');
});
Expand All @@ -269,13 +271,13 @@ describe('placeOrder', function () {
const { contracts, order, domain } = await loadFixture(placeSellDaiOrderSetup);
const { swapOperator } = contracts;

const badOrder = createContractOrder(domain, order, { buyToken: ETH_ADDRESS });
const badOrder = createContractOrder(domain, order, { buyToken: ETH });
const placeOrder = swapOperator.placeOrder(badOrder.contractOrder, badOrder.orderUID);
await expect(placeOrder).to.be.revertedWithCustomError(swapOperator, 'InvalidTokenAddress').withArgs('buyToken');
});

it('validates that order.validTo is at most 60 minutes in the future', async function () {
const MAX_VALID_TO_PERIOD_SECONDS = 60 * 60; // 60 minutes
const MAX_VALID_TO_PERIOD_SECONDS = 60 * 60 * 24 * 31; // 31 days
const { contracts, order, domain } = await loadFixture(placeSellWethOrderSetup);
const { swapOperator } = contracts;
const { timestamp } = await ethers.provider.getBlock('latest');
Expand Down Expand Up @@ -943,48 +945,47 @@ describe('placeOrder', function () {

// TODO: transfers assets to swapOperator tests

it('should set totalOutAmount in ETH as pool.swapValue when selling ETH', async function () {
it('should set totalOutAmount in ETH as pool.assetsInSwapOperator when selling ETH', async function () {
const { contracts, order, contractOrder, orderUID } = await loadFixture(placeSellWethOrderSetup);
const { swapOperator, pool } = contracts;

expect(await pool.swapValue()).to.eq(0);
expect(await pool.assetsInSwapOperator(ETH)).to.eq(0);

await swapOperator.placeOrder(contractOrder, orderUID);

// sellAmount & already in ETH
const totalOutAmountInEth = order.sellAmount.add(order.feeAmount);
expect(await pool.swapValue()).to.eq(totalOutAmountInEth);
expect(await pool.assetsInSwapOperator(ETH)).to.eq(totalOutAmountInEth);
});

it('should set totalOutAmount in ETH as pool.swapValue when selling non-ETH assets', async function () {
const orderSetupsToTest = [placeSellDaiOrderSetup, placeNonEthOrderSellStethSetup, placeNonEthOrderSellDaiSetup];
for (const orderSetup of orderSetupsToTest) {
const { contracts, order, contractOrder, orderUID } = await loadFixture(orderSetup);
const { swapOperator, pool, priceFeedOracle } = contracts;
const { swapOperator, pool } = contracts;

expect(await pool.swapValue()).to.eq(0);
expect(await pool.assetsInSwapOperator(order.sellToken)).to.eq(0);

await swapOperator.placeOrder(contractOrder, orderUID);

// convert non-ETH sellAmount + fee to ETH
const { sellAmount, feeAmount } = contractOrder;
const totalOutAmountInEth = await priceFeedOracle.getEthForAsset(order.sellToken, sellAmount.add(feeAmount));
expect(await pool.swapValue()).to.be.equal(totalOutAmountInEth);
expect(await pool.assetsInSwapOperator(order.sellToken)).to.be.equal(sellAmount.add(feeAmount));
}
});

it('should set totalOutAmount in ETH as pool.swapValue on non-ETH asset swaps', async function () {
it('should set totalOutAmount in ETH as pool.assetsInSwapOperator on non-ETH asset swaps', async function () {
const { contracts, order, orderUID, contractOrder } = await loadFixture(placeNonEthOrderSellStethSetup);
const { swapOperator, pool, priceFeedOracle, stEth } = contracts;
const { sellAmount, feeAmount } = order;

expect(await pool.swapValue()).to.eq(0);
expect(await pool.assetsInSwapOperator(order.sellToken)).to.eq(0);

await swapOperator.placeOrder(contractOrder, orderUID);

// convert stETH sellAmount + fee to ETH
const totalOutAmountInEth = await priceFeedOracle.getEthForAsset(stEth.address, sellAmount.add(feeAmount));
expect(await pool.swapValue()).to.eq(totalOutAmountInEth);
expect(await pool.assetsInSwapOperator(order.sellToken)).to.eq(totalOutAmountInEth);
});

it('approves CoW vault relayer to spend exactly sellAmount + fee', async function () {
Expand Down

0 comments on commit 061ec70

Please sign in to comment.