1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-12-23 01:29:49 +01:00

uptate libs

This commit is contained in:
mihaisc 2020-07-16 12:25:01 +03:00
parent e29bd48716
commit 97476709b9
12 changed files with 134 additions and 128 deletions

View File

@ -5,33 +5,38 @@ import { useState } from 'react'
import { useEffect } from 'react'
import shortid from 'shortid'
export function AllDdos() {
const { accountId, ocean } = useOcean()
const { accountId, ocean } = useOcean()
const [ddos, setDdos] = useState<DDO[] | undefined>()
const [ddos, setDdos] = useState<DDO[] | undefined>()
useEffect(() => {
async function init() {
if (ocean === undefined) return
const assets = await ocean.assets.query({
page: 1,
offset: 10,
query: {},
sort: { created: -1 }
})
useEffect(() => {
async function init() {
if (ocean === undefined) return
const assets = await ocean.assets.query({
page: 1,
offset: 10,
query: {},
sort: { created: -1 }
})
setDdos(assets.results)
}
init()
}, [ocean])
setDdos(assets.results)
}
init()
}, [ocean])
return (
<>
<div>Assets</div> <br/>
<div style={{flexDirection: "column"}}>{ddos?.map((ddo) => {
return <div key={shortid.generate()}><span >{ddo.id} / {ddo.dataToken }</span><br/></div>
})}
return (
<>
<div>Assets</div> <br />
<div style={{ flexDirection: 'column' }}>
{ddos?.map((ddo) => {
return (
<div key={shortid.generate()}>
{ddo.id}
<br />
</div>
</>
)
)
})}
</div>
</>
)
}

View File

@ -12,5 +12,5 @@
.container div {
display: flex;
padding: 10px;
margin-top: 40px;
margin-bottom: 10px;
}

View File

@ -5,32 +5,34 @@ import { useState } from 'react'
import { useEffect } from 'react'
export function ConsumeDdo() {
const { accountId, ocean } = useOcean()
const { consumeStepText, consume, consumeError} = useConsume()
const [did, setDid]=useState<string|undefined>()
useEffect(() => {
async function init() {
const { accountId, ocean } = useOcean()
const { consumeStepText, consume, consumeError } = useConsume()
const [did, setDid] = useState<string | undefined>()
useEffect(() => {
async function init() {}
init()
}, [ocean])
}
init()
}, [ocean])
const consumeDid = async () => {
if (did === undefined) return
const consumeDid = async () => {
if(did === undefined) return
await consume(did,'access')
}
await consume(did, 'access')
}
const handleChange = (e :any) =>{
setDid(e.target.value);
}
return (
<>
<div>Consume</div>
<div>DID <input onChange={handleChange}></input></div>
<div><button onClick={consumeDid}>Consume did</button></div>
<div>{consumeStepText}</div>
<div>{consumeError}</div>
</>
)
const handleChange = (e: any) => {
setDid(e.target.value)
}
return (
<>
<div>Consume</div>
<div>
DID <input onChange={handleChange}></input>
</div>
<div>
<button onClick={consumeDid}>Consume did</button>
</div>
<div>{consumeStepText}</div>
<div>{consumeError}</div>
</>
)
}

View File

