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

added interfaces fixedrate and erc tokens and array creation helpers

This commit is contained in:
Bogdan Fazakas 2021-11-09 10:57:57 +02:00
parent 6606c6d7d7
commit 9a8be41549
7 changed files with 64 additions and 199 deletions

View File

@ -6,6 +6,7 @@ import defaultDatatokensEnterpriseABI from '@oceanprotocol/contracts/artifacts/c
import Decimal from 'decimal.js'
import { LoggerInstance, getFairGasPrice } from '../utils'
import { Contract } from 'web3-eth-contract'
import { FreOrderParams, FreCreationParams } from '../interfaces'
/**
* ERC20 ROLES
@ -24,24 +25,6 @@ export interface OrderParams {
consumeFeeAmount: string
}
export interface FreParams {
exchangeContract: string
exchangeId: string
maxBaseTokenAmount: string
}
export interface FixedRateParams {
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
}
export interface DispenserParams {
maxTokens: string
maxBalance: string
@ -187,8 +170,7 @@ export class Datatoken {
public async estGasCreateFixedRate(
dtAddress: string,
address: string,
fixedPriceAddress: string,
fixedRateParams: FixedRateParams,
fixedRateParams: FreCreationParams,
contractInstance?: Contract
): Promise<any> {
const dtContract =
@ -204,7 +186,7 @@ export class Datatoken {
try {
estGas = await dtContract.methods
.createFixedRate(
fixedPriceAddress,
fixedRateParams.fixedRateAddress,
[
fixedRateParams.baseTokenAddress,
address,
@ -238,8 +220,7 @@ export class Datatoken {
public async createFixedRate(
dtAddress: string,
address: string,
fixedPriceAddress: string,
fixedRateParams: FixedRateParams
fixedRateParams: FreCreationParams
): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
@ -253,7 +234,6 @@ export class Datatoken {
const estGas = await this.estGasCreateFixedRate(
dtAddress,
address,
fixedPriceAddress,
fixedRateParams,
dtContract
)
@ -261,7 +241,7 @@ export class Datatoken {
// Call createFixedRate contract method
const trxReceipt = await dtContract.methods
.createFixedRate(
fixedPriceAddress,
fixedRateParams.fixedRateAddress,
[
fixedRateParams.baseTokenAddress,
fixedRateParams.owner,
@ -943,7 +923,7 @@ export class Datatoken {
dtAddress: string,
address: string,
orderParams: OrderParams,
freParams: FreParams,
freParams: FreOrderParams,
contractInstance?: Contract
): Promise<any> {
const dtContract =
@ -974,7 +954,7 @@ export class Datatoken {
dtAddress: string,
address: string,
orderParams: OrderParams,
freParams: FreParams
freParams: FreOrderParams
): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensEnterpriseABI, dtAddress)
try {

View File

@ -1,10 +1,17 @@
import { Contract } from 'web3-eth-contract'
import Web3 from 'web3'
import BigNumber from 'bignumber.js'
import { TransactionReceipt } from 'web3-core'
import { AbiItem } from 'web3-utils'
import defaultFactory721ABI from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import { LoggerInstance, getFairGasPrice, generateDtName } from '../utils'
import {
LoggerInstance,
getFairGasPrice,
generateDtName,
getFreCreationParams,
getErcCreationParams,
getPoolCreationParams
} from '../utils'
import { FreCreationParams, Erc20CreateParams, PoolCreationParams } from '../interfaces'
interface Template {
templateAddress: string
@ -28,47 +35,6 @@ export interface NFTCreateData {
baseURI: string
}
export interface ErcCreateParams {
templateIndex: number
minter: string
feeManager: string
mpFeeAddress: string
feeToken: string
feeAmount: string
cap: string
name?: string
symbol?: string
}
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
}
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
*/
@ -607,13 +573,13 @@ export class NFTFactory {
public async estGasCreateNftWithErc(
address: string,
nftCreateData: NFTCreateData,
ercParams: ErcCreateParams
ercParams: Erc20CreateParams
): Promise<any> {
// Get estimated gas value
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
const ercCreateData = this.getErcCreationParams(ercParams)
const ercCreateData = getErcCreationParams(ercParams, this.web3)
estGas = await this.factory721.methods
.createNftWithErc(nftCreateData, ercCreateData)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
@ -635,9 +601,9 @@ export class NFTFactory {
public async createNftWithErc(
address: string,
nftCreateData: NFTCreateData,
ercParams: ErcCreateParams
ercParams: Erc20CreateParams
): Promise<TransactionReceipt> {
const ercCreateData = this.getErcCreationParams(ercParams)
const ercCreateData = getErcCreationParams(ercParams, this.web3)
const estGas = await this.estGasCreateNftWithErc(
address,
@ -668,14 +634,14 @@ export class NFTFactory {
public async estGasCreateNftErcWithPool(
address: string,
nftCreateData: NFTCreateData,
ercParams: ErcCreateParams,
poolParams: PoolParams
ercParams: Erc20CreateParams,
poolParams: PoolCreationParams
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
const ercCreateData = this.getErcCreationParams(ercParams)
const poolData = this.getPoolCreationParams(poolParams)
const ercCreateData = getErcCreationParams(ercParams, this.web3)
const poolData = getPoolCreationParams(poolParams, this.web3)
estGas = await this.factory721.methods
.createNftErcWithPool(nftCreateData, ercCreateData, poolData)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
@ -698,8 +664,8 @@ export class NFTFactory {
public async createNftErcWithPool(
address: string,
nftCreateData: NFTCreateData,
ercParams: ErcCreateParams,
poolParams: PoolParams
ercParams: Erc20CreateParams,
poolParams: PoolCreationParams
): Promise<TransactionReceipt> {
const estGas = await this.estGasCreateNftErcWithPool(
address,
@ -707,8 +673,8 @@ export class NFTFactory {
ercParams,
poolParams
)
const ercCreateData = this.getErcCreationParams(ercParams)
const poolData = this.getPoolCreationParams(poolParams)
const ercCreateData = getErcCreationParams(ercParams, this.web3)
const poolData = getPoolCreationParams(poolParams, this.web3)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
@ -732,15 +698,15 @@ export class NFTFactory {
public async estGasCreateNftErcWithFixedRate(
address: string,
nftCreateData: NFTCreateData,
ercParams: ErcCreateParams,
freParams: FixedRateParams
ercParams: Erc20CreateParams,
freParams: FreCreationParams
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
const ercCreateData = this.getErcCreationParams(ercParams)
const ercCreateData = getErcCreationParams(ercParams, this.web3)
const fixedData = this.getFreCreationParams(freParams)
const fixedData = getFreCreationParams(freParams, this.web3)
try {
estGas = await this.factory721.methods
@ -765,11 +731,11 @@ export class NFTFactory {
public async createNftErcWithFixedRate(
address: string,
nftCreateData: NFTCreateData,
ercParams: ErcCreateParams,
freParams: FixedRateParams
ercParams: Erc20CreateParams,
freParams: FreCreationParams
): Promise<TransactionReceipt> {
const ercCreateData = this.getErcCreationParams(ercParams)
const fixedData = this.getFreCreationParams(freParams)
const ercCreateData = getErcCreationParams(ercParams, this.web3)
const fixedData = getFreCreationParams(freParams, this.web3)
const estGas = await this.estGasCreateNftErcWithFixedRate(
address,
@ -789,72 +755,4 @@ 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)
],
bytess: []
}
}
getFreCreationParams(freParams: FixedRateParams): any {
if (!freParams.allowedConsumer)
freParams.allowedConsumer = '0x0000000000000000000000000000000000000000'
const withMint = freParams.withMint ? 1 : 0
return {
fixedPriceAddress: freParams.fixedRateAddress,
addresses: [
freParams.baseTokenAddress,
freParams.owner,
freParams.marketFeeCollector,
freParams.allowedConsumer
],
uints: [
freParams.baseTokenDecimals,
freParams.dataTokenDecimals,
freParams.fixedRate,
freParams.marketFee,
withMint
]
}
}
getPoolCreationParams(poolParams: PoolParams): any {
return {
addresses: [
poolParams.ssContract,
poolParams.basetokenAddress,
poolParams.basetokenSender,
poolParams.publisherAddress,
poolParams.marketFeeCollector,
poolParams.poolTemplateAddress
],
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]
}
}
}

View File

@ -2,3 +2,4 @@ export * from './Logger'
export * from './GasUtils'
export * from './Logger'
export * from './DatatokenName'
export * from './ContractParams'

View File

@ -18,12 +18,11 @@ import {
Datatoken,
NFTDatatoken,
OrderParams,
FreParams,
FixedRateParams,
DispenserParams
} from '../../src/datatokens'
import { AbiItem } from 'web3-utils'
import { LoggerInstance } from '../../src/utils'
import { FreCreationParams, FreOrderParams } from '../../src/interfaces'
const web3 = new Web3('http://127.0.0.1:8545')
@ -152,7 +151,8 @@ describe('Datatoken', () => {
})
it('#createFixedRate - should create FRE for the erc20 dt', async () => {
const freParams: FixedRateParams = {
const freParams: FreCreationParams = {
fixedRateAddress: contractHandler.fixedRateAddress,
baseTokenAddress: contractHandler.daiAddress,
owner: nftOwner,
marketFeeCollector: nftOwner,
@ -161,12 +161,7 @@ describe('Datatoken', () => {
fixedRate: web3.utils.toWei('1'),
marketFee: 1e15
}
const fre = await datatoken.createFixedRate(
datatokenAddress,
nftOwner,
contractHandler.fixedRateAddress,
freParams
)
const fre = await datatoken.createFixedRate(datatokenAddress, nftOwner, freParams)
assert(fre !== null)
fixedRateAddress = fre.events.NewFixedRate.address
exchangeId = fre.events.NewFixedRate.returnValues[0]
@ -309,7 +304,7 @@ describe('Datatoken', () => {
consumeFeeToken: '0x0000000000000000000000000000000000000000',
consumeFeeAmount: '0'
}
const fre: FreParams = {
const fre: FreOrderParams = {
exchangeContract: fixedRateAddress,
exchangeId: exchangeId,
maxBaseTokenAmount: '1'

View File

@ -14,13 +14,12 @@ 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, NFTCreateData } from '../../src/factories/NFTFactory'
import {
NFTFactory,
NFTCreateData,
ErcCreateParams,
PoolParams,
FixedRateParams
} from '../../src/factories/NFTFactory'
FreCreationParams,
Erc20CreateParams,
PoolCreationParams
} from '../../src/interfaces'
const web3 = new Web3('http://127.0.0.1:8545')
@ -182,7 +181,7 @@ describe('NFT Factory test', () => {
baseURI: 'https://oceanprotocol.com/nft/'
}
const ercParams: ErcCreateParams = {
const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
@ -218,7 +217,7 @@ describe('NFT Factory test', () => {
baseURI: 'https://oceanprotocol.com/nft/'
}
const ercParams: ErcCreateParams = {
const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: user2,
feeManager: user3,
@ -230,7 +229,7 @@ describe('NFT Factory test', () => {
symbol: 'ERC20DT1Symbol'
}
const poolParams: PoolParams = {
const poolParams: PoolCreationParams = {
ssContract: contracts.sideStakingAddress,
basetokenAddress: contracts.daiAddress,
basetokenSender: contracts.factory721Address,
@ -268,7 +267,7 @@ describe('NFT Factory test', () => {
baseURI: 'https://oceanprotocol.com/nft/'
}
const ercParams: ErcCreateParams = {
const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
@ -280,7 +279,7 @@ describe('NFT Factory test', () => {
symbol: 'ERC20DT1Symbol'
}
const freParams: FixedRateParams = {
const freParams: FreCreationParams = {
fixedRateAddress: contracts.fixedRateAddress,
baseTokenAddress: contracts.daiAddress,
owner: contracts.accounts[0],

View File

@ -13,14 +13,10 @@ import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/M
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 { LoggerInstance } from '../../../src/utils'
import {
NFTFactory,
NFTCreateData,
ErcCreateParams,
PoolParams
} from '../../../src/factories/NFTFactory'
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
import { Router } from '../../../src/pools/Router'
import { BigNumber } from 'bignumber.js'
import { Erc20CreateParams, PoolCreationParams } from '../../../src/interfaces'
const { keccak256 } = require('@ethersproject/keccak256')
const web3 = new Web3('http://127.0.0.1:8545')
const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75'
@ -185,7 +181,7 @@ describe('Router unit test', () => {
baseURI: 'https://oceanprotocol.com/nft/'
}
const ercParams: ErcCreateParams = {
const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
@ -197,7 +193,7 @@ describe('Router unit test', () => {
symbol: 'ERC20DT1Symbol'
}
const poolParams: PoolParams = {
const poolParams: PoolCreationParams = {
ssContract: contracts.sideStakingAddress,
basetokenAddress: contracts.daiAddress,
basetokenSender: contracts.factory721Address,
@ -238,7 +234,7 @@ describe('Router unit test', () => {
baseURI: 'https://oceanprotocol.com/nft2/'
}
const ercParams2: ErcCreateParams = {
const ercParams2: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
@ -250,7 +246,7 @@ describe('Router unit test', () => {
symbol: 'ERC20DT1Symbol2'
}
const poolParams2: PoolParams = {
const poolParams2: PoolCreationParams = {
ssContract: contracts.sideStakingAddress,
basetokenAddress: contracts.daiAddress,
basetokenSender: contracts.factory721Address,

View File

@ -15,13 +15,9 @@ import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/M
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 { LoggerInstance } from '../../../../src/utils'
import {
NFTFactory,
NFTCreateData,
ErcCreateParams,
PoolParams
} from '../../../../src/factories/NFTFactory'
import { NFTFactory, NFTCreateData } from '../../../../src/factories/NFTFactory'
import { Pool } from '../../../../src/pools/balancer/Pool'
import { PoolCreationParams, Erc20CreateParams } from '../../../../src/interfaces'
const { keccak256 } = require('@ethersproject/keccak256')
const web3 = new Web3('http://127.0.0.1:8545')
const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75'
@ -141,7 +137,7 @@ describe('Pool unit test', () => {
baseURI: 'https://oceanprotocol.com/nft/'
}
const ercParams: ErcCreateParams = {
const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
@ -155,7 +151,7 @@ describe('Pool unit test', () => {
const basetokenInitialLiq = await pool.amountToUnits(contracts.daiAddress, '2000')
const poolParams: PoolParams = {
const poolParams: PoolCreationParams = {
ssContract: contracts.sideStakingAddress,
basetokenAddress: contracts.daiAddress,
basetokenSender: contracts.factory721Address,
@ -585,7 +581,7 @@ describe('Pool unit test', () => {
baseURI: 'https://oceanprotocol.com/nft/'
}
const ercParams: ErcCreateParams = {
const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
@ -597,7 +593,7 @@ describe('Pool unit test', () => {
symbol: 'ERC20DT1Symbol'
}
const poolParams: PoolParams = {
const poolParams: PoolCreationParams = {
ssContract: contracts.sideStakingAddress,
basetokenAddress: contracts.usdcAddress,
basetokenSender: contracts.factory721Address,