Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add create with dispenser #1170

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"test:dispenser": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/dispenser/Dispenser.test.ts'",
"test:dt": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/Datatoken.test.ts'",
"test:nftDt": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/NFT.test.ts'",
"test:factory": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/NFTFactory.test.ts'",
"test:factory": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/NftFactory.test.ts'",
"test:router": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/Router.test.ts'",
"test:unit": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/**/*.test.ts'",
"test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit",
Expand Down
73 changes: 72 additions & 1 deletion src/factories/NFTFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
getErcCreationParams,
getPoolCreationParams
} from '../utils'
import { FreCreationParams, Erc20CreateParams, PoolCreationParams } from '../interfaces'
import {
FreCreationParams,
Erc20CreateParams,
PoolCreationParams,
DispenserCreationParams
} from '../interfaces'

interface Template {
templateAddress: string
Expand Down Expand Up @@ -750,4 +755,70 @@ export class NftFactory {

return trxReceipt
}

/** Estimate gas cost for createNftErcWithFixedRate method
* @param address Caller address
* @param nftCreateData input data for NFT Creation
* @param ercParams input data for ERC20 Creation
* @param dispenserParams input data for Dispenser Creation
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async estGasCreateNftErcWithDispenser(
address: string,
nftCreateData: NftCreateData,
ercParams: Erc20CreateParams,
dispenserParams: DispenserCreationParams
): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas

const ercCreateData = getErcCreationParams(ercParams)

try {
estGas = await this.factory721.methods
.createNftErcWithDispenser(nftCreateData, ercCreateData, dispenserParams)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
LoggerInstance.error('Failed to estimate gas for createNftErcWithDispenser', e)
}
return estGas
}

/**
* @dev createNftErcWithDispenser
* Creates a new NFT, then a ERC20, then a Dispenser, all in one call
* Use this carefully, because if Dispenser creation fails, you are still going to pay a lot of gas
* @param address Caller address
* @param nftCreateData input data for NFT Creation
* @param ercParams input data for ERC20 Creation
* @param dispenserParams input data for Dispenser Creation
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async createNftErcWithDispenser(
address: string,
nftCreateData: NftCreateData,
ercParams: Erc20CreateParams,
dispenserParams: DispenserCreationParams
): Promise<TransactionReceipt> {
const ercCreateData = getErcCreationParams(ercParams)

const estGas = await this.estGasCreateNftErcWithDispenser(
address,
nftCreateData,
ercParams,
dispenserParams
)

// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.createNftErcWithDispenser(nftCreateData, ercCreateData, dispenserParams)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})

return trxReceipt
}
}
5 changes: 3 additions & 2 deletions src/interfaces/DispenserInterface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface DispenserCreationParams {
maxTokens: string
maxBalance: string
dispenserAddress: string
maxTokens: string // how many tokens cand be dispensed when someone requests . If maxTokens=2 then someone can't request 3 in one tx
maxBalance: string // how many dt the user has in it's wallet before the dispenser will not dispense dt
withMint?: boolean // true if we want to allow the dispenser to be a minter
allowedSwapper?: string // only account that can ask tokens. set address(0) if not required
}
2 changes: 1 addition & 1 deletion src/utils/ConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Config from '../models/Config'
import fs from 'fs'
import { homedir } from 'os'
// eslint-disable-next-line import/no-named-default
import { default as DefaultContractsAddresses } from '@oceanprotocol/contracts/addresses/address.json'
import { default as DefaultContractsAddresses } from '@oceanprotocol/contracts/address.json'
import LoggerInstance from './Logger'

const configHelperNetworksBase: Config = {
Expand Down
1 change: 1 addition & 0 deletions src/utils/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './ContractParams'
export * from './FetchHelper'
export * from './ConfigHelper'
export * from './DdoHelpers'
export * from './Constants'
46 changes: 46 additions & 0 deletions test/unit/NftFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Erc20CreateParams,
PoolCreationParams
} from '../../src/interfaces'
import { ZERO_ADDRESS } from '../../src/utils'

const web3 = new Web3('http://127.0.0.1:8545')

Expand Down Expand Up @@ -294,6 +295,51 @@ describe('Nft Factory test', () => {
dtAddress2 = txReceipt.events.TokenCreated.returnValues.newTokenAddress
})

it('#createNftErcWithDispenser- should create an NFT, a datatoken and create a Dispenser', async () => {
// we prepare transaction parameters objects
const nftData: NftCreateData = {
name: '72120Bundle',
symbol: '72Bundle',
templateIndex: 1,
tokenURI: 'https://oceanprotocol.com/nft/'
}

const ercParams: Erc20CreateParams = {
templateIndex: 1,
minter: contracts.accounts[0],
feeManager: user3,
mpFeeAddress: user2,
feeToken: '0x0000000000000000000000000000000000000000',
cap: '1000000',
feeAmount: '0',
name: 'ERC20B1',
symbol: 'ERC20DT1Symbol'
}

const dispenserParams = {
dispenserAddress: contracts.dispenserAddress,
maxTokens: web3.utils.toWei('1'),
maxBalance: web3.utils.toWei('1'),
withMint: true,
allowedSwapper: ZERO_ADDRESS
}

const txReceipt = await nftFactory.createNftErcWithDispenser(
contracts.accounts[0],
nftData,
ercParams,
dispenserParams
)

// EVENTS HAVE BEEN EMITTED
expect(txReceipt.events.NFTCreated.event === 'NFTCreated')
expect(txReceipt.events.TokenCreated.event === 'TokenCreated')
expect(txReceipt.events.DispenserCreated.event === 'DispenserCreated')

// stored for later use in startMultipleTokenOrder test
dtAddress2 = txReceipt.events.TokenCreated.returnValues.newTokenAddress
})

it('#startMultipleTokenOrder- should succed to start multiple orders', async () => {
const consumer = user2 // could be different user
const dtAmount = web3.utils.toWei('1')
Expand Down