From a8233e404669739d0138bb5dfe5232c37f71d7fa Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 7 Apr 2022 16:08:38 +0200 Subject: [PATCH 01/15] remove unused variables --- test/unit/pools/balancer/Pool.test.ts | 145 ++++++++++++-------------- 1 file changed, 68 insertions(+), 77 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 11f78373..ba79dea4 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -27,14 +27,10 @@ import { describe('Pool unit test', () => { let factoryOwner: string - let nftOwner: string let user1: string let user2: string - let user3: string let contracts: Addresses let pool: Pool - let dtAddress: string - let dtAddress2: string let poolAddress: string let erc20Token: string let erc20Contract: Contract @@ -44,10 +40,8 @@ describe('Pool unit test', () => { before(async () => { const accounts = await web3.eth.getAccounts() factoryOwner = accounts[0] - nftOwner = accounts[1] - user1 = accounts[2] - user2 = accounts[3] - user3 = accounts[4] + user1 = accounts[3] + user2 = accounts[4] }) it('should deploy contracts', async () => { @@ -85,7 +79,7 @@ describe('Pool unit test', () => { contracts.erc721FactoryAddress ) - assert(parseInt(allowCheck) >= 8000) + assert(parseInt(allowCheck) >= 2000) allowCheck = await allowance( web3, contracts.usdcAddress, @@ -113,7 +107,7 @@ describe('Pool unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, minter: factoryOwner, - paymentCollector: user3, + paymentCollector: user2, mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', @@ -122,8 +116,6 @@ describe('Pool unit test', () => { symbol: 'ERC20DT1Symbol' } - const baseTokenInitialLiq = await amountToUnits(web3, contracts.daiAddress, '2000') - const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, @@ -157,13 +149,13 @@ describe('Pool unit test', () => { poolAddress = txReceipt.events.NewPool.returnValues.poolAddress erc20Contract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], erc20Token) - // user2 has no dt1 - expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0') + // user1 has no dt1 + expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') }) // it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { - // expect(await daiContract.methods.balanceOf(user2).call()).to.equal( - // web3.utils.toWei(await pool.sharesBalance(user2, contracts.daiAddress)) + // expect(await daiContract.methods.balanceOf(user1).call()).to.equal( + // web3.utils.toWei(await pool.sharesBalance(user1, contracts.daiAddress)) // ) // }) @@ -241,13 +233,13 @@ describe('Pool unit test', () => { it('#swapExactAmountIn - should swap', async () => { await daiContract.methods - .transfer(user2, web3.utils.toWei('1000')) + .transfer(user1, web3.utils.toWei('1000')) .send({ from: factoryOwner }) - expect(await daiContract.methods.balanceOf(user2).call()).to.equal( + expect(await daiContract.methods.balanceOf(user1).call()).to.equal( web3.utils.toWei('1000') ) - expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0') - await approve(web3, user2, contracts.daiAddress, poolAddress, '10') + expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') + await approve(web3, user1, contracts.daiAddress, poolAddress, '10') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.daiAddress, @@ -260,19 +252,19 @@ describe('Pool unit test', () => { swapMarketFee: '0.1' } const tx = await pool.swapExactAmountIn( - user2, + user1, poolAddress, tokenInOutMarket, amountsInOutMaxFee ) - expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( + expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal( tx.events.LOG_SWAP.returnValues.tokenAmountOut ) }) it('#swapExactAmountOut - should swap', async () => { - await approve(web3, user2, contracts.daiAddress, poolAddress, '100') - expect(await daiContract.methods.balanceOf(user2).call()).to.equal( + await approve(web3, user1, contracts.daiAddress, poolAddress, '100') + expect(await daiContract.methods.balanceOf(user1).call()).to.equal( web3.utils.toWei('990') ) const tokenInOutMarket: TokenInOutMarket = { @@ -286,7 +278,7 @@ describe('Pool unit test', () => { swapMarketFee: '0.1' } const tx = await pool.swapExactAmountOut( - user2, + user1, poolAddress, tokenInOutMarket, amountsInOutMaxFee @@ -294,30 +286,30 @@ describe('Pool unit test', () => { assert(tx != null) }) - it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => { + it('#joinPool- user1 should add liquidity, receiving LP tokens', async () => { const BPTAmountOut = '0.01' const maxAmountsIn = [ '50', // Amounts IN '50' // Amounts IN ] - await approve(web3, user2, erc20Token, poolAddress, '50') - await approve(web3, user2, contracts.daiAddress, poolAddress, '50') - const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn) + await approve(web3, user1, erc20Token, poolAddress, '50') + await approve(web3, user1, contracts.daiAddress, poolAddress, '50') + const tx = await pool.joinPool(user1, poolAddress, BPTAmountOut, maxAmountsIn) assert(tx != null) - expect(await pool.sharesBalance(user2, poolAddress)).to.equal(BPTAmountOut) + expect(await pool.sharesBalance(user1, poolAddress)).to.equal(BPTAmountOut) expect(tx.events.LOG_JOIN.event === 'LOG_JOIN') expect(tx.events.LOG_BPT.event === 'LOG_BPT') }) - it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => { + it('#joinswapExternAmountIn- user1 should add liquidity, receiving LP tokens', async () => { const daiAmountIn = '100' const minBPTOut = '0.1' - await approve(web3, user2, contracts.daiAddress, poolAddress, '100', true) - expect(await allowance(web3, contracts.daiAddress, user2, poolAddress)).to.equal( + await approve(web3, user1, contracts.daiAddress, poolAddress, '100', true) + expect(await allowance(web3, contracts.daiAddress, user1, poolAddress)).to.equal( '100' ) const tx = await pool.joinswapExternAmountIn( - user2, + user1, poolAddress, daiAmountIn, minBPTOut @@ -333,14 +325,14 @@ describe('Pool unit test', () => { ) }) - it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => { + it('#exitPool- user1 exit the pool receiving both tokens, burning LP', async () => { const BPTAmountIn = '0.5' const minAmountOut = [ '1', // min amount out for OCEAN AND DT '1' ] - const tx = await pool.exitPool(user2, poolAddress, BPTAmountIn, minAmountOut) + const tx = await pool.exitPool(user1, poolAddress, BPTAmountIn, minAmountOut) assert(tx != null) @@ -348,12 +340,12 @@ describe('Pool unit test', () => { expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.daiAddress) }) - it('#exitswapPoolAmountIn- user2 exit the pool receiving only DAI', async () => { + it('#exitswapPoolAmountIn- user1 exit the pool receiving only DAI', async () => { const BPTAmountIn = '0.5' const minDAIOut = '0.5' const tx = await pool.exitswapPoolAmountIn( - user2, + user1, poolAddress, BPTAmountIn, minDAIOut @@ -367,12 +359,12 @@ describe('Pool unit test', () => { expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) }) - it('#exitswapExternAmountOut- user2 exit the pool receiving only DAI', async () => { + it('#exitswapExternAmountOut- user1 exit the pool receiving only DAI', async () => { const maxBTPIn = '0.5' const exactDAIOut = '1' const tx = await pool.exitswapPoolAmountIn( - user2, + user1, poolAddress, maxBTPIn, exactDAIOut @@ -387,7 +379,6 @@ describe('Pool unit test', () => { }) it('#getAmountInExactOut- should get the amount in for exact out', async () => { - const maxBTPIn = '0.5' const exactDAIOut = '1' const result = await pool.getAmountInExactOut( @@ -474,9 +465,9 @@ describe('Pool unit test', () => { // factoryOwner is the marketFeeCollector assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) - // user3 has no DAI (we are going to send DAI fee to him) - assert((await daiContract.methods.balanceOf(user3).call()) === '0') - // only marketFeeCollector can call this, set user3 as receiver + // user2 has no DAI (we are going to send DAI fee to him) + assert((await daiContract.methods.balanceOf(user2).call()) === '0') + // only marketFeeCollector can call this, set user2 as receiver await pool.collectMarketFee(factoryOwner, poolAddress) // DAI fees have been collected assert((await pool.getMarketFees(poolAddress, contracts.daiAddress)) === '0') @@ -547,10 +538,10 @@ describe('Pool unit test', () => { await pool.updatePublishMarketFee( factoryOwner, poolAddress, - user3, + user2, await pool.getMarketFee(poolAddress) ) - assert((await pool.getMarketFeeCollector(poolAddress)) === user3) + assert((await pool.getMarketFeeCollector(poolAddress)) === user2) }) }) @@ -570,7 +561,7 @@ describe('Pool unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, minter: factoryOwner, - paymentCollector: user3, + paymentCollector: user2, mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', @@ -616,8 +607,8 @@ describe('Pool unit test', () => { poolAddress = txReceipt.events.NewPool.returnValues.poolAddress erc20Contract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], erc20Token) - // user2 has no dt1 - expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0') + // user1 has no dt1 + expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') }) it('#calcPoolOutGivenSingleIn - should get the amount of pool OUT for exact token IN', async () => { @@ -692,8 +683,8 @@ describe('Pool unit test', () => { }) // it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { - // expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( - // await pool.sharesBalance(user2, contracts.usdcAddress) + // expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( + // await pool.sharesBalance(user1, contracts.usdcAddress) // ) // }) @@ -772,14 +763,14 @@ describe('Pool unit test', () => { it('#swapExactAmountIn - should swap', async () => { const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC await usdcContract.methods - .transfer(user2, transferAmount) + .transfer(user1, transferAmount) .send({ from: factoryOwner }) - expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( + expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( transferAmount.toString() ) - expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0') - await approve(web3, user2, contracts.usdcAddress, poolAddress, '10') + expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') + await approve(web3, user1, contracts.usdcAddress, poolAddress, '10') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.usdcAddress, tokenOut: erc20Token, @@ -791,21 +782,21 @@ describe('Pool unit test', () => { swapMarketFee: '0.1' } const tx = await pool.swapExactAmountIn( - user2, + user1, poolAddress, tokenInOutMarket, amountsInOutMaxFee ) - expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( + expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal( tx.events.LOG_SWAP.returnValues.tokenAmountOut ) }) it('#swapExactAmountOut - should swap', async () => { - expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( + expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( (await amountToUnits(web3, contracts.usdcAddress, '990')).toString() ) - await approve(web3, user2, contracts.usdcAddress, poolAddress, '100') + await approve(web3, user1, contracts.usdcAddress, poolAddress, '100') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.usdcAddress, tokenOut: erc20Token, @@ -817,7 +808,7 @@ describe('Pool unit test', () => { swapMarketFee: '0.1' } const tx = await pool.swapExactAmountOut( - user2, + user1, poolAddress, tokenInOutMarket, amountsInOutMaxFee @@ -826,18 +817,18 @@ describe('Pool unit test', () => { // console.log(tx.events) }) - it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => { + it('#joinPool- user1 should add liquidity, receiving LP tokens', async () => { const BPTAmountOut = '0.01' const maxAmountsIn = [ '50', // Amounts IN '50' // Amounts IN ] - await approve(web3, user2, erc20Token, poolAddress, '50') - await approve(web3, user2, contracts.usdcAddress, poolAddress, '50') - const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn) + await approve(web3, user1, erc20Token, poolAddress, '50') + await approve(web3, user1, contracts.usdcAddress, poolAddress, '50') + const tx = await pool.joinPool(user1, poolAddress, BPTAmountOut, maxAmountsIn) assert(tx != null) - expect(await pool.sharesBalance(user2, poolAddress)).to.equal(BPTAmountOut) + expect(await pool.sharesBalance(user1, poolAddress)).to.equal(BPTAmountOut) expect(tx.events.LOG_JOIN.event === 'LOG_JOIN') expect(tx.events.LOG_BPT.event === 'LOG_BPT') @@ -845,13 +836,13 @@ describe('Pool unit test', () => { // console.log(tx.events.LOG_JOIN) // console.log(tx.events.LOG_BPT) }) - it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => { + it('#joinswapExternAmountIn- user1 should add liquidity, receiving LP tokens', async () => { const usdcAmountIn = '100' const minBPTOut = '0.1' - await approve(web3, user2, contracts.usdcAddress, poolAddress, '100', true) + await approve(web3, user1, contracts.usdcAddress, poolAddress, '100', true) const tx = await pool.joinswapExternAmountIn( - user2, + user1, poolAddress, usdcAmountIn, minBPTOut @@ -867,14 +858,14 @@ describe('Pool unit test', () => { ) }) - it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => { + it('#exitPool- user1 exit the pool receiving both tokens, burning LP', async () => { const BPTAmountIn = '0.5' const minAmountOut = [ '1', // min amount out for USDC AND DT '1' ] - const tx = await pool.exitPool(user2, poolAddress, BPTAmountIn, minAmountOut) + const tx = await pool.exitPool(user1, poolAddress, BPTAmountIn, minAmountOut) assert(tx != null) @@ -882,12 +873,12 @@ describe('Pool unit test', () => { expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.usdcAddress) }) - it('#exitswapPoolAmountIn- user2 exit the pool receiving only USDC', async () => { + it('#exitswapPoolAmountIn- user1 exit the pool receiving only USDC', async () => { const BPTAmountIn = '0.5' const minUSDCOut = '0.5' const tx = await pool.exitswapPoolAmountIn( - user2, + user1, poolAddress, BPTAmountIn, minUSDCOut @@ -992,9 +983,9 @@ describe('Pool unit test', () => { ) // factoryOwner is the marketFeeCollector assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) - // user3 has no USDC (we are going to send USDC fee to him) - assert((await usdcContract.methods.balanceOf(user3).call()) === '0') - // only marketFeeCollector can call this, set user3 as receiver + // user2 has no USDC (we are going to send USDC fee to him) + assert((await usdcContract.methods.balanceOf(user2).call()) === '0') + // only marketFeeCollector can call this, set user2 as receiver await pool.collectMarketFee(factoryOwner, poolAddress) // USDC fees have been collected assert((await pool.getMarketFees(poolAddress, contracts.usdcAddress)) === '0') @@ -1075,11 +1066,11 @@ describe('Pool unit test', () => { await pool.updatePublishMarketFee( factoryOwner, poolAddress, - user3, + user2, await pool.getMarketFee(poolAddress) ) - assert((await pool.getMarketFeeCollector(poolAddress)) === user3) + assert((await pool.getMarketFeeCollector(poolAddress)) === user2) }) }) }) From 757a6136b0d041591c9aa0b4254834af9479e3fa Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 7 Apr 2022 16:11:30 +0200 Subject: [PATCH 02/15] remove abi imports --- test/unit/pools/balancer/Pool.test.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index ba79dea4..77ab4368 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -1,9 +1,7 @@ import { assert, expect } from 'chai' import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' -import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' @@ -48,7 +46,7 @@ describe('Pool unit test', () => { contracts = await deployContracts(web3, factoryOwner) // initialize Pool instance - pool = new Pool(web3, PoolTemplate.abi as AbiItem[]) + pool = new Pool(web3) assert(pool != null) daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) @@ -132,11 +130,7 @@ describe('Pool unit test', () => { swapFeeMarketRunner: '0.001' } - const nftFactory = new NftFactory( - contracts.erc721FactoryAddress, - web3, - ERC721Factory.abi as AbiItem[] - ) + const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const txReceipt = await nftFactory.createNftErc20WithPool( factoryOwner, @@ -590,11 +584,7 @@ describe('Pool unit test', () => { swapFeeMarketRunner: '0.001' } - const nftFactory = new NftFactory( - contracts.erc721FactoryAddress, - web3, - ERC721Factory.abi as AbiItem[] - ) + const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const txReceipt = await nftFactory.createNftErc20WithPool( factoryOwner, From 9200be874b0cff64d28b8b622d1302a1c130d0f3 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 7 Apr 2022 16:32:57 +0200 Subject: [PATCH 03/15] refactor dai and usdc constants --- test/unit/pools/balancer/Pool.test.ts | 54 +++++++++++++++------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 77ab4368..124b3628 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -12,7 +12,8 @@ import { NftFactory, NftCreateData, Pool, - unitsToAmount + unitsToAmount, + ZERO_ADDRESS } from '../../../../src' import { PoolCreationParams, @@ -35,6 +36,9 @@ describe('Pool unit test', () => { let daiContract: Contract let usdcContract: Contract + const DAI_AMOUNT = 2000 + const USDC_AMOUNT = 10000 + before(async () => { const accounts = await web3.eth.getAccounts() factoryOwner = accounts[0] @@ -47,46 +51,50 @@ describe('Pool unit test', () => { // initialize Pool instance pool = new Pool(web3) - assert(pool != null) daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) - usdcContract = new web3.eth.Contract( MockERC20.abi as AbiItem[], contracts.usdcAddress ) + await approve( web3, factoryOwner, contracts.daiAddress, contracts.erc721FactoryAddress, - '2000' + DAI_AMOUNT.toString() ) + + assert( + parseInt( + await allowance( + web3, + contracts.daiAddress, + factoryOwner, + contracts.erc721FactoryAddress + ) + ) >= DAI_AMOUNT + ) + await approve( web3, factoryOwner, contracts.usdcAddress, contracts.erc721FactoryAddress, - '10000' + USDC_AMOUNT.toString() ) - let allowCheck = await allowance( - web3, - contracts.daiAddress, - factoryOwner, - contracts.erc721FactoryAddress + assert( + parseInt( + await allowance( + web3, + contracts.usdcAddress, + factoryOwner, + contracts.erc721FactoryAddress + ) + ) >= USDC_AMOUNT ) - - assert(parseInt(allowCheck) >= 2000) - allowCheck = await allowance( - web3, - contracts.usdcAddress, - factoryOwner, - contracts.erc721FactoryAddress - ) - assert(parseInt(allowCheck) >= 10000) - - await amountToUnits(web3, contracts.usdcAddress, '20') }) describe('Test a pool with DAI (18 Decimals)', () => { @@ -107,7 +115,7 @@ describe('Pool unit test', () => { minter: factoryOwner, paymentCollector: user2, mpFeeAddress: factoryOwner, - feeToken: '0x0000000000000000000000000000000000000000', + feeToken: ZERO_ADDRESS, cap: '1000000', feeAmount: '0', name: 'ERC20B1', @@ -557,7 +565,7 @@ describe('Pool unit test', () => { minter: factoryOwner, paymentCollector: user2, mpFeeAddress: factoryOwner, - feeToken: '0x0000000000000000000000000000000000000000', + feeToken: ZERO_ADDRESS, cap: '1000000', feeAmount: '0', name: 'ERC20B1', From 751a7369a4bb37f8ba47df18476659bb2dc6cfa3 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 7 Apr 2022 17:26:11 +0200 Subject: [PATCH 04/15] add constants --- test/unit/pools/balancer/Pool.test.ts | 107 +++++++++++++------------- 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 124b3628..2cb93c0d 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -35,15 +35,51 @@ describe('Pool unit test', () => { let erc20Contract: Contract let daiContract: Contract let usdcContract: Contract + let ercParams: Erc20CreateParams + const VESTING_AMOUNT = '10000' + 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_ZERO = '0' const DAI_AMOUNT = 2000 const USDC_AMOUNT = 10000 + const RATE = '1' + const VESTED_BLOCKS = 2500000 + const FEE_LIQUIDITY_PROVIDER = '0.001' + const FEE_MARKET = '0.001' + + 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() factoryOwner = accounts[0] user1 = accounts[3] user2 = accounts[4] + + 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 () => { @@ -101,27 +137,6 @@ describe('Pool unit test', () => { it('#create a pool', async () => { // CREATE A POOL // we prepare transaction parameters objects - 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 poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, @@ -129,20 +144,20 @@ describe('Pool unit test', () => { publisherAddress: factoryOwner, marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, - rate: '1', + rate: RATE, baseTokenDecimals: 18, - vestingAmount: '10000', - vestedBlocks: 2500000, + vestingAmount: VESTING_AMOUNT, + vestedBlocks: VESTED_BLOCKS, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: '0.001', - swapFeeMarketRunner: '0.001' + swapFeeLiquidityProvider: FEE_LIQUIDITY_PROVIDER, + swapFeeMarketRunner: FEE_MARKET } const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const txReceipt = await nftFactory.createNftErc20WithPool( factoryOwner, - nftData, + NFT_DATA, ercParams, poolParams ) @@ -176,6 +191,7 @@ describe('Pool unit test', () => { (2 * Number(dtOwnerLPTBalance)).toString() ) }) + it('#getCurrentTokens - should return current pool tokens', async () => { const currentTokens = await pool.getCurrentTokens(poolAddress) expect(currentTokens[0]).to.equal(erc20Token) @@ -303,6 +319,7 @@ describe('Pool unit test', () => { expect(tx.events.LOG_JOIN.event === 'LOG_JOIN') expect(tx.events.LOG_BPT.event === 'LOG_BPT') }) + it('#joinswapExternAmountIn- user1 should add liquidity, receiving LP tokens', async () => { const daiAmountIn = '100' const minBPTOut = '0.1' @@ -551,27 +568,6 @@ describe('Pool unit test', () => { it('#create a pool', async () => { // CREATE A POOL // we prepare transaction parameters objects - 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 poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.usdcAddress, @@ -579,24 +575,24 @@ describe('Pool unit test', () => { publisherAddress: factoryOwner, marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, - rate: '1', + rate: RATE, baseTokenDecimals: await usdcContract.methods.decimals().call(), - vestingAmount: '10000', - vestedBlocks: 2500000, + vestingAmount: VESTING_AMOUNT, + vestedBlocks: VESTED_BLOCKS, initialBaseTokenLiquidity: await unitsToAmount( web3, contracts.usdcAddress, await amountToUnits(web3, contracts.usdcAddress, '2000') ), - swapFeeLiquidityProvider: '0.001', - swapFeeMarketRunner: '0.001' + swapFeeLiquidityProvider: FEE_LIQUIDITY_PROVIDER, + swapFeeMarketRunner: FEE_MARKET } const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const txReceipt = await nftFactory.createNftErc20WithPool( factoryOwner, - nftData, + NFT_DATA, ercParams, poolParams ) @@ -701,6 +697,7 @@ describe('Pool unit test', () => { (2 * Number(dtOwnerLPTBalance)).toString() ) }) + it('#getCurrentTokens - should return current pool tokens', async () => { const currentTokens = await pool.getCurrentTokens(poolAddress) expect(currentTokens[0]).to.equal(erc20Token) @@ -834,6 +831,7 @@ describe('Pool unit test', () => { // console.log(tx.events.LOG_JOIN) // console.log(tx.events.LOG_BPT) }) + it('#joinswapExternAmountIn- user1 should add liquidity, receiving LP tokens', async () => { const usdcAmountIn = '100' const minBPTOut = '0.1' @@ -891,7 +889,6 @@ describe('Pool unit test', () => { }) it('#getAmountInExactOut- should get the amount in for exact out', async () => { - const maxBTPIn = '0.5' const exactUSDCOut = '1' const result = await pool.getAmountInExactOut( From e07bebeb30a5eb5dc7af0ed9339b7d8c6329ec69 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 13 Apr 2022 20:01:17 +0200 Subject: [PATCH 05/15] uncomment and fix broken tests --- test/unit/pools/balancer/Pool.test.ts | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 2cb93c0d..b8831c23 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -170,11 +170,11 @@ describe('Pool unit test', () => { expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') }) - // it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { - // expect(await daiContract.methods.balanceOf(user1).call()).to.equal( - // web3.utils.toWei(await pool.sharesBalance(user1, contracts.daiAddress)) - // ) - // }) + it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { + expect(await daiContract.methods.balanceOf(user1).call()).to.equal( + web3.utils.toWei(await pool.sharesBalance(user1, contracts.daiAddress)) + ) + }) it('#getNumTokens - should return num of tokens in pool (2)', async () => { expect(await pool.getNumTokens(poolAddress)).to.equal('2') @@ -320,7 +320,7 @@ describe('Pool unit test', () => { expect(tx.events.LOG_BPT.event === 'LOG_BPT') }) - it('#joinswapExternAmountIn- user1 should add liquidity, receiving LP tokens', async () => { + it('#joinswapExternAmountIn - user1 should add liquidity, receiving LP tokens', async () => { const daiAmountIn = '100' const minBPTOut = '0.1' await approve(web3, user1, contracts.daiAddress, poolAddress, '100', true) @@ -359,7 +359,7 @@ describe('Pool unit test', () => { expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.daiAddress) }) - it('#exitswapPoolAmountIn- user1 exit the pool receiving only DAI', async () => { + it('#exitswapPoolAmountIn - user1 exit the pool receiving only DAI', async () => { const BPTAmountIn = '0.5' const minDAIOut = '0.5' @@ -424,7 +424,7 @@ describe('Pool unit test', () => { assert(amountIn > spotPrice) }) - it('#getAmountOutExactIn- should get the amount out for exact In', async () => { + it('#getAmountOutExactIn - should get the amount out for exact In', async () => { const exactDTIn = '1' const result = await pool.getAmountOutExactIn( @@ -451,11 +451,11 @@ describe('Pool unit test', () => { it('#getSpotPrice- should get the spot price', async () => { assert( (await pool.getSpotPrice(poolAddress, erc20Token, contracts.daiAddress, '0.1')) != - null + null ) assert( (await pool.getSpotPrice(poolAddress, contracts.daiAddress, erc20Token, '0.1')) != - null + null ) }) @@ -510,7 +510,7 @@ describe('Pool unit test', () => { it('#getOPCCollector- should get market fees for each token', async () => { assert( (await pool.getOPCCollector(poolAddress)) === - contracts.opfCommunityFeeCollectorAddress + contracts.opfCommunityFeeCollectorAddress ) }) @@ -676,11 +676,11 @@ describe('Pool unit test', () => { ) }) - // it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { - // expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( - // await pool.sharesBalance(user1, contracts.usdcAddress) - // ) - // }) + it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { + expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( + await pool.sharesBalance(user1, contracts.usdcAddress) + ) + }) it('#getNumTokens - should return num of tokens in pool (2)', async () => { expect(await pool.getNumTokens(poolAddress)).to.equal('2') @@ -832,7 +832,7 @@ describe('Pool unit test', () => { // console.log(tx.events.LOG_BPT) }) - it('#joinswapExternAmountIn- user1 should add liquidity, receiving LP tokens', async () => { + it('#joinswapExternAmountIn - user1 should add liquidity, receiving LP tokens', async () => { const usdcAmountIn = '100' const minBPTOut = '0.1' await approve(web3, user1, contracts.usdcAddress, poolAddress, '100', true) @@ -869,7 +869,7 @@ describe('Pool unit test', () => { expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.usdcAddress) }) - it('#exitswapPoolAmountIn- user1 exit the pool receiving only USDC', async () => { + it('#exitswapPoolAmountIn - user1 exit the pool receiving only USDC', async () => { const BPTAmountIn = '0.5' const minUSDCOut = '0.5' @@ -911,7 +911,7 @@ describe('Pool unit test', () => { assert(amountIn > spotPrice) }) - it('#getAmountOutExactIn- should get the amount out for exact In', async () => { + it('#getAmountOutExactIn - should get the amount out for exact In', async () => { const exactDTIn = '1' const result = await pool.getAmountOutExactIn( @@ -1004,7 +1004,7 @@ describe('Pool unit test', () => { it('#getOPCCollector- should get market fees for each token', async () => { assert( (await pool.getOPCCollector(poolAddress)) === - contracts.opfCommunityFeeCollectorAddress + contracts.opfCommunityFeeCollectorAddress ) }) From f09f03e9b665f69a51bf0ca7da65b1fcc462e06c Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 13 Apr 2022 20:05:20 +0200 Subject: [PATCH 06/15] fix lint errors --- test/unit/pools/balancer/Pool.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index b8831c23..7eb3d2e0 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -451,11 +451,11 @@ describe('Pool unit test', () => { it('#getSpotPrice- should get the spot price', async () => { assert( (await pool.getSpotPrice(poolAddress, erc20Token, contracts.daiAddress, '0.1')) != - null + null ) assert( (await pool.getSpotPrice(poolAddress, contracts.daiAddress, erc20Token, '0.1')) != - null + null ) }) @@ -510,7 +510,7 @@ describe('Pool unit test', () => { it('#getOPCCollector- should get market fees for each token', async () => { assert( (await pool.getOPCCollector(poolAddress)) === - contracts.opfCommunityFeeCollectorAddress + contracts.opfCommunityFeeCollectorAddress ) }) @@ -1004,7 +1004,7 @@ describe('Pool unit test', () => { it('#getOPCCollector- should get market fees for each token', async () => { assert( (await pool.getOPCCollector(poolAddress)) === - contracts.opfCommunityFeeCollectorAddress + contracts.opfCommunityFeeCollectorAddress ) }) From 755e0451534579b979ff9023d68876102b5c3b3a Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 28 Apr 2022 17:44:17 +0200 Subject: [PATCH 07/15] simplify constants --- test/unit/pools/balancer/Pool.test.ts | 65 +++++++++++---------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 7eb3d2e0..1b8a7102 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -37,26 +37,11 @@ describe('Pool unit test', () => { let usdcContract: Contract let ercParams: Erc20CreateParams - const VESTING_AMOUNT = '10000' - 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_ZERO = '0' - const DAI_AMOUNT = 2000 - const USDC_AMOUNT = 10000 - const RATE = '1' - const VESTED_BLOCKS = 2500000 - const FEE_LIQUIDITY_PROVIDER = '0.001' - const FEE_MARKET = '0.001' - - 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 } @@ -67,7 +52,7 @@ describe('Pool unit test', () => { user1 = accounts[3] user2 = accounts[4] - NFT_DATA.owner = factoryOwner + nftData.owner = factoryOwner ercParams = { templateIndex: 1, @@ -75,10 +60,10 @@ describe('Pool unit test', () => { paymentCollector: user2, mpFeeAddress: factoryOwner, feeToken: ZERO_ADDRESS, - cap: CAP_AMOUNT, - feeAmount: FEE_ZERO, - name: ERC20_NAME, - symbol: ERC20_SYMBOL + cap: '1000000', + feeAmount: '0', + name: 'ERC20B1', + symbol: 'ERC20DT1Symbol' } }) @@ -99,7 +84,7 @@ describe('Pool unit test', () => { factoryOwner, contracts.daiAddress, contracts.erc721FactoryAddress, - DAI_AMOUNT.toString() + '2000' ) assert( @@ -110,7 +95,7 @@ describe('Pool unit test', () => { factoryOwner, contracts.erc721FactoryAddress ) - ) >= DAI_AMOUNT + ) >= 2000 ) await approve( @@ -118,7 +103,7 @@ describe('Pool unit test', () => { factoryOwner, contracts.usdcAddress, contracts.erc721FactoryAddress, - USDC_AMOUNT.toString() + '10000' ) assert( @@ -129,7 +114,7 @@ describe('Pool unit test', () => { factoryOwner, contracts.erc721FactoryAddress ) - ) >= USDC_AMOUNT + ) >= 10000 ) }) @@ -144,20 +129,20 @@ describe('Pool unit test', () => { publisherAddress: factoryOwner, marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, - rate: RATE, + rate: '1', baseTokenDecimals: 18, - vestingAmount: VESTING_AMOUNT, - vestedBlocks: VESTED_BLOCKS, + vestingAmount: '10000', + vestedBlocks: 2500000, initialBaseTokenLiquidity: '2000', - swapFeeLiquidityProvider: FEE_LIQUIDITY_PROVIDER, - swapFeeMarketRunner: FEE_MARKET + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const txReceipt = await nftFactory.createNftErc20WithPool( factoryOwner, - NFT_DATA, + nftData, ercParams, poolParams ) @@ -575,24 +560,24 @@ describe('Pool unit test', () => { publisherAddress: factoryOwner, marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, - rate: RATE, + rate: '1', baseTokenDecimals: await usdcContract.methods.decimals().call(), - vestingAmount: VESTING_AMOUNT, - vestedBlocks: VESTED_BLOCKS, + vestingAmount: '10000', + vestedBlocks: 2500000, initialBaseTokenLiquidity: await unitsToAmount( web3, contracts.usdcAddress, await amountToUnits(web3, contracts.usdcAddress, '2000') ), - swapFeeLiquidityProvider: FEE_LIQUIDITY_PROVIDER, - swapFeeMarketRunner: FEE_MARKET + swapFeeLiquidityProvider: '0.001', + swapFeeMarketRunner: '0.001' } const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const txReceipt = await nftFactory.createNftErc20WithPool( factoryOwner, - NFT_DATA, + nftData, ercParams, poolParams ) From d77966a646aed8fa70afcff4153c6a5b385cce3e Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 23 May 2022 15:54:21 +0200 Subject: [PATCH 08/15] initialize accounts with null --- test/unit/pools/balancer/Pool.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index b50d18dc..b505ceee 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -43,7 +43,7 @@ describe('Pool unit test', () => { templateIndex: 1, tokenURI: 'https://oceanprotocol.com/nft/', transferable: true, - owner: factoryOwner + owner: null } before(async () => { From 03f3d3b7d5ebd53513bb28a87d7b3c6a4f7a822d Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 26 May 2022 13:10:18 +0200 Subject: [PATCH 09/15] use balance() function --- test/unit/pools/balancer/Pool.test.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index b505ceee..92d7c93d 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -1,7 +1,6 @@ import { assert, expect } from 'chai' import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' -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' @@ -13,7 +12,8 @@ import { NftCreateData, Pool, unitsToAmount, - ZERO_ADDRESS + ZERO_ADDRESS, + balance } from '../../../../src' import { PoolCreationParams, @@ -32,7 +32,6 @@ describe('Pool unit test', () => { let pool: Pool let poolAddress: string let erc20Token: string - let erc20Contract: Contract let daiContract: Contract let usdcContract: Contract let ercParams: Erc20CreateParams @@ -151,9 +150,8 @@ describe('Pool unit test', () => { erc20Token = txReceipt.events.TokenCreated.returnValues.newTokenAddress poolAddress = txReceipt.events.NewPool.returnValues.poolAddress - erc20Contract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], erc20Token) // user1 has no dt1 - expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, erc20Token, user1)).to.equal('0') }) it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { @@ -242,7 +240,7 @@ describe('Pool unit test', () => { expect(await daiContract.methods.balanceOf(user1).call()).to.equal( web3.utils.toWei('1000') ) - expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, erc20Token, user1)).to.equal('0') await approve(web3, user1, contracts.daiAddress, poolAddress, '10') const tokenInOutMarket: TokenInOutMarket = { @@ -261,7 +259,7 @@ describe('Pool unit test', () => { tokenInOutMarket, amountsInOutMaxFee ) - expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal( + expect(await balance(web3, erc20Token, user1)).to.equal( tx.events.LOG_SWAP.returnValues.tokenAmountOut ) }) @@ -548,9 +546,8 @@ describe('Pool unit test', () => { erc20Token = txReceipt.events.TokenCreated.returnValues.newTokenAddress poolAddress = txReceipt.events.NewPool.returnValues.poolAddress - erc20Contract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], erc20Token) // user1 has no dt1 - expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, erc20Token, user1)).to.equal('0') }) it('#calcPoolOutGivenSingleIn - should get the amount of pool OUT for exact token IN', async () => { @@ -712,7 +709,7 @@ describe('Pool unit test', () => { transferAmount.toString() ) - expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal('0') + expect(await balance(web3, erc20Token, user1)).to.equal('0') await approve(web3, user1, contracts.usdcAddress, poolAddress, '10') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.usdcAddress, @@ -730,7 +727,7 @@ describe('Pool unit test', () => { tokenInOutMarket, amountsInOutMaxFee ) - expect(await erc20Contract.methods.balanceOf(user1).call()).to.equal( + expect(await balance(web3, erc20Token, user1)).to.equal( tx.events.LOG_SWAP.returnValues.tokenAmountOut ) }) From b44f1f802aa63f61f80b7428f1d24750814d9d1d Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 26 May 2022 13:28:53 +0200 Subject: [PATCH 10/15] use balance() and transfer() functions --- test/unit/pools/balancer/Pool.test.ts | 75 ++++++++++++--------------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 92d7c93d..be22293b 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -1,7 +1,5 @@ import { assert, expect } from 'chai' -import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' -import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { @@ -13,7 +11,8 @@ import { Pool, unitsToAmount, ZERO_ADDRESS, - balance + balance, + transfer } from '../../../../src' import { PoolCreationParams, @@ -32,8 +31,6 @@ describe('Pool unit test', () => { let pool: Pool let poolAddress: string let erc20Token: string - let daiContract: Contract - let usdcContract: Contract let ercParams: Erc20CreateParams const nftData: NftCreateData = { @@ -73,12 +70,6 @@ describe('Pool unit test', () => { pool = new Pool(web3, 8996) assert(pool != null) - daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) - usdcContract = new web3.eth.Contract( - MockERC20.abi as AbiItem[], - contracts.usdcAddress - ) - await approve( web3, factoryOwner, @@ -155,7 +146,7 @@ describe('Pool unit test', () => { }) it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { - expect(await daiContract.methods.balanceOf(user1).call()).to.equal( + expect(await balance(web3, contracts.daiAddress, user1)).to.equal( web3.utils.toWei(await pool.sharesBalance(user1, contracts.daiAddress)) ) }) @@ -234,12 +225,8 @@ describe('Pool unit test', () => { }) it('#swapExactAmountIn - should swap', async () => { - await daiContract.methods - .transfer(user1, web3.utils.toWei('1000')) - .send({ from: factoryOwner }) - expect(await daiContract.methods.balanceOf(user1).call()).to.equal( - web3.utils.toWei('1000') - ) + await transfer(web3, factoryOwner, contracts.daiAddress, user1, '1000') + expect(await balance(web3, contracts.daiAddress, user1)).to.equal('1000') expect(await balance(web3, erc20Token, user1)).to.equal('0') await approve(web3, user1, contracts.daiAddress, poolAddress, '10') @@ -266,9 +253,7 @@ describe('Pool unit test', () => { it('#swapExactAmountOut - should swap', async () => { await approve(web3, user1, contracts.daiAddress, poolAddress, '100') - expect(await daiContract.methods.balanceOf(user1).call()).to.equal( - web3.utils.toWei('990') - ) + expect(await balance(web3, contracts.daiAddress, user1)).to.equal('990') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.daiAddress, tokenOut: erc20Token, @@ -438,7 +423,7 @@ describe('Pool unit test', () => { // factoryOwner is the marketFeeCollector assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) // user2 has no DAI (we are going to send DAI fee to him) - assert((await daiContract.methods.balanceOf(user2).call()) === '0') + assert((await balance(web3, contracts.daiAddress, user2)) === '0') // only marketFeeCollector can call this, set user2 as receiver await pool.collectMarketFee(factoryOwner, poolAddress) // DAI fees have been collected @@ -471,9 +456,11 @@ describe('Pool unit test', () => { assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0') // opf collector has no DAI assert( - (await daiContract.methods - .balanceOf(contracts.opfCommunityFeeCollectorAddress) - .call()) === '0' + (await balance( + web3, + contracts.daiAddress, + contracts.opfCommunityFeeCollectorAddress + )) === '0' ) // anyone can call callectOPF await pool.collectOPC(factoryOwner, poolAddress) @@ -481,9 +468,11 @@ describe('Pool unit test', () => { assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) === '0') // OPF collector got DAI assert( - (await daiContract.methods - .balanceOf(contracts.opfCommunityFeeCollectorAddress) - .call()) > '0' + (await balance( + web3, + contracts.daiAddress, + contracts.opfCommunityFeeCollectorAddress + )) > '0' ) // Spot price hasn't changed after fee collection assert( @@ -522,7 +511,7 @@ describe('Pool unit test', () => { marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', - baseTokenDecimals: await usdcContract.methods.decimals().call(), + baseTokenDecimals: 6, vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: await unitsToAmount( @@ -622,7 +611,7 @@ describe('Pool unit test', () => { }) it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { - expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( + expect(await balance(web3, contracts.usdcAddress, user1)).to.equal( await pool.sharesBalance(user1, contracts.usdcAddress) ) }) @@ -702,10 +691,8 @@ describe('Pool unit test', () => { it('#swapExactAmountIn - should swap', async () => { const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC - await usdcContract.methods - .transfer(user1, transferAmount) - .send({ from: factoryOwner }) - expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( + await transfer(web3, factoryOwner, contracts.usdcAddress, user1, transferAmount) + expect(await balance(web3, contracts.usdcAddress, user1)).to.equal( transferAmount.toString() ) @@ -733,7 +720,7 @@ describe('Pool unit test', () => { }) it('#swapExactAmountOut - should swap', async () => { - expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( + expect(await balance(web3, contracts.usdcAddress, user1)).to.equal( (await amountToUnits(web3, contracts.usdcAddress, '990')).toString() ) await approve(web3, user1, contracts.usdcAddress, poolAddress, '100') @@ -889,7 +876,7 @@ describe('Pool unit test', () => { // factoryOwner is the marketFeeCollector assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) // user2 has no USDC (we are going to send USDC fee to him) - assert((await usdcContract.methods.balanceOf(user2).call()) === '0') + assert((await balance(web3, contracts.usdcAddress, user2)) === '0') // only marketFeeCollector can call this, set user2 as receiver await pool.collectMarketFee(factoryOwner, poolAddress) // USDC fees have been collected @@ -932,9 +919,11 @@ describe('Pool unit test', () => { assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0') // opf collector has no USDC assert( - (await usdcContract.methods - .balanceOf(contracts.opfCommunityFeeCollectorAddress) - .call()) === '0' + (await balance( + web3, + contracts.usdcAddress, + contracts.opfCommunityFeeCollectorAddress + )) === '0' ) // anyone can call callectOPF await pool.collectOPC(factoryOwner, poolAddress) @@ -942,9 +931,11 @@ describe('Pool unit test', () => { assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) === '0') // OPF collector got USDC assert( - (await usdcContract.methods - .balanceOf(contracts.opfCommunityFeeCollectorAddress) - .call()) > '0' + (await balance( + web3, + contracts.usdcAddress, + contracts.opfCommunityFeeCollectorAddress + )) > '0' ) // Spot price hasn't changed after fee collection assert( From a7c0cff63c4d80eda33e825bc9343b61c4d69e85 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 26 May 2022 19:04:24 +0200 Subject: [PATCH 11/15] fix decimals issue --- test/unit/pools/balancer/Pool.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index be22293b..d2eeef8c 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -247,7 +247,11 @@ describe('Pool unit test', () => { amountsInOutMaxFee ) expect(await balance(web3, erc20Token, user1)).to.equal( - tx.events.LOG_SWAP.returnValues.tokenAmountOut + await unitsToAmount( + web3, + erc20Token, + tx.events.LOG_SWAP.returnValues.tokenAmountOut + ) ) }) @@ -715,7 +719,11 @@ describe('Pool unit test', () => { amountsInOutMaxFee ) expect(await balance(web3, erc20Token, user1)).to.equal( - tx.events.LOG_SWAP.returnValues.tokenAmountOut + await unitsToAmount( + web3, + erc20Token, + tx.events.LOG_SWAP.returnValues.tokenAmountOut + ) ) }) From b3102c78c12349d78d9118ae587080c00a8aa141 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Fri, 27 May 2022 10:55:12 +0200 Subject: [PATCH 12/15] add decimals() function --- src/utils/ContractUtils.ts | 1 + src/utils/TokenUtils.ts | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 17066603..94012c76 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -117,6 +117,7 @@ export async function getPoolCreationParams( ] } } + export async function unitsToAmount( web3: Web3, token: string, diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index c8008c3f..a22a5e0c 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -183,3 +183,14 @@ export async function balance( return await unitsToAmount(web3, tokenAddress, trxReceipt, tokenDecimals) } + +/** + * Get decimals for any erc20 + * @param {Web3} web3 + * @param {String} tokenAdress + * @return {Promise} Number of decimals of the token + */ +export async function decimals(web3: Web3, tokenAddress: string): Promise { + const tokenContract = new web3.eth.Contract(minAbi, tokenAddress) + return await tokenContract.methods.decimals().call() +} From 2b3b5c871d3318280b0d36d3dcd0085edec6bf4d Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Fri, 27 May 2022 11:05:09 +0200 Subject: [PATCH 13/15] use decimals() in Pool --- test/unit/pools/balancer/Pool.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index d2eeef8c..44df0744 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -1,5 +1,4 @@ import { assert, expect } from 'chai' -import { Contract } from 'web3-eth-contract' import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { @@ -12,7 +11,8 @@ import { unitsToAmount, ZERO_ADDRESS, balance, - transfer + transfer, + decimals } from '../../../../src' import { PoolCreationParams, @@ -515,7 +515,7 @@ describe('Pool unit test', () => { marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', - baseTokenDecimals: 6, + baseTokenDecimals: await decimals(web3, contracts.usdcAddress), vestingAmount: '10000', vestedBlocks: 2500000, initialBaseTokenLiquidity: await unitsToAmount( @@ -728,10 +728,10 @@ describe('Pool unit test', () => { }) it('#swapExactAmountOut - should swap', async () => { + await approve(web3, user1, contracts.usdcAddress, poolAddress, '100') expect(await balance(web3, contracts.usdcAddress, user1)).to.equal( (await amountToUnits(web3, contracts.usdcAddress, '990')).toString() ) - await approve(web3, user1, contracts.usdcAddress, poolAddress, '100') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.usdcAddress, tokenOut: erc20Token, From 4e923810079fa5245ba3e5354a98e17c94fb9886 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Fri, 27 May 2022 17:16:45 +0200 Subject: [PATCH 14/15] fix error in decimals --- test/unit/pools/balancer/Pool.test.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 44df0744..57dedf50 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -227,6 +227,7 @@ describe('Pool unit test', () => { it('#swapExactAmountIn - should swap', async () => { await transfer(web3, factoryOwner, contracts.daiAddress, user1, '1000') expect(await balance(web3, contracts.daiAddress, user1)).to.equal('1000') + expect(await balance(web3, erc20Token, user1)).to.equal('0') await approve(web3, user1, contracts.daiAddress, poolAddress, '10') @@ -518,11 +519,7 @@ describe('Pool unit test', () => { baseTokenDecimals: await decimals(web3, contracts.usdcAddress), vestingAmount: '10000', vestedBlocks: 2500000, - initialBaseTokenLiquidity: await unitsToAmount( - web3, - contracts.usdcAddress, - await amountToUnits(web3, contracts.usdcAddress, '2000') - ), + initialBaseTokenLiquidity: '2000', swapFeeLiquidityProvider: '0.001', swapFeeMarketRunner: '0.001' } @@ -694,14 +691,12 @@ describe('Pool unit test', () => { }) it('#swapExactAmountIn - should swap', async () => { - const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC - await transfer(web3, factoryOwner, contracts.usdcAddress, user1, transferAmount) - expect(await balance(web3, contracts.usdcAddress, user1)).to.equal( - transferAmount.toString() - ) + await transfer(web3, factoryOwner, contracts.usdcAddress, user1, '1000') + expect(await balance(web3, contracts.usdcAddress, user1)).to.equal('1000') expect(await balance(web3, erc20Token, user1)).to.equal('0') await approve(web3, user1, contracts.usdcAddress, poolAddress, '10') + const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.usdcAddress, tokenOut: erc20Token, @@ -729,9 +724,7 @@ describe('Pool unit test', () => { it('#swapExactAmountOut - should swap', async () => { await approve(web3, user1, contracts.usdcAddress, poolAddress, '100') - expect(await balance(web3, contracts.usdcAddress, user1)).to.equal( - (await amountToUnits(web3, contracts.usdcAddress, '990')).toString() - ) + expect(await balance(web3, contracts.usdcAddress, user1)).to.equal('990') const tokenInOutMarket: TokenInOutMarket = { tokenIn: contracts.usdcAddress, tokenOut: erc20Token, From 28e46a31f7972538f9fed92ce553bd234d35ae4b Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 30 May 2022 11:09:19 +0200 Subject: [PATCH 15/15] remove unused imports --- test/unit/pools/balancer/Pool.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 57dedf50..c3bbe2f4 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -3,7 +3,6 @@ import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { allowance, - amountToUnits, approve, NftFactory, NftCreateData,