1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

publish draft

This commit is contained in:
mihaisc 2021-12-21 15:54:39 +02:00
parent b86ac219f3
commit 16475b55d1
2 changed files with 103 additions and 90 deletions

View File

@ -2,11 +2,20 @@
"extends": ["eslint:recommended", "prettier"], "extends": ["eslint:recommended", "prettier"],
"parserOptions": { "parserOptions": {
"sourceType": "module", "sourceType": "module",
"ecmaFeatures": { "jsx": true } "ecmaFeatures": {
"jsx": true
}
},
"env": {
"browser": true,
"node": true,
"es2020": true,
"jest": true
}, },
"env": { "browser": true, "node": true, "es2020": true, "jest": true },
"settings": { "settings": {
"react": { "version": "detect" } "react": {
"version": "detect"
}
}, },
"overrides": [ "overrides": [
{ {
@ -30,7 +39,14 @@
"react/jsx-no-bind": "off", "react/jsx-no-bind": "off",
"@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/explicit-function-return-type": "off",
"no-use-before-define": "off", "no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": "error" "@typescript-eslint/no-use-before-define": "error",
"prefer-destructuring": [
"error",
{
"object": true,
"array": false
}
]
} }
} }
] ]

View File

@ -22,23 +22,18 @@ import {
Erc20CreateParams, Erc20CreateParams,
FreCreationParams, FreCreationParams,
PoolCreationParams, PoolCreationParams,
ProviderInstance ProviderInstance,
ZERO_ADDRESS
} from '@oceanprotocol/lib' } from '@oceanprotocol/lib'
import { useSiteMetadata } from '@hooks/useSiteMetadata' import { useSiteMetadata } from '@hooks/useSiteMetadata'
import Web3 from 'web3' import Web3 from 'web3'
import axios from 'axios' import axios from 'axios'
import { useCancelToken } from '@hooks/useCancelToken' import { useCancelToken } from '@hooks/useCancelToken'
import { getOceanConfig } from '@utils/ocean'
// TODO: restore FormikPersist, add back clear form action // TODO: restore FormikPersist, add back clear form action
const formName = 'ocean-publish-form' const formName = 'ocean-publish-form'
async function fetchMethod(url: string): Promise<Response> {
const result = await fetch(url)
if (!result) {
throw result.json()
}
return result
}
export default function PublishPage({ export default function PublishPage({
content content
}: { }: {
@ -59,8 +54,13 @@ export default function PublishPage({
async function handleSubmit(values: FormPublishData) { async function handleSubmit(values: FormPublishData) {
try { try {
const ocean = '0x8967BCF84170c91B0d24D4302C2376283b0B3a07' // --------------------------------------------------
// 1. Create NFT & datatokens & create pricing schema
// --------------------------------------------------
const config = getOceanConfig(chainId)
console.log('config', config)
// image not included here for gas fees reasons. It is also an issue to reaserch how we add the image in the nft
const nftCreateData: NftCreateData = { const nftCreateData: NftCreateData = {
name: values.metadata.nft.name, name: values.metadata.nft.name,
symbol: values.metadata.nft.symbol, symbol: values.metadata.nft.symbol,
@ -69,12 +69,15 @@ export default function PublishPage({
templateIndex: 1 templateIndex: 1
} }
// TODO: cap is hardcoded for now to 1000, this needs to be discussed at some point
// fee is default 0 for now
// TODO: templateIndex is hardcoded for now but this is incorrect, in the future it should be something like 1 for pools, and 2 for fre and free
const ercParams: Erc20CreateParams = { const ercParams: Erc20CreateParams = {
templateIndex: 1, templateIndex: 1,
minter: accountId, minter: accountId,
feeManager: accountId, feeManager: accountId,
mpFeeAddress: accountId, mpFeeAddress: appConfig.marketFeeAddress,
feeToken: ocean, feeToken: config.oceanTokenAddress,
feeAmount: `0`, feeAmount: `0`,
cap: '1000', cap: '1000',
name: values.services[0].dataTokenOptions.name, name: values.services[0].dataTokenOptions.name,
@ -83,29 +86,34 @@ export default function PublishPage({
let erc721Address = '' let erc721Address = ''
let datatokenAddress = '' let datatokenAddress = ''
// TODO: cleaner code for this huge switch !??!?
switch (values.pricing.type) { switch (values.pricing.type) {
case 'dynamic': { case 'dynamic': {
// no vesting in market by default, maybe at a later time , vestingAmount and vestedBlocks are hardcoded
// we use only ocean as basetoken
// TODO: discuss swapFeeLiquidityProvider, swapFeeMarketPlaceRunner
const poolParams: PoolCreationParams = { const poolParams: PoolCreationParams = {
// ssContract: '0xeD8CA02627867f459593DD35bC2B0C465B9E9518', ssContract: config.sideStakingAddress,
ssContract: '0xeD8CA02627867f459593DD35bC2B0C465B9E9518', basetokenAddress: config.oceanTokenAddress,
basetokenAddress: ocean, basetokenSender: config.erc721FactoryAddress,
basetokenSender: '0xa15024b732A8f2146423D14209eFd074e61964F3',
publisherAddress: accountId, publisherAddress: accountId,
marketFeeCollector: accountId, marketFeeCollector: appConfig.marketFeeAddress,
poolTemplateAddress: '0x3B942aCcb370e92A07C3227f9fdAc058F62e68C0', poolTemplateAddress: config.poolTemplateAddress,
rate: '1', rate: values.pricing.price,
basetokenDecimals: 18, basetokenDecimals: 18,
vestingAmount: '0', vestingAmount: '0',
vestedBlocks: 2726000, vestedBlocks: 2726000,
initialBasetokenLiquidity: '20', initialBasetokenLiquidity: values.pricing.amountOcean,
swapFeeLiquidityProvider: 1e15, swapFeeLiquidityProvider: 1e15,
swapFeeMarketPlaceRunner: 1e15 swapFeeMarketRunner: 1e15
} }
const token = new Datatoken(web3) const token = new Datatoken(web3)
// the spender in this case is the erc721Factory because we are delegating
token.approve( token.approve(
ocean, config.oceanTokenAddress,
'0xa15024b732A8f2146423D14209eFd074e61964F3', config.erc721FactoryAddress,
'1000', values.pricing.amountOcean.toString(),
accountId accountId
) )
const result = await nftFactory.createNftErcWithPool( const result = await nftFactory.createNftErcWithPool(
@ -114,21 +122,22 @@ export default function PublishPage({
ercParams, ercParams,
poolParams poolParams
) )
console.log('pool cre', result)
;[erc721Address] = result.events.NFTCreated.returnValues erc721Address = result.events.NFTCreated.returnValues[0]
datatokenAddress = result.events.TokenCreated.returnValues[0]
break break
} }
case 'fixed': { case 'fixed': {
const freParams: FreCreationParams = { const freParams: FreCreationParams = {
fixedRateAddress: `0x235C9bE4D23dCbd16c1Bf89ec839cb7C452FD9e9`, fixedRateAddress: config.fixedRateExchangeAddress,
baseTokenAddress: ocean, baseTokenAddress: config.oceanTokenAddress,
owner: accountId, owner: accountId,
marketFeeCollector: appConfig.marketFeeAddress, marketFeeCollector: appConfig.marketFeeAddress,
baseTokenDecimals: 18, baseTokenDecimals: 18,
dataTokenDecimals: 18, dataTokenDecimals: 18,
fixedRate: '1', fixedRate: values.pricing.price,
marketFee: 1, marketFee: 0.1,
withMint: false withMint: true
} }
const result = await nftFactory.createNftErcWithFixedRate( const result = await nftFactory.createNftErcWithFixedRate(
@ -137,65 +146,45 @@ export default function PublishPage({
ercParams, ercParams,
freParams freParams
) )
console.log('create nft', result)
console.log(
'events',
result.events.NFTCreated.returnValues,
result.events.TokenCreated.returnValues
)
erc721Address = result.events.NFTCreated.returnValues[0] erc721Address = result.events.NFTCreated.returnValues[0]
datatokenAddress = result.events.TokenCreated.returnValues[0] datatokenAddress = result.events.TokenCreated.returnValues[0]
console.log('create address', erc721Address, datatokenAddress)
break break
} }
case 'free': { case 'free': {
// TODO: create dispenser here // maxTokens - how many tokens cand be dispensed when someone requests . If maxTokens=2 then someone can't request 3 in one tx
// maxBalance - how many dt the user has in it's wallet before the dispenser will not dispense dt
// both will be just 1 for the market
const dispenserParams = {
dispenserAddress: config.dispenserAddress,
maxTokens: web3.utils.toWei('1'),
maxBalance: web3.utils.toWei('1'),
withMint: true,
allowedSwapper: ZERO_ADDRESS
}
const result = await nftFactory.createNftErcWithDispenser(
accountId,
nftCreateData,
ercParams,
dispenserParams
)
erc721Address = result.events.NFTCreated.returnValues[0]
datatokenAddress = result.events.TokenCreated.returnValues[0]
// console.log('params ', ercParams)
// const result = await nftFactory.createNftWithErc(
// accountId,
// nftCreateData,
// ercParams
// )
// console.log('result', result.events.NFTCreated.returnValues[0])
// const encrypted = await ProviderInstance.encrypt(
// '0xasdasda',
// accountId,
// values.metadata,
// 'https://providerv4.rinkeby.oceanprotocol.com/',
// async (url: string) => {
// return (await axios.post(url, { cancelToken: newCancelToken() }))
// .data
// }
// )
// console.log('encrypted', encrypted)
// const published =
// const nft = new Nft(web3)
// const est = await nft.estSetTokenURI(
// result.events.NFTCreated.address,
// accountId,
// values.metadata.nft.image
// )
// console.log('est ', est)
// const resss = await nft.setTokenURI(
// result.events.NFTCreated.address,
// accountId,
// values.metadata.nft.image
// )
break break
} }
} }
erc721Address = '0x7f9696ebfa882db0ac9f83fb0d2dca5b2edb4532' // --------------------------------------------------
datatokenAddress = '0xa15024b732A8f2146423D14209eFd074e61964F3' // 2. Construct and publish DDO
// --------------------------------------------------
const ddo = await transformPublishFormToDdo( const ddo = await transformPublishFormToDdo(
values, values,
datatokenAddress, datatokenAddress,
erc721Address erc721Address
) )
console.log('formated ddo', JSON.stringify(ddo))
const encryptedResponse = await ProviderInstance.encrypt( const encryptedResponse = await ProviderInstance.encrypt(
ddo.id, ddo.id,
accountId, accountId,
@ -209,28 +198,36 @@ export default function PublishPage({
} }
) )
const encryptedDddo = encryptedResponse.data const encryptedDddo = encryptedResponse.data
console.log('encrypted ddo', encryptedDddo)
const metadataHash = getHash(JSON.stringify(ddo)) // TODO: this whole setMetadata needs to go in a function ,too many hardcoded/calculated params
// TODO: hash generation : this needs to be moved in a function (probably on ocean.js) after we figure out what is going on in provider, leave it here for now
const metadataHash = getHash(Web3.utils.stringToHex(JSON.stringify(ddo)))
const nft = new Nft(web3) const nft = new Nft(web3)
const flags = Web3.utils.stringToHex('0') // theoretically used by aquarius or provider, not implemented yet, will remain hardcoded
const flags = '0x2'
// const data = web3.utils.stringToHex(encryptedDddo)
// const dataHash = web3.utils.stringToHex(metadataHash)
// console.log('hex data', data)
console.log('hex hash', metadataHash)
console.log('hex flags', flags)
const res = await nft.setMetadata( const res = await nft.setMetadata(
erc721Address, erc721Address,
accountId, accountId,
0, 0,
'https://providerv4.rinkeby.oceanprotocol.com/', 'https://providerv4.rinkeby.oceanprotocol.com/',
'', '',
'0x2', flags,
encryptedDddo, encryptedDddo,
'0x' + metadataHash '0x' + metadataHash
) )
console.log('res meta', res)
// --------------------------------------------------
// 3. Integrity check of DDO before & after publishing
// --------------------------------------------------
// TODO: not sure we want to do this at this step, seems overkill
// if we want to do this we just need to fetch it from aquarius. If we want to fetch from chain and decrypt, we would have more metamask pop-ups (not UX friendly)
// decrypt also validates the checksum
// TODO: remove the commented lines of code until `setSuccess`, didn't remove them yet because maybe i missed something
// -------------------------------------------------- // --------------------------------------------------
// 1. Mint NFT & datatokens & put in pool // 1. Mint NFT & datatokens & put in pool
@ -238,7 +235,7 @@ export default function PublishPage({
// const nftOptions = values.metadata.nft // const nftOptions = values.metadata.nft
// const nftCreateData = generateNftCreateData(nftOptions) // const nftCreateData = generateNftCreateData(nftOptions)
// TODO: figure out syntax of ercParams we most likely need to pass // figure out syntax of ercParams we most likely need to pass
// to createNftWithErc() as we need to pass options for the datatoken. // to createNftWithErc() as we need to pass options for the datatoken.
// const ercParams = {} // const ercParams = {}
// const priceOptions = { // const priceOptions = {
@ -247,7 +244,7 @@ export default function PublishPage({
// } // }
// const txMint = await createNftWithErc(accountId, nftCreateData) // const txMint = await createNftWithErc(accountId, nftCreateData)
// TODO: figure out how to get nftAddress & datatokenAddress from tx log. // figure out how to get nftAddress & datatokenAddress from tx log.
// const { nftAddress, datatokenAddress } = txMint.logs[0].args // const { nftAddress, datatokenAddress } = txMint.logs[0].args
// if (!nftAddress || !datatokenAddress) { throw new Error() } // if (!nftAddress || !datatokenAddress) { throw new Error() }
// //