mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
fixed price
This commit is contained in:
parent
3650da31c8
commit
10bab63c9c
@ -7,8 +7,7 @@ import { Config, ConfigHelper } from '@oceanprotocol/lib'
|
|||||||
import { AllDdos } from './AllDdos'
|
import { AllDdos } from './AllDdos'
|
||||||
import { ConsumeDdo } from './ConsumeDdo'
|
import { ConsumeDdo } from './ConsumeDdo'
|
||||||
|
|
||||||
import WalletConnectProvider from '@walletconnect/web3-provider'
|
|
||||||
import Torus from '@toruslabs/torus-embed'
|
|
||||||
import { NetworkMonitor } from './NetworkMonitor'
|
import { NetworkMonitor } from './NetworkMonitor'
|
||||||
import { MetadataExample } from './MetadataExample'
|
import { MetadataExample } from './MetadataExample'
|
||||||
|
|
||||||
@ -23,22 +22,7 @@ const config = {
|
|||||||
const configRinkeby = new ConfigHelper().getConfig('rinkeby')
|
const configRinkeby = new ConfigHelper().getConfig('rinkeby')
|
||||||
|
|
||||||
const providerOptions = {
|
const providerOptions = {
|
||||||
walletconnect: {
|
|
||||||
package: WalletConnectProvider,
|
|
||||||
options: {
|
|
||||||
infuraId: ''
|
|
||||||
}
|
|
||||||
},
|
|
||||||
torus: {
|
|
||||||
package: Torus,
|
|
||||||
options: {
|
|
||||||
networkParams: {
|
|
||||||
host: config.nodeUri // optional
|
|
||||||
// chainId: 1337, // optional
|
|
||||||
// networkId: 1337 // optional
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const web3ModalOpts = {
|
export const web3ModalOpts = {
|
||||||
|
@ -32,7 +32,16 @@ export function Publish() {
|
|||||||
|
|
||||||
const publishAsset = async () => {
|
const publishAsset = async () => {
|
||||||
console.log(isLoading)
|
console.log(isLoading)
|
||||||
const ddo = await publish(asset as Metadata, '90', 'access', '', '')
|
|
||||||
|
const priceOptions = {
|
||||||
|
price:10,
|
||||||
|
tokensToMint:10,
|
||||||
|
type: 'simple',
|
||||||
|
weightOnDataToken: '',
|
||||||
|
liquidityProviderFee: ''
|
||||||
|
}
|
||||||
|
|
||||||
|
const ddo = await publish(asset as Metadata, priceOptions, 'access', '', '')
|
||||||
console.log(ddo)
|
console.log(ddo)
|
||||||
console.log(isLoading)
|
console.log(isLoading)
|
||||||
const pool = ocean.pool.createDTPool(
|
const pool = ocean.pool.createDTPool(
|
||||||
|
8
src/hooks/usePublish/PriceOptions.ts
Normal file
8
src/hooks/usePublish/PriceOptions.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export interface PriceOptions {
|
||||||
|
price?: number
|
||||||
|
tokensToMint: number
|
||||||
|
type: 'simple' | 'advanced' | string
|
||||||
|
weightOnDataToken: string
|
||||||
|
liquidityProviderFee: string
|
||||||
|
}
|
||||||
|
|
@ -1,5 +0,0 @@
|
|||||||
import { ServiceType } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
|
|
||||||
export interface ServiceConfig {
|
|
||||||
serviceType: ServiceType
|
|
||||||
cost: string
|
|
||||||
}
|
|
@ -1,2 +1,2 @@
|
|||||||
export * from './usePublish'
|
export * from './usePublish'
|
||||||
export * from './ServiceConfig'
|
export * from './PriceOptions'
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { DDO, Metadata, DataTokens, Logger } from '@oceanprotocol/lib'
|
import { DDO, Metadata, Logger } from '@oceanprotocol/lib'
|
||||||
import { useOcean } from '../../providers'
|
import { useOcean } from '../../providers'
|
||||||
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
|
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
|
||||||
import {
|
import {
|
||||||
@ -7,13 +7,13 @@ import {
|
|||||||
ServiceComputePrivacy,
|
ServiceComputePrivacy,
|
||||||
ServiceType
|
ServiceType
|
||||||
} from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
|
} from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
|
||||||
import { ServiceConfig } from './ServiceConfig'
|
import { PriceOptions } from './PriceOptions'
|
||||||
import { publishFeedback } from '../../utils'
|
import { publishFeedback } from '../../utils'
|
||||||
|
|
||||||
interface UsePublish {
|
interface UsePublish {
|
||||||
publish: (
|
publish: (
|
||||||
asset: Metadata,
|
asset: Metadata,
|
||||||
tokensToMint: string,
|
priceOptions: PriceOptions,
|
||||||
serviceConfigs: ServiceType,
|
serviceConfigs: ServiceType,
|
||||||
mpAddress: string,
|
mpAddress: string,
|
||||||
mpFee: string
|
mpFee: string
|
||||||
@ -26,7 +26,7 @@ interface UsePublish {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function usePublish(): UsePublish {
|
function usePublish(): UsePublish {
|
||||||
const { ocean, status, account, accountId, config } = useOcean()
|
const { ocean, status, account, accountId } = useOcean()
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
const [publishStep, setPublishStep] = useState<number | undefined>()
|
const [publishStep, setPublishStep] = useState<number | undefined>()
|
||||||
const [publishStepText, setPublishStepText] = useState<string | undefined>()
|
const [publishStepText, setPublishStepText] = useState<string | undefined>()
|
||||||
@ -40,33 +40,23 @@ function usePublish(): UsePublish {
|
|||||||
/**
|
/**
|
||||||
* Publish an asset.It also creates the datatoken, mints tokens and gives the market allowance
|
* Publish an asset.It also creates the datatoken, mints tokens and gives the market allowance
|
||||||
* @param {Metadata} asset The metadata of the asset.
|
* @param {Metadata} asset The metadata of the asset.
|
||||||
* @param {string} tokensToMint Numer of tokens to mint and give allowance to market
|
* @param {PriceOptions} priceOptions : number of tokens to mint, datatoken weight , liquidity fee, type : simple, advanced
|
||||||
* @param {ServiceConfig[]} serviceConfigs Desired services of the asset, ex: [{serviceType: 'access', cost:'1'}]
|
* @param {ServiceType} serviceType Desired service type of the asset access or compute
|
||||||
* @param {string} mpAddress The address of the market
|
* @param {string} mpAddress The address of the market
|
||||||
* @param {string} mpFee The fee of the market
|
* @param {string} mpFee The fee of the market
|
||||||
* @return {Promise<DDO>} Returns the newly published ddo
|
* @return {Promise<DDO>} Returns the newly published ddo
|
||||||
*/
|
*/
|
||||||
async function publish(
|
async function publish(
|
||||||
asset: Metadata,
|
asset: Metadata,
|
||||||
tokensToMint: string,
|
priceOptions: PriceOptions,
|
||||||
serviceType: ServiceType,
|
serviceType: ServiceType
|
||||||
mpAddress: string,
|
|
||||||
mpFee: string
|
|
||||||
): Promise<DDO> {
|
): Promise<DDO> {
|
||||||
if (status !== ProviderStatus.CONNECTED || !ocean || !account) return
|
if (status !== ProviderStatus.CONNECTED || !ocean || !account) return
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
setPublishError(undefined)
|
setPublishError(undefined)
|
||||||
try {
|
try {
|
||||||
setStep(0)
|
const tokensToMint = priceOptions.tokensToMint.toString()
|
||||||
|
|
||||||
const dtAddress = await ocean.datatokens.create(config.metadataStoreUri,"name",'DT',"1000", accountId)
|
|
||||||
Logger.log('datatoken created', dtAddress)
|
|
||||||
|
|
||||||
setStep(1)
|
|
||||||
await mint(dtAddress, tokensToMint)
|
|
||||||
Logger.log(`minted ${tokensToMint} tokens`)
|
|
||||||
|
|
||||||
setStep(2)
|
|
||||||
const publishedDate =
|
const publishedDate =
|
||||||
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
|
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
|
||||||
const timeout = 0
|
const timeout = 0
|
||||||
@ -134,11 +124,17 @@ function usePublish(): UsePublish {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Logger.log('services created', services)
|
Logger.log('services created', services)
|
||||||
setStep(3)
|
|
||||||
const ddo = await ocean.assets.create(asset, account, services, dtAddress)
|
|
||||||
Logger.log('ddo created', ddo)
|
|
||||||
setStep(4)
|
|
||||||
|
|
||||||
|
const ddo = await ocean.assets
|
||||||
|
.create(asset, account, services)
|
||||||
|
.next(setStep)
|
||||||
|
Logger.log('ddo created', ddo)
|
||||||
|
setStep(7)
|
||||||
|
await mint(ddo.dataToken, tokensToMint)
|
||||||
|
Logger.log(`minted ${tokensToMint} tokens`)
|
||||||
|
|
||||||
|
await createPricing(priceOptions, ddo.dataToken)
|
||||||
|
setStep(8)
|
||||||
return ddo
|
return ddo
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setPublishError(error.message)
|
setPublishError(error.message)
|
||||||
@ -149,6 +145,34 @@ function usePublish(): UsePublish {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createPricing(
|
||||||
|
priceOptions: PriceOptions,
|
||||||
|
dataTokenAddress: string
|
||||||
|
) {
|
||||||
|
switch (priceOptions.type) {
|
||||||
|
case 'advanced': {
|
||||||
|
// weight is hardcoded at 9 (90%) and publisher fee at 0.03(this was a random value set by me)
|
||||||
|
const pool = await ocean.pool.createDTPool(
|
||||||
|
accountId,
|
||||||
|
dataTokenAddress,
|
||||||
|
priceOptions.tokensToMint.toString(),
|
||||||
|
priceOptions.weightOnDataToken,
|
||||||
|
priceOptions.liquidityProviderFee
|
||||||
|
)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'simple': {
|
||||||
|
const fixedPriceExchange = await ocean.fixedRateExchange.create(
|
||||||
|
dataTokenAddress,
|
||||||
|
priceOptions.price.toString(),
|
||||||
|
accountId
|
||||||
|
)
|
||||||
|
await ocean.fixedRateExchange.activate(fixedPriceExchange, accountId)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function mint(tokenAddress: string, tokensToMint: string) {
|
async function mint(tokenAddress: string, tokensToMint: string) {
|
||||||
Logger.log('mint function', tokenAddress, accountId)
|
Logger.log('mint function', tokenAddress, accountId)
|
||||||
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
|
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
|
||||||
|
@ -54,6 +54,48 @@ export async function getBestDataTokenPrice(
|
|||||||
|
|
||||||
return bestPool.price
|
return bestPool.price
|
||||||
}
|
}
|
||||||
|
export async function getCheapestExchange(
|
||||||
|
ocean: Ocean,
|
||||||
|
dataTokenAddress: string
|
||||||
|
) {
|
||||||
|
if (!ocean || !dataTokenAddress) return
|
||||||
|
|
||||||
|
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
||||||
|
dataTokenAddress,
|
||||||
|
'1'
|
||||||
|
)
|
||||||
|
Logger.log('Exchanges found', tokenExchanges)
|
||||||
|
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
||||||
|
return {
|
||||||
|
address: '',
|
||||||
|
price: ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let cheapestExchangeAddress
|
||||||
|
let cheapestExchangePrice = new Decimal(999999999999)
|
||||||
|
|
||||||
|
if (tokenExchanges) {
|
||||||
|
for (let i = 0; i < tokenExchanges.length; i++) {
|
||||||
|
const exchangePrice = tokenExchanges[i].fixedRate
|
||||||
|
|
||||||
|
const decimalExchangePrice = new Decimal(exchangePrice)
|
||||||
|
Logger.log(
|
||||||
|
'Pool price ',
|
||||||
|
tokenExchanges[i],
|
||||||
|
decimalExchangePrice.toString()
|
||||||
|
)
|
||||||
|
if (decimalExchangePrice < cheapestExchangePrice) {
|
||||||
|
cheapestExchangePrice = decimalExchangePrice
|
||||||
|
cheapestExchangeAddress = tokenExchanges[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
address: cheapestExchangeAddress,
|
||||||
|
price: cheapestExchangePrice.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function checkAndBuyDT(
|
export async function checkAndBuyDT(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
@ -65,25 +107,37 @@ export async function checkAndBuyDT(
|
|||||||
account
|
account
|
||||||
)
|
)
|
||||||
Logger.log(`User has ${userOwnedTokens} tokens`)
|
Logger.log(`User has ${userOwnedTokens} tokens`)
|
||||||
let cheapestPool
|
|
||||||
if (userOwnedTokens === '0') {
|
if (userOwnedTokens === '0') {
|
||||||
cheapestPool = await getCheapestPool(
|
const cheapestPool = await getCheapestPool(
|
||||||
ocean,
|
ocean,
|
||||||
account.getId(),
|
account.getId(),
|
||||||
dataTokenAddress
|
dataTokenAddress
|
||||||
)
|
)
|
||||||
|
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
||||||
Decimal.set({ precision: 5 })
|
Decimal.set({ precision: 5 })
|
||||||
const price = new Decimal(cheapestPool.poolPrice).times(1.05).toString()
|
const cheapestPoolPrice = new Decimal(cheapestPool.price)
|
||||||
const maxPrice = new Decimal(cheapestPool.poolPrice).times(2).toString()
|
const cheapestExchangePrice = new Decimal(cheapestExchange.price)
|
||||||
Logger.log('Buying token', cheapestPool, account.getId(), price)
|
|
||||||
const buyResponse = await ocean.pool.buyDT(
|
if (cheapestExchangePrice > cheapestPoolPrice) {
|
||||||
account.getId(),
|
const price = new Decimal(cheapestPool.price).times(1.05).toString()
|
||||||
cheapestPool.poolAddress,
|
const maxPrice = new Decimal(cheapestPool.price).times(2).toString()
|
||||||
'1',
|
Logger.log('Buying token', cheapestPool, account.getId(), price)
|
||||||
price,
|
const buyResponse = await ocean.pool.buyDT(
|
||||||
maxPrice
|
account.getId(),
|
||||||
)
|
cheapestPool.address,
|
||||||
Logger.log('DT buy response', buyResponse)
|
'1',
|
||||||
return buyResponse
|
price,
|
||||||
|
maxPrice
|
||||||
|
)
|
||||||
|
Logger.log('DT buy response', buyResponse)
|
||||||
|
return buyResponse
|
||||||
|
} else {
|
||||||
|
const exchange = await ocean.fixedRateExchange.buyDT(
|
||||||
|
cheapestExchange.address,
|
||||||
|
'1',
|
||||||
|
account.getId()
|
||||||
|
)
|
||||||
|
return exchange
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,12 @@ export const feedback: { [key in number]: string } = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const publishFeedback: { [key in number]: string } = {
|
export const publishFeedback: { [key in number]: string } = {
|
||||||
0: '1/4 Creating datatoken ...',
|
0: '1/6 Creating datatoken ...',
|
||||||
1: '2/4 Minting tokens ...',
|
2: '2/6 Encrypting files ...',
|
||||||
3: '3/4 Publishing asset ...',
|
4: '3/6 Generating proof ...',
|
||||||
4: '4/4 Asset published succesfully'
|
6: '4/6 Storing ddo ...',
|
||||||
|
7: '5/6 Minting tokens ...',
|
||||||
|
8: '6/6 Asset published succesfully'
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from './web3'
|
export * from './web3'
|
||||||
|
Loading…
Reference in New Issue
Block a user