From 11cffc949e07ebbb9316a60a3d7405e7cdebb864 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 4 Apr 2022 16:31:04 +0200 Subject: [PATCH 01/16] remove unused variables --- .../pools/fixedRate/FixedRateExchange.test.ts | 157 ++++++++---------- 1 file changed, 73 insertions(+), 84 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 0d995079..89c11f50 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -12,35 +12,24 @@ import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types' describe('Fixed Rate unit test', () => { let factoryOwner: string - let nftOwner: string let exchangeOwner: string let user1: string let user2: string - let user3: string - let user4: string - let initialBlock: number let fixedRateAddress: string - let daiAddress: string - let usdcAddress: string let exchangeId: string let contracts: Addresses let fixedRate: FixedRateExchange let dtAddress: string - let dtAddress2: string let dtContract: Contract let daiContract: Contract let usdcContract: Contract - const vestedBlocks = 2500000 const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000' before(async () => { const accounts = await web3.eth.getAccounts() factoryOwner = accounts[0] - nftOwner = accounts[1] - user1 = accounts[2] - user2 = accounts[3] - user3 = accounts[4] - user4 = accounts[5] + user1 = accounts[1] + user2 = accounts[2] exchangeOwner = accounts[0] }) @@ -77,7 +66,7 @@ describe('Fixed Rate unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, minter: factoryOwner, - paymentCollector: user3, + paymentCollector: user2, mpFeeAddress: factoryOwner, feeToken: ADDRESS_ZERO, cap: '1000000', @@ -90,7 +79,7 @@ describe('Fixed Rate unit test', () => { fixedRateAddress: contracts.fixedRateAddress, baseTokenAddress: contracts.daiAddress, owner: exchangeOwner, - marketFeeCollector: user3, + marketFeeCollector: user2, baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: '1', @@ -111,8 +100,8 @@ describe('Fixed Rate unit test', () => { exchangeId = txReceipt.events.NewFixedRate.returnValues.exchangeId dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress) - // user2 has no dt1 - expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') + // user1 has no dt1 + expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') fixedRateAddress = contracts.fixedRateAddress fixedRate = new FixedRateExchange( @@ -212,7 +201,7 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.getAmountBTOut(exchangeId, '100')).to.equal('99.7') }) - it('#buyDT - user2 should buy some dt', async () => { + it('#buyDT - user1 should buy some dt', async () => { // total supply is ZERO right now so dt owner mints 1000 DT and approves the fixed rate contract await dtContract.methods .mint(exchangeOwner, web3.utils.toWei('1000')) @@ -220,56 +209,56 @@ describe('Fixed Rate unit test', () => { await dtContract.methods .approve(fixedRateAddress, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) - // user2 gets 100 DAI so he can buy DTs + // user1 gets 100 DAI so he can buy DTs await daiContract.methods - .transfer(user2, web3.utils.toWei('100')) + .transfer(user1, web3.utils.toWei('100')) .send({ from: exchangeOwner }) await daiContract.methods .approve(fixedRateAddress, web3.utils.toWei('100')) - .send({ from: user2 }) + .send({ from: user1 }) - // user2 has no dts but has 100 DAI - expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') - const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user2).call()) + // user1 has no dts but has 100 DAI + expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') + const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call()) - // user2 buys 10 DT - const tx = await fixedRate.buyDT(user2, exchangeId, '10', '11') + // user1 buys 10 DT + const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) const args = tx.events.Swapped.returnValues expect(args.exchangeId).to.equal(exchangeId) - expect(args.by).to.equal(user2) + expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(dtAddress) - expect(await dtContract.methods.balanceOf(user2).call()).to.equal( + expect(await dtContract.methods.balanceOf(user1).call()).to.equal( args.datatokenSwappedAmount ) expect( daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await daiContract.methods.balanceOf(user2).call()) + ).to.equal(await daiContract.methods.balanceOf(user1).call()) // baseToken stays in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10') // no dt in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('0') }) - it('#sellDT - user2 should sell some dt', async () => { + it('#sellDT - user1 should sell some dt', async () => { await dtContract.methods .approve(fixedRateAddress, web3.utils.toWei('10')) - .send({ from: user2 }) - const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user2).call()) - const tx = await fixedRate.sellDT(user2, exchangeId, '10', '9') + .send({ from: user1 }) + const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call()) + const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) const args = tx.events.Swapped.returnValues expect(args.exchangeId).to.equal(exchangeId) - expect(args.by).to.equal(user2) + expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(contracts.daiAddress) - expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') + expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') expect( daiBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await daiContract.methods.balanceOf(user2).call()) + ).to.equal(await daiContract.methods.balanceOf(user1).call()) // DTs stay in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10') // no BTs in the contract (except for the fees, but not accounted here) @@ -301,7 +290,7 @@ describe('Fixed Rate unit test', () => { // we made 2 swaps for 10 DT at rate 1, the fee is 0.1% for ocean community and always in baseToken so it's 0.01 DAI expect(result.marketFeeAvailable).to.equal('0.02') // formatted for baseToken decimals expect(result.oceanFeeAvailable).to.equal('0.04') // formatted for baseToken decimals - expect(result.marketFeeCollector).to.equal(user3) + expect(result.marketFeeCollector).to.equal(user2) expect(result.opcFee).to.equal('0.002') }) @@ -309,8 +298,8 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO) }) it('#setAllowedSwapper- should set an allowed swapper, if exchangeOwner', async () => { - await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user2) - expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user2) + await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user1) + expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user1) }) it('#setAllowedSwapper- should disable allowed swapper(return address(0)), if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ADDRESS_ZERO) @@ -319,8 +308,8 @@ describe('Fixed Rate unit test', () => { it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => { // there are no bt in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') - // user2 buys 1 DT - await fixedRate.buyDT(user2, exchangeId, '1', '2') + // user1 buys 1 DT + await fixedRate.buyDT(user1, exchangeId, '1', '2') // 1 DAI in the contract const fixedRateDetails = await fixedRate.getExchange(exchangeId) expect(fixedRateDetails.btBalance).to.equal('1') @@ -348,7 +337,7 @@ describe('Fixed Rate unit test', () => { // expect(result.marketFeeAvailable).to.equal('0.021') // formatted for baseToken decimals // // same for ocean fee // expect(result.oceanFeeAvailable).to.equal('0.042') // formatted for baseToken decimals - // expect(result.marketFeeCollector).to.equal(user3) + // expect(result.marketFeeCollector).to.equal(user2) // // user4 calls collectMarketFee // await fixedRate.collectMarketFee(user4, exchangeId) @@ -356,26 +345,26 @@ describe('Fixed Rate unit test', () => { // expect(result.marketFeeAvailable).to.equal('0') // // ocean fee still available // expect(result.oceanFeeAvailable).to.equal('0.042') - // // user3 is the marketFeeCollector - // expect(await daiContract.methods.balanceOf(user3).call()).to.equal( + // // user2 is the marketFeeCollector + // expect(await daiContract.methods.balanceOf(user2).call()).to.equal( // web3.utils.toWei('1.021') // ) // }) it('#updateMarketFee- should update Market fee if market fee collector', async () => { expect((await fixedRate.getFeesInfo(exchangeId)).marketFee).to.equal('0.001') - // user3 is marketFeeCollector - await fixedRate.updateMarketFee(user3, exchangeId, '0.01') + // user2 is marketFeeCollector + await fixedRate.updateMarketFee(user2, exchangeId, '0.01') expect((await fixedRate.getFeesInfo(exchangeId)).marketFee).to.equal('0.01') }) it('#updateMarketFeeCollector - should update Market fee collector if market fee collector', async () => { - expect((await fixedRate.getFeesInfo(exchangeId)).marketFeeCollector).to.equal(user3) - - await fixedRate.updateMarketFeeCollector(user3, exchangeId, user2) - expect((await fixedRate.getFeesInfo(exchangeId)).marketFeeCollector).to.equal(user2) + + await fixedRate.updateMarketFeeCollector(user2, exchangeId, user1) + + expect((await fixedRate.getFeesInfo(exchangeId)).marketFeeCollector).to.equal(user1) }) }) describe('Test a Fixed Rate Exchange with USDC (6 Decimals)', () => { @@ -397,7 +386,7 @@ describe('Fixed Rate unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, minter: factoryOwner, - paymentCollector: user3, + paymentCollector: user2, mpFeeAddress: factoryOwner, feeToken: ADDRESS_ZERO, cap: '1000000', @@ -410,7 +399,7 @@ describe('Fixed Rate unit test', () => { fixedRateAddress: contracts.fixedRateAddress, baseTokenAddress: contracts.usdcAddress, owner: exchangeOwner, - marketFeeCollector: user3, + marketFeeCollector: user2, baseTokenDecimals: 6, datatokenDecimals: 18, fixedRate: '1', @@ -431,8 +420,8 @@ describe('Fixed Rate unit test', () => { exchangeId = txReceipt.events.NewFixedRate.returnValues.exchangeId dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress) - // user2 has no dt1 - expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') + // user1 has no dt1 + expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') fixedRateAddress = contracts.fixedRateAddress fixedRate = new FixedRateExchange( @@ -528,7 +517,7 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.getAmountBTOut(exchangeId, '100')).to.equal('99.7') }) - it('#buyDT - user2 should buy some dt', async () => { + it('#buyDT - user1 should buy some dt', async () => { // total supply is ZERO right now so dt owner mints 1000 DT and approves the fixed rate contract await dtContract.methods .mint(exchangeOwner, web3.utils.toWei('1000')) @@ -536,54 +525,54 @@ describe('Fixed Rate unit test', () => { await dtContract.methods .approve(fixedRateAddress, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) - // user2 gets 100 USDC so he can buy DTs - await usdcContract.methods.transfer(user2, 100 * 1e6).send({ from: exchangeOwner }) + // user1 gets 100 USDC so he can buy DTs + await usdcContract.methods.transfer(user1, 100 * 1e6).send({ from: exchangeOwner }) await usdcContract.methods .approve(fixedRateAddress, 100 * 1e6) - .send({ from: user2 }) + .send({ from: user1 }) - // user2 has no dts but has 100 USDC - expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') - const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user2).call()) + // user1 has no dts but has 100 USDC + expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') + const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call()) - // user2 buys 10 DT - const tx = await fixedRate.buyDT(user2, exchangeId, '10', '11') + // user1 buys 10 DT + const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) const args = tx.events.Swapped.returnValues expect(args.exchangeId).to.equal(exchangeId) - expect(args.by).to.equal(user2) + expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(dtAddress) - expect(await dtContract.methods.balanceOf(user2).call()).to.equal( + expect(await dtContract.methods.balanceOf(user1).call()).to.equal( args.datatokenSwappedAmount ) expect( usdcBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await usdcContract.methods.balanceOf(user2).call()) + ).to.equal(await usdcContract.methods.balanceOf(user1).call()) // baseToken stays in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10') // no dt in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('0') }) - it('#sellDT - user2 should sell some dt', async () => { + it('#sellDT - user1 should sell some dt', async () => { await dtContract.methods .approve(fixedRateAddress, web3.utils.toWei('10')) - .send({ from: user2 }) - const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user2).call()) - const tx = await fixedRate.sellDT(user2, exchangeId, '10', '9') + .send({ from: user1 }) + const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call()) + const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) const args = tx.events.Swapped.returnValues expect(args.exchangeId).to.equal(exchangeId) - expect(args.by).to.equal(user2) + expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(contracts.usdcAddress) - expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') + expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') expect( usdcBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await usdcContract.methods.balanceOf(user2).call()) + ).to.equal(await usdcContract.methods.balanceOf(user1).call()) // DTs stay in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10') // no BTs in the contract (except for the fees, but not accounted here) @@ -615,7 +604,7 @@ describe('Fixed Rate unit test', () => { // we made 2 swaps for 10 DT at rate 1, the fee is 0.1% for ocean community and always in baseToken so it's 0.01 USDC expect(result.marketFeeAvailable).to.equal('0.02') // formatted for baseToken decimals expect(result.oceanFeeAvailable).to.equal('0.04') // formatted for baseToken decimals - expect(result.marketFeeCollector).to.equal(user3) + expect(result.marketFeeCollector).to.equal(user2) expect(result.opcFee).to.equal('0.002') }) @@ -623,8 +612,8 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO) }) it('#setAllowedSwapper- should set an allowed swapper, if exchangeOwner', async () => { - await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user2) - expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user2) + await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user1) + expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user1) }) it('#setAllowedSwapper- should disable allowed swapper(return address(0)), if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ADDRESS_ZERO) @@ -633,8 +622,8 @@ describe('Fixed Rate unit test', () => { it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => { // there are no bt in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') - // user2 buys 1 DT - await fixedRate.buyDT(user2, exchangeId, '1', '2') + // user1 buys 1 DT + await fixedRate.buyDT(user1, exchangeId, '1', '2') // 1 DAI in the contract const exchangeDetails = await fixedRate.getExchange(exchangeId) expect(exchangeDetails.btBalance).to.equal('1') @@ -658,18 +647,18 @@ describe('Fixed Rate unit test', () => { it('#updateMarketFee- should update Market fee if market fee collector', async () => { expect((await fixedRate.getFeesInfo(exchangeId)).marketFee).to.equal('0.001') - // user3 is marketFeeCollector - await fixedRate.updateMarketFee(user3, exchangeId, '0.01') + // user2 is marketFeeCollector + await fixedRate.updateMarketFee(user2, exchangeId, '0.01') expect((await fixedRate.getFeesInfo(exchangeId)).marketFee).to.equal('0.01') }) it('#updateMarketFeeCollector - should update Market fee collector if market fee collector', async () => { - expect((await fixedRate.getFeesInfo(exchangeId)).marketFeeCollector).to.equal(user3) - - await fixedRate.updateMarketFeeCollector(user3, exchangeId, user2) - expect((await fixedRate.getFeesInfo(exchangeId)).marketFeeCollector).to.equal(user2) + + await fixedRate.updateMarketFeeCollector(user2, exchangeId, user1) + + expect((await fixedRate.getFeesInfo(exchangeId)).marketFeeCollector).to.equal(user1) }) }) }) From a29f47ed7c47499f120f49dbf1825569a6335439 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 4 Apr 2022 16:33:11 +0200 Subject: [PATCH 02/16] use ZERO_ADDRESS constant --- .../pools/fixedRate/FixedRateExchange.test.ts | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 89c11f50..bbca8abd 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -7,7 +7,12 @@ import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedR import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' -import { NftFactory, NftCreateData, FixedRateExchange } from '../../../../src' +import { + NftFactory, + NftCreateData, + FixedRateExchange, + ZERO_ADDRESS +} from '../../../../src' import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types' describe('Fixed Rate unit test', () => { @@ -23,7 +28,6 @@ describe('Fixed Rate unit test', () => { let dtContract: Contract let daiContract: Contract let usdcContract: Contract - const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000' before(async () => { const accounts = await web3.eth.getAccounts() @@ -68,7 +72,7 @@ describe('Fixed Rate unit test', () => { minter: factoryOwner, paymentCollector: user2, mpFeeAddress: factoryOwner, - feeToken: ADDRESS_ZERO, + feeToken: ZERO_ADDRESS, cap: '1000000', feeAmount: '0', name: 'ERC20B1', @@ -84,7 +88,7 @@ describe('Fixed Rate unit test', () => { datatokenDecimals: 18, fixedRate: '1', marketFee: '0.001', - allowedConsumer: ADDRESS_ZERO, + allowedConsumer: ZERO_ADDRESS, withMint: false } @@ -95,7 +99,6 @@ describe('Fixed Rate unit test', () => { freParams ) - initialBlock = await web3.eth.getBlockNumber() dtAddress = txReceipt.events.TokenCreated.returnValues.newTokenAddress exchangeId = txReceipt.events.NewFixedRate.returnValues.exchangeId @@ -295,15 +298,15 @@ describe('Fixed Rate unit test', () => { }) it('#getAllowedSwapper- should return address(0) if not set, if exchangeOwner', async () => { - expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO) + expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) it('#setAllowedSwapper- should set an allowed swapper, if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user1) expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user1) }) it('#setAllowedSwapper- should disable allowed swapper(return address(0)), if exchangeOwner', async () => { - await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ADDRESS_ZERO) - expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO) + await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ZERO_ADDRESS) + expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => { // there are no bt in the contract @@ -388,7 +391,7 @@ describe('Fixed Rate unit test', () => { minter: factoryOwner, paymentCollector: user2, mpFeeAddress: factoryOwner, - feeToken: ADDRESS_ZERO, + feeToken: ZERO_ADDRESS, cap: '1000000', feeAmount: '0', name: 'ERC20B1', @@ -404,7 +407,7 @@ describe('Fixed Rate unit test', () => { datatokenDecimals: 18, fixedRate: '1', marketFee: '0.001', - allowedConsumer: ADDRESS_ZERO, + allowedConsumer: ZERO_ADDRESS, withMint: false } @@ -415,7 +418,6 @@ describe('Fixed Rate unit test', () => { freParams ) - initialBlock = await web3.eth.getBlockNumber() dtAddress = txReceipt.events.TokenCreated.returnValues.newTokenAddress exchangeId = txReceipt.events.NewFixedRate.returnValues.exchangeId @@ -609,15 +611,15 @@ describe('Fixed Rate unit test', () => { }) it('#getAllowedSwapper- should return address(0) if not set, if exchangeOwner', async () => { - expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO) + expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) it('#setAllowedSwapper- should set an allowed swapper, if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user1) expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user1) }) it('#setAllowedSwapper- should disable allowed swapper(return address(0)), if exchangeOwner', async () => { - await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ADDRESS_ZERO) - expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ADDRESS_ZERO) + await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ZERO_ADDRESS) + expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => { // there are no bt in the contract From ea86aa7dccedd3d14bd094065a7aabcccda842a4 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 4 Apr 2022 16:37:11 +0200 Subject: [PATCH 03/16] remove ABI import --- test/unit/pools/fixedRate/FixedRateExchange.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index bbca8abd..5680bd14 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -3,7 +3,6 @@ import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' import BN from 'bn.js' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' @@ -110,7 +109,7 @@ describe('Fixed Rate unit test', () => { fixedRate = new FixedRateExchange( web3, fixedRateAddress, - FixedRate.abi as AbiItem[], + null, contracts.oceanAddress ) assert(fixedRate != null) From 3ddb36b46818a0e2c93fc3704b2be4e18404702b Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 4 Apr 2022 16:42:16 +0200 Subject: [PATCH 04/16] use contracts.fixedRateAddress --- .../pools/fixedRate/FixedRateExchange.test.ts | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 5680bd14..835cbddf 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -19,7 +19,6 @@ describe('Fixed Rate unit test', () => { let exchangeOwner: string let user1: string let user2: string - let fixedRateAddress: string let exchangeId: string let contracts: Addresses let fixedRate: FixedRateExchange @@ -105,10 +104,9 @@ describe('Fixed Rate unit test', () => { // user1 has no dt1 expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') - fixedRateAddress = contracts.fixedRateAddress fixedRate = new FixedRateExchange( web3, - fixedRateAddress, + contracts.fixedRateAddress, null, contracts.oceanAddress ) @@ -209,14 +207,14 @@ describe('Fixed Rate unit test', () => { .mint(exchangeOwner, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) await dtContract.methods - .approve(fixedRateAddress, web3.utils.toWei('1000')) + .approve(contracts.fixedRateAddress, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) // user1 gets 100 DAI so he can buy DTs await daiContract.methods .transfer(user1, web3.utils.toWei('100')) .send({ from: exchangeOwner }) await daiContract.methods - .approve(fixedRateAddress, web3.utils.toWei('100')) + .approve(contracts.fixedRateAddress, web3.utils.toWei('100')) .send({ from: user1 }) // user1 has no dts but has 100 DAI @@ -246,7 +244,7 @@ describe('Fixed Rate unit test', () => { it('#sellDT - user1 should sell some dt', async () => { await dtContract.methods - .approve(fixedRateAddress, web3.utils.toWei('10')) + .approve(contracts.fixedRateAddress, web3.utils.toWei('10')) .send({ from: user1 }) const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call()) const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') @@ -424,11 +422,10 @@ describe('Fixed Rate unit test', () => { // user1 has no dt1 expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') - fixedRateAddress = contracts.fixedRateAddress fixedRate = new FixedRateExchange( web3, - fixedRateAddress, - FixedRate.abi as AbiItem[], + contracts.fixedRateAddress, + null, contracts.oceanAddress ) assert(fixedRate != null) @@ -524,12 +521,12 @@ describe('Fixed Rate unit test', () => { .mint(exchangeOwner, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) await dtContract.methods - .approve(fixedRateAddress, web3.utils.toWei('1000')) + .approve(contracts.fixedRateAddress, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) // user1 gets 100 USDC so he can buy DTs await usdcContract.methods.transfer(user1, 100 * 1e6).send({ from: exchangeOwner }) await usdcContract.methods - .approve(fixedRateAddress, 100 * 1e6) + .approve(contracts.fixedRateAddress, 100 * 1e6) .send({ from: user1 }) // user1 has no dts but has 100 USDC @@ -559,7 +556,7 @@ describe('Fixed Rate unit test', () => { it('#sellDT - user1 should sell some dt', async () => { await dtContract.methods - .approve(fixedRateAddress, web3.utils.toWei('10')) + .approve(contracts.fixedRateAddress, web3.utils.toWei('10')) .send({ from: user1 }) const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call()) const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') From a0a523079406aa6b22af58917322216764929846 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 18:14:18 +0200 Subject: [PATCH 05/16] sepparate functions --- .../pools/fixedRate/FixedRateExchange.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 835cbddf..c9333dbe 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -117,14 +117,17 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.isActive(exchangeId)).to.equal(true) expect(await fixedRate.isActive('0x00')).to.equal(false) }) + it('#getOwner - should get exchange owner given an id', async () => { expect(await fixedRate.getExchangeOwner(exchangeId)).to.equal(exchangeOwner) }) + it('#getOPFCollector - should get OPF collector', async () => { expect(await fixedRate.getOPCCollector()).to.equal( contracts.opfCommunityFeeCollectorAddress ) }) + it('#getRouter - should get Router address', async () => { expect(await fixedRate.getRouter()).to.equal(contracts.routerAddress) }) @@ -184,10 +187,12 @@ describe('Fixed Rate unit test', () => { // exchange owner hasn't approved any DT for sell expect(await fixedRate.getDTSupply(exchangeId)).to.equal('0') }) + it('#getBTSupply - should get the bt supply in the exchange', async () => { // no baseToken at the beginning expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0') }) + it('#calcBaseInGivenOutDT - should get bt amount in for a specific dt amount', async () => { // 100.2 DAI for 100 DT (0.1% market fee and 0.1% ocean fee) expect( @@ -196,6 +201,7 @@ describe('Fixed Rate unit test', () => { ).baseTokenAmount ).to.equal('100.3') }) + it('#getAmountBTOut - should get bt amount out for a specific dt amount', async () => { // 99.8 DAI for 100 DT (0.1% market fee and 0.1% ocean fee) expect(await fixedRate.getAmountBTOut(exchangeId, '100')).to.equal('99.7') @@ -297,14 +303,17 @@ describe('Fixed Rate unit test', () => { it('#getAllowedSwapper- should return address(0) if not set, if exchangeOwner', async () => { expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) + it('#setAllowedSwapper- should set an allowed swapper, if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user1) expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user1) }) + it('#setAllowedSwapper- should disable allowed swapper(return address(0)), if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ZERO_ADDRESS) expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) + it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => { // there are no bt in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') @@ -318,6 +327,7 @@ describe('Fixed Rate unit test', () => { // btBalance is zero expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') }) + it('#collectDT- should collect DT in the contract, if exchangeOwner', async () => { const result = await fixedRate.getExchange(exchangeId) // 9 dts left @@ -435,14 +445,17 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.isActive(exchangeId)).to.equal(true) expect(await fixedRate.isActive('0x00')).to.equal(false) }) + it('#getOwner - should get exchange owner given an id', async () => { expect(await fixedRate.getExchangeOwner(exchangeId)).to.equal(exchangeOwner) }) + it('#getOPFCollector - should get OPF collector', async () => { expect(await fixedRate.getOPCCollector()).to.equal( contracts.opfCommunityFeeCollectorAddress ) }) + it('#getRouter - should get Router address', async () => { expect(await fixedRate.getRouter()).to.equal(contracts.routerAddress) }) @@ -498,10 +511,12 @@ describe('Fixed Rate unit test', () => { // exchange owner hasn't approved any DT for sell expect(await fixedRate.getDTSupply(exchangeId)).to.equal('0') }) + it('#getBTSupply - should get the bt supply in the exchange', async () => { // no baseToken at the beginning expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0') }) + it('#calcBaseInGivenOutDT - should get bt amount in for a specific dt amount', async () => { // 100.2 USDC for 100 DT (0.1% market fee and 0.1% ocean fee) expect( @@ -510,6 +525,7 @@ describe('Fixed Rate unit test', () => { ).baseTokenAmount ).to.equal('100.3') }) + it('#getAmountBTOut - should get bt amount out for a specific dt amount', async () => { // 99.8 USDC for 100 DT (0.1% market fee and 0.1% ocean fee) expect(await fixedRate.getAmountBTOut(exchangeId, '100')).to.equal('99.7') @@ -609,14 +625,17 @@ describe('Fixed Rate unit test', () => { it('#getAllowedSwapper- should return address(0) if not set, if exchangeOwner', async () => { expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) + it('#setAllowedSwapper- should set an allowed swapper, if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, user1) expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(user1) }) + it('#setAllowedSwapper- should disable allowed swapper(return address(0)), if exchangeOwner', async () => { await fixedRate.setAllowedSwapper(exchangeOwner, exchangeId, ZERO_ADDRESS) expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS) }) + it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => { // there are no bt in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') @@ -630,6 +649,7 @@ describe('Fixed Rate unit test', () => { // btBalance is zero expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0') }) + it('#collectDT- should collect DT in the contract, if exchangeOwner', async () => { const result = await fixedRate.getExchange(exchangeId) // 9 dts left From 3660f5c9139c0359bf7328c18049ae707d6996d8 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 18:40:27 +0200 Subject: [PATCH 06/16] refactor constants --- .../pools/fixedRate/FixedRateExchange.test.ts | 81 +++++++++---------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index c9333dbe..a0446e73 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -26,6 +26,25 @@ describe('Fixed Rate unit test', () => { let dtContract: Contract let daiContract: Contract let usdcContract: Contract + let ercParams: Erc20CreateParams + + const CAP_AMOUNT = '1000000' + const NFT_NAME = '72120Bundle' + const NFT_SYMBOL = '72Bundle' + const NFT_TOKEN_URI = 'https://oceanprotocol.com/nft/' + const ERC20_NAME = 'ERC20B1' + const ERC20_SYMBOL = 'ERC20DT1Symbol' + const FEE = '0.001' + const FEE_ZERO = '0' + + const NFT_DATA: NftCreateData = { + name: NFT_NAME, + symbol: NFT_SYMBOL, + templateIndex: 1, + tokenURI: NFT_TOKEN_URI, + transferable: true, + owner: factoryOwner + } before(async () => { const accounts = await web3.eth.getAccounts() @@ -33,6 +52,20 @@ describe('Fixed Rate unit test', () => { user1 = accounts[1] user2 = accounts[2] exchangeOwner = accounts[0] + + NFT_DATA.owner = factoryOwner + + ercParams = { + templateIndex: 1, + minter: factoryOwner, + paymentCollector: user2, + mpFeeAddress: factoryOwner, + feeToken: ZERO_ADDRESS, + cap: CAP_AMOUNT, + feeAmount: FEE_ZERO, + name: ERC20_NAME, + symbol: ERC20_SYMBOL + } }) it('should deploy contracts', async () => { @@ -56,27 +89,6 @@ describe('Fixed Rate unit test', () => { const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) - const nftData: NftCreateData = { - name: '72120Bundle', - symbol: '72Bundle', - templateIndex: 1, - tokenURI: 'https://oceanprotocol.com/nft/', - transferable: true, - owner: factoryOwner - } - - const ercParams: Erc20CreateParams = { - templateIndex: 1, - minter: factoryOwner, - paymentCollector: user2, - mpFeeAddress: factoryOwner, - feeToken: ZERO_ADDRESS, - cap: '1000000', - feeAmount: '0', - name: 'ERC20B1', - symbol: 'ERC20DT1Symbol' - } - const freParams: FreCreationParams = { fixedRateAddress: contracts.fixedRateAddress, baseTokenAddress: contracts.daiAddress, @@ -85,14 +97,14 @@ describe('Fixed Rate unit test', () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: '1', - marketFee: '0.001', + marketFee: FEE, allowedConsumer: ZERO_ADDRESS, withMint: false } const txReceipt = await nftFactory.createNftErc20WithFixedRate( exchangeOwner, - nftData, + NFT_DATA, ercParams, freParams ) @@ -384,27 +396,6 @@ describe('Fixed Rate unit test', () => { const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) - const nftData: NftCreateData = { - name: '72120Bundle', - symbol: '72Bundle', - templateIndex: 1, - tokenURI: 'https://oceanprotocol.com/nft/', - transferable: true, - owner: factoryOwner - } - - const ercParams: Erc20CreateParams = { - templateIndex: 1, - minter: factoryOwner, - paymentCollector: user2, - mpFeeAddress: factoryOwner, - feeToken: ZERO_ADDRESS, - cap: '1000000', - feeAmount: '0', - name: 'ERC20B1', - symbol: 'ERC20DT1Symbol' - } - const freParams: FreCreationParams = { fixedRateAddress: contracts.fixedRateAddress, baseTokenAddress: contracts.usdcAddress, @@ -420,7 +411,7 @@ describe('Fixed Rate unit test', () => { const txReceipt = await nftFactory.createNftErc20WithFixedRate( exchangeOwner, - nftData, + NFT_DATA, ercParams, freParams ) From 72be0aa02a82c20611d84abd54e40d9e6f104c70 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 13 Apr 2022 17:23:21 +0200 Subject: [PATCH 07/16] uncomment and fix broken tests --- .../pools/fixedRate/FixedRateExchange.test.ts | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index a0446e73..e34b0725 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -352,26 +352,31 @@ describe('Fixed Rate unit test', () => { // Only allowance left since dt is ZERO expect(result2.dtSupply).to.equal('990') }) - // it('#collectMarketFee- should collect marketFee and send it to marketFeeCollector, anyone can call it', async () => { - // let result = await fixedRate.getFeesInfo(exchangeId) - // // we made 2 swaps for 10 DT at rate 1, the fee is 0.1% for market and always in baseToken so it's 0.01 DAI - // // plus another swap for 1 DT - // expect(result.marketFeeAvailable).to.equal('0.021') // formatted for baseToken decimals - // // same for ocean fee - // expect(result.oceanFeeAvailable).to.equal('0.042') // formatted for baseToken decimals - // expect(result.marketFeeCollector).to.equal(user2) - // // user4 calls collectMarketFee - // await fixedRate.collectMarketFee(user4, exchangeId) - // result = await fixedRate.getFeesInfo(exchangeId) - // expect(result.marketFeeAvailable).to.equal('0') - // // ocean fee still available - // expect(result.oceanFeeAvailable).to.equal('0.042') - // // user2 is the marketFeeCollector - // expect(await daiContract.methods.balanceOf(user2).call()).to.equal( - // web3.utils.toWei('1.021') - // ) - // }) + it('#collectMarketFee- should collect marketFee and send it to marketFeeCollector, anyone can call it', async () => { + let result = await fixedRate.getFeesInfo(exchangeId) + // we made 2 swaps for 10 DT at rate 1, the fee is 0.1% for market and always in baseToken so it's 0.01 DAI + // plus another swap for 1 DT + expect(result.marketFeeAvailable).to.equal('0.021') // formatted for baseToken decimals + // same for ocean fee + expect(result.oceanFeeAvailable).to.equal('0.042') // formatted for baseToken decimals + expect(result.marketFeeCollector).to.equal(user2) + + const daiBalanceBeforeCollect = new BN( + await daiContract.methods.balanceOf(user2).call() + ) + + // user4 calls collectMarketFee + await fixedRate.collectMarketFee(user2, exchangeId) + result = await fixedRate.getFeesInfo(exchangeId) + expect(result.marketFeeAvailable).to.equal('0') + // ocean fee still available + expect(result.oceanFeeAvailable).to.equal('0.042') + // user2 is the marketFeeCollector + expect(await daiContract.methods.balanceOf(user2).call()).to.equal( + daiBalanceBeforeCollect.add(new BN(web3.utils.toWei('0.021'))).toString() + ) + }) it('#updateMarketFee- should update Market fee if market fee collector', async () => { expect((await fixedRate.getFeesInfo(exchangeId)).marketFee).to.equal('0.001') From 9ede24badff24c90e68ac0eab182b36fe7766996 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 19 Apr 2022 08:41:30 +0200 Subject: [PATCH 08/16] fix estimate gas in sellDT() --- src/pools/fixedRate/FixedRateExchange.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 90e2bab9..a7874ac2 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -274,7 +274,7 @@ export class FixedRateExchange { exchange.baseToken, minBaseTokenAmount ) - const estGas = await this.estBuyDT( + const estGas = await this.estSellDT( address, exchangeId, dtAmountFormatted, From 5428a8a13238b589184daf06b9257a62c7098c9f Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 19 Apr 2022 08:49:29 +0200 Subject: [PATCH 09/16] fix estimate gas in deactivateMint() and updateMarketFee() --- src/pools/fixedRate/FixedRateExchange.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index a7874ac2..78e848c3 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -788,7 +788,7 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.withMint === false) return null - const estGas = await this.estDeactivate(address, exchangeId) + const estGas = await this.estDeactivateMint(address, exchangeId) const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, false) @@ -1095,7 +1095,7 @@ export class FixedRateExchange { exchangeId: string, newMarketFee: string ): Promise { - const estGas = await this.estSetRate( + const estGas = await this.estUpdateMarketFee( address, exchangeId, this.web3.utils.toWei(newMarketFee) From 2d115306e7f0bb884110eaaf8b0714aa8600f240 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 28 Apr 2022 17:18:53 +0200 Subject: [PATCH 10/16] simplify constants --- .../pools/fixedRate/FixedRateExchange.test.ts | 53 ++++++++----------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index e34b0725..ebb36591 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -26,26 +26,28 @@ describe('Fixed Rate unit test', () => { let dtContract: Contract let daiContract: Contract let usdcContract: Contract - let ercParams: Erc20CreateParams - const CAP_AMOUNT = '1000000' - const NFT_NAME = '72120Bundle' - const NFT_SYMBOL = '72Bundle' - const NFT_TOKEN_URI = 'https://oceanprotocol.com/nft/' - const ERC20_NAME = 'ERC20B1' - const ERC20_SYMBOL = 'ERC20DT1Symbol' - const FEE = '0.001' - const FEE_ZERO = '0' - - const NFT_DATA: NftCreateData = { - name: NFT_NAME, - symbol: NFT_SYMBOL, + const nftData: NftCreateData = { + name: '72120Bundle', + symbol: '72Bundle', templateIndex: 1, - tokenURI: NFT_TOKEN_URI, + tokenURI: 'https://oceanprotocol.com/nft/', transferable: true, owner: factoryOwner } + const ercParams: Erc20CreateParams = { + templateIndex: 1, + minter: factoryOwner, + paymentCollector: user2, + mpFeeAddress: factoryOwner, + feeToken: ZERO_ADDRESS, + cap: '1000000', + feeAmount: '0', + name: 'ERC20B1', + symbol: 'ERC20DT1Symbol' + } + before(async () => { const accounts = await web3.eth.getAccounts() factoryOwner = accounts[0] @@ -53,19 +55,10 @@ describe('Fixed Rate unit test', () => { user2 = accounts[2] exchangeOwner = accounts[0] - NFT_DATA.owner = factoryOwner - - ercParams = { - templateIndex: 1, - minter: factoryOwner, - paymentCollector: user2, - mpFeeAddress: factoryOwner, - feeToken: ZERO_ADDRESS, - cap: CAP_AMOUNT, - feeAmount: FEE_ZERO, - name: ERC20_NAME, - symbol: ERC20_SYMBOL - } + nftData.owner = factoryOwner + ercParams.minter = factoryOwner + ercParams.paymentCollector = user2 + ercParams.mpFeeAddress = factoryOwner }) it('should deploy contracts', async () => { @@ -97,14 +90,14 @@ describe('Fixed Rate unit test', () => { baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: '1', - marketFee: FEE, + marketFee: '0.001', allowedConsumer: ZERO_ADDRESS, withMint: false } const txReceipt = await nftFactory.createNftErc20WithFixedRate( exchangeOwner, - NFT_DATA, + nftData, ercParams, freParams ) @@ -416,7 +409,7 @@ describe('Fixed Rate unit test', () => { const txReceipt = await nftFactory.createNftErc20WithFixedRate( exchangeOwner, - NFT_DATA, + nftData, ercParams, freParams ) From 71d49ec230d6c41dc075cec7618770a25e1e8c89 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 23 May 2022 15:53:47 +0200 Subject: [PATCH 11/16] initialize accounts with null --- test/unit/pools/fixedRate/FixedRateExchange.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 60ed181a..f5aaf4b5 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -33,14 +33,14 @@ describe('Fixed Rate unit test', () => { templateIndex: 1, tokenURI: 'https://oceanprotocol.com/nft/', transferable: true, - owner: factoryOwner + owner: null } const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: factoryOwner, - paymentCollector: user2, - mpFeeAddress: factoryOwner, + minter: null, + paymentCollector: null, + mpFeeAddress: null, feeToken: ZERO_ADDRESS, cap: '1000000', feeAmount: '0', From e42e031c1e1492a51ec6b88e31c828b702fde083 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 24 May 2022 10:36:36 +0200 Subject: [PATCH 12/16] use transfer() approve() and balance() functions --- .../pools/fixedRate/FixedRateExchange.test.ts | 88 +++++++------------ 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index f5aaf4b5..9fb0973a 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -3,14 +3,16 @@ import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' import BN from 'bn.js' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { NftFactory, NftCreateData, FixedRateExchange, - ZERO_ADDRESS + ZERO_ADDRESS, + approve, + transfer, + balance } from '../../../../src' import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types' @@ -24,8 +26,6 @@ describe('Fixed Rate unit test', () => { let fixedRate: FixedRateExchange let dtAddress: string let dtContract: Contract - let daiContract: Contract - let usdcContract: Contract const nftData: NftCreateData = { name: '72120Bundle', @@ -63,16 +63,6 @@ describe('Fixed Rate unit test', () => { it('should deploy contracts', async () => { contracts = await deployContracts(web3, factoryOwner) - - // initialize fixed rate - // - - daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) - - usdcContract = new web3.eth.Contract( - MockERC20.abi as AbiItem[], - contracts.usdcAddress - ) }) describe('Test a Fixed Rate Exchange with DAI (18 Decimals)', () => { @@ -107,7 +97,7 @@ describe('Fixed Rate unit test', () => { dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress) // user1 has no dt1 - expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, dtAddress, user1)).to.equal('0') fixedRate = new FixedRateExchange( web3, @@ -212,20 +202,14 @@ describe('Fixed Rate unit test', () => { await dtContract.methods .mint(exchangeOwner, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) - await dtContract.methods - .approve(contracts.fixedRateAddress, web3.utils.toWei('1000')) - .send({ from: exchangeOwner }) + await approve(web3, exchangeOwner, dtAddress, contracts.fixedRateAddress, '1000') // user1 gets 100 DAI so he can buy DTs - await daiContract.methods - .transfer(user1, web3.utils.toWei('100')) - .send({ from: exchangeOwner }) - await daiContract.methods - .approve(contracts.fixedRateAddress, web3.utils.toWei('100')) - .send({ from: user1 }) + await transfer(web3, exchangeOwner, contracts.daiAddress, user1, '100') + await approve(web3, user1, contracts.daiAddress, contracts.fixedRateAddress, '100') // user1 has no dts but has 100 DAI - expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') - const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call()) + expect(await balance(web3, dtAddress, user1)).to.equal('0') + const daiBalanceBefore = new BN(await balance(web3, contracts.daiAddress, user1)) // user1 buys 10 DT const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11') @@ -236,12 +220,10 @@ describe('Fixed Rate unit test', () => { expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(dtAddress) - expect(await dtContract.methods.balanceOf(user1).call()).to.equal( - args.datatokenSwappedAmount - ) + expect(await balance(web3, dtAddress, user1)).to.equal(args.datatokenSwappedAmount) expect( daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await daiContract.methods.balanceOf(user1).call()) + ).to.equal(await balance(web3, contracts.daiAddress, user1)) // baseToken stays in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10') // no dt in the contract @@ -249,10 +231,8 @@ describe('Fixed Rate unit test', () => { }) it('#sellDT - user1 should sell some dt', async () => { - await dtContract.methods - .approve(contracts.fixedRateAddress, web3.utils.toWei('10')) - .send({ from: user1 }) - const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call()) + await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '100') + const daiBalanceBefore = new BN(await balance(web3, contracts.daiAddress, user1)) const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) @@ -261,10 +241,10 @@ describe('Fixed Rate unit test', () => { expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(contracts.daiAddress) - expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, dtAddress, user1)).to.equal('0') expect( daiBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await daiContract.methods.balanceOf(user1).call()) + ).to.equal(await balance(web3, contracts.daiAddress, user1)) // DTs stay in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10') // no BTs in the contract (except for the fees, but not accounted here) @@ -351,7 +331,7 @@ describe('Fixed Rate unit test', () => { expect(result.marketFeeCollector).to.equal(user2) const daiBalanceBeforeCollect = new BN( - await daiContract.methods.balanceOf(user2).call() + await balance(web3, contracts.daiAddress, user2) ) // user4 calls collectMarketFee @@ -361,7 +341,7 @@ describe('Fixed Rate unit test', () => { // ocean fee still available expect(result.oceanFeeAvailable).to.equal('0.042') // user2 is the marketFeeCollector - expect(await daiContract.methods.balanceOf(user2).call()).to.equal( + expect(await balance(web3, contracts.daiAddress, user2)).to.equal( daiBalanceBeforeCollect.add(new BN(web3.utils.toWei('0.021'))).toString() ) }) @@ -414,7 +394,7 @@ describe('Fixed Rate unit test', () => { dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress) // user1 has no dt1 - expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, dtAddress, user1)).to.equal('0') fixedRate = new FixedRateExchange( web3, @@ -515,18 +495,14 @@ describe('Fixed Rate unit test', () => { await dtContract.methods .mint(exchangeOwner, web3.utils.toWei('1000')) .send({ from: exchangeOwner }) - await dtContract.methods - .approve(contracts.fixedRateAddress, web3.utils.toWei('1000')) - .send({ from: exchangeOwner }) + await approve(web3, exchangeOwner, dtAddress, contracts.fixedRateAddress, '1000') // user1 gets 100 USDC so he can buy DTs - await usdcContract.methods.transfer(user1, 100 * 1e6).send({ from: exchangeOwner }) - await usdcContract.methods - .approve(contracts.fixedRateAddress, 100 * 1e6) - .send({ from: user1 }) + await transfer(web3, exchangeOwner, contracts.usdcAddress, user1, '100') + await approve(web3, user1, contracts.usdcAddress, contracts.fixedRateAddress, '100') // user1 has no dts but has 100 USDC - expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') - const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call()) + expect(await balance(web3, dtAddress, user1)).to.equal('0') + const usdcBalanceBefore = new BN(await balance(web3, contracts.usdcAddress, user1)) // user1 buys 10 DT const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11') @@ -537,12 +513,10 @@ describe('Fixed Rate unit test', () => { expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(dtAddress) - expect(await dtContract.methods.balanceOf(user1).call()).to.equal( - args.datatokenSwappedAmount - ) + expect(await balance(web3, dtAddress, user1)).to.equal(args.datatokenSwappedAmount) expect( usdcBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await usdcContract.methods.balanceOf(user1).call()) + ).to.equal(await balance(web3, contracts.usdcAddress, user1)) // baseToken stays in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10') // no dt in the contract @@ -550,10 +524,8 @@ describe('Fixed Rate unit test', () => { }) it('#sellDT - user1 should sell some dt', async () => { - await dtContract.methods - .approve(contracts.fixedRateAddress, web3.utils.toWei('10')) - .send({ from: user1 }) - const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call()) + await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '10') + const usdcBalanceBefore = new BN(await balance(web3, contracts.usdcAddress, user1)) const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) @@ -562,10 +534,10 @@ describe('Fixed Rate unit test', () => { expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(contracts.usdcAddress) - expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, dtAddress, user1)).to.equal('0') expect( usdcBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString() - ).to.equal(await usdcContract.methods.balanceOf(user1).call()) + ).to.equal(await balance(web3, contracts.usdcAddress, user1)) // DTs stay in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10') // no BTs in the contract (except for the fees, but not accounted here) From e53070807b74faebfb2fbc3b8a089c2b0b0ffbce Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 24 May 2022 11:19:18 +0200 Subject: [PATCH 13/16] fix decimals in balance check --- test/unit/pools/fixedRate/FixedRateExchange.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 9fb0973a..0a00816b 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -12,7 +12,8 @@ import { ZERO_ADDRESS, approve, transfer, - balance + balance, + unitsToAmount } from '../../../../src' import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types' @@ -220,7 +221,9 @@ describe('Fixed Rate unit test', () => { expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(dtAddress) - expect(await balance(web3, dtAddress, user1)).to.equal(args.datatokenSwappedAmount) + expect(await balance(web3, dtAddress, user1)).to.equal( + await unitsToAmount(web3, dtAddress, args.datatokenSwappedAmount) + ) expect( daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() ).to.equal(await balance(web3, contracts.daiAddress, user1)) @@ -513,7 +516,9 @@ describe('Fixed Rate unit test', () => { expect(args.by).to.equal(user1) expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10')) expect(args.tokenOutAddress).to.equal(dtAddress) - expect(await balance(web3, dtAddress, user1)).to.equal(args.datatokenSwappedAmount) + expect(await balance(web3, dtAddress, user1)).to.equal( + await unitsToAmount(web3, dtAddress, args.datatokenSwappedAmount) + ) expect( usdcBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() ).to.equal(await balance(web3, contracts.usdcAddress, user1)) From 6d3a9edb23a01f65b10643d71dc80dbac98414ff Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 24 May 2022 11:27:36 +0200 Subject: [PATCH 14/16] fix decimals in balance check --- .../pools/fixedRate/FixedRateExchange.test.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 0a00816b..31b64b93 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -225,7 +225,9 @@ describe('Fixed Rate unit test', () => { await unitsToAmount(web3, dtAddress, args.datatokenSwappedAmount) ) expect( - daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() + daiBalanceBefore + .sub(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .toString() ).to.equal(await balance(web3, contracts.daiAddress, user1)) // baseToken stays in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10') @@ -246,7 +248,9 @@ describe('Fixed Rate unit test', () => { expect(args.tokenOutAddress).to.equal(contracts.daiAddress) expect(await balance(web3, dtAddress, user1)).to.equal('0') expect( - daiBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString() + daiBalanceBefore + .add(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .toString() ).to.equal(await balance(web3, contracts.daiAddress, user1)) // DTs stay in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10') @@ -345,7 +349,7 @@ describe('Fixed Rate unit test', () => { expect(result.oceanFeeAvailable).to.equal('0.042') // user2 is the marketFeeCollector expect(await balance(web3, contracts.daiAddress, user2)).to.equal( - daiBalanceBeforeCollect.add(new BN(web3.utils.toWei('0.021'))).toString() + daiBalanceBeforeCollect.add(new BN('0.021')).toString() ) }) @@ -520,7 +524,9 @@ describe('Fixed Rate unit test', () => { await unitsToAmount(web3, dtAddress, args.datatokenSwappedAmount) ) expect( - usdcBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString() + usdcBalanceBefore + .sub(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .toString() ).to.equal(await balance(web3, contracts.usdcAddress, user1)) // baseToken stays in the contract expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10') @@ -541,7 +547,9 @@ describe('Fixed Rate unit test', () => { expect(args.tokenOutAddress).to.equal(contracts.usdcAddress) expect(await balance(web3, dtAddress, user1)).to.equal('0') expect( - usdcBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString() + usdcBalanceBefore + .add(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .toString() ).to.equal(await balance(web3, contracts.usdcAddress, user1)) // DTs stay in the contract expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10') From c29bf475c4f6ec78c7f39cb346066292ab82ca1c Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 24 May 2022 15:01:50 +0200 Subject: [PATCH 15/16] show outputs --- test/unit/pools/fixedRate/FixedRateExchange.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 31b64b93..2e41289c 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -224,6 +224,9 @@ describe('Fixed Rate unit test', () => { expect(await balance(web3, dtAddress, user1)).to.equal( await unitsToAmount(web3, dtAddress, args.datatokenSwappedAmount) ) + console.log(daiBalanceBefore) + console.log(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount)) + console.log(await balance(web3, contracts.daiAddress, user1)) expect( daiBalanceBefore .sub(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) From 82d9e4cf5f9ce39091f72832cd83406729f42e6a Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 24 May 2022 17:26:29 +0200 Subject: [PATCH 16/16] fix token address for decimal calculation in unitsToAmount() --- .../pools/fixedRate/FixedRateExchange.test.ts | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 2e41289c..dba2b639 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -1,7 +1,7 @@ import { assert, expect } from 'chai' import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' -import BN from 'bn.js' +import BigNumber from 'bignumber.js' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' @@ -210,7 +210,9 @@ describe('Fixed Rate unit test', () => { // user1 has no dts but has 100 DAI expect(await balance(web3, dtAddress, user1)).to.equal('0') - const daiBalanceBefore = new BN(await balance(web3, contracts.daiAddress, user1)) + const daiBalanceBefore = new BigNumber( + await balance(web3, contracts.daiAddress, user1) + ) // user1 buys 10 DT const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11') @@ -224,12 +226,13 @@ describe('Fixed Rate unit test', () => { expect(await balance(web3, dtAddress, user1)).to.equal( await unitsToAmount(web3, dtAddress, args.datatokenSwappedAmount) ) - console.log(daiBalanceBefore) - console.log(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount)) - console.log(await balance(web3, contracts.daiAddress, user1)) expect( daiBalanceBefore - .sub(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .minus( + new BigNumber( + await unitsToAmount(web3, contracts.daiAddress, args.baseTokenSwappedAmount) + ) + ) .toString() ).to.equal(await balance(web3, contracts.daiAddress, user1)) // baseToken stays in the contract @@ -240,7 +243,9 @@ describe('Fixed Rate unit test', () => { it('#sellDT - user1 should sell some dt', async () => { await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '100') - const daiBalanceBefore = new BN(await balance(web3, contracts.daiAddress, user1)) + const daiBalanceBefore = new BigNumber( + await balance(web3, contracts.daiAddress, user1) + ) const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) @@ -252,7 +257,11 @@ describe('Fixed Rate unit test', () => { expect(await balance(web3, dtAddress, user1)).to.equal('0') expect( daiBalanceBefore - .add(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .plus( + new BigNumber( + await unitsToAmount(web3, contracts.daiAddress, args.baseTokenSwappedAmount) + ) + ) .toString() ).to.equal(await balance(web3, contracts.daiAddress, user1)) // DTs stay in the contract @@ -340,7 +349,7 @@ describe('Fixed Rate unit test', () => { expect(result.oceanFeeAvailable).to.equal('0.042') // formatted for baseToken decimals expect(result.marketFeeCollector).to.equal(user2) - const daiBalanceBeforeCollect = new BN( + const daiBalanceBeforeCollect = new BigNumber( await balance(web3, contracts.daiAddress, user2) ) @@ -352,7 +361,7 @@ describe('Fixed Rate unit test', () => { expect(result.oceanFeeAvailable).to.equal('0.042') // user2 is the marketFeeCollector expect(await balance(web3, contracts.daiAddress, user2)).to.equal( - daiBalanceBeforeCollect.add(new BN('0.021')).toString() + daiBalanceBeforeCollect.plus(new BigNumber('0.021')).toString() ) }) @@ -512,7 +521,9 @@ describe('Fixed Rate unit test', () => { // user1 has no dts but has 100 USDC expect(await balance(web3, dtAddress, user1)).to.equal('0') - const usdcBalanceBefore = new BN(await balance(web3, contracts.usdcAddress, user1)) + const usdcBalanceBefore = new BigNumber( + await balance(web3, contracts.usdcAddress, user1) + ) // user1 buys 10 DT const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11') @@ -528,7 +539,15 @@ describe('Fixed Rate unit test', () => { ) expect( usdcBalanceBefore - .sub(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .minus( + new BigNumber( + await unitsToAmount( + web3, + contracts.usdcAddress, + args.baseTokenSwappedAmount + ) + ) + ) .toString() ).to.equal(await balance(web3, contracts.usdcAddress, user1)) // baseToken stays in the contract @@ -539,7 +558,9 @@ describe('Fixed Rate unit test', () => { it('#sellDT - user1 should sell some dt', async () => { await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '10') - const usdcBalanceBefore = new BN(await balance(web3, contracts.usdcAddress, user1)) + const usdcBalanceBefore = new BigNumber( + await balance(web3, contracts.usdcAddress, user1) + ) const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9') // console.log(tx.events.Swapped.returnValues) assert(tx.events.Swapped != null) @@ -551,7 +572,15 @@ describe('Fixed Rate unit test', () => { expect(await balance(web3, dtAddress, user1)).to.equal('0') expect( usdcBalanceBefore - .add(new BN(await unitsToAmount(web3, dtAddress, args.baseTokenSwappedAmount))) + .plus( + new BigNumber( + await unitsToAmount( + web3, + contracts.usdcAddress, + args.baseTokenSwappedAmount + ) + ) + ) .toString() ).to.equal(await balance(web3, contracts.usdcAddress, user1)) // DTs stay in the contract