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

Updating CodeExamples.md

This commit is contained in:
GitHub Actions Bot 2022-08-12 06:26:46 +00:00
parent 164e217a5f
commit b26f89e278

View File

@ -28,9 +28,7 @@ Here are the steps:
8. [Publish Data NFT and a Datatoken with a dispenser](#-publish-data-nft-and-a-datatoken-with-a-dispenser)
## 0. Prerequisites
Before we start it is important that you have all of the necessary prerequisites installed on your computer.
- **A Unix based operating system (Linux or Mac)**. If you are a Windows user you can try to run linux inside a virtual machine but this is outside of the scope of this article.
- **Git**. Instructions for installing Git can be found here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
- **Node.js** can be downloaded from here: https://nodejs.org/en/download/
@ -145,7 +143,6 @@ Now we define the variables which we will need later
```
We also define some constants that we will use:
```Typescript
const POOL_NFT_NAME = 'Datatoken 1'
const POOL_NFT_SYMBOL = 'DT1'
@ -155,8 +152,7 @@ We also define some constants that we will use:
const DISP_NFT_SYMBOL = 'DT3'
```
We will need a file to publish, so here we define the file that we intend to publish.
We will need a file to publish, so here we define the file that we intend to publish.
```Typescript
const ASSET_URL: Files = {
datatokenAddress: '0x0',
@ -172,7 +168,6 @@ We will need a file to publish, so here we define the file that we intend to pub
```
Next, we define the metadata that will describe our data asset. This is what we call the DDO
```Typescript
const DDO = {
'@context': ['https://w3id.org/did/v1'],
@ -203,63 +198,53 @@ Next, we define the metadata that will describe our data asset. This is what we
```
We load the configuration:
```Typescript
config = await getTestConfig(web3)
aquarius = new Aquarius(config.metadataCacheUri)
providerUrl = config.providerUri
```
As we go along it's a good idea to console log the values so that you check they are right
```Typescript
console.log(`Aquarius URL: ${config.metadataCacheUri}`)
console.log(`Provider URL: ${providerUrl}`)
```
## 5. Initialize accounts and deploy contracts
### 5.1 Initialize accounts
### 5.1 Initialize accounts
```Typescript
const accounts = await web3.eth.getAccounts()
publisherAccount = accounts[0]
consumerAccount = accounts[1]
stakerAccount = accounts[2]
```
Again, lets console log the values so that we can check that they have been saved properly
```Typescript
console.log(`Publisher account address: ${publisherAccount}`)
console.log(`Consumer account address: ${consumerAccount}`)
console.log(`Staker account address: ${stakerAccount}`)
```
### 5.2 Next, lets get the address of the deployed contracts
### 5.2 Next, lets get the address of the deployed contracts
```Typescript
addresses = getAddresses()
```
### 5.3 We send some OCEAN to consumer and staker accounts
### 5.3 We send some OCEAN to consumer and staker accounts
```Typescript
transfer(web3, publisherAccount, addresses.Ocean, consumerAccount, '100')
transfer(web3, publisherAccount, addresses.Ocean, stakerAccount, '100')
```
## 6. Publish Data NFT and a Datatoken with a liquidity pool
For pool creation, the OCEAN token is used as the base token. The base token can be changed into something else, such as USDC, DAI etc., but it will require an extra fee.
### 6.1 Publish a dataset (create NFT + Datatoken) with a liquidity pool
### 6.1 Publish a dataset (create NFT + Datatoken) with a liquidity pool
```Typescript
const factory = new NftFactory(addresses.ERC721Factory, web3)
@ -298,9 +283,7 @@ For pool creation, the OCEAN token is used as the base token. The base token can
swapFeeMarketRunner: '0.001'
}
```
Before we call the contract we have to call `approve` so that the contract can move our tokens. This is standard when using any ERC20 Datatokens
```Typescript
await approve(
web3,
@ -311,9 +294,7 @@ Before we call the contract we have to call `approve` so that the contract can m
)
```
Now we can make the contract call
```Typescript
const tx = await factory.createNftWithDatatokenWithPool(
publisherAccount,
@ -326,24 +307,19 @@ Now we can make the contract call
poolDatatokenAddress = tx.events.TokenCreated.returnValues[0]
poolAddress = tx.events.NewPool.returnValues[0]
```
Now, we did quite a few things there. Let's check that we successfully published a dataset (create NFT + Datatoken) with a liquidity pool
```Typescript
console.log(`Pool NFT address: ${poolNftAddress}`)
console.log(`Pool Datatoken address: ${poolDatatokenAddress}`)
console.log(`Pool address: ${poolAddress}`)
```
### 6.2 Set metadata in the pool NFT
### 6.2 Set metadata in the pool NFT
```Typescript
const nft = new Nft(web3)
```
Now we update the ddo and set the right did
```Typescript
DDO.chainId = await web3.eth.getChainId()
DDO.id =
@ -351,9 +327,7 @@ Now we update the ddo and set the right did
SHA256(web3.utils.toChecksumAddress(poolNftAddress) + DDO.chainId.toString(10))
DDO.nftAddress = poolNftAddress
```
Next we encrypt the file or files using Ocean Provider. The provider is an off chain proxy built specifically for this task
```Typescript
ASSET_URL.datatokenAddress = poolDatatokenAddress
ASSET_URL.nftAddress = poolNftAddress
@ -361,9 +335,7 @@ Next we encrypt the file or files using Ocean Provider. The provider is an off c
DDO.services[0].files = await encryptedFiles
DDO.services[0].datatokenAddress = poolDatatokenAddress
```
Now let's console log the result to check everything is working
```Typescript
console.log(`DID: ${DDO.id}`)
@ -380,32 +352,26 @@ Now let's console log the result to check everything is working
encryptedDDO,
'0x' + metadataHash
)
```
### 6.3 User should add liquidity to the pool, receiving LP tokens
### 6.3 User should add liquidity to the pool, receiving LP tokens
```Typescript
const pool = new Pool(web3)
```
Before we call the contract we have to call `approve` so that the contract can move our tokens. This is standard when using any ERC20 Datatokens
```Typescript
await approve(web3, stakerAccount, addresses.Ocean, poolAddress, '5', true)
```
Now we can make the contract call
```Typescript
await pool.joinswapExternAmountIn(stakerAccount, poolAddress, '5', '0.1')
```
### 6.4 Marketplace displays pool asset for sale
### 6.4 Marketplace displays pool asset for sale
```Typescript
const pool = new Pool(web3)
const prices = await pool.getAmountInExactOut(
@ -416,45 +382,34 @@ Now we can make the contract call
'0.01'
)
```
Now let's console log the result to check everything is working
```Typescript
console.log(`Price of 1 ${POOL_NFT_SYMBOL} is ${prices.tokenAmount} OCEAN`)
```
### 6.5 Consumer buys a pool data asset, and downloads it
### 6.5 Consumer buys a pool data asset, and downloads it
```Typescript
const datatoken = new Datatoken(web3)
const consumerETHBalance = await web3.eth.getBalance(consumerAccount)
```
Now let's console log the result to check everything is working
```Typescript
console.log(`Consumer ETH balance: ${consumerETHBalance}`)
let consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
```
Now let's console log consumerOCEANBalance to check everything is working
```Typescript
console.log(`Consumer OCEAN balance before swap: ${consumerOCEANBalance}`)
let consumerDTBalance = await balance(web3, poolDatatokenAddress, consumerAccount)
```
Now let's console log POOL_NFT_SYMBOL and consumerDTBalance to check everything is working
```Typescript
console.log(`Consumer ${POOL_NFT_SYMBOL} balance before swap: ${consumerDTBalance}`)
```
Before we call the contract we have to call `approve` so that the contract can move our tokens. This is standard when using any ERC20 Datatokens
```Typescript
await approve(web3, consumerAccount, addresses.Ocean, poolAddress, '100')
@ -471,9 +426,7 @@ Before we call the contract we have to call `approve` so that the contract can m
}
```
Now we can make the contract call
```Typescript
await pool.swapExactAmountOut(
consumerAccount,
@ -484,16 +437,12 @@ Now we can make the contract call
consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
```
Now let's console log the Consumer OCEAN balance after swap to check everything is working
```Typescript
console.log(`Consumer OCEAN balance after swap: ${consumerOCEANBalance}`)
consumerDTBalance = await balance(web3, poolDatatokenAddress, consumerAccount)
```
Next let's console log the POOL_NFT_SYMBOL and consumerDTBalance
```Typescript
console.log(`Consumer ${POOL_NFT_SYMBOL} balance after swap: ${consumerDTBalance}`)
@ -501,9 +450,7 @@ Next let's console log the POOL_NFT_SYMBOL and consumerDTBalance
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
```
The next step is to initialize the provider instance
```Typescript
const initializeData = await ProviderInstance.initialize(
resolvedDDO.id,
@ -525,9 +472,7 @@ The next step is to initialize the provider instance
}
```
Now let's make a payment
```Typescript
const tx = await datatoken.startOrder(
poolDatatokenAddress,
@ -538,9 +483,7 @@ Now let's make a payment
)
```
Next up, let's get the URL
```Typescript
const downloadURL = await ProviderInstance.getDownloadUrl(
DDO.id,
@ -553,9 +496,7 @@ Next up, let's get the URL
)
```
Now let's console log the Download URL to check everything is working
```Typescript
console.log(`Download URL: ${downloadURL}`)
@ -564,9 +505,7 @@ Now let's console log the Download URL to check everything is working
consumerDTBalance = await balance(web3, poolDatatokenAddress, consumerAccount)
```
Now let's console log the Consumer balance after order to check everything is working
```Typescript
console.log(`Consumer ${POOL_NFT_SYMBOL} balance after order: ${consumerDTBalance}`)
@ -576,13 +515,12 @@ Now let's console log the Consumer balance after order to check everything is wo
} catch (e) {
assert.fail('Download failed')
}
```
## 7. Publish Data NFT and a Datatoken with a fixed rate exchange
### 7.1 Publish a dataset (create NFT + Datatoken) with a fixed rate exchange
### 7.1 Publish a dataset (create NFT + Datatoken) with a fixed rate exchange
```Typescript
const factory = new NftFactory(addresses.ERC721Factory, web3)
@ -631,26 +569,21 @@ Now let's console log the Consumer balance after order to check everything is wo
freId = tx.events.NewFixedRate.returnValues.exchangeId
```
Now let's console log each of those values to check everything is working
```Typescript
console.log(`Fixed rate exchange NFT address: ${freNftAddress}`)
console.log(`Fixed rate exchange Datatoken address: ${freDatatokenAddress}`)
console.log(`Fixed rate exchange address: ${freAddress}`)
console.log(`Fixed rate exchange Id: ${freId}`)
```
### 7.2 Set metadata in the fixed rate exchange NFT
### 7.2 Set metadata in the fixed rate exchange NFT
```Typescript
const nft = new Nft(web3)
```
Now we are going to update the ddo and set the did
```Typescript
DDO.chainId = await web3.eth.getChainId()
DDO.id =
@ -659,9 +592,7 @@ Now we are going to update the ddo and set the did
DDO.nftAddress = freNftAddress
```
Next, let's encrypt the file(s) using provider
```Typescript
ASSET_URL.datatokenAddress = freDatatokenAddress
ASSET_URL.nftAddress = freNftAddress
@ -670,9 +601,7 @@ Next, let's encrypt the file(s) using provider
DDO.services[0].datatokenAddress = freDatatokenAddress
```
Now let's console log the DID to check everything is working
```Typescript
console.log(`DID: ${DDO.id}`)
@ -692,24 +621,20 @@ Now let's console log the DID to check everything is working
})
```
### 7.3 Marketplace displays fixed rate asset for sale
### 7.3 Marketplace displays fixed rate asset for sale
```Typescript
const fixedRate = new FixedRateExchange(freAddress, web3)
const oceanAmount = await (
await fixedRate.calcBaseInGivenDatatokensOut(freId, '1')
).baseTokenAmount
```
Now that the market has fetched those values it can display the asset on the front end. In our case we will just console log the results:
```Typescript
console.log(`Price of 1 ${FRE_NFT_SYMBOL} is ${oceanAmount} OCEAN`)
```
### 7.4 Consumer buys a fixed rate asset data asset, and downloads it
### 7.4 Consumer buys a fixed rate asset data asset, and downloads it
```Typescript
const datatoken = new Datatoken(web3)
const DATATOKEN_AMOUNT = '10000'
@ -719,9 +644,7 @@ Now that the market has fetched those values it can display the asset on the fro
const consumerETHBalance = await web3.eth.getBalance(consumerAccount)
```
Let's do a quick check of the consumer ETH balance before the swap
```Typescript
console.log(`Consumer ETH balance: ${consumerETHBalance}`)
let consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
@ -730,9 +653,7 @@ Let's do a quick check of the consumer ETH balance before the swap
console.log(`Consumer ${FRE_NFT_SYMBOL} balance before swap: ${consumerDTBalance}`)
```
Before we call the contract we have to call `approve` so that the contract can move our tokens. This is standard when using any ERC20 Datatokens
```Typescript
await approve(web3, consumerAccount, addresses.Ocean, freAddress, '100')
await approve(
@ -745,9 +666,7 @@ Before we call the contract we have to call `approve` so that the contract can m
const fixedRate = new FixedRateExchange(freAddress, web3)
```
Now we can make the contract call
```Typescript
await fixedRate.buyDatatokens(consumerAccount, freId, '1', '2')
@ -760,9 +679,7 @@ Now we can make the contract call
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
```
Next, we need to initialize the provider
```Typescript
const initializeData = await ProviderInstance.initialize(
resolvedDDO.id,
@ -784,9 +701,7 @@ Next, we need to initialize the provider
}
```
Lets now make the payment
```Typescript
const tx = await datatoken.startOrder(
freDatatokenAddress,
@ -796,9 +711,7 @@ Lets now make the payment
providerFees
)
```
Now we can get the url
```Typescript
const downloadURL = await ProviderInstance.getDownloadUrl(
DDO.id,
@ -811,9 +724,7 @@ Now we can get the url
)
```
Lets check that the download URL was successfully received
```Typescript
console.log(`Download URL: ${downloadURL}`)
@ -828,13 +739,12 @@ Lets check that the download URL was successfully received
} catch (e) {
assert.fail('Download failed')
}
```
## 8. Publish Data NFT and a Datatoken with a dispenser
### 8.1 Publish a dataset (create NFT + Datatoken) with a dispenser
### 8.1 Publish a dataset (create NFT + Datatoken) with a dispenser
```Typescript
const factory = new NftFactory(addresses.ERC721Factory, web3)
@ -876,25 +786,20 @@ Lets check that the download URL was successfully received
dispenserDatatokenAddress = tx.events.TokenCreated.returnValues[0]
dispenserAddress = tx.events.DispenserCreated.returnValues[0]
```
Lets check that we managed to received all of those values without any problems
```Typescript
console.log(`Dispenser NFT address: ${dispenserNftAddress}`)
console.log(`Dispenser Datatoken address: ${dispenserDatatokenAddress}`)
console.log(`Dispenser address: ${dispenserAddress}`)
```
### 8.2 Set metadata in the dispenser NFT
### 8.2 Set metadata in the dispenser NFT
```Typescript
const nft = new Nft(web3)
```
Lets start by updating the ddo and setting the did
```Typescript
DDO.chainId = await web3.eth.getChainId()
DDO.id =
@ -903,9 +808,7 @@ Lets start by updating the ddo and setting the did
DDO.nftAddress = dispenserNftAddress
```
Now we need to encrypt file(s) using provider
```Typescript
ASSET_URL.datatokenAddress = dispenserDatatokenAddress
ASSET_URL.nftAddress = dispenserNftAddress
@ -928,11 +831,10 @@ Now we need to encrypt file(s) using provider
encryptedDDO,
'0x' + metadataHash
)
```
### 8.3 Consumer gets a dispenser data asset, and downloads it
### 8.3 Consumer gets a dispenser data asset, and downloads it
```Typescript
const datatoken = new Datatoken(web3)
const dispenser = new Dispenser(addresses.Dispenser, web3)
@ -961,9 +863,7 @@ Now we need to encrypt file(s) using provider
const resolvedDDO = await aquarius.waitForAqua(DDO.id)
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
```
At this point we need to encrypt file(s) using provider
```Typescript
const initializeData = await ProviderInstance.initialize(
resolvedDDO.id,
@ -984,9 +884,7 @@ At this point we need to encrypt file(s) using provider
validUntil: initializeData.providerFee.validUntil
}
```
Now we need to make the payment
```Typescript
const tx = await datatoken.startOrder(
dispenserDatatokenAddress,
@ -996,9 +894,7 @@ Now we need to make the payment
providerFees
)
```
Now we can get the download URL
```Typescript
const downloadURL = await ProviderInstance.getDownloadUrl(
DDO.id,
@ -1010,9 +906,7 @@ Now we can get the download URL
web3
)
```
Let's check we received the download URL ok
```Typescript
console.log(`Download URL: ${downloadURL}`)
@ -1025,11 +919,11 @@ Let's check we received the download URL ok
} catch (e) {
assert.fail('Download failed')
}
```
## Editing this file
## Editing this file
Please note that CodeExamples.md is an autogenerated file, you should not edit it directly.
Updates should be done in `test/integration/CodeExamples.test.ts` and all markdown should have three forward slashes before it
e.g. `/// # H1 Title`