From 193c3bedd1f258c3290cd4a1a511c795e107d559 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Mon, 8 Nov 2021 14:58:09 +0200 Subject: [PATCH] refactor nftfactory interfaces --- src/factories/NFTFactory.ts | 179 ++++++++++++++++++++++++++++------- test/unit/NFTFactory.test.ts | 134 ++++++++++++++------------ 2 files changed, 214 insertions(+), 99 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index ce236cb7..f9144cf1 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -11,7 +11,7 @@ interface Template { isActive: boolean } -interface TokenOrder { +export interface TokenOrder { tokenAddress: string consumer: string amount: string | number @@ -21,32 +21,54 @@ interface TokenOrder { consumeFeeAmount: number } -interface NFTCreateData { +export interface NFTCreateData { name: string symbol: string templateIndex: number baseURI: string } -interface ErcCreateData { +export interface ErcCreateParams { templateIndex: number - strings: string[] - addresses: string[] - uints: (string | number)[] - bytess: string[] + minter: string + feeManager: string + mpFeeAddress: string + feeToken: string + feeAmount: string + cap: string + name?: string + symbol?: string } -interface PoolData { - addresses: string[] - ssParams: (string | number | BigNumber)[] - swapFees: number[] +export interface PoolParams { + ssContract: string + basetokenAddress: string + basetokenSender: string + publisherAddress: string + marketFeeCollector: string + poolTemplateAddress: string + rate: string + basetokenDecimals: number + vestingAmount: string + vestedBlocks: number + initialBasetokenLiquidity: string + swapFeeLiquidityProvider: number + swapFeeMarketPlaceRunner: number } -interface FixedData { - fixedPriceAddress: string - addresses: string[] - uints: (string | number)[] +export interface FixedRateParams { + fixedRateAddress: string + baseTokenAddress: string + owner: string + marketFeeCollector: string + baseTokenDecimals: number + dataTokenDecimals: number + fixedRate: string + marketFee: number + withMint?: boolean // add FixedPriced contract as minter if withMint == true + allowedConsumer?: string // only account that consume the exhchange } + /** * Provides an interface for NFT Factory contract */ @@ -585,12 +607,13 @@ export class NFTFactory { public async estGasCreateNftWithErc( address: string, nftCreateData: NFTCreateData, - ercCreateData: ErcCreateData + ercParams: ErcCreateParams ): Promise { // Get estimated gas value const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { + const ercCreateData = this.getErcCreationParams(ercParams) estGas = await this.factory721.methods .createNftWithErc(nftCreateData, ercCreateData) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) @@ -612,8 +635,10 @@ export class NFTFactory { public async createNftWithErc( address: string, nftCreateData: NFTCreateData, - ercCreateData: ErcCreateData + ercParams: ErcCreateParams ): Promise { + const ercCreateData = this.getErcCreationParams(ercParams) + const estGas = await this.estGasCreateNftWithErc( address, nftCreateData, @@ -636,19 +661,21 @@ export class NFTFactory { * Estimate gas cost for createNftErcWithPool method * @param address Caller address * @param _NftCreateData input data for NFT Creation - * @param _ErcCreateData input data for ERC20 Creation + * @param ercParams input data for ERC20 Creation * @param _PoolData input data for Pool Creation * @return {Promise} transaction receipt */ public async estGasCreateNftErcWithPool( address: string, nftCreateData: NFTCreateData, - ercCreateData: ErcCreateData, - poolData: PoolData + ercParams: ErcCreateParams, + poolParams: PoolParams ): Promise { const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { + const ercCreateData = this.getErcCreationParams(ercParams) + const poolData = this.getPoolCreationParams(poolParams) estGas = await this.factory721.methods .createNftErcWithPool(nftCreateData, ercCreateData, poolData) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) @@ -671,9 +698,11 @@ export class NFTFactory { public async createNftErcWithPool( address: string, nftCreateData: NFTCreateData, - ercCreateData: ErcCreateData, - poolData: PoolData + ercParams: ErcCreateParams, + poolParams: PoolParams ): Promise { + const ercCreateData = this.getErcCreationParams(ercParams) + const poolData = this.getPoolCreationParams(poolParams) const estGas = await this.estGasCreateNftErcWithPool( address, nftCreateData, @@ -695,20 +724,24 @@ export class NFTFactory { /** Estimate gas cost for createNftErcWithFixedRate method * @param address Caller address - * @param _NftCreateData input data for NFT Creation - * @param _ErcCreateData input data for ERC20 Creation - * @param _FixedData input data for FixedRate Creation + * @param nftCreateData input data for NFT Creation + * @param ercParams input data for ERC20 Creation + * @param freParams input data for FixedRate Creation * @return {Promise} transaction receipt */ - public async estGasCreateNftErcWithFixedRate( address: string, nftCreateData: NFTCreateData, - ercCreateData: ErcCreateData, - fixedData: FixedData + ercParams: ErcCreateParams, + freParams: FixedRateParams ): Promise { const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas + + const ercCreateData = this.getErcCreationParams(ercParams) + + const fixedData = this.getFreCreationParams(freParams) + try { estGas = await this.factory721.methods .createNftErcWithFixedRate(nftCreateData, ercCreateData, fixedData) @@ -724,24 +757,27 @@ export class NFTFactory { * Creates a new NFT, then a ERC20, then a FixedRateExchange, all in one call * Use this carefully, because if Fixed Rate creation fails, you are still going to pay a lot of gas * @param address Caller address - * @param _NftCreateData input data for NFT Creation - * @param _ErcCreateData input data for ERC20 Creation - * @param _FixedData input data for FixedRate Creation + * @param nftCreateData input data for NFT Creation + * @param ercParams input data for ERC20 Creation + * @param freParams input data for FixedRate Creation * @return {Promise} transaction receipt */ - public async createNftErcWithFixedRate( address: string, nftCreateData: NFTCreateData, - ercCreateData: ErcCreateData, - fixedData: FixedData + ercParams: ErcCreateParams, + freParams: FixedRateParams ): Promise { + const ercCreateData = this.getErcCreationParams(ercParams) + const fixedData = this.getFreCreationParams(freParams) + const estGas = await this.estGasCreateNftErcWithFixedRate( address, nftCreateData, - ercCreateData, - fixedData + ercParams, + freParams ) + // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods .createNftErcWithFixedRate(nftCreateData, ercCreateData, fixedData) @@ -753,4 +789,75 @@ export class NFTFactory { return trxReceipt } + + getErcCreationParams(ercParams: ErcCreateParams): any { + let name, symbol + // Generate name & symbol if not present + if (!ercParams.name || !ercParams.symbol) { + ;({ name, symbol } = generateDtName()) + } + + return { + templateIndex: ercParams.templateIndex, + strings: [ercParams.name || name, ercParams.symbol || symbol], + addresses: [ + ercParams.minter, + ercParams.feeManager, + ercParams.mpFeeAddress, + ercParams.feeToken + ], + uints: [ + this.web3.utils.toWei(ercParams.cap), + this.web3.utils.toWei(ercParams.feeAmount) + ], + bytes: [] + } + } + + getFreCreationParams(freParams: FixedRateParams): any { + if (!freParams.allowedConsumer) + freParams.allowedConsumer = '0x0000000000000000000000000000000000000000' + const withMint = freParams.withMint ? 1 : 0 + + return { + fixedPriceAddress: freParams.fixedRateAddress, + address: [ + freParams.baseTokenAddress, + freParams.owner, + freParams.marketFeeCollector, + freParams.allowedConsumer + ], + uints: [ + freParams.baseTokenDecimals, + freParams.dataTokenDecimals, + freParams.fixedRate, + freParams.marketFee, + withMint + ] + } + } + + getPoolCreationParams(poolParams: PoolParams): any { + return { + ssParams: [ + this.web3.utils.toWei(poolParams.rate), + poolParams.basetokenDecimals, + this.web3.utils.toWei(poolParams.vestingAmount), + poolParams.vestedBlocks, + this.web3.utils.toWei(poolParams.initialBasetokenLiquidity) + ], + swapFees: [ + poolParams.swapFeeLiquidityProvider, + poolParams.swapFeeMarketPlaceRunner + ], + addresses: [ + poolParams.ssContract, + poolParams.basetokenAddress, + poolParams.basetokenSender, + poolParams.publisherAddress, + poolParams.marketFeeCollector, + poolParams.poolTemplateAddress + ] + } + } } diff --git a/test/unit/NFTFactory.test.ts b/test/unit/NFTFactory.test.ts index 3b2ee439..b5ad8a0b 100644 --- a/test/unit/NFTFactory.test.ts +++ b/test/unit/NFTFactory.test.ts @@ -14,7 +14,13 @@ import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/M import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import { LoggerInstance } from '../../src/utils' // import { NFTDataToken } from '../../../src/datatokens/NFTDatatoken' -import { NFTFactory } from '../../src/factories/NFTFactory' +import { + NFTFactory, + NFTCreateData, + ErcCreateParams, + PoolParams, + FixedRateParams +} from '../../src/factories/NFTFactory' const web3 = new Web3('http://127.0.0.1:8545') @@ -169,29 +175,29 @@ describe('NFT Factory test', () => { it('#createNftwithErc - should create an NFT and a Datatoken ', async () => { // we prepare transaction parameters objects - const nftData = { + const nftData: NFTCreateData = { name: '72120Bundle', symbol: '72Bundle', templateIndex: 1, baseURI: 'https://oceanprotocol.com/nft/' } - const ercData = { + + const ercParams: ErcCreateParams = { templateIndex: 1, - strings: ['ERC20B1', 'ERC20DT1Symbol'], - addresses: [ - contracts.accounts[0], - user3, - user2, - '0x0000000000000000000000000000000000000000' - ], - uints: [web3.utils.toWei('10000'), 0], - bytess: [] + minter: contracts.accounts[0], + feeManager: user3, + mpFeeAddress: user2, + feeToken: '0x0000000000000000000000000000000000000000', + cap: '10000', + feeAmount: '0', + name: 'ERC20B1', + symbol: 'ERC20DT1Symbol' } const txReceipt = await nftFactory.createNftWithErc( contracts.accounts[0], nftData, - ercData + ercParams ) // EVENTS HAVE BEEN EMITTED @@ -205,47 +211,46 @@ describe('NFT Factory test', () => { it('#createNftErcWithPool- should create an NFT, a Datatoken and a pool DT/DAI', async () => { // we prepare transaction parameters objects - const nftData = { + const nftData: NFTCreateData = { name: '72120Bundle', symbol: '72Bundle', templateIndex: 1, baseURI: 'https://oceanprotocol.com/nft/' } - const ercData = { + + const ercParams: ErcCreateParams = { templateIndex: 1, - strings: ['ERC20B1', 'ERC20DT1Symbol'], - addresses: [user2, user3, user2, '0x0000000000000000000000000000000000000000'], - uints: [web3.utils.toWei('1000000'), 0], - bytess: [] + minter: user2, + feeManager: user3, + mpFeeAddress: user2, + feeToken: '0x0000000000000000000000000000000000000000', + cap: '1000000', + feeAmount: '0', + name: 'ERC20B1', + symbol: 'ERC20DT1Symbol' } - const poolData = { - addresses: [ - contracts.sideStakingAddress, - contracts.daiAddress, - contracts.factory721Address, - contracts.accounts[0], - contracts.accounts[0], - contracts.poolTemplateAddress - ], - ssParams: [ - web3.utils.toWei('1'), // rate - 18, // basetokenDecimals - web3.utils.toWei('10000'), - 2500000, // vested blocks - web3.utils.toWei('2000') // baseToken initial pool liquidity - ], - swapFees: [ - 1e15, // - 1e15 - ] + const poolParams: PoolParams = { + ssContract: contracts.sideStakingAddress, + basetokenAddress: contracts.daiAddress, + basetokenSender: contracts.factory721Address, + publisherAddress: contracts.accounts[0], + marketFeeCollector: contracts.accounts[0], + poolTemplateAddress: contracts.poolTemplateAddress, + rate: '1', + basetokenDecimals: 18, + vestingAmount: '10000', + vestedBlocks: 2500000, + initialBasetokenLiquidity: '2000', + swapFeeLiquidityProvider: 1e15, + swapFeeMarketPlaceRunner: 1e15 } const txReceipt = await nftFactory.createNftErcWithPool( contracts.accounts[0], nftData, - ercData, - poolData + ercParams, + poolParams ) // EVENTS HAVE BEEN EMITTED @@ -256,41 +261,44 @@ describe('NFT Factory test', () => { it('#createNftErcWithFixedRate- should create an NFT, a datatoken and create a Fixed Rate Exchange', async () => { // we prepare transaction parameters objects - const nftData = { + const nftData:NFTCreateData = { name: '72120Bundle', symbol: '72Bundle', templateIndex: 1, baseURI: 'https://oceanprotocol.com/nft/' } - const ercData = { + + const ercParams: ErcCreateParams = { templateIndex: 1, - strings: ['ERC20B1', 'ERC20DT1Symbol'], - addresses: [ - contracts.accounts[0], - user3, - user2, - '0x0000000000000000000000000000000000000000' - ], - uints: [web3.utils.toWei('1000000'), 0], - bytess: [] + minter: contracts.accounts[0],, + feeManager: user3, + mpFeeAddress: user2, + feeToken: '0x0000000000000000000000000000000000000000', + cap: '1000000', + feeAmount: '0', + name: 'ERC20B1', + symbol: 'ERC20DT1Symbol' } - const fixedData = { - fixedPriceAddress: contracts.fixedRateAddress, - addresses: [ - contracts.daiAddress, - contracts.accounts[0], - contracts.accounts[0], - contracts.accounts[0] - ], - uints: [18, 18, web3.utils.toWei('1'), 1e15, 0] + + const freParams: FixedRateParams = { + fixedRateAddress:contracts.fixedRateAddress, + baseTokenAddress: contracts.daiAddress, + owner: contracts.accounts[0], + marketFeeCollector: contracts.accounts[0], + baseTokenDecimals: 18, + dataTokenDecimals: 18, + fixedRate: '1', + marketFee: 1e15, + allowedConsumer: contracts.accounts[0], + withMint: false } const txReceipt = await nftFactory.createNftErcWithFixedRate( contracts.accounts[0], nftData, - ercData, - fixedData + ercParams, + freParams ) // EVENTS HAVE BEEN EMITTED