@ -32,7 +32,7 @@ export function Publish() {
const marketAddress = '0x4D156A2ef69ffdDC55838176C6712C90f60a2285'
const publishAsset = async () => {
const ddo = await publish(asset as Metadata, 4,marketAddress)
const ddo = await publish(asset as Metadata, '4', marketAddress)
console.log(ddo)
setDdo(ddo)
}

View File

@ -3,13 +3,12 @@ import { useOcean } from '@oceanprotocol/react'
import { useEffect } from 'react'
export function Wallet() {
const {web3, ocean, connect, logout, accountId } = useOcean()
const { web3, ocean, connect, logout, accountId } = useOcean()
const conn = async () => {
const { default: WalletConnectProvider } = await import(
'@walletconnect/web3-provider'
)
const providerOptions = {
/* See Provider Options Section */
walletconnect: {
package: WalletConnectProvider, // required
@ -19,19 +18,18 @@ export function Wallet() {
}
}
// await connect({ cacheProvider: true, providerOptions })
await connect()
// await connect({ cacheProvider: true, providerOptions })
await connect()
}
const init = async () => {
if (ocean === undefined || accountId ===undefined) return
if (ocean === undefined || accountId === undefined) return
const assets = await ocean.assets.ownerAssets(accountId)
console.log(assets)
}
useEffect(() => {
init()
}, [ocean,accountId])
}, [ocean, accountId])
const disc = async () => {
await logout()

View File

@ -9,4 +9,3 @@ ReactDOM.render(
</React.StrictMode>,
document.getElementById('root')
)

26
package-lock.json generated
View File

@ -371,18 +371,18 @@
}
},
"@oceanprotocol/contracts": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.2.2.tgz",
"integrity": "sha512-wu5Ub5F50vCAON0GKyv4anPPLm+oWfHViksiAewVS/xvbbnSCt4gHws2Uc1ct25tiO/2AHAyJkqEiC0ep8SHeQ=="
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.2.3.tgz",
"integrity": "sha512-5Oohzno3tnISMYG1jhtHxKMMkcIypTZAPlIoFjE5vDlAW51tyuxwYxBXHYQ2/FLRVFkmUmy5yASKg9zbnKKwQw=="
},
"@oceanprotocol/lib": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.2.tgz",
"integrity": "sha512-v4Y0u7WnhhA33uIMuGKlWqGdlZLen7xRcwCBMvTlLp13Z3K+lOcg7UTByVxnKy2Tg2//wMYUWfuhSsk4nDA/DA==",
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.4.tgz",
"integrity": "sha512-H+soD+W+fOdKXgFxM8pkhAs9JoJDMWxudvTJxT696Ch+z1CvbcEh4bFNlUpjWhVgwjMTVCigYum3BG+V1mqN1Q==",
"requires": {
"@ethereum-navigator/navigator": "^0.5.0",
"@oceanprotocol/contracts": "^0.2.3",
"bignumber.js": "^9.0.0",
"deprecated-decorator": "^0.1.6",
"fs": "0.0.1-security",
"node-fetch": "^2.6.0",
"save-file": "^2.3.1",
@ -390,6 +390,13 @@
"web3": "^1.2.9",
"web3-eth-contract": "^1.2.9",
"whatwg-url": "^8.0.0"
},
"dependencies": {
"@oceanprotocol/contracts": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.2.3.tgz",
"integrity": "sha512-5Oohzno3tnISMYG1jhtHxKMMkcIypTZAPlIoFjE5vDlAW51tyuxwYxBXHYQ2/FLRVFkmUmy5yASKg9zbnKKwQw=="
}
}
},
"@octokit/auth-token": {
@ -2000,11 +2007,6 @@
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
"integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
},
"deprecated-decorator": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/deprecated-decorator/-/deprecated-decorator-0.1.6.tgz",
"integrity": "sha1-AJZjF7ehL+kvPMgx91g68ym4bDc="
},
"deprecated-obj": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/deprecated-obj/-/deprecated-obj-1.0.1.tgz",

View File

@ -21,8 +21,8 @@
"dist/"
],
"dependencies": {
"@oceanprotocol/contracts": "^0.2.2",
"@oceanprotocol/lib": "^0.1.2",
"@oceanprotocol/contracts": "^0.2.3",
"@oceanprotocol/lib": "^0.1.4",
"@walletconnect/web3-provider": "^1.0.15",
"axios": "^0.19.2",
"react": "^16.9.41",

View File

@ -27,7 +27,6 @@ function useConsume(): UseConsume {
const [consumeStepText, setConsumeStepText] = useState<string | undefined>()
const [consumeError, setConsumeError] = useState<string | undefined>()
function setStep(index: number) {
setConsumeStep(index)
setConsumeStepText(consumeFeedback[index])
@ -47,10 +46,11 @@ function useConsume(): UseConsume {
setStep(2)
const res = JSON.parse(order)
Logger.log('order parsed', res)
Logger.log('ocean.datatokens before transfer', ocean.datatokens)
const tokenTransfer = await ocean.datatokens.transferWei(
res.dataToken,
res.to,
res.numTokens,
String(res.numTokens),
res.from
)
Logger.log('token transfered', tokenTransfer)

View File

@ -7,52 +7,62 @@ import { Service } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
interface UsePublish {
publish: (
asset: Metadata,
tokensToMint: number,
tokensToMint: string,
marketAddress: string,
cost?: number
cost?: string
) => Promise<DDO>
mint: (tokenAddress: string, tokensToMint: number) => void
mint: (tokenAddress: string, tokensToMint: string) => void
}
function usePublish(): UsePublish {
const { web3, ocean, status, account, accountId, config } = useOcean()
function createDataToken() {
return new DataTokens(
ocean.datatokens.factoryAddress,
ocean.datatokens.factoryABI.abi,
ocean.datatokens.datatokensABI.abi,
web3
)
}
async function publish(
asset: Metadata,
tokensToMint: number,
tokensToMint: string,
marketAddress: string,
cost = 1
cost = '1'
): Promise<DDO> {
if (status !== ProviderStatus.CONNECTED) return
const datatoken = new DataTokens(
ocean.datatokens.factoryAddress,
(ocean.datatokens.factoryABI as any).abi,
(ocean.datatokens.datatokensABI as any).abi,
web3
)
const datatoken = createDataToken()
Logger.log('datatoken created', datatoken)
Logger.log('datatokens created', datatoken)
Logger.log('ocean dt', ocean.datatokens)
const data = { t: 1, url: config.metadataStoreUri }
const blob = JSON.stringify(data)
const tokenAddress = await datatoken.create(blob, accountId)
Logger.log('datatoken created', datatoken)
Logger.log('tokensto mint', tokensToMint)
await mint(tokenAddress,tokensToMint,datatoken)
await mint(tokenAddress, tokensToMint, datatoken)
Logger.log('giving allowance to ', marketAddress)
await giveMarketAllowance(tokenAddress,marketAddress, tokensToMint,datatoken)
await giveMarketAllowance(
tokenAddress,
marketAddress,
tokensToMint,
datatoken
)
Logger.log('tokenAddress created', tokenAddress)
const publishedDate = new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const timeout = 0
let services: Service[] = []
const price = datatoken.toWei(cost)
switch (asset.main.type) {
case 'dataset': {
const accessService = await ocean.assets.createAccessServiceAttributes(
account,
cost.toString(),
price,
publishedDate,
timeout
)
@ -77,35 +87,22 @@ function usePublish(): UsePublish {
async function mint(
tokenAddress: string,
tokensToMint: number,
tokensToMint: string,
datatoken?: DataTokens
) {
if (datatoken === undefined)
datatoken = new DataTokens(
ocean.datatokens.factoryAddress,
(ocean.datatokens.factoryABI as any).abi,
(ocean.datatokens.datatokensABI as any).abi,
web3
)
Logger.log('mint function',tokenAddress, accountId)
if (datatoken === undefined) datatoken = createDataToken()
Logger.log('mint function', tokenAddress, accountId)
await datatoken.mint(tokenAddress, accountId, tokensToMint)
}
async function giveMarketAllowance(
tokenAddress: string,
marketAddress: string,
tokens: number,
tokens: string,
datatoken?: DataTokens
) {
if (datatoken === undefined)
datatoken = new DataTokens(
ocean.datatokens.factoryAddress,
(ocean.datatokens.factoryABI as any).abi,
(ocean.datatokens.datatokensABI as any).abi,
web3
)
if (datatoken === undefined) datatoken = createDataToken()
await datatoken.approve(tokenAddress, marketAddress, tokens, accountId)
}
return {

View File

@ -3,7 +3,7 @@ import Web3 from 'web3'
import ProviderStatus from './ProviderStatus'
import { Ocean, Logger, Account, Config } from '@oceanprotocol/lib'
import Web3Modal, { ICoreOptions } from 'web3modal'
import {getDefaultProviders} from './getDefaultProviders'
import { getDefaultProviders } from './getDefaultProviders'
interface OceanProviderValue {
web3: Web3 | undefined
web3Provider: any
@ -50,7 +50,7 @@ function OceanProvider({
async function connect(opts?: Partial<ICoreOptions>) {
Logger.log('Connecting ....')
if(opts===undefined) {
if (opts === undefined) {
opts = await getDefaultProviders()
}
const instance = new Web3Modal(opts)
@ -62,6 +62,10 @@ function OceanProvider({
const web3 = new Web3(provider)
setWeb3(web3)
// const factory = require('@oceanprotocol/contracts/artifacts/development/Factory.json')
// const datatokensTemplate = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json')
// config.factoryABI = factory.abi
// config.datatokensABI = datatokensTemplate.abi
config.web3Provider = web3
const ocean = await Ocean.getInstance(config)

View File

@ -1,17 +1,16 @@
export async function getDefaultProviders(){
const { default: WalletConnectProvider } = await import(
'@walletconnect/web3-provider'
)
const providerOptions = {
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: 'INFURA_ID' // required
}
}
}
export async function getDefaultProviders() {
const { default: WalletConnectProvider } = await import(
'@walletconnect/web3-provider'
)
return { cacheProvider: true, providerOptions }
}
const providerOptions = {
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: 'INFURA_ID' // required
}
}
}
return { cacheProvider: true, providerOptions }
}