diff --git a/test/TestContractHandler.ts b/test/TestContractHandler.ts index 4575a4a2..6c5d58af 100644 --- a/test/TestContractHandler.ts +++ b/test/TestContractHandler.ts @@ -1,468 +1,236 @@ import Web3 from 'web3' -import { Contract } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' +import OPFCommunityFeeCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' +import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' +import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' +import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' -import { getAddresses } from './config' +import Router from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' +import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' +import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' +import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' +import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' +import { getAddresses, GAS_PRICE } from './config' -export class TestContractHandler { - public accounts: string[] - public ERC721Factory: Contract - public ERC20Template: Contract - public ERC721Template: Contract - public Router: Contract - public SideStaking: Contract - public FixedRate: Contract - public Dispenser: Contract - public OPFCollector: Contract - public PoolTemplate: Contract - public MockERC20: Contract - public MockOcean: Contract - - public ERC721FactoryBytecode: string - public ERC20TemplateBytecode: string - public ERC721TemplateBytecode: string - public RouterBytecode: string - public SideStakingBytecode: string - public FixedRateBytecode: string - public DispenserBytecode: string - public PoolTemplateBytecode: string - public OPFCollectorBytecode: string - public MockERC20Bytecode: string - public OPFBytecode: string - - public factory721Address: string - public template721Address: string - public template20Address: string - public routerAddress: string - public sideStakingAddress: string - public fixedRateAddress: string - public dispenserAddress: string - public poolTemplateAddress: string - public opfCollectorAddress: string - public oceanAddress: string - public daiAddress: string - public usdcAddress: string - public web3: Web3 - - constructor( - web3: Web3, - ERC721TemplateABI: AbiItem | AbiItem[], - ERC20TemplateABI: AbiItem | AbiItem[], - PoolTemplateABI?: AbiItem | AbiItem[], - ERC721FactoryABI?: AbiItem | AbiItem[], - RouterABI?: AbiItem | AbiItem[], - SideStakingABI?: AbiItem | AbiItem[], - FixedRateABI?: AbiItem | AbiItem[], - DispenserABI?: AbiItem | AbiItem[], - OPFABI?: AbiItem | AbiItem[], - - template721Bytecode?: string, - template20Bytecode?: string, - poolTemplateBytecode?: string, - factory721Bytecode?: string, - routerBytecode?: string, - sideStakingBytecode?: string, - fixedRateBytecode?: string, - dispenserBytecode?: string, - opfBytecode?: string - ) { - this.web3 = web3 - this.ERC721Template = new this.web3.eth.Contract(ERC721TemplateABI) - this.ERC20Template = new this.web3.eth.Contract(ERC20TemplateABI) - this.PoolTemplate = new this.web3.eth.Contract(PoolTemplateABI) - this.ERC721Factory = new this.web3.eth.Contract(ERC721FactoryABI) - this.Router = new this.web3.eth.Contract(RouterABI) - this.SideStaking = new this.web3.eth.Contract(SideStakingABI) - this.FixedRate = new this.web3.eth.Contract(FixedRateABI) - this.Dispenser = new this.web3.eth.Contract(DispenserABI) - this.MockERC20 = new this.web3.eth.Contract(MockERC20.abi as AbiItem[]) - this.OPFCollector = new this.web3.eth.Contract(OPFABI) - - this.ERC721FactoryBytecode = factory721Bytecode - this.ERC20TemplateBytecode = template20Bytecode - this.PoolTemplateBytecode = poolTemplateBytecode - this.ERC721TemplateBytecode = template721Bytecode - this.RouterBytecode = routerBytecode - this.SideStakingBytecode = sideStakingBytecode - this.FixedRateBytecode = fixedRateBytecode - this.DispenserBytecode = dispenserBytecode - this.MockERC20Bytecode = MockERC20.bytecode - this.OPFBytecode = opfBytecode - } - - public async getAccounts(): Promise { - this.accounts = await this.web3.eth.getAccounts() - return this.accounts - } - - public async deployContracts(owner: string, routerABI?: AbiItem | AbiItem[]) { - const addresses = getAddresses() - - let estGas - if (addresses.OPFCommunityFeeCollector) { - this.opfCollectorAddress = addresses.OPFCommunityFeeCollector - } else { - // DEPLOY OPF Fee Collector - // get est gascost - estGas = await this.OPFCollector.deploy({ - data: this.OPFBytecode, - arguments: [owner, owner] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.opfCollectorAddress = await this.OPFCollector.deploy({ - data: this.OPFBytecode, - arguments: [owner, owner] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - if (addresses.poolTemplate) { - this.poolTemplateAddress = addresses.poolTemplate - } else { - // DEPLOY POOL TEMPLATE - // get est gascost - estGas = await this.PoolTemplate.deploy({ - data: this.PoolTemplateBytecode, - arguments: [] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.poolTemplateAddress = await this.PoolTemplate.deploy({ - data: this.PoolTemplateBytecode, - arguments: [] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - if (addresses.ERC20Template['1']) { - this.template20Address = addresses.ERC20Template['1'] - } else { - // DEPLOY ERC20 TEMPLATE - // get est gascost - estGas = await this.ERC20Template.deploy({ - data: this.ERC20TemplateBytecode, - arguments: [] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.template20Address = await this.ERC20Template.deploy({ - data: this.ERC20TemplateBytecode, - arguments: [] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - if (addresses.ERC721Template['1']) { - this.template721Address = addresses.ERC721Template['1'] - } else { - // DEPLOY ERC721 TEMPLATE - // get est gascost - estGas = await this.ERC721Template.deploy({ - data: this.ERC721TemplateBytecode, - arguments: [] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.template721Address = await this.ERC721Template.deploy({ - data: this.ERC721TemplateBytecode, - arguments: [] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - if (addresses.Ocean) { - this.oceanAddress = addresses.Ocean - } else { - // DEPLOY OCEAN MOCK - // get est gascost - estGas = await this.MockERC20.deploy({ - data: this.MockERC20Bytecode, - arguments: ['OCEAN', 'OCEAN', 18] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.oceanAddress = await this.MockERC20.deploy({ - data: this.MockERC20Bytecode, - arguments: ['OCEAN', 'OCEAN', 18] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - if (addresses.Router) { - this.routerAddress = addresses.Router - } else { - // DEPLOY ROUTER - estGas = await this.Router.deploy({ - data: this.RouterBytecode, - arguments: [ - owner, - this.oceanAddress, - this.poolTemplateAddress, - this.opfCollectorAddress, - [] - ] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.routerAddress = await this.Router.deploy({ - data: this.RouterBytecode, - arguments: [ - owner, - this.oceanAddress, - this.poolTemplateAddress, - this.opfCollectorAddress, - [] - ] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - if (addresses.Staking) { - this.sideStakingAddress = addresses.Staking - } else { - // DEPLOY SIDE STAKING - estGas = await this.SideStaking.deploy({ - data: this.SideStakingBytecode, - arguments: [this.routerAddress] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.sideStakingAddress = await this.SideStaking.deploy({ - data: this.SideStakingBytecode, - arguments: [this.routerAddress] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - // DEPLOY FIXED RATE - - if (addresses.FixedPrice) { - this.fixedRateAddress = addresses.FixedPrice - } else { - estGas = await this.FixedRate.deploy({ - data: this.FixedRateBytecode, - arguments: [this.routerAddress, this.opfCollectorAddress] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.fixedRateAddress = await this.FixedRate.deploy({ - data: this.FixedRateBytecode, - arguments: [this.routerAddress, this.opfCollectorAddress] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - // DEPLOY Dispenser - - if (addresses.Dispenser) { - this.dispenserAddress = addresses.Dispenser - } else { - estGas = await this.Dispenser.deploy({ - data: this.DispenserBytecode, - arguments: [this.routerAddress] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.dispenserAddress = await this.Dispenser.deploy({ - data: this.DispenserBytecode, - arguments: [this.routerAddress] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - // DEPLOY ERC721 FACTORY - - if (addresses.ERC721Factory) { - this.factory721Address = addresses.ERC721Factory - } else { - estGas = await this.ERC721Factory.deploy({ - data: this.ERC721FactoryBytecode, - arguments: [ - this.template721Address, - this.template20Address, - this.opfCollectorAddress, - this.routerAddress - ] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - - // deploy the contract and get it's address - this.factory721Address = await this.ERC721Factory.deploy({ - data: this.ERC721FactoryBytecode, - arguments: [ - this.template721Address, - this.template20Address, - this.opfCollectorAddress, - this.routerAddress - ] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - // DEPLOY USDC MOCK - - if (addresses.MockUSDC) { - this.usdcAddress = addresses.MockUSDC - } else { - // get est gascost - estGas = await this.MockERC20.deploy({ - data: this.MockERC20Bytecode, - arguments: ['USDC', 'USDC', 6] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.usdcAddress = await this.MockERC20.deploy({ - data: this.MockERC20Bytecode, - arguments: ['USDC', 'USDC', 6] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - // DEPLOY DAI MOCK - - if (addresses.MockDAI) { - this.daiAddress = addresses.MockDAI - } else { - // get est gascost - estGas = await this.MockERC20.deploy({ - data: this.MockERC20Bytecode, - arguments: ['DAI', 'DAI', 18] - }).estimateGas(function (err, estGas) { - if (err) console.log('DeployContracts: ' + err) - return estGas - }) - // deploy the contract and get it's address - this.daiAddress = await this.MockERC20.deploy({ - data: this.MockERC20Bytecode, - arguments: ['DAI', 'DAI', 18] - }) - .send({ - from: owner, - gas: estGas + 1, - gasPrice: '3000000000' - }) - .then(function (contract) { - return contract.options.address - }) - } - - if (!addresses.Router) { - const RouterContract = new this.web3.eth.Contract(routerABI, this.routerAddress) - - await RouterContract.methods - .addFactory(this.factory721Address) - .send({ from: owner }) - await RouterContract.methods - .addFixedRateContract(this.fixedRateAddress) - .send({ from: owner }) - await RouterContract.methods - .addDispenserContract(this.dispenserAddress) - .send({ from: owner }) - await RouterContract.methods - .addSSContract(this.sideStakingAddress) - .send({ from: owner }) - // TODO: add OPF deployment - // await RouterContract.methods - // .changeRouterOwner(this.opfCollectorAddress) - // .send({ from: owner }) - } - } +const estimateGasAndDeployContract = async ( + web3: Web3, + abi: AbiItem | AbiItem[], + bytecode: string, + argumentsArray: any[], + owner: string +) => { + const contract = new web3.eth.Contract(abi) + // get est gascost + const estimatedGas = await contract + .deploy({ + data: bytecode, + arguments: argumentsArray + }) + .estimateGas(function (err, estimatedGas) { + if (err) console.log('DeployContracts: ' + err) + return estimatedGas + }) + // deploy the contract and get its address + return await contract + .deploy({ + data: bytecode, + arguments: argumentsArray + }) + .send({ + from: owner, + gas: estimatedGas + 1, + gasPrice: GAS_PRICE + }) + .then(function (contract) { + return contract.options.address + }) +} + +export interface Addresses { + opfCommunityFeeCollectorAddress: string + poolTemplateAddress: string + erc20TemplateAddress: string + erc721TemplateAddress: string + oceanAddress: string + routerAddress: string + sideStakingAddress: string + fixedRateAddress: string + dispenserAddress: string + erc721FactoryAddress: string + daiAddress: string + usdcAddress: string +} + +export const deployContracts = async (web3: Web3, owner: string): Promise => { + const addresses: Addresses = {} as Addresses + const configAddresses = getAddresses() + + // deploy OPF free collector + addresses.opfCommunityFeeCollectorAddress = + configAddresses.OPFCommunityFeeCollector || + (await estimateGasAndDeployContract( + web3, + OPFCommunityFeeCollector.abi as AbiItem[], + OPFCommunityFeeCollector.bytecode, + [owner, owner], + owner + )) + + // deploy pool template + addresses.poolTemplateAddress = + configAddresses.poolTemplate || + (await estimateGasAndDeployContract( + web3, + PoolTemplate.abi as AbiItem[], + PoolTemplate.bytecode, + [], + owner + )) + + // deploy ERC20 template + addresses.erc20TemplateAddress = + configAddresses.ERC20Template['1'] || + (await estimateGasAndDeployContract( + web3, + ERC20Template.abi as AbiItem[], + ERC20Template.bytecode, + [], + owner + )) + + // deploy ERC721 template + addresses.erc721TemplateAddress = + configAddresses.ERC721Template['1'] || + (await estimateGasAndDeployContract( + web3, + ERC721Template.abi as AbiItem[], + ERC721Template.bytecode, + [], + owner + )) + + // deploy OCEAN mock tocken + addresses.oceanAddress = + configAddresses.Ocean || + (await estimateGasAndDeployContract( + web3, + MockERC20.abi as AbiItem[], + MockERC20.bytecode, + ['OCEAN', 'OCEAN', 18], + owner + )) + + // deploy router + addresses.routerAddress = + configAddresses.Router || + (await estimateGasAndDeployContract( + web3, + Router.abi as AbiItem[], + Router.bytecode, + [ + owner, + addresses.oceanAddress, + addresses.poolTemplateAddress, + addresses.opfCommunityFeeCollectorAddress, + [] + ], + owner + )) + + // deploy side stacking + addresses.sideStakingAddress = + configAddresses.Staking || + (await estimateGasAndDeployContract( + web3, + SideStaking.abi as AbiItem[], + SideStaking.bytecode, + [addresses.routerAddress], + owner + )) + + // deploy fixed rate + addresses.fixedRateAddress = + configAddresses.FixedPrice || + (await estimateGasAndDeployContract( + web3, + FixedRate.abi as AbiItem[], + FixedRate.bytecode, + [addresses.routerAddress, addresses.opfCommunityFeeCollectorAddress], + owner + )) + + // deploy dispenser + addresses.dispenserAddress = + configAddresses.Dispenser || + (await estimateGasAndDeployContract( + web3, + Dispenser.abi as AbiItem[], + Dispenser.bytecode, + [addresses.routerAddress], + owner + )) + + // deploy ERC721 factory + addresses.erc721FactoryAddress = + configAddresses.ERC721Factory || + (await estimateGasAndDeployContract( + web3, + ERC721Factory.abi as AbiItem[], + ERC721Factory.bytecode, + [ + addresses.erc721TemplateAddress, + addresses.erc20TemplateAddress, + addresses.opfCommunityFeeCollectorAddress, + addresses.routerAddress + ], + owner + )) + + // deploy USDC mock tocken + addresses.usdcAddress = + configAddresses.MockUSDC || + (await estimateGasAndDeployContract( + web3, + MockERC20.abi as AbiItem[], + MockERC20.bytecode, + ['USDC', 'USDC', 6], + owner + )) + + // deploy DAI mock tocken + addresses.daiAddress = + configAddresses.MockDAI || + (await estimateGasAndDeployContract( + web3, + MockERC20.abi as AbiItem[], + MockERC20.bytecode, + ['DAI', 'DAI', 18], + owner + )) + + if (!configAddresses.Router) { + const RouterContract = new web3.eth.Contract( + Router.abi as AbiItem[], + addresses.routerAddress + ) + + await RouterContract.methods + .addFactory(addresses.erc721FactoryAddress) + .send({ from: owner }) + await RouterContract.methods + .addFixedRateContract(addresses.fixedRateAddress) + .send({ from: owner }) + await RouterContract.methods + .addDispenserContract(addresses.dispenserAddress) + .send({ from: owner }) + await RouterContract.methods + .addSSContract(addresses.sideStakingAddress) + .send({ from: owner }) + // TODO: add OPF deployment + // await RouterContract.methods + // .changeRouterOwner(this.opfCommunityFeeCollectorAddress) + // .send({ from: owner }) + } + + return addresses } diff --git a/test/config.ts b/test/config.ts index d83d9242..f86adce1 100644 --- a/test/config.ts +++ b/test/config.ts @@ -10,6 +10,8 @@ import { LoggerInstance.setLevel(LogLevel.Error) +export const GAS_PRICE = '3000000000' + // by default, we connect with development network export const web3 = new Web3(process.env.NODE_URI || configHelperNetworks[1].nodeUri) diff --git a/test/unit/NftFactory.test.ts b/test/unit/NftFactory.test.ts index 9b81246b..fbcaabc8 100644 --- a/test/unit/NftFactory.test.ts +++ b/test/unit/NftFactory.test.ts @@ -1,15 +1,8 @@ import { assert, expect } from 'chai' import { AbiItem } from 'web3-utils/types' -import { TestContractHandler } from '../TestContractHandler' -import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' -import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import Router from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' +import { deployContracts, Addresses } from '../TestContractHandler' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import OPFCommunityFeeCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.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 { web3 } from '../config' import { NftFactory, NftCreateData, TokenOrder, ZERO_ADDRESS, signHash } from '../../src' import { @@ -25,71 +18,51 @@ describe('Nft Factory test', () => { let user1: string let user2: string let user3: string - let contracts: TestContractHandler + let contracts: Addresses let nftFactory: NftFactory let dtAddress: string let dtAddress2: string let nftAddress: string + before(async () => { + const accounts = await web3.eth.getAccounts() + factoryOwner = accounts[0] + nftOwner = accounts[1] + user1 = accounts[2] + user2 = accounts[3] + user3 = accounts[4] + }) + it('should deploy contracts', async () => { - contracts = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - Router.abi as AbiItem[], - SideStaking.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCommunityFeeCollector.abi as AbiItem[], - - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - Router.bytecode, - SideStaking.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCommunityFeeCollector.bytecode - ) - await contracts.getAccounts() - factoryOwner = contracts.accounts[0] - nftOwner = contracts.accounts[1] - user1 = contracts.accounts[2] - user2 = contracts.accounts[3] - user3 = contracts.accounts[4] - - await contracts.deployContracts(factoryOwner, Router.abi as AbiItem[]) + contracts = await deployContracts(web3, factoryOwner) const daiContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, + MockERC20.abi as AbiItem[], contracts.daiAddress ) await daiContract.methods - .approve(contracts.factory721Address, web3.utils.toWei('10000')) - .send({ from: contracts.accounts[0] }) + .approve(contracts.erc721FactoryAddress, web3.utils.toWei('10000')) + .send({ from: factoryOwner }) }) it('should initiate NFTFactory instance', async () => { - nftFactory = new NftFactory(contracts.factory721Address, web3) + nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) }) it('#getOwner - should return actual owner', async () => { const owner = await nftFactory.getOwner() - assert(owner === contracts.accounts[0]) + assert(owner === factoryOwner) }) it('#getNFTTemplate - should return NFT template struct', async () => { const nftTemplate = await nftFactory.getNFTTemplate(1) assert(nftTemplate.isActive === true) - assert(nftTemplate.templateAddress === contracts.template721Address) + assert(nftTemplate.templateAddress === contracts.erc721TemplateAddress) }) it('#getTokenTemplate - should return Token template struct', async () => { const tokenTemplate = await nftFactory.getTokenTemplate(1) assert(tokenTemplate.isActive === true) - assert(tokenTemplate.templateAddress === contracts.template20Address) + assert(tokenTemplate.templateAddress === contracts.erc20TemplateAddress) }) it('#createNftwithErc - should create an NFT and a Datatoken ', async () => { @@ -103,7 +76,7 @@ describe('Nft Factory test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, mpFeeAddress: user2, feeToken: '0x0000000000000000000000000000000000000000', @@ -114,7 +87,7 @@ describe('Nft Factory test', () => { } const txReceipt = await nftFactory.createNftWithErc20( - contracts.accounts[0], + factoryOwner, nftData, ercParams ) @@ -152,9 +125,9 @@ describe('Nft Factory test', () => { const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: 18, @@ -166,7 +139,7 @@ describe('Nft Factory test', () => { } const txReceipt = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData, ercParams, poolParams @@ -189,7 +162,7 @@ describe('Nft Factory test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, mpFeeAddress: user2, feeToken: '0x0000000000000000000000000000000000000000', @@ -202,18 +175,18 @@ describe('Nft Factory test', () => { const freParams: FreCreationParams = { fixedRateAddress: contracts.fixedRateAddress, baseTokenAddress: contracts.daiAddress, - owner: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + owner: factoryOwner, + marketFeeCollector: factoryOwner, baseTokenDecimals: 18, datatokenDecimals: 18, fixedRate: '1', marketFee: '0.001', - allowedConsumer: contracts.accounts[0], + allowedConsumer: factoryOwner, withMint: false } const txReceipt = await nftFactory.createNftErc20WithFixedRate( - contracts.accounts[0], + factoryOwner, nftData, ercParams, freParams @@ -239,7 +212,7 @@ describe('Nft Factory test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, mpFeeAddress: user2, feeToken: '0x0000000000000000000000000000000000000000', @@ -258,7 +231,7 @@ describe('Nft Factory test', () => { } const txReceipt = await nftFactory.createNftErc20WithDispenser( - contracts.accounts[0], + factoryOwner, nftData, ercParams, dispenserParams @@ -286,11 +259,11 @@ describe('Nft Factory test', () => { expect(await dtContract.methods.balanceOf(user2).call()).to.equal('0') // dt owner mint dtAmount to user2 - await dtContract.methods.mint(user2, dtAmount).send({ from: contracts.accounts[0] }) + await dtContract.methods.mint(user2, dtAmount).send({ from: factoryOwner }) // user2 approves NFTFactory to move his dtAmount await dtContract.methods - .approve(contracts.factory721Address, dtAmount) + .approve(contracts.erc721FactoryAddress, dtAmount) .send({ from: user2 }) // we reuse another DT created in a previous test @@ -298,10 +271,10 @@ describe('Nft Factory test', () => { expect(await dtContract2.methods.balanceOf(user2).call()).to.equal('0') // dt owner mint dtAmount to user2 - await dtContract2.methods.mint(user2, dtAmount).send({ from: contracts.accounts[0] }) + await dtContract2.methods.mint(user2, dtAmount).send({ from: factoryOwner }) // user2 approves NFTFactory to move his dtAmount await dtContract2.methods - .approve(contracts.factory721Address, dtAmount) + .approve(contracts.erc721FactoryAddress, dtAmount) .send({ from: user2 }) // we check user2 has enought DTs @@ -375,7 +348,7 @@ describe('Nft Factory test', () => { it('#addNFTTemplate - should add a new erc721 token template', async () => { const currentNFTTemplateCount = await nftFactory.getCurrentNFTTemplateCount() - await nftFactory.addNFTTemplate(factoryOwner, contracts.template721Address) + await nftFactory.addNFTTemplate(factoryOwner, contracts.erc721TemplateAddress) expect( (await nftFactory.getCurrentNFTTemplateCount()) === currentNFTTemplateCount + 1 @@ -409,7 +382,7 @@ describe('Nft Factory test', () => { it('#addTokenTemplate - should add a new erc20 token template', async () => { const currentTokenTemplateCount = await nftFactory.getCurrentTokenTemplateCount() - await nftFactory.addTokenTemplate(factoryOwner, contracts.template20Address) + await nftFactory.addTokenTemplate(factoryOwner, contracts.erc20TemplateAddress) expect( (await nftFactory.getCurrentTokenTemplateCount()) === currentTokenTemplateCount + 1 diff --git a/test/unit/pools/Router.test.ts b/test/unit/pools/Router.test.ts index 4bdd63ac..66b3e99c 100644 --- a/test/unit/pools/Router.test.ts +++ b/test/unit/pools/Router.test.ts @@ -1,15 +1,9 @@ import { assert, expect } from 'chai' import { AbiItem } from 'web3-utils/types' -import { TestContractHandler } from '../../TestContractHandler' +import { deployContracts, Addresses } from '../../TestContractHandler' import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' -import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import OPFCommunityFeeCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.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 { web3 } from '../../config' import { NftFactory, NftCreateData } from '../../../src' import { Router } from '../../../src/pools/Router' @@ -23,51 +17,31 @@ describe('Router unit test', () => { let user1: string let user2: string let user3: string - let contracts: TestContractHandler + let contracts: Addresses let router: Router let dtAddress: string let dtAddress2: string let nftAddress: string + before(async () => { + const accounts = await web3.eth.getAccounts() + factoryOwner = accounts[0] + nftOwner = accounts[1] + user1 = accounts[2] + user2 = accounts[3] + user3 = accounts[4] + }) + it('should deploy contracts', async () => { - contracts = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - FactoryRouter.abi as AbiItem[], - SideStaking.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCommunityFeeCollector.abi as AbiItem[], - - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - FactoryRouter.bytecode, - SideStaking.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCommunityFeeCollector.bytecode - ) - await contracts.getAccounts() - factoryOwner = contracts.accounts[0] - nftOwner = contracts.accounts[1] - user1 = contracts.accounts[2] - user2 = contracts.accounts[3] - user3 = contracts.accounts[4] - - await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[]) + contracts = await deployContracts(web3, factoryOwner) const daiContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, + MockERC20.abi as AbiItem[], contracts.daiAddress ) await daiContract.methods - .approve(contracts.factory721Address, web3.utils.toWei('10000')) - .send({ from: contracts.accounts[0] }) + .approve(contracts.erc721FactoryAddress, web3.utils.toWei('10000')) + .send({ from: factoryOwner }) }) it('should initiate Router instance', async () => { @@ -76,12 +50,12 @@ describe('Router unit test', () => { it('#getOwner - should return actual owner', async () => { const owner = await router.getOwner() - assert(owner === contracts.accounts[0]) + assert(owner === factoryOwner) }) it('#getNFTFactory - should return NFT Factory address', async () => { const factory = await router.getNFTFactory() - assert(factory === contracts.factory721Address) + assert(factory === contracts.erc721FactoryAddress) }) it('#isOceanTokens - should return true if in oceanTokens list', async () => { @@ -104,13 +78,13 @@ describe('Router unit test', () => { it('#buyDTBatch - should buy multiple DT in one call', async () => { // APPROVE DAI const daiContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, + MockERC20.abi as AbiItem[], contracts.daiAddress ) await daiContract.methods .transfer(user2, web3.utils.toWei('2')) - .send({ from: contracts.accounts[0] }) + .send({ from: factoryOwner }) await daiContract.methods .approve(contracts.routerAddress, web3.utils.toWei('2')) .send({ from: user2 }) @@ -126,9 +100,9 @@ describe('Router unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', feeAmount: '0', @@ -139,9 +113,9 @@ describe('Router unit test', () => { const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: 18, @@ -153,13 +127,13 @@ describe('Router unit test', () => { } const nftFactory = new NftFactory( - contracts.factory721Address, + contracts.erc721FactoryAddress, web3, ERC721Factory.abi as AbiItem[] ) const txReceipt = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData, ercParams, poolParams @@ -179,9 +153,9 @@ describe('Router unit test', () => { const ercParams2: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', feeAmount: '0', @@ -192,9 +166,9 @@ describe('Router unit test', () => { const poolParams2: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: 18, @@ -206,7 +180,7 @@ describe('Router unit test', () => { } const txReceipt2 = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData2, ercParams2, poolParams2 @@ -245,7 +219,7 @@ describe('Router unit test', () => { amountsOut: web3.utils.toWei('0.1'), // when swapExactAmountIn is MIN amount OUT maxPrice: web3.utils.toWei('10'), // max price (only for pools), swapMarketFee: web3.utils.toWei('0.1'), - marketFeeAddress: contracts.accounts[0] + marketFeeAddress: factoryOwner } const operations2: Operation = { @@ -258,7 +232,7 @@ describe('Router unit test', () => { amountsOut: web3.utils.toWei('0.1'), // when swapExactAmountIn is MIN amount OUT maxPrice: web3.utils.toWei('10'), // max price (only for pools) swapMarketFee: web3.utils.toWei('0.1'), - marketFeeAddress: contracts.accounts[0] + marketFeeAddress: factoryOwner } await router.buyDTBatch(user2, [operations1, operations2]) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 355b9c23..73552f82 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -2,15 +2,10 @@ 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 ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' -import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { TestContractHandler } from '../../../TestContractHandler' +import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' +import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { allowance, @@ -36,7 +31,7 @@ describe('Pool unit test', () => { let user1: string let user2: string let user3: string - let contracts: TestContractHandler + let contracts: Addresses let pool: Pool let dtAddress: string let dtAddress2: string @@ -46,79 +41,56 @@ describe('Pool unit test', () => { let daiContract: Contract let usdcContract: Contract + before(async () => { + const accounts = await web3.eth.getAccounts() + factoryOwner = accounts[0] + nftOwner = accounts[1] + user1 = accounts[2] + user2 = accounts[3] + user3 = accounts[4] + }) + it('should deploy contracts', async () => { - contracts = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - FactoryRouter.abi as AbiItem[], - SideStaking.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCollector.abi as AbiItem[], - - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - FactoryRouter.bytecode, - SideStaking.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCollector.bytecode - ) - await contracts.getAccounts() - factoryOwner = contracts.accounts[0] - nftOwner = contracts.accounts[1] - user1 = contracts.accounts[2] - user2 = contracts.accounts[3] - user3 = contracts.accounts[4] - - await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[]) + contracts = await deployContracts(web3, factoryOwner) // initialize Pool instance pool = new Pool(web3, PoolTemplate.abi as AbiItem[]) assert(pool != null) - daiContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, - contracts.daiAddress - ) + daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) usdcContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, + MockERC20.abi as AbiItem[], contracts.usdcAddress ) await approve( web3, - contracts.accounts[0], + factoryOwner, contracts.daiAddress, - contracts.factory721Address, + contracts.erc721FactoryAddress, '2000' ) await approve( web3, - contracts.accounts[0], + factoryOwner, contracts.usdcAddress, - contracts.factory721Address, + contracts.erc721FactoryAddress, '10000' ) let allowCheck = await allowance( web3, contracts.daiAddress, - contracts.accounts[0], - contracts.factory721Address + factoryOwner, + contracts.erc721FactoryAddress ) assert(parseInt(allowCheck) >= 8000) allowCheck = await allowance( web3, contracts.usdcAddress, - contracts.accounts[0], - contracts.factory721Address + factoryOwner, + contracts.erc721FactoryAddress ) assert(parseInt(allowCheck) >= 10000) @@ -138,9 +110,9 @@ describe('Pool unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', feeAmount: '0', @@ -153,9 +125,9 @@ describe('Pool unit test', () => { const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: 18, @@ -167,13 +139,13 @@ describe('Pool unit test', () => { } const nftFactory = new NftFactory( - contracts.factory721Address, + contracts.erc721FactoryAddress, web3, ERC721Factory.abi as AbiItem[] ) const txReceipt = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData, ercParams, poolParams @@ -199,11 +171,8 @@ describe('Pool unit test', () => { it('#getPoolSharesTotalSupply - should return totalSupply of LPT', async () => { // dt owner which added liquidity has half of pool shares (the rest is in the sidestaking contracta) - const dtOwnerLPTBalance = await pool.sharesBalance( - contracts.accounts[0], - poolAddress - ) - expect(await pool.sharesBalance(contracts.accounts[0], poolAddress)).to.equal( + const dtOwnerLPTBalance = await pool.sharesBalance(factoryOwner, poolAddress) + expect(await pool.sharesBalance(factoryOwner, poolAddress)).to.equal( await pool.sharesBalance(contracts.sideStakingAddress, poolAddress) ) // total supply is twice the dtOwner balance @@ -268,153 +237,152 @@ describe('Pool unit test', () => { expect(await pool.getDatatoken(poolAddress)).to.equal(erc20Token) }) - // TODO: uncomment test after contracts update - // it('#swapExactAmountIn - should swap', async () => { - // await daiContract.methods - // .transfer(user2, web3.utils.toWei('1000')) - // .send({ from: contracts.accounts[0] }) - // expect(await daiContract.methods.balanceOf(user2).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') + it('#swapExactAmountIn - should swap', async () => { + await daiContract.methods + .transfer(user2, web3.utils.toWei('1000')) + .send({ from: factoryOwner }) + expect(await daiContract.methods.balanceOf(user2).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') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.daiAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsInMaxFee = { - // tokenAmountIn: '10', - // minAmountOut: '1', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountIn( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( - // tx.events.LOG_SWAP.returnValues.tokenAmountOut - // ) - // }) + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.daiAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsInMaxFee = { + tokenAmountIn: '10', + minAmountOut: '1', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountIn( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + expect(await erc20Contract.methods.balanceOf(user2).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( - // web3.utils.toWei('990') - // ) - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.daiAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsOutMaxFee = { - // maxAmountIn: '100', - // tokenAmountOut: '50', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountOut( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // assert(tx != null) - // }) + it('#swapExactAmountOut - should swap', async () => { + await approve(web3, user2, contracts.daiAddress, poolAddress, '100') + expect(await daiContract.methods.balanceOf(user2).call()).to.equal( + web3.utils.toWei('990') + ) + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.daiAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsOutMaxFee = { + maxAmountIn: '100', + tokenAmountOut: '50', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountOut( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + assert(tx != null) + }) - // it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => { - // const BPTAmountOut = '0.01' - // const maxAmountsIn = [ - // '50', // Amounts IN - // '50' // Amounts IN - // ] + it('#joinPool- user2 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) - // assert(tx != null) - // expect(await pool.sharesBalance(user2, 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 () => { - // 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( - // '100' - // ) - // const tx = await pool.joinswapExternAmountIn( - // user2, - // poolAddress, - // daiAmountIn, - // minBPTOut - // ) + await approve(web3, user2, erc20Token, poolAddress, '50') + await approve(web3, user2, contracts.daiAddress, poolAddress, '50') + const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn) + assert(tx != null) + expect(await pool.sharesBalance(user2, 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 () => { + 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( + '100' + ) + const tx = await pool.joinswapExternAmountIn( + user2, + poolAddress, + daiAmountIn, + minBPTOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') - // expect(tx.events.LOG_BPT.event === 'LOG_BPT') - // // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event - // expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( - // tx.events.LOG_JOIN[1].returnValues.bptAmount - // ) - // }) + expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') + expect(tx.events.LOG_BPT.event === 'LOG_BPT') + // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event + expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( + tx.events.LOG_JOIN[1].returnValues.bptAmount + ) + }) - // it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => { - // const BPTAmountIn = '0.5' - // const minAmountOut = [ - // '1', // min amount out for OCEAN AND DT - // '1' - // ] + it('#exitPool- user2 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(user2, poolAddress, BPTAmountIn, minAmountOut) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(erc20Token) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.daiAddress) - // }) + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(erc20Token) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.daiAddress) + }) - // it('#exitswapPoolAmountIn- user2 exit the pool receiving only DAI', async () => { - // const BPTAmountIn = '0.5' - // const minDAIOut = '0.5' + it('#exitswapPoolAmountIn- user2 exit the pool receiving only DAI', async () => { + const BPTAmountIn = '0.5' + const minDAIOut = '0.5' - // const tx = await pool.exitswapPoolAmountIn( - // user2, - // poolAddress, - // BPTAmountIn, - // minDAIOut - // ) + const tx = await pool.exitswapPoolAmountIn( + user2, + poolAddress, + BPTAmountIn, + minDAIOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.daiAddress) + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.daiAddress) - // // DTs were also unstaked in the same transaction (went to the staking contract) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) - // }) + // DTs were also unstaked in the same transaction (went to the staking contract) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) + }) - // it('#exitswapExternAmountOut- user2 exit the pool receiving only DAI', async () => { - // const maxBTPIn = '0.5' - // const exactDAIOut = '1' + it('#exitswapExternAmountOut- user2 exit the pool receiving only DAI', async () => { + const maxBTPIn = '0.5' + const exactDAIOut = '1' - // const tx = await pool.exitswapPoolAmountIn( - // user2, - // poolAddress, - // maxBTPIn, - // exactDAIOut - // ) + const tx = await pool.exitswapPoolAmountIn( + user2, + poolAddress, + maxBTPIn, + exactDAIOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.daiAddress) + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.daiAddress) - // // DTs were also unstaked in the same transaction (went to the staking contract) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) - // }) + // DTs were also unstaked in the same transaction (went to the staking contract) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) + }) it('#getAmountInExactOut- should get the amount in for exact out', async () => { const maxBTPIn = '0.5' @@ -479,97 +447,103 @@ describe('Pool unit test', () => { ) }) - // it('#getMarketFees- should get market fees for each token', async () => { - // // we haven't performed any swap DT => DAI so there's no fee in erc20Token - // // but there's a fee in DAI - // assert((await pool.getMarketFees(poolAddress, erc20Token)) === '0') - // assert((await pool.getMarketFees(poolAddress, contracts.daiAddress)) > '0') - // }) + it('#getMarketFees- should get market fees for each token', async () => { + // we haven't performed any swap DT => DAI so there's no fee in erc20Token + // but there's a fee in DAI + assert((await pool.getMarketFees(poolAddress, erc20Token)) === '0') + assert((await pool.getMarketFees(poolAddress, contracts.daiAddress)) > '0') + }) - // it('#getCommunityFees- should get community fees for each token', async () => { - // // we haven't performed any swap DT => DAI so there's no fee in erc20Token - // // but there's a fee in DAI + it('#getCommunityFees- should get community fees for each token', async () => { + // we haven't performed any swap DT => DAI so there's no fee in erc20Token + // but there's a fee in DAI - // assert((await pool.getCommunityFees(poolAddress, erc20Token)) === '0') - // assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0') - // }) + assert((await pool.getCommunityFees(poolAddress, erc20Token)) === '0') + assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0') + }) - // it('#collectMarketFee- should collect market fees for each token', async () => { - // const spotPriceBefore = await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.daiAddress, - // '0.1' - // ) + it('#collectMarketFee- should collect market fees for each token', async () => { + const spotPriceBefore = await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.daiAddress, + '0.1' + ) - // // contracts.accounts[0] is the marketFeeCollector - // assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) - // // 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 - // await pool.collectMarketFee(contracts.accounts[0], poolAddress) - // // DAI fees have been collected - // assert((await pool.getMarketFees(poolAddress, contracts.daiAddress)) === '0') + // 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 + await pool.collectMarketFee(factoryOwner, poolAddress) + // DAI fees have been collected + assert((await pool.getMarketFees(poolAddress, contracts.daiAddress)) === '0') - // // Spot price hasn't changed after fee collection - // assert( - // (await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.daiAddress, - // '0.1' - // )) === spotPriceBefore - // ) - // }) + // Spot price hasn't changed after fee collection + assert( + (await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.daiAddress, + '0.1' + )) === spotPriceBefore + ) + }) it('#getMarketFeeCollector- should get market fees for each token', async () => { - // contracts.accounts[0] is the marketFeeCollector - assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) + // factoryOwner is the marketFeeCollector + assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) }) it('#getOPCCollector- should get market fees for each token', async () => { - assert((await pool.getOPCCollector(poolAddress)) === contracts.opfCollectorAddress) + assert( + (await pool.getOPCCollector(poolAddress)) === + contracts.opfCommunityFeeCollectorAddress + ) }) - // it('#collectCommunityFee- should get community fees for each token', async () => { - // const spotPriceBefore = await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.daiAddress, - // '0.1' - // ) - // // some fee are available in DAI - // assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0') - // // opf collector has no DAI - // assert( - // (await daiContract.methods.balanceOf(contracts.opfCollectorAddress).call()) === - // '0' - // ) - // // anyone can call callectOPF - // await pool.collectOPC(contracts.accounts[0], poolAddress) - // // DAI fees have been collected - // assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) === '0') - // // OPF collector got DAI - // assert( - // (await daiContract.methods.balanceOf(contracts.opfCollectorAddress).call()) > '0' - // ) - // // Spot price hasn't changed after fee collection - // assert( - // (await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.daiAddress, - // '0.1' - // )) === spotPriceBefore - // ) - // }) + it('#collectCommunityFee- should get community fees for each token', async () => { + const spotPriceBefore = await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.daiAddress, + '0.1' + ) + // some fee are available in DAI + assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0') + // opf collector has no DAI + assert( + (await daiContract.methods + .balanceOf(contracts.opfCommunityFeeCollectorAddress) + .call()) === '0' + ) + // anyone can call callectOPF + await pool.collectOPC(factoryOwner, poolAddress) + // DAI fees have been collected + assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) === '0') + // OPF collector got DAI + assert( + (await daiContract.methods + .balanceOf(contracts.opfCommunityFeeCollectorAddress) + .call()) > '0' + ) + // Spot price hasn't changed after fee collection + assert( + (await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.daiAddress, + '0.1' + )) === spotPriceBefore + ) + }) it('#updateMarketFeeCollector- should update market fee collector', async () => { - // contracts.accounts[0] is the marketFeeCollector + // factoryOwner is the marketFeeCollector - assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) + assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) await pool.updatePublishMarketFee( - contracts.accounts[0], + factoryOwner, poolAddress, user3, await pool.getMarketFee(poolAddress) @@ -591,9 +565,9 @@ describe('Pool unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', feeAmount: '0', @@ -604,9 +578,9 @@ describe('Pool unit test', () => { const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.usdcAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: await usdcContract.methods.decimals().call(), @@ -622,13 +596,13 @@ describe('Pool unit test', () => { } const nftFactory = new NftFactory( - contracts.factory721Address, + contracts.erc721FactoryAddress, web3, ERC721Factory.abi as AbiItem[] ) const txReceipt = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData, ercParams, poolParams @@ -725,11 +699,8 @@ describe('Pool unit test', () => { it('#getPoolSharesTotalSupply - should return totalSupply of LPT', async () => { // dt owner which added liquidity has half of pool shares (the rest is in the sidestaking contracta) - const dtOwnerLPTBalance = await pool.sharesBalance( - contracts.accounts[0], - poolAddress - ) - expect(await pool.sharesBalance(contracts.accounts[0], poolAddress)).to.equal( + const dtOwnerLPTBalance = await pool.sharesBalance(factoryOwner, poolAddress) + expect(await pool.sharesBalance(factoryOwner, poolAddress)).to.equal( await pool.sharesBalance(contracts.sideStakingAddress, poolAddress) ) // total supply is twice the dtOwner balance @@ -793,134 +764,138 @@ describe('Pool unit test', () => { it('#getDatatoken - should return the datatoken address', async () => { expect(await pool.getDatatoken(poolAddress)).to.equal(erc20Token) }) - // TODO: uncomment test after contract update - // it('#swapExactAmountIn - should swap', async () => { - // const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC - // await usdcContract.methods - // .transfer(user2, transferAmount) - // .send({ from: contracts.accounts[0] }) - // expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( - // transferAmount.toString() - // ) - // expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0') - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '10') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.usdcAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsInMaxFee = { - // tokenAmountIn: '10', - // minAmountOut: '1', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountIn( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( - // tx.events.LOG_SWAP.returnValues.tokenAmountOut - // ) - // }) + it('#swapExactAmountIn - should swap', async () => { + const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC + await usdcContract.methods + .transfer(user2, transferAmount) + .send({ from: factoryOwner }) + expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( + transferAmount.toString() + ) - // it('#swapExactAmountOut - should swap', async () => { - // expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( - // (await amountToUnits(web3, contracts.usdcAddress, '990')).toString() - // ) - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '100') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.usdcAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsOutMaxFee = { - // maxAmountIn: '100', - // tokenAmountOut: '50', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountOut( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // assert(tx != null) - // // console.log(tx.events) - // }) + expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0') + await approve(web3, user2, contracts.usdcAddress, poolAddress, '10') + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.usdcAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsInMaxFee = { + tokenAmountIn: '10', + minAmountOut: '1', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountIn( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( + tx.events.LOG_SWAP.returnValues.tokenAmountOut + ) + }) - // it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => { - // const BPTAmountOut = '0.01' - // const maxAmountsIn = [ - // '50', // Amounts IN - // '50' // Amounts IN - // ] + it('#swapExactAmountOut - should swap', async () => { + expect(await usdcContract.methods.balanceOf(user2).call()).to.equal( + (await amountToUnits(web3, contracts.usdcAddress, '990')).toString() + ) + await approve(web3, user2, contracts.usdcAddress, poolAddress, '100') + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.usdcAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsOutMaxFee = { + maxAmountIn: '100', + tokenAmountOut: '50', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountOut( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + assert(tx != null) + // console.log(tx.events) + }) - // await approve(web3, user2, erc20Token, poolAddress, '50') - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '50') - // const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn) - // assert(tx != null) - // expect(await pool.sharesBalance(user2, 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 () => { - // const usdcAmountIn = '100' - // const minBPTOut = '0.1' - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '100', true) + it('#joinPool- user2 should add liquidity, receiving LP tokens', async () => { + const BPTAmountOut = '0.01' + const maxAmountsIn = [ + '50', // Amounts IN + '50' // Amounts IN + ] - // const tx = await pool.joinswapExternAmountIn( - // user2, - // poolAddress, - // usdcAmountIn, - // minBPTOut - // ) + await approve(web3, user2, erc20Token, poolAddress, '50') + await approve(web3, user2, contracts.usdcAddress, poolAddress, '50') + const tx = await pool.joinPool(user2, poolAddress, BPTAmountOut, maxAmountsIn) + assert(tx != null) + expect(await pool.sharesBalance(user2, poolAddress)).to.equal(BPTAmountOut) + expect(tx.events.LOG_JOIN.event === 'LOG_JOIN') + expect(tx.events.LOG_BPT.event === 'LOG_BPT') - // assert(tx != null) + // console.log(tx) + // console.log(tx.events.LOG_JOIN) + // console.log(tx.events.LOG_BPT) + }) + it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => { + const usdcAmountIn = '100' + const minBPTOut = '0.1' + await approve(web3, user2, contracts.usdcAddress, poolAddress, '100', true) - // expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') - // expect(tx.events.LOG_BPT.event === 'LOG_BPT') - // // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event - // expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( - // tx.events.LOG_JOIN[1].returnValues.bptAmount - // ) - // }) + const tx = await pool.joinswapExternAmountIn( + user2, + poolAddress, + usdcAmountIn, + minBPTOut + ) - // it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => { - // const BPTAmountIn = '0.5' - // const minAmountOut = [ - // '1', // min amount out for USDC AND DT - // '1' - // ] + assert(tx != null) - // const tx = await pool.exitPool(user2, poolAddress, BPTAmountIn, minAmountOut) + expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') + expect(tx.events.LOG_BPT.event === 'LOG_BPT') + // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event + expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( + tx.events.LOG_JOIN[1].returnValues.bptAmount + ) + }) - // assert(tx != null) + it('#exitPool- user2 exit the pool receiving both tokens, burning LP', async () => { + const BPTAmountIn = '0.5' + const minAmountOut = [ + '1', // min amount out for USDC AND DT + '1' + ] - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(erc20Token) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.usdcAddress) - // }) + const tx = await pool.exitPool(user2, poolAddress, BPTAmountIn, minAmountOut) - // it('#exitswapPoolAmountIn- user2 exit the pool receiving only USDC', async () => { - // const BPTAmountIn = '0.5' - // const minUSDCOut = '0.5' + assert(tx != null) - // const tx = await pool.exitswapPoolAmountIn( - // user2, - // poolAddress, - // BPTAmountIn, - // minUSDCOut - // ) + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(erc20Token) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(contracts.usdcAddress) + }) - // assert(tx != null) + it('#exitswapPoolAmountIn- user2 exit the pool receiving only USDC', async () => { + const BPTAmountIn = '0.5' + const minUSDCOut = '0.5' - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.usdcAddress) + const tx = await pool.exitswapPoolAmountIn( + user2, + poolAddress, + BPTAmountIn, + minUSDCOut + ) - // // DTs were also unstaked in the same transaction (went to the staking contract) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) - // }) + assert(tx != null) + + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.usdcAddress) + + // DTs were also unstaked in the same transaction (went to the staking contract) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) + }) it('#getAmountInExactOut- should get the amount in for exact out', async () => { const maxBTPIn = '0.5' @@ -989,55 +964,58 @@ describe('Pool unit test', () => { ) }) - // it('#getMarketFees- should get market fees for each token', async () => { - // // we haven't performed any swap DT => USDC so there's no fee in erc20Token - // // but there's a fee in USDC - // assert((await pool.getMarketFees(poolAddress, erc20Token)) === '0') - // assert((await pool.getMarketFees(poolAddress, contracts.usdcAddress)) > '0') - // }) + it('#getMarketFees- should get market fees for each token', async () => { + // we haven't performed any swap DT => USDC so there's no fee in erc20Token + // but there's a fee in USDC + assert((await pool.getMarketFees(poolAddress, erc20Token)) === '0') + assert((await pool.getMarketFees(poolAddress, contracts.usdcAddress)) > '0') + }) - // it('#getCommunityFees- should get community fees for each token', async () => { - // // we haven't performed any swap DT => USDC so there's no fee in erc20Token - // // but there's a fee in USDC + it('#getCommunityFees- should get community fees for each token', async () => { + // we haven't performed any swap DT => USDC so there's no fee in erc20Token + // but there's a fee in USDC - // assert((await pool.getCommunityFees(poolAddress, erc20Token)) === '0') - // assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0') - // }) + assert((await pool.getCommunityFees(poolAddress, erc20Token)) === '0') + assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0') + }) - // it('#collectMarketFee- should collect market fees for each token', async () => { - // const spotPriceBefore = await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.usdcAddress, - // '0.1' - // ) - // // contracts.accounts[0] is the marketFeeCollector - // assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) - // // 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 - // await pool.collectMarketFee(contracts.accounts[0], poolAddress) - // // USDC fees have been collected - // assert((await pool.getMarketFees(poolAddress, contracts.usdcAddress)) === '0') + it('#collectMarketFee- should collect market fees for each token', async () => { + const spotPriceBefore = await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.usdcAddress, + '0.1' + ) + // 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 + await pool.collectMarketFee(factoryOwner, poolAddress) + // USDC fees have been collected + assert((await pool.getMarketFees(poolAddress, contracts.usdcAddress)) === '0') - // // Spot price hasn't changed after fee collection - // assert( - // (await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.usdcAddress, - // '0.1' - // )) === spotPriceBefore - // ) - // }) + // Spot price hasn't changed after fee collection + assert( + (await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.usdcAddress, + '0.1' + )) === spotPriceBefore + ) + }) it('#getMarketFeeCollector- should get market fees for each token', async () => { - // contracts.accounts[0] is the marketFeeCollector - assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) + // factoryOwner is the marketFeeCollector + assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) }) it('#getOPCCollector- should get market fees for each token', async () => { - assert((await pool.getOPCCollector(poolAddress)) === contracts.opfCollectorAddress) + assert( + (await pool.getOPCCollector(poolAddress)) === + contracts.opfCommunityFeeCollectorAddress + ) }) it('#getCurrentMarketFees- should get curent market fees for each token', async () => { @@ -1050,45 +1028,48 @@ describe('Pool unit test', () => { assert(curentOPFFees !== null) }) - // it('#collectCommunityFee- should get community fees for each token', async () => { - // const spotPriceBefore = await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.usdcAddress, - // '0.1' - // ) - // // some fee are available in USDC - // assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0') - // // opf collector has no USDC - // assert( - // (await usdcContract.methods.balanceOf(contracts.opfCollectorAddress).call()) === - // '0' - // ) - // // anyone can call callectOPF - // await pool.collectOPC(contracts.accounts[0], poolAddress) - // // USDC fees have been collected - // assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) === '0') - // // OPF collector got USDC - // assert( - // (await usdcContract.methods.balanceOf(contracts.opfCollectorAddress).call()) > '0' - // ) - // // Spot price hasn't changed after fee collection - // assert( - // (await pool.getSpotPrice( - // poolAddress, - // erc20Token, - // contracts.usdcAddress, - // '0.1' - // )) === spotPriceBefore - // ) - // }) + it('#collectCommunityFee- should get community fees for each token', async () => { + const spotPriceBefore = await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.usdcAddress, + '0.1' + ) + // some fee are available in USDC + assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0') + // opf collector has no USDC + assert( + (await usdcContract.methods + .balanceOf(contracts.opfCommunityFeeCollectorAddress) + .call()) === '0' + ) + // anyone can call callectOPF + await pool.collectOPC(factoryOwner, poolAddress) + // USDC fees have been collected + assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) === '0') + // OPF collector got USDC + assert( + (await usdcContract.methods + .balanceOf(contracts.opfCommunityFeeCollectorAddress) + .call()) > '0' + ) + // Spot price hasn't changed after fee collection + assert( + (await pool.getSpotPrice( + poolAddress, + erc20Token, + contracts.usdcAddress, + '0.1' + )) === spotPriceBefore + ) + }) it('#updateMarketFeeCollector- should update market fee collector', async () => { - // contracts.accounts[0] is the marketFeeCollector - assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0]) + // factoryOwner is the marketFeeCollector + assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) await pool.updatePublishMarketFee( - contracts.accounts[0], + factoryOwner, poolAddress, user3, await pool.getMarketFee(poolAddress) diff --git a/test/unit/pools/dispenser/Dispenser.test.ts b/test/unit/pools/dispenser/Dispenser.test.ts index 005ac5de..69f7452d 100644 --- a/test/unit/pools/dispenser/Dispenser.test.ts +++ b/test/unit/pools/dispenser/Dispenser.test.ts @@ -1,15 +1,8 @@ import { AbiItem } from 'web3-utils' import { assert, expect } from 'chai' -import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' -import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import DispenserTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' -import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { TestContractHandler } from '../../../TestContractHandler' +import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { NftFactory, @@ -26,7 +19,7 @@ describe('Dispenser flow', () => { let user1: string let user2: string let user3: string - let contracts: TestContractHandler + let contracts: Addresses let DispenserAddress: string let DispenserClass: Dispenser let nftFactory: NftFactory @@ -34,37 +27,17 @@ describe('Dispenser flow', () => { let nftAddress: string let dtAddress: string + before(async () => { + const accounts = await web3.eth.getAccounts() + factoryOwner = accounts[0] + nftOwner = accounts[1] + user1 = accounts[2] + user2 = accounts[3] + user3 = accounts[4] + }) + it('should deploy contracts', async () => { - contracts = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - FactoryRouter.abi as AbiItem[], - SideStaking.abi as AbiItem[], - FixedRate.abi as AbiItem[], - DispenserTemplate.abi as AbiItem[], - OPFCollector.abi as AbiItem[], - - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - FactoryRouter.bytecode, - SideStaking.bytecode, - FixedRate.bytecode, - DispenserTemplate.bytecode, - OPFCollector.bytecode - ) - await contracts.getAccounts() - factoryOwner = contracts.accounts[0] - nftOwner = contracts.accounts[1] - user1 = contracts.accounts[2] - user2 = contracts.accounts[3] - user3 = contracts.accounts[4] - - await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[]) + contracts = await deployContracts(web3, factoryOwner) }) it('should initialize Dispenser class', async () => { @@ -77,7 +50,7 @@ describe('Dispenser flow', () => { }) it('#createNftwithErc - should create an NFT and a Datatoken ', async () => { - nftFactory = new NftFactory(contracts.factory721Address, web3) + nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftData: NftCreateData = { name: '72120Bundle', @@ -88,7 +61,7 @@ describe('Dispenser flow', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, mpFeeAddress: user2, feeToken: '0x0000000000000000000000000000000000000000', @@ -99,7 +72,7 @@ describe('Dispenser flow', () => { } const txReceipt = await nftFactory.createNftWithErc20( - contracts.accounts[0], + factoryOwner, nftData, ercParams ) @@ -113,7 +86,7 @@ describe('Dispenser flow', () => { it('Make user2 minter', async () => { datatoken = new Datatoken(web3, ERC20Template.abi as AbiItem[]) - await datatoken.addMinter(dtAddress, contracts.accounts[0], user2) + await datatoken.addMinter(dtAddress, factoryOwner, user2) assert((await datatoken.getDTPermissions(dtAddress, user2)).minter === true) }) @@ -125,7 +98,7 @@ describe('Dispenser flow', () => { } const tx = await datatoken.createDispenser( dtAddress, - contracts.accounts[0], + factoryOwner, contracts.dispenserAddress, dispenserParams ) @@ -133,42 +106,33 @@ describe('Dispenser flow', () => { }) it('Activate dispenser', async () => { - const tx = await DispenserClass.activate(dtAddress, '1', '1', contracts.accounts[0]) + const tx = await DispenserClass.activate(dtAddress, '1', '1', factoryOwner) assert(tx, 'Cannot activate dispenser') }) it('user2 gets the dispenser status', async () => { const status = await DispenserClass.status(dtAddress) assert(status.active === true, 'Dispenser not active') - assert(status.owner === contracts.accounts[0], 'Dispenser owner is not alice') + assert(status.owner === factoryOwner, 'Dispenser owner is not alice') assert(status.isMinter === true, 'Dispenser is not a minter') }) it('user2 deactivates the dispenser', async () => { - const tx = await DispenserClass.deactivate(dtAddress, contracts.accounts[0]) + const tx = await DispenserClass.deactivate(dtAddress, factoryOwner) assert(tx, 'Cannot deactivate dispenser') const status = await DispenserClass.status(dtAddress) assert(status.active === false, 'Dispenser is still active') }) it('user2 sets user3 as an AllowedSwapper for the dispenser', async () => { - const tx = await DispenserClass.setAllowedSwapper( - dtAddress, - contracts.accounts[0], - user3 - ) + const tx = await DispenserClass.setAllowedSwapper(dtAddress, factoryOwner, user3) assert(tx, 'Cannot set Allowed Swapper') const status = await DispenserClass.status(dtAddress) assert(status.allowedSwapper === user3, 'user3 is Allowed Swapper') }) it('User3 requests datatokens', async () => { - const activate = await DispenserClass.activate( - dtAddress, - '10', - '10', - contracts.accounts[0] - ) + const activate = await DispenserClass.activate(dtAddress, '10', '10', factoryOwner) const check = await DispenserClass.isDispensable(dtAddress, datatoken, user3, '1') assert(check === true, 'isDispensable should return true') const tx = await DispenserClass.dispense(dtAddress, user3, '1', user3) @@ -176,7 +140,7 @@ describe('Dispenser flow', () => { }) it('user2 withdraws all datatokens', async () => { - const tx = await DispenserClass.ownerWithdraw(dtAddress, contracts.accounts[0]) + const tx = await DispenserClass.ownerWithdraw(dtAddress, factoryOwner) assert(tx, 'user2 failed to withdraw all her tokens') const status = await DispenserClass.status(dtAddress) assert(status.balance === '0', 'Balance > 0') diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 9047634e..f53f41f1 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -2,16 +2,10 @@ import { assert, expect } from 'chai' import { AbiItem } from 'web3-utils/types' import { Contract } from 'web3-eth-contract' import BN from 'bn.js' -import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' -import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SSContract from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' -import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { TestContractHandler } from '../../../TestContractHandler' +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 { FreCreationParams, Erc20CreateParams } from '../../../../src/@types' @@ -29,7 +23,7 @@ describe('Fixed Rate unit test', () => { let daiAddress: string let usdcAddress: string let exchangeId: string - let contracts: TestContractHandler + let contracts: Addresses let fixedRate: FixedRateExchange let dtAddress: string let dtAddress2: string @@ -38,50 +32,28 @@ describe('Fixed Rate unit test', () => { 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] + exchangeOwner = accounts[0] + }) + it('should deploy contracts', async () => { - contracts = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - FactoryRouter.abi as AbiItem[], - SSContract.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCollector.abi as AbiItem[], - - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - FactoryRouter.bytecode, - SSContract.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCollector.bytecode - ) - await contracts.getAccounts() - factoryOwner = contracts.accounts[0] - nftOwner = contracts.accounts[1] - user1 = contracts.accounts[2] - user2 = contracts.accounts[3] - user3 = contracts.accounts[4] - user4 = contracts.accounts[5] - exchangeOwner = contracts.accounts[0] - - await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[]) + contracts = await deployContracts(web3, factoryOwner) // initialize fixed rate // - daiContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, - contracts.daiAddress - ) + daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) usdcContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, + MockERC20.abi as AbiItem[], contracts.usdcAddress ) }) @@ -91,7 +63,7 @@ describe('Fixed Rate unit test', () => { // CREATE AN Exchange // we prepare transaction parameters objects - const nftFactory = new NftFactory(contracts.factory721Address, web3) + const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftData: NftCreateData = { name: '72120Bundle', @@ -102,9 +74,9 @@ describe('Fixed Rate unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: ADDRESS_ZERO, cap: '1000000', feeAmount: '0', @@ -158,7 +130,9 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.getExchangeOwner(exchangeId)).to.equal(exchangeOwner) }) it('#getOPFCollector - should get OPF collector', async () => { - expect(await fixedRate.getOPCCollector()).to.equal(contracts.opfCollectorAddress) + expect(await fixedRate.getOPCCollector()).to.equal( + contracts.opfCommunityFeeCollectorAddress + ) }) it('#getRouter - should get Router address', async () => { expect(await fixedRate.getRouter()).to.equal(contracts.routerAddress) @@ -407,7 +381,7 @@ describe('Fixed Rate unit test', () => { // CREATE AN Exchange // we prepare transaction parameters objects - const nftFactory = new NftFactory(contracts.factory721Address, web3) + const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftData: NftCreateData = { name: '72120Bundle', @@ -418,9 +392,9 @@ describe('Fixed Rate unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: ADDRESS_ZERO, cap: '1000000', feeAmount: '0', @@ -474,7 +448,9 @@ describe('Fixed Rate unit test', () => { expect(await fixedRate.getExchangeOwner(exchangeId)).to.equal(exchangeOwner) }) it('#getOPFCollector - should get OPF collector', async () => { - expect(await fixedRate.getOPCCollector()).to.equal(contracts.opfCollectorAddress) + expect(await fixedRate.getOPCCollector()).to.equal( + contracts.opfCommunityFeeCollectorAddress + ) }) it('#getRouter - should get Router address', async () => { expect(await fixedRate.getRouter()).to.equal(contracts.routerAddress) diff --git a/test/unit/pools/ssContracts/SideStaking.test.ts b/test/unit/pools/ssContracts/SideStaking.test.ts index bd8cc81b..ea053213 100644 --- a/test/unit/pools/ssContracts/SideStaking.test.ts +++ b/test/unit/pools/ssContracts/SideStaking.test.ts @@ -1,16 +1,11 @@ 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 ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' import SSContract from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import FactoryRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' -import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { TestContractHandler } from '../../../TestContractHandler' +import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' +import { deployContracts, Addresses } from '../../../TestContractHandler' import { web3 } from '../../../config' import { allowance, @@ -38,7 +33,7 @@ describe('SideStaking unit test', () => { let user3: string let initialBlock: number let sideStakingAddress: string - let contracts: TestContractHandler + let contracts: Addresses let pool: Pool let sideStaking: SideStaking let dtAddress: string @@ -50,37 +45,18 @@ describe('SideStaking unit test', () => { let usdcContract: Contract const vestedBlocks = 2500000 - it('should deploy contracts', async () => { - contracts = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - FactoryRouter.abi as AbiItem[], - SSContract.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCollector.abi as AbiItem[], + before(async () => { + const accounts = await web3.eth.getAccounts() + factoryOwner = accounts[0] + nftOwner = accounts[1] + user1 = accounts[2] + user2 = accounts[3] + user3 = accounts[4] + }) - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - FactoryRouter.bytecode, - SSContract.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCollector.bytecode - ) - await contracts.getAccounts() - factoryOwner = contracts.accounts[0] - nftOwner = contracts.accounts[1] - user1 = contracts.accounts[2] - user2 = contracts.accounts[3] - user3 = contracts.accounts[4] + it('should deploy contracts', async () => { + contracts = await deployContracts(web3, factoryOwner) sideStakingAddress = contracts.sideStakingAddress - await contracts.deployContracts(factoryOwner, FactoryRouter.abi as AbiItem[]) // initialize Pool instance pool = new Pool(web3, PoolTemplate.abi as AbiItem[]) @@ -89,42 +65,39 @@ describe('SideStaking unit test', () => { sideStaking = new SideStaking(web3, SSContract.abi as AbiItem[]) assert(sideStaking != null) - daiContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, - contracts.daiAddress - ) + daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress) usdcContract = new web3.eth.Contract( - contracts.MockERC20.options.jsonInterface, + MockERC20.abi as AbiItem[], contracts.usdcAddress ) await approve( web3, - contracts.accounts[0], + factoryOwner, contracts.daiAddress, - contracts.factory721Address, + contracts.erc721FactoryAddress, '2000' ) await approve( web3, - contracts.accounts[0], + factoryOwner, contracts.usdcAddress, - contracts.factory721Address, + contracts.erc721FactoryAddress, '10000' ) let allowCheck = await allowance( web3, contracts.daiAddress, - contracts.accounts[0], - contracts.factory721Address + factoryOwner, + contracts.erc721FactoryAddress ) assert(parseInt(allowCheck) >= 2000) allowCheck = await allowance( web3, contracts.usdcAddress, - contracts.accounts[0], - contracts.factory721Address + factoryOwner, + contracts.erc721FactoryAddress ) assert(parseInt(allowCheck) >= 10000) @@ -140,7 +113,7 @@ describe('SideStaking unit test', () => { it('#create a pool', async () => { // CREATE A POOL // we prepare transaction parameters objects - const nftFactory = new NftFactory(contracts.factory721Address, web3) + const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftData: NftCreateData = { name: '72120Bundle', @@ -151,9 +124,9 @@ describe('SideStaking unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', feeAmount: '0', @@ -164,9 +137,9 @@ describe('SideStaking unit test', () => { const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.daiAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: 18, @@ -178,7 +151,7 @@ describe('SideStaking unit test', () => { } const txReceipt = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData, ercParams, poolParams @@ -229,7 +202,7 @@ describe('SideStaking unit test', () => { it('#getPublisherAddress - should get publisher address', async () => { expect( await sideStaking.getPublisherAddress(sideStakingAddress, erc20Token) - ).to.equal(contracts.accounts[0]) + ).to.equal(factoryOwner) }) it('#getBaseTokenBalance ', async () => { expect( @@ -265,12 +238,10 @@ describe('SideStaking unit test', () => { }) it('#getVesting ', async () => { - expect( - await erc20Contract.methods.balanceOf(contracts.accounts[0]).call() - ).to.equal('0') + expect(await erc20Contract.methods.balanceOf(factoryOwner).call()).to.equal('0') const tx = await sideStaking.getVesting( - contracts.accounts[0], + factoryOwner, sideStakingAddress, erc20Token ) @@ -287,103 +258,103 @@ describe('SideStaking unit test', () => { ).to.equal((await web3.eth.getBlockNumber()).toString()) }) - // it('#swapExactAmountIn - should swap', async () => { - // await daiContract.methods - // .transfer(user2, web3.utils.toWei('1000')) - // .send({ from: contracts.accounts[0] }) - // await approve(web3, user2, contracts.daiAddress, poolAddress, '10') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.daiAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsInMaxFee = { - // tokenAmountIn: '10', - // minAmountOut: '1', - // swapMarketFee: '0.1' - // } + it('#swapExactAmountIn - should swap', async () => { + await daiContract.methods + .transfer(user2, web3.utils.toWei('1000')) + .send({ from: factoryOwner }) + await approve(web3, user2, contracts.daiAddress, poolAddress, '10') + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.daiAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsInMaxFee = { + tokenAmountIn: '10', + minAmountOut: '1', + swapMarketFee: '0.1' + } - // const tx = await pool.swapExactAmountIn( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( - // tx.events.LOG_SWAP.returnValues.tokenAmountOut - // ) - // }) + const tx = await pool.swapExactAmountIn( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( + tx.events.LOG_SWAP.returnValues.tokenAmountOut + ) + }) - // it('#swapExactAmountOut - should swap', async () => { - // await approve(web3, user2, contracts.daiAddress, poolAddress, '100') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.daiAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsOutMaxFee = { - // maxAmountIn: '100', - // tokenAmountOut: '50', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountOut( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // assert(tx != null) - // }) + it('#swapExactAmountOut - should swap', async () => { + await approve(web3, user2, contracts.daiAddress, poolAddress, '100') + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.daiAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsOutMaxFee = { + maxAmountIn: '100', + tokenAmountOut: '50', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountOut( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + assert(tx != null) + }) - // it('#joinswapExternAmountIn- user2 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( - // '100' - // ) - // const tx = await pool.joinswapExternAmountIn( - // user2, - // poolAddress, - // daiAmountIn, - // minBPTOut - // ) + it('#joinswapExternAmountIn- user2 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( + '100' + ) + const tx = await pool.joinswapExternAmountIn( + user2, + poolAddress, + daiAmountIn, + minBPTOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') - // expect(tx.events.LOG_BPT.event === 'LOG_BPT') - // // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event - // expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( - // tx.events.LOG_JOIN[1].returnValues.bptAmount - // ) - // }) + expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') + expect(tx.events.LOG_BPT.event === 'LOG_BPT') + // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event + expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( + tx.events.LOG_JOIN[1].returnValues.bptAmount + ) + }) - // it('#exitswapPoolAmountIn- user2 exit the pool receiving only DAI', async () => { - // const BPTAmountIn = '0.5' - // const minDAIOut = '0.5' + it('#exitswapPoolAmountIn- user2 exit the pool receiving only DAI', async () => { + const BPTAmountIn = '0.5' + const minDAIOut = '0.5' - // const tx = await pool.exitswapPoolAmountIn( - // user2, - // poolAddress, - // BPTAmountIn, - // minDAIOut - // ) + const tx = await pool.exitswapPoolAmountIn( + user2, + poolAddress, + BPTAmountIn, + minDAIOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.daiAddress) + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.daiAddress) - // // DTs were also unstaked in the same transaction (went to the staking contract) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) - // }) + // DTs were also unstaked in the same transaction (went to the staking contract) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) + }) }) describe('Test a pool with USDC (6 Decimals)', () => { it('#create a pool', async () => { // CREATE A POOL // we prepare transaction parameters objects - const nftFactory = new NftFactory(contracts.factory721Address, web3) + const nftFactory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftData: NftCreateData = { name: '72120Bundle', @@ -394,9 +365,9 @@ describe('SideStaking unit test', () => { const ercParams: Erc20CreateParams = { templateIndex: 1, - minter: contracts.accounts[0], + minter: factoryOwner, paymentCollector: user3, - mpFeeAddress: contracts.accounts[0], + mpFeeAddress: factoryOwner, feeToken: '0x0000000000000000000000000000000000000000', cap: '1000000', feeAmount: '0', @@ -407,9 +378,9 @@ describe('SideStaking unit test', () => { const poolParams: PoolCreationParams = { ssContract: contracts.sideStakingAddress, baseTokenAddress: contracts.usdcAddress, - baseTokenSender: contracts.factory721Address, - publisherAddress: contracts.accounts[0], - marketFeeCollector: contracts.accounts[0], + baseTokenSender: contracts.erc721FactoryAddress, + publisherAddress: factoryOwner, + marketFeeCollector: factoryOwner, poolTemplateAddress: contracts.poolTemplateAddress, rate: '1', baseTokenDecimals: await usdcContract.methods.decimals().call(), @@ -425,7 +396,7 @@ describe('SideStaking unit test', () => { } const txReceipt = await nftFactory.createNftErc20WithPool( - contracts.accounts[0], + factoryOwner, nftData, ercParams, poolParams @@ -474,12 +445,10 @@ describe('SideStaking unit test', () => { }) it('#getVesting ', async () => { - expect( - await erc20Contract.methods.balanceOf(contracts.accounts[0]).call() - ).to.equal('0') + expect(await erc20Contract.methods.balanceOf(factoryOwner).call()).to.equal('0') const tx = await sideStaking.getVesting( - contracts.accounts[0], + factoryOwner, sideStakingAddress, erc20Token ) @@ -496,95 +465,95 @@ describe('SideStaking unit test', () => { ).to.equal((await web3.eth.getBlockNumber()).toString()) }) - // it('#swapExactAmountIn - should swap', async () => { - // const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC - // await usdcContract.methods - // .transfer(user2, transferAmount) - // .send({ from: contracts.accounts[0] }) + it('#swapExactAmountIn - should swap', async () => { + const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC + await usdcContract.methods + .transfer(user2, transferAmount) + .send({ from: factoryOwner }) - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '10') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.usdcAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsInMaxFee = { - // tokenAmountIn: '10', - // minAmountOut: '1', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountIn( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( - // tx.events.LOG_SWAP.returnValues.tokenAmountOut - // ) - // }) + await approve(web3, user2, contracts.usdcAddress, poolAddress, '10') + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.usdcAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsInMaxFee = { + tokenAmountIn: '10', + minAmountOut: '1', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountIn( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal( + tx.events.LOG_SWAP.returnValues.tokenAmountOut + ) + }) - // it('#swapExactAmountOut - should swap', async () => { - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '100') - // const tokenInOutMarket: TokenInOutMarket = { - // tokenIn: contracts.usdcAddress, - // tokenOut: erc20Token, - // marketFeeAddress: contracts.accounts[0] - // } - // const amountsInOutMaxFee: AmountsOutMaxFee = { - // maxAmountIn: '100', - // tokenAmountOut: '50', - // swapMarketFee: '0.1' - // } - // const tx = await pool.swapExactAmountOut( - // user2, - // poolAddress, - // tokenInOutMarket, - // amountsInOutMaxFee - // ) - // assert(tx != null) - // // console.log(tx.events) - // }) + it('#swapExactAmountOut - should swap', async () => { + await approve(web3, user2, contracts.usdcAddress, poolAddress, '100') + const tokenInOutMarket: TokenInOutMarket = { + tokenIn: contracts.usdcAddress, + tokenOut: erc20Token, + marketFeeAddress: factoryOwner + } + const amountsInOutMaxFee: AmountsOutMaxFee = { + maxAmountIn: '100', + tokenAmountOut: '50', + swapMarketFee: '0.1' + } + const tx = await pool.swapExactAmountOut( + user2, + poolAddress, + tokenInOutMarket, + amountsInOutMaxFee + ) + assert(tx != null) + // console.log(tx.events) + }) - // it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => { - // const usdcAmountIn = '100' - // const minBPTOut = '0.1' - // await approve(web3, user2, contracts.usdcAddress, poolAddress, '100', true) + it('#joinswapExternAmountIn- user2 should add liquidity, receiving LP tokens', async () => { + const usdcAmountIn = '100' + const minBPTOut = '0.1' + await approve(web3, user2, contracts.usdcAddress, poolAddress, '100', true) - // const tx = await pool.joinswapExternAmountIn( - // user2, - // poolAddress, - // usdcAmountIn, - // minBPTOut - // ) + const tx = await pool.joinswapExternAmountIn( + user2, + poolAddress, + usdcAmountIn, + minBPTOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') - // expect(tx.events.LOG_BPT.event === 'LOG_BPT') - // // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event - // expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( - // tx.events.LOG_JOIN[1].returnValues.bptAmount - // ) - // }) + expect(tx.events.LOG_JOIN[0].event === 'LOG_JOIN') + expect(tx.events.LOG_BPT.event === 'LOG_BPT') + // 2 JOIN EVENTS BECAUSE SIDE STAKING ALSO STAKED DTs, TODO: we should add to whom has been sent in the LOG_BPT event + expect(tx.events.LOG_JOIN[0].returnValues.bptAmount).to.equal( + tx.events.LOG_JOIN[1].returnValues.bptAmount + ) + }) - // it('#exitswapPoolAmountIn- user2 exit the pool receiving only USDC', async () => { - // const BPTAmountIn = '0.5' - // const minUSDCOut = '0.5' + it('#exitswapPoolAmountIn- user2 exit the pool receiving only USDC', async () => { + const BPTAmountIn = '0.5' + const minUSDCOut = '0.5' - // const tx = await pool.exitswapPoolAmountIn( - // user2, - // poolAddress, - // BPTAmountIn, - // minUSDCOut - // ) + const tx = await pool.exitswapPoolAmountIn( + user2, + poolAddress, + BPTAmountIn, + minUSDCOut + ) - // assert(tx != null) + assert(tx != null) - // expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.usdcAddress) + expect(tx.events.LOG_EXIT[0].returnValues.tokenOut).to.equal(contracts.usdcAddress) - // // DTs were also unstaked in the same transaction (went to the staking contract) - // expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) - // }) + // DTs were also unstaked in the same transaction (went to the staking contract) + expect(tx.events.LOG_EXIT[1].returnValues.tokenOut).to.equal(erc20Token) + }) }) }) diff --git a/test/unit/tokens/Datatoken.test.ts b/test/unit/tokens/Datatoken.test.ts index 090443c1..405340a2 100644 --- a/test/unit/tokens/Datatoken.test.ts +++ b/test/unit/tokens/Datatoken.test.ts @@ -1,15 +1,10 @@ import { assert } from 'chai' import ERC20TemplateEnterprise from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json' -import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import Router from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { TestContractHandler } from '../../TestContractHandler' +import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json' +import { deployContracts, Addresses } from '../../TestContractHandler' import { AbiItem } from 'web3-utils' import { web3 } from '../../config' import { @@ -29,7 +24,7 @@ describe('Datatoken', () => { let user1: string let user2: string let user3: string - let contractHandler: TestContractHandler + let contracts: Addresses let nftDatatoken: Nft let datatoken: Datatoken let nftFactory: NftFactory @@ -41,48 +36,29 @@ describe('Datatoken', () => { const nftName = 'NFTName' const nftSymbol = 'NFTSymbol' - it('should deploy contracts', async () => { - contractHandler = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - Router.abi as AbiItem[], - SideStaking.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCollector.abi as AbiItem[], + before(async () => { + const accounts = await web3.eth.getAccounts() + nftOwner = accounts[0] + user1 = accounts[1] + user2 = accounts[2] + user3 = accounts[3] + }) - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - Router.bytecode, - SideStaking.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCollector.bytecode - ) - await contractHandler.getAccounts() - nftOwner = contractHandler.accounts[0] - user1 = contractHandler.accounts[1] - user2 = contractHandler.accounts[2] - user3 = contractHandler.accounts[3] - await contractHandler.deployContracts(nftOwner, Router.abi as AbiItem[]) + it('should deploy contracts', async () => { + contracts = await deployContracts(web3, nftOwner) const daiContract = new web3.eth.Contract( - contractHandler.MockERC20.options.jsonInterface, - contractHandler.daiAddress + MockERC20.abi as AbiItem[], + contracts.daiAddress ) await daiContract.methods - .approve(contractHandler.factory721Address, web3.utils.toWei('10000')) - .send({ from: contractHandler.accounts[0] }) + .approve(contracts.erc721FactoryAddress, web3.utils.toWei('10000')) + .send({ from: nftOwner }) }) it('should initialize NFTFactory instance and create a new NFT', async () => { nftFactory = new NftFactory( - contractHandler.factory721Address, + contracts.erc721FactoryAddress, web3, ERC721Factory.abi as AbiItem[] ) @@ -163,8 +139,8 @@ describe('Datatoken', () => { it('#createFixedRate - should create FRE for the erc20 dt', async () => { const freParams: FreCreationParams = { - fixedRateAddress: contractHandler.fixedRateAddress, - baseTokenAddress: contractHandler.daiAddress, + fixedRateAddress: contracts.fixedRateAddress, + baseTokenAddress: contracts.daiAddress, owner: nftOwner, marketFeeCollector: nftOwner, baseTokenDecimals: 18, @@ -181,8 +157,8 @@ describe('Datatoken', () => { it('#createFixedRate - should FAIL create FRE if NOT ERC20Deployer', async () => { assert((await nftDatatoken.isErc20Deployer(nftAddress, user3)) === false) const freParams: FreCreationParams = { - fixedRateAddress: contractHandler.fixedRateAddress, - baseTokenAddress: contractHandler.daiAddress, + fixedRateAddress: contracts.fixedRateAddress, + baseTokenAddress: contracts.daiAddress, owner: nftOwner, marketFeeCollector: nftOwner, baseTokenDecimals: 18, @@ -206,7 +182,7 @@ describe('Datatoken', () => { const dispenser = await datatoken.createDispenser( datatokenAddress, nftOwner, - contractHandler.dispenserAddress, + contracts.dispenserAddress, dispenserParams ) assert(dispenser !== null) @@ -222,7 +198,7 @@ describe('Datatoken', () => { await datatoken.createDispenser( datatokenAddress, user2, - contractHandler.dispenserAddress, + contracts.dispenserAddress, dispenserParams ) } catch (e) { @@ -427,7 +403,7 @@ describe('Datatoken', () => { datatokenAddress, nftOwner, order, - contractHandler.dispenserAddress + contracts.dispenserAddress ) assert(buyFromDispenseTx !== null) }) diff --git a/test/unit/tokens/Nft.test.ts b/test/unit/tokens/Nft.test.ts index ee0c4e9a..aa92082d 100644 --- a/test/unit/tokens/Nft.test.ts +++ b/test/unit/tokens/Nft.test.ts @@ -1,14 +1,7 @@ import { assert } from 'chai' -import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' -import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' -import Router from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' -import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' -import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' -import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' -import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { TestContractHandler } from '../../TestContractHandler' +import { deployContracts, Addresses } from '../../TestContractHandler' import { AbiItem } from 'web3-utils' import sha256 from 'crypto-js/sha256' import { web3 } from '../../config' @@ -20,7 +13,7 @@ describe('NFT', () => { let user1: string let user2: string let user3: string - let contractHandler: TestContractHandler + let contracts: Addresses let nftDatatoken: Nft let nftFactory: NftFactory let nftAddress: string @@ -30,40 +23,21 @@ describe('NFT', () => { const publishMarketFeeAdress = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75' const oceanAddress = '0x967da4048cd07ab37855c090aaf366e4ce1b9f48' - it('should deploy contracts', async () => { - contractHandler = new TestContractHandler( - web3, - ERC721Template.abi as AbiItem[], - ERC20Template.abi as AbiItem[], - PoolTemplate.abi as AbiItem[], - ERC721Factory.abi as AbiItem[], - Router.abi as AbiItem[], - SideStaking.abi as AbiItem[], - FixedRate.abi as AbiItem[], - Dispenser.abi as AbiItem[], - OPFCollector.abi as AbiItem[], + before(async () => { + const accounts = await web3.eth.getAccounts() + nftOwner = accounts[0] + user1 = accounts[1] + user2 = accounts[2] + user3 = accounts[3] + }) - ERC721Template.bytecode, - ERC20Template.bytecode, - PoolTemplate.bytecode, - ERC721Factory.bytecode, - Router.bytecode, - SideStaking.bytecode, - FixedRate.bytecode, - Dispenser.bytecode, - OPFCollector.bytecode - ) - await contractHandler.getAccounts() - nftOwner = contractHandler.accounts[0] - user1 = contractHandler.accounts[1] - user2 = contractHandler.accounts[2] - user3 = contractHandler.accounts[3] - await contractHandler.deployContracts(nftOwner, Router.abi as AbiItem[]) + it('should deploy contracts', async () => { + contracts = await deployContracts(web3, nftOwner) }) it('should initialize NFTFactory instance and create a new NFT', async () => { nftFactory = new NftFactory( - contractHandler.factory721Address, + contracts.erc721FactoryAddress, web3, ERC721Factory.abi as AbiItem[] )