1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

added tests for NFTDatatoken class and some tweaks

This commit is contained in:
Bogdan Fazakas 2021-10-25 09:29:45 +03:00
parent 8973bb94eb
commit 5e31a7ebcf
13 changed files with 5896 additions and 2897 deletions

8266
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -46,7 +46,7 @@
}, },
"dependencies": { "dependencies": {
"@ethereum-navigator/navigator": "^0.5.3", "@ethereum-navigator/navigator": "^0.5.3",
"@oceanprotocol/contracts": "https://github.com/oceanprotocol/contracts#v4main", "@oceanprotocol/contracts": "github:oceanprotocol/contracts#v4main",
"cross-fetch": "^3.1.4", "cross-fetch": "^3.1.4",
"crypto-js": "^4.0.0", "crypto-js": "^4.0.0",
"decimal.js": "^10.2.1", "decimal.js": "^10.2.1",

View File

@ -1,108 +0,0 @@
{
"rinkeby": {
"DTFactory": "0x3fd7A00106038Fb5c802c6d63fa7147Fe429E83a",
"BFactory": "0x53eDF9289B0898e1652Ce009AACf8D25fA9A42F8",
"FixedRateExchange": "0xeD1DfC5F3a589CfC4E8B91C1fbfC18FC6699Fbde",
"Metadata": "0xFD8a7b6297153397B7eb4356C47dbd381d58bFF4",
"Ocean": "0x8967BCF84170c91B0d24D4302C2376283b0B3a07",
"Dispenser": "0x623744Cd25Ed553d3b4722667697F926cf99658B",
"chainId": 4,
"startBlock": 7294090
},
"ropsten": {
"DTFactory": "0x6ebcCa6df2CAba986FCF44E64Ee82251c1455Dcc",
"BFactory": "0x75be6e18c80A487C8b49663bf14f80A6495045B2",
"FixedRateExchange": "0xA7a711A09396DF82D9be46A26B48BafdB9BB4fA6",
"Metadata": "0x3cd7Ef1F207E1a46AAd7D5d7F5f0A5cF081Fc726",
"Ocean": "0x5e8DCB2AfA23844bcc311B00Ad1A0C30025aADE9",
"Dispenser": "0xc37F8341Ac6e4a94538302bCd4d49Cf0852D30C0",
"chainId": 3,
"startBlock": 9227563
},
"development": {
"Ocean": "0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB",
"OPFCommunityFeeCollector": "0x9E545E3C0baAB3E08CdfD552C960A1050f373042",
"chainId": 31337,
"startBlock": 0,
"v4": {
"Metadata": "0x851356ae760d987E095750cCeb3bC6014560891C",
"ERC721Factory": "0x95401dc811bb5740090279Ba06cfA8fcF6113778",
"ERC20Template": "0xa82fF9aFd8f496c3d6ac40E2a0F282E47488CFc9",
"ERC721Template": "0xf5059a5D33d5853360D16C683c16e67980206f36",
"Router": "0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690",
"FixedPrice": "0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB",
"Staking": "0x9E545E3C0baAB3E08CdfD552C960A1050f373042",
"poolTemplate": "0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E"
},
"v3": {}
},
"mainnet": {
"DTFactory": "0x57317f97E9EA49eBd19f7c9bB7c180b8cDcbDeB9",
"BFactory": "0xbe0083053744ECb871510C88dC0f6b77Da162706",
"FixedRateExchange": "0x608d05214E42722B94a54cF6114d4840FCfF84e1",
"Metadata": "0x1a4b70d8c9DcA47cD6D0Fb3c52BB8634CA1C0Fdf",
"Ocean": "0x967da4048cD07aB37855c090aAF366e4ce1b9F48",
"Dispenser": "0xDEfD0018969cd2d4E648209F876ADe184815f038",
"chainId": 1,
"startBlock": 11005222
},
"polygon": {
"DTFactory": "0xF6410bf5d773C7a41ebFf972f38e7463FA242477",
"BFactory": "0x69B6E54Ad2b3c2801d11d8Ad56ea1d892555b776",
"FixedRateExchange": "0x2112Eb973af1DBf83a4f11eda82f7a7527D7Fde5",
"Metadata": "0x80E63f73cAc60c1662f27D2DFd2EA834acddBaa8",
"Ocean": "0x282d8efCe846A88B159800bd4130ad77443Fa1A1",
"Dispenser": "0x30E4CC2C7A9c6aA2b2Ce93586E3Df24a3A00bcDD",
"chainId": 137,
"startBlock": 11005222
},
"moonbeamalpha": {
"DTFactory": "0x80E63f73cAc60c1662f27D2DFd2EA834acddBaa8",
"BFactory": "0x30E4CC2C7A9c6aA2b2Ce93586E3Df24a3A00bcDD",
"FixedRateExchange": "0xDEfD0018969cd2d4E648209F876ADe184815f038",
"Metadata": "0x9C9eE07b8Ce907D2f9244F8317C1Ed29A3193bAe",
"Ocean": "0x3c21a90599b5B7f37014cA5Bf30d3f1b73d7e391",
"Dispenser": "0x042D709b72B437d7d387F2679bD4ac060e561c9f",
"chainId": 1287,
"startBlock": 90707
},
"undefined": {
"DTFactory": "0xC54b688d81b6B3bab9834D9D7afd8655641EEdA7",
"BFactory": "0x082DD8b8A6eD335364739004280bD4eF41cB6Bd9",
"FixedRateExchange": "0x19dcb303B4b56e7983b7F441696B326264c9008c",
"Metadata": "0x5F71F2ffF1639D927d43e4965C2EC6cCe9505222",
"Dispenser": "0xA4eD47bCAAe977fB1AE39A5DF905AC7420a5e095",
"chainId": 0,
"startBlock": 0
},
"gaiaxtestnet": {
"DTFactory": "0x2720d405ef7cDC8a2E2e5AeBC8883C99611d893C",
"BFactory": "0xc37F8341Ac6e4a94538302bCd4d49Cf0852D30C0",
"FixedRateExchange": "0x3c21a90599b5B7f37014cA5Bf30d3f1b73d7e391",
"Metadata": "0xCfDdA22C9837aE76E0faA845354f33C62E03653a",
"Dispenser": "0x4E6058dC00e90C0DCA47A5d0D3346F409939A5ab",
"Ocean": "0x80E63f73cAc60c1662f27D2DFd2EA834acddBaa8",
"chainId": 2021000,
"startBlock": 177644
},
"mumbai": {
"DTFactory": "0x4E6058dC00e90C0DCA47A5d0D3346F409939A5ab",
"BFactory": "0x159924ca0F47D6F704B97E29099b89e518A17B5E",
"FixedRateExchange": "0xc313e19146Fc9a04470689C9d41a4D3054693531",
"Metadata": "0x98679D582AB3398C03D3308dEB9c7AeC50B52ded",
"Dispenser": "0x1d535147a97bd87c8443125376E6671B60556E07",
"Ocean": "0xd8992Ed72C445c35Cb4A2be468568Ed1079357c8",
"chainId": 80001,
"startBlock": 14791845
},
"bsc": {
"DTFactory": "0xE9397625Df9B63f0C152f975234b7988b54710B8",
"BFactory": "0xc313e19146Fc9a04470689C9d41a4D3054693531",
"FixedRateExchange": "0x98679D582AB3398C03D3308dEB9c7AeC50B52ded",
"Metadata": "0x1d535147a97bd87c8443125376E6671B60556E07",
"Dispenser": "0xFe7967A5176fDAFa8DE109b3507016B885a82D6e",
"Ocean": "0xdce07662ca8ebc241316a15b611c89711414dd1a",
"chainId": 56,
"startBlock": 8114772
}
}

View File

@ -3,7 +3,7 @@ import { AbiItem } from 'web3-utils'
import { TransactionReceipt } from 'web3-eth' import { TransactionReceipt } from 'web3-eth'
import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { Logger, getFairGasPrice } from '../utils' import { LoggerInstance, getFairGasPrice } from '../utils'
/** /**
* ERC20 ROLES * ERC20 ROLES
@ -19,7 +19,6 @@ export class Datatoken {
public factoryABI: AbiItem | AbiItem[] public factoryABI: AbiItem | AbiItem[]
public datatokensABI: AbiItem | AbiItem[] public datatokensABI: AbiItem | AbiItem[]
public web3: Web3 public web3: Web3
private logger: Logger
public startBlock: number public startBlock: number
/** /**
@ -27,14 +26,8 @@ export class Datatoken {
* @param {AbiItem | AbiItem[]} datatokensABI * @param {AbiItem | AbiItem[]} datatokensABI
* @param {Web3} web3 * @param {Web3} web3
*/ */
constructor( constructor(web3: Web3, datatokensABI?: AbiItem | AbiItem[], startBlock?: number) {
web3: Web3,
logger: Logger,
datatokensABI?: AbiItem | AbiItem[],
startBlock?: number
) {
this.web3 = web3 this.web3 = web3
this.logger = logger
this.datatokensABI = datatokensABI || (defaultDatatokensABI.abi as AbiItem[]) this.datatokensABI = datatokensABI || (defaultDatatokensABI.abi as AbiItem[])
this.startBlock = startBlock || 0 this.startBlock = startBlock || 0
} }
@ -93,7 +86,7 @@ export class Datatoken {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress) const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
if ((await this.getDTPermissions(dtAddress, address)).minter != true) { if ((await this.getDTPermissions(dtAddress, address)).minter !== true) {
throw new Error(`Caller is not Minter`) throw new Error(`Caller is not Minter`)
} }
@ -333,7 +326,7 @@ export class Datatoken {
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to transfer tokens: ${e.message}`) LoggerInstance.error(`ERROR: Failed to transfer tokens: ${e.message}`)
throw new Error(`Failed Failed to transfer tokens: ${e.message}`) throw new Error(`Failed Failed to transfer tokens: ${e.message}`)
} }
} }
@ -398,7 +391,7 @@ export class Datatoken {
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to start order : ${e.message}`) LoggerInstance.error(`ERROR: Failed to start order : ${e.message}`)
throw new Error(`Failed to start order: ${e.message}`) throw new Error(`Failed to start order: ${e.message}`)
} }
} }
@ -472,6 +465,7 @@ export class Datatoken {
return trxReceipt return trxReceipt
} }
/** Returns ERC20 user's permissions for a datatoken /** Returns ERC20 user's permissions for a datatoken
* @param {String} dtAddress Datatoken adress * @param {String} dtAddress Datatoken adress
* @param {String} address user adress * @param {String} address user adress

View File

@ -2,7 +2,7 @@ import Web3 from 'web3'
import { AbiItem } from 'web3-utils' import { AbiItem } from 'web3-utils'
import { TransactionReceipt } from 'web3-eth' import { TransactionReceipt } from 'web3-eth'
import defaultNFTDatatokenABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' import defaultNFTDatatokenABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
import { Logger, getFairGasPrice, generateDtName } from '../utils' import { LoggerInstance, getFairGasPrice, generateDtName, Logger } from '../utils'
/** /**
* ERC721 ROLES * ERC721 ROLES
@ -20,29 +20,26 @@ export class NFTDatatoken {
public factory721ABI: AbiItem | AbiItem[] public factory721ABI: AbiItem | AbiItem[]
public nftDatatokenABI: AbiItem | AbiItem[] public nftDatatokenABI: AbiItem | AbiItem[]
public web3: Web3 public web3: Web3
private logger: Logger
public startBlock: number public startBlock: number
constructor( constructor(web3: Web3, nftDatatokenABI?: AbiItem | AbiItem[], startBlock?: number) {
web3: Web3,
logger: Logger,
nftDatatokenABI?: AbiItem | AbiItem[],
startBlock?: number
) {
this.nftDatatokenABI = nftDatatokenABI || (defaultNFTDatatokenABI.abi as AbiItem[]) this.nftDatatokenABI = nftDatatokenABI || (defaultNFTDatatokenABI.abi as AbiItem[])
this.web3 = web3 this.web3 = web3
this.logger = logger
this.startBlock = startBlock || 0 this.startBlock = startBlock || 0
} }
/** /**
* Create new ERC20 datatoken - only user with ERC20Deployer permission can succeed * Create new ERC20 datatoken - only user with ERC20Deployer permission can succeed
* @param {String} address * @param {String} nftAddress ERC721 addreess
* @param {String} nftAddress * @param {String} address User address
* @param {String} minter User set as initial minter for the ERC20 * @param {String} minter User set as initial minter for the ERC20
* @param {String} feeManager initial feeManager for this DT
* @param {String} mpFeeAddress Consume marketplace fee address
* @param {String} feeToken address of the token marketplace wants to add fee on top
* @param {String} feeAmount amount of feeToken to be transferred to mpFeeAddress on top, will be converted to WEI
* @param {String} cap Maximum cap (Number) - will be converted to wei
* @param {String} name Token name * @param {String} name Token name
* @param {String} symbol Token symbol * @param {String} symbol Token symbol
* @param {String} cap Maximum cap (Number) - will be converted to wei
* @param {Number} templateIndex NFT template index * @param {Number} templateIndex NFT template index
* @return {Promise<string>} ERC20 datatoken address * @return {Promise<string>} ERC20 datatoken address
*/ */
@ -50,6 +47,10 @@ export class NFTDatatoken {
nftAddress: string, nftAddress: string,
address: string, address: string,
minter: string, minter: string,
feeManager: string,
mpFeeAddress: string,
feeToken: string,
feeAmount: string,
cap: string, cap: string,
name?: string, name?: string,
symbol?: string, symbol?: string,
@ -63,33 +64,33 @@ export class NFTDatatoken {
} }
// Create 721contract object // Create 721contract object
const contract721 = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress) const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress)
// Estimate gas for ERC20 token creation // Estimate gas for ERC20 token creation
const gasLimitDefault = this.GASLIMIT_DEFAULT const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await contract721.methods estGas = await nftContract.methods
.createERC20( .createERC20(
templateIndex, templateIndex,
[name, symbol], [name, symbol],
[minter], [minter, feeManager, mpFeeAddress, feeToken],
[this.web3.utils.toWei(cap)], [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)],
null []
) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
estGas = gasLimitDefault estGas = gasLimitDefault
} }
// Invoke createERC20 token function of the contract // Call createERC20 token function of the contract
const trxReceipt = await contract721.methods const trxReceipt = await nftContract.methods
.createERC20( .createERC20(
templateIndex, templateIndex,
[name, symbol], [name, symbol],
[minter], [minter, feeManager, mpFeeAddress, feeToken],
[this.web3.utils.toWei(cap)], [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)],
null []
) )
.send({ .send({
from: address, from: address,
@ -101,7 +102,7 @@ export class NFTDatatoken {
try { try {
tokenAddress = trxReceipt.events.ERC20Created.returnValues[0] tokenAddress = trxReceipt.events.ERC20Created.returnValues[0]
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to create datatoken : ${e.message}`) LoggerInstance.error(`ERROR: Failed to create datatoken : ${e.message}`)
} }
return tokenAddress return tokenAddress
} }
@ -235,7 +236,7 @@ export class NFTDatatoken {
throw new Error(`Caller is not Manager`) throw new Error(`Caller is not Manager`)
} }
//Estimate gas for removeFromCreateERC20List method // Estimate gas for removeFromCreateERC20List method
const gasLimitDefault = this.GASLIMIT_DEFAULT const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
@ -272,11 +273,11 @@ export class NFTDatatoken {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress) const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress)
if ((await this.getNFTPermissions(nftAddress, address)).manager != true) { if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) {
throw new Error(`Caller is not Manager`) throw new Error(`Caller is not Manager`)
} }
//Estimate gas cost for addToMetadataList method // Estimate gas cost for addToMetadataList method
const gasLimitDefault = this.GASLIMIT_DEFAULT const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
@ -311,11 +312,11 @@ export class NFTDatatoken {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress) const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress)
if ((await this.getNFTPermissions(nftAddress, address)).manager != true) { if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) {
throw new Error(`Caller is not Manager`) throw new Error(`Caller is not Manager`)
} }
//Estimate gas cost for removeFromMetadataList method // Estimate gas cost for removeFromMetadataList method
const gasLimitDefault = this.GASLIMIT_DEFAULT const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
@ -352,7 +353,7 @@ export class NFTDatatoken {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress) const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress)
if ((await this.getNFTPermissions(nftAddress, address)).manager != true) { if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) {
throw new Error(`Caller is not Manager`) throw new Error(`Caller is not Manager`)
} }
@ -391,7 +392,7 @@ export class NFTDatatoken {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress) const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress)
if ((await this.getNFTPermissions(nftAddress, address)).manager != true) { if ((await this.getNFTPermissions(nftAddress, address)).manager !== true) {
throw new Error(`Caller is not Manager`) throw new Error(`Caller is not Manager`)
} }
@ -480,7 +481,7 @@ export class NFTDatatoken {
throw new Error(`Caller is not NFT Owner`) throw new Error(`Caller is not NFT Owner`)
} }
let tokenIdentifier = tokenId || 1 const tokenIdentifier = tokenId || 1
// Estimate gas cost for transfer NFT method // Estimate gas cost for transfer NFT method
const gasLimitDefault = this.GASLIMIT_DEFAULT const gasLimitDefault = this.GASLIMIT_DEFAULT

View File

@ -3,7 +3,7 @@ import Web3 from 'web3'
import { TransactionReceipt } from 'web3-core' import { TransactionReceipt } from 'web3-core'
import { AbiItem } from 'web3-utils' import { AbiItem } from 'web3-utils'
import defaultFactory721ABI from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json' import defaultFactory721ABI from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import { Logger, getFairGasPrice, generateDtName } from '../utils' import { LoggerInstance, getFairGasPrice, generateDtName } from '../utils'
interface Template { interface Template {
templateAddress: string templateAddress: string
@ -17,7 +17,6 @@ export class NFTFactory {
public factory721Address: string public factory721Address: string
public factory721ABI: AbiItem | AbiItem[] public factory721ABI: AbiItem | AbiItem[]
public web3: Web3 public web3: Web3
private logger: Logger
public startBlock: number public startBlock: number
public factory721: Contract public factory721: Contract
@ -30,14 +29,12 @@ export class NFTFactory {
constructor( constructor(
factory721Address: string, factory721Address: string,
web3: Web3, web3: Web3,
logger: Logger,
factory721ABI?: AbiItem | AbiItem[], factory721ABI?: AbiItem | AbiItem[],
startBlock?: number startBlock?: number
) { ) {
this.factory721Address = factory721Address this.factory721Address = factory721Address
this.factory721ABI = factory721ABI || (defaultFactory721ABI.abi as AbiItem[]) this.factory721ABI = factory721ABI || (defaultFactory721ABI.abi as AbiItem[])
this.web3 = web3 this.web3 = web3
this.logger = logger
this.startBlock = startBlock || 0 this.startBlock = startBlock || 0
this.factory721 = new this.web3.eth.Contract( this.factory721 = new this.web3.eth.Contract(
this.factory721ABI, this.factory721ABI,
@ -89,7 +86,7 @@ export class NFTFactory {
try { try {
tokenAddress = trxReceipt.events.TokenCreated.returnValues[0] tokenAddress = trxReceipt.events.TokenCreated.returnValues[0]
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to create datatoken : ${e.message}`) LoggerInstance.error(`ERROR: Failed to create datatoken : ${e.message}`)
} }
return tokenAddress return tokenAddress
} }

View File

@ -1 +1,5 @@
export * from './pools/index' export * from './pools'
export * from './datatokens'
export * from './factories'
export * from './models'
export * from './utils'

1
src/models/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './Config'

View File

@ -4,7 +4,7 @@ import { Contract } from 'web3-eth-contract'
import defaultPoolABI from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IPool.sol/IPool.json' import defaultPoolABI from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IPool.sol/IPool.json'
import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IERC20.sol/IERC20.json' import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IERC20.sol/IERC20.json'
import { PoolFactory } from './PoolFactory' import { PoolFactory } from './PoolFactory'
import { Logger } from '../../utils' import { LoggerInstance } from '../../utils'
export class OceanPool extends PoolFactory { export class OceanPool extends PoolFactory {
public oceanAddress: string = null public oceanAddress: string = null
@ -18,20 +18,16 @@ export class OceanPool extends PoolFactory {
constructor( constructor(
web3: Web3, web3: Web3,
logger: Logger,
routerAddress: string = null, routerAddress: string = null,
oceanAddress: string = null, oceanAddress: string = null,
startBlock?: number startBlock?: number
) { ) {
super(web3, logger, routerAddress) super(web3, routerAddress)
this.poolABI = defaultPoolABI.abi as AbiItem[] this.poolABI = defaultPoolABI.abi as AbiItem[]
this.erc20ABI = defaultERC20ABI.abi as AbiItem[] this.erc20ABI = defaultERC20ABI.abi as AbiItem[]
this.vault = new this.web3.eth.Contract(this.vaultABI, this.vaultAddress) this.vault = new this.web3.eth.Contract(this.vaultABI, this.vaultAddress)
// if (oceanAddress) {
// this.oceanAddress = oceanAddress
// }
if (startBlock) this.startBlock = startBlock if (startBlock) this.startBlock = startBlock
else this.startBlock = 0 else this.startBlock = 0
} }

View File

@ -2,7 +2,7 @@ import Web3 from 'web3'
import { AbiItem } from 'web3-utils' import { AbiItem } from 'web3-utils'
import { Contract } from 'web3-eth-contract' import { Contract } from 'web3-eth-contract'
import defaultRouterABI from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IFactoryRouter.sol/IFactoryRouter.json' import defaultRouterABI from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IFactoryRouter.sol/IFactoryRouter.json'
import { Logger } from '../../utils' import { LoggerInstance } from '../../utils'
import { TransactionReceipt } from 'web3-eth' import { TransactionReceipt } from 'web3-eth'
export class PoolFactory { export class PoolFactory {
@ -12,7 +12,6 @@ export class PoolFactory {
public routerAddress: string public routerAddress: string
public logger: Logger
public router: Contract public router: Contract
/** /**
@ -21,16 +20,10 @@ export class PoolFactory {
* @param {AbiItem | AbiItem[]} routerABI * @param {AbiItem | AbiItem[]} routerABI
* @param {Web3} web3 * @param {Web3} web3
*/ */
constructor( constructor(web3: Web3, routerAddress: string, routerABI?: AbiItem | AbiItem[]) {
web3: Web3,
logger: Logger,
routerAddress: string,
routerABI?: AbiItem | AbiItem[]
) {
this.web3 = web3 this.web3 = web3
this.routerAddress = routerAddress this.routerAddress = routerAddress
this.routerABI = routerABI || (defaultRouterABI.abi as AbiItem[]) this.routerABI = routerABI || (defaultRouterABI.abi as AbiItem[])
this.logger = logger
this.router = new this.web3.eth.Contract(this.routerABI, this.routerAddress) this.router = new this.web3.eth.Contract(this.routerABI, this.routerAddress)
} }
@ -49,8 +42,8 @@ export class PoolFactory {
.deployPool(tokens, weightsInWei, swapFeePercentage, swapMarketFee, owner) .deployPool(tokens, weightsInWei, swapFeePercentage, swapMarketFee, owner)
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
this.logger.log('Error estimate gas deployPool') LoggerInstance.log('Error estimate gas deployPool')
this.logger.log(e) LoggerInstance.log(e)
estGas = gasLimitDefault estGas = gasLimitDefault
} }
return estGas return estGas

View File

@ -2,8 +2,8 @@ import Config from '../models/Config'
import fs from 'fs' import fs from 'fs'
import { homedir } from 'os' import { homedir } from 'os'
// eslint-disable-next-line import/no-named-default // eslint-disable-next-line import/no-named-default
import { default as DefaultContractsAddresses } from '../data/address.json' // temporary untill we can get them from the contract arficats import { default as DefaultContractsAddresses } from '@oceanprotocol/contracts/addresses/address.json'
import Logger from './Logger' import LoggerInstance from './Logger'
export interface ConfigHelperConfig extends Config { export interface ConfigHelperConfig extends Config {
networkId: number networkId: number
@ -231,7 +231,7 @@ export class ConfigHelper {
let config = configHelperNetworks.find((c) => c[filterBy] === network) let config = configHelperNetworks.find((c) => c[filterBy] === network)
if (!config) { if (!config) {
Logger.error(`No config found for given network '${network}'`) LoggerInstance.error(`No config found for given network '${network}'`)
return null return null
} }

View File

@ -1,16 +1,281 @@
// import { assert } from 'chai' import { assert } from 'chai'
// import { LoggerInstance } from '../../../src/utils' import Web3 from 'web3'
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
import { TestContractHandler } from '../TestContractHandler'
import { NFTFactory } from '../../src/factories/NFTFactory'
import { NFTDatatoken } from '../../src/datatokens/NFTDatatoken'
import { AbiItem } from 'web3-utils'
import { LoggerInstance } from '../../src/utils'
// const web3 = new Web3('http://127.0.0.1:8545') const web3 = new Web3('http://127.0.0.1:8545')
// describe('NFTDatatoken', () => { describe('NFTDatatoken', () => {
// let nftOwner: string let nftOwner: string
// let user1: string let user1: string
// let user2: string let user2: string
// let contracts: TestContractHandler let contractHandler: TestContractHandler
// let nftDatatoken: NFTDataToken let nftDatatoken: NFTDatatoken
// let nftFactory: NFTFactory let nftFactory: NFTFactory
// let erc20Factory: DT20Factory let nftAddress: string
// let nftAddress: string
// } const nftName = 'NFTName'
const nftSymbol = 'NFTSymbol'
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
)
await contractHandler.getAccounts()
nftOwner = contractHandler.accounts[0]
user1 = contractHandler.accounts[1]
user2 = contractHandler.accounts[2]
await contractHandler.deployContracts(nftOwner)
})
it('should initialize NFTFactory instance and create a new NFT', async () => {
nftFactory = new NFTFactory(
contractHandler.factory721Address,
web3,
ERC721Factory.abi as AbiItem
)
nftAddress = await nftFactory.createNFT(nftOwner, nftName, nftSymbol, 1)
nftDatatoken = new NFTDatatoken(web3, ERC721Template.abi as AbiItem)
})
it('#createERC20 - should create a new ERC20 DT from NFT contract', async () => {
await nftDatatoken.addERC20Deployer(nftAddress, nftOwner, nftOwner)
const erc20Address = await nftDatatoken.createERC20(
nftAddress,
nftOwner,
nftOwner,
nftOwner,
publishMarketFeeAdress,
oceanAddress,
'0,1',
'10000',
nftName,
nftSymbol,
1
)
assert(erc20Address !== null)
})
// Manager
it('#addManager - should add a new Manager', async () => {
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).manager === false)
await nftDatatoken.addManager(nftAddress, nftOwner, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).manager === true)
})
it('#addManager - should fail to add a new Manager, if NOT NFT Owner', async () => {
try {
await nftDatatoken.addManager(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not NFT Owner')
}
})
it('#removeManager - should remove a Manager', async () => {
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).manager === true)
await nftDatatoken.removeManager(nftAddress, nftOwner, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).manager === false)
})
it('#removeManager - should fail to remove a new Manager, if NOT NFT Owner', async () => {
try {
await nftDatatoken.removeManager(nftAddress, user1, nftOwner)
} catch (e) {
assert(e.message === 'Caller is not NFT Owner')
}
})
// ERC20Deployer
it('#addERC20Deployer -should add ERC20deployer if Manager', async () => {
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).deployERC20 === false
)
await nftDatatoken.addERC20Deployer(nftAddress, nftOwner, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).deployERC20 === true)
})
it('#addERC20Deployer - should fail to add ERC20deployer if NOT Manager', async () => {
try {
await nftDatatoken.addERC20Deployer(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not Manager')
}
})
it('#removeERC20Deployer - remove ERC20deployer if Manager', async () => {
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).deployERC20 === false
)
await nftDatatoken.removeERC20Deployer(nftAddress, nftOwner, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).deployERC20 === true)
})
it('#removeERC20Deployer - should fail and remove ERC20deployer if NOT Manager', async () => {
try {
await nftDatatoken.removeERC20Deployer(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not Manager')
}
})
// MetadataUpdate
it('#addMetadataUpdate - should add to remove Metadata Updater if Manager', async () => {
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).updateMetadata === false
)
await nftDatatoken.addMetadataUpdater(nftAddress, nftOwner, user1)
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).updateMetadata === true
)
})
it('#addMetadataUpdate - should fail to add Metadata Updater if NOT Manager', async () => {
try {
await nftDatatoken.addMetadataUpdater(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not Manager')
}
})
it('#removeMetadataUpdate - remove Metadata Updater if Manager', async () => {
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).updateMetadata === false
)
await nftDatatoken.removeMetadataUpdater(nftAddress, nftOwner, user1)
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).updateMetadata === true
)
})
it('#removeMetadataUpdate - should fail to remove Metadata Updater if NOT Manager', async () => {
try {
await nftDatatoken.removeMetadataUpdater(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not Manager')
}
})
// StoreUpdater
it('#addStoreUpdater - should add to remove Store Updater if Manager', async () => {
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === false)
await nftDatatoken.addStoreUpdater(nftAddress, nftOwner, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === true)
})
it('#addStoreUpdater - should fail to add Store Updater if NOT Manager', async () => {
try {
await nftDatatoken.addStoreUpdater(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not Manager')
}
})
it('#removeStoreUpdater - remove Metadata Updater if Manager', async () => {
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === false)
await nftDatatoken.removeStoreUpdater(nftAddress, nftOwner, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === true)
})
it('#removeStoreUpdater - should fail to remove Metadata Updater if NOT Manager', async () => {
try {
await nftDatatoken.removeStoreUpdater(nftAddress, user1, user1)
} catch (e) {
assert(e.message === 'Caller is not Manager')
}
})
// Transfer test
it('#transferNFT - should fail to transfer the NFT and clean all permissions, if NOT NFT Owner', async () => {
assert((await nftDatatoken.getNFTOwner(nftAddress)) !== user1)
try {
await nftDatatoken.transferNFT(nftAddress, user1, user1, 1)
} catch (e) {
assert(e.message === 'Caller is not NFT Owner')
}
})
it('#transferNFT - should transfer the NFT and clean all permissions, set new owner as manager', async () => {
await nftDatatoken.addManager(nftAddress, nftOwner, user2)
await nftDatatoken.addMetadataUpdater(nftAddress, nftOwner, user1)
await nftDatatoken.addStoreUpdater(nftAddress, user2, user1)
await nftDatatoken.addERC20Deployer(nftAddress, user2, user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user2)).manager === true)
assert((await nftDatatoken.getNFTPermissions(nftAddress, nftOwner)).manager === true)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).manager === false)
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).updateMetadata === true
)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === true)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).deployERC20 === true)
assert((await nftDatatoken.getNFTOwner(nftAddress)) === nftOwner)
await nftDatatoken.transferNFT(nftAddress, nftOwner, user1, 1)
assert((await nftDatatoken.getNFTOwner(nftAddress)) === user1)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user2)).manager === false)
assert((await nftDatatoken.getNFTPermissions(nftAddress, nftOwner)).manager === false)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).manager === true)
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).updateMetadata === false
)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === false)
assert(
(await nftDatatoken.getNFTPermissions(nftAddress, user1)).deployERC20 === false
)
})
// Clear permisions
it('#cleanPermissions - should fail to cleanPermissions if NOT NFTOwner', async () => {
try {
await nftDatatoken.cleanPermissions(nftAddress, user1)
} catch (e) {
assert(e.message === 'Caller is not NFT Owner')
}
})
it('#cleanPermissions - should cleanPermissions if NFTOwner', async () => {
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === true)
assert((await nftDatatoken.getNFTPermissions(nftAddress, nftOwner)).manager === true)
await nftDatatoken.cleanPermissions(nftAddress, nftOwner)
assert((await nftDatatoken.getNFTPermissions(nftAddress, user1)).store === false)
assert((await nftDatatoken.getNFTPermissions(nftAddress, nftOwner)).manager === false)
await nftDatatoken.addManager(nftAddress, nftOwner, nftOwner)
assert((await nftDatatoken.getNFTPermissions(nftAddress, nftOwner)).manager === true)
})
})

View File

@ -66,7 +66,7 @@ describe('NFT Factory test', () => {
}) })
it('should initiate NFTFactory instance', async () => { it('should initiate NFTFactory instance', async () => {
nftFactory = new NFTFactory(contracts.factory721Address, web3, LoggerInstance) nftFactory = new NFTFactory(contracts.factory721Address, web3)
}) })
it('#getCurrentNFTCount - should return actual nft count (0)', async () => { it('#getCurrentNFTCount - should return actual nft count (0)', async () => {