From 726e34c8fc013523d7db6bad7a86687c9a462f2d Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Wed, 21 Dec 2022 15:04:30 +0000 Subject: [PATCH] Updating guide and formatting --- CodeExamples.md | 43 ++++++++++++++++++++ KeyValueExamples.md | 57 --------------------------- test/integration/CodeExamples.test.ts | 20 ++++++++-- 3 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 KeyValueExamples.md diff --git a/CodeExamples.md b/CodeExamples.md index fc3fe46b..afc4f547 100644 --- a/CodeExamples.md +++ b/CodeExamples.md @@ -677,6 +677,49 @@ Let's check we received the download URL ok ``` + ### 8.1 Add key-value pair to data NFT +Data NFTs can store arbitrary key-value pairs on-chain. This opens up their usage for a broad variety of applications, such as comments & ratings, attestations, and privately sharing data (when the value is encrypted). + +Let's see how! + +Here are the steps: + +1. Setup (same as above) +2. Publish data NFT (same as above) +3. Add key-value pair to data NFT (use the `setData` method) +4. Retrieve value from data NFT (use the `getData` method) + +Let's start by using the `setData` method to update the nft key value store with some data +```Typescript + const nft = new Nft(web3) + const data = 'SomeData' + try { + await nft.setData(freNftAddress, publisherAccount, '1', data) + } catch (e) { + console.error(e) + assert.fail('Download failed') + } +``` + +Under the hood, this uses [ERC725](https://erc725alliance.org/), which augments ERC721 with a well-defined way to set and get key-value pairs. + + + ### 8.2 Add key-value pair to data NFT +```Typescript + const nft = new Nft(web3) + try { + Use the `getData` method to get the data stored in the nft key value store + const data = await nft.getData(freNftAddress, '1') + console.log('Data: ', data) + } catch (e) { + console.error(e) + assert.fail('Download failed') + } +``` + +That's it! Note the simplicity. All data was stored and retrieved from on-chain. We don't need Ocean Provider or Ocean Aquarius for these use cases (though the latter can help for fast querying & retrieval). + + ## Editing this file Please note that CodeExamples.md is an autogenerated file, you should not edit it directly. diff --git a/KeyValueExamples.md b/KeyValueExamples.md deleted file mode 100644 index 400b2d02..00000000 --- a/KeyValueExamples.md +++ /dev/null @@ -1,57 +0,0 @@ - - -# Quickstart: Key-value database - -Data NFTs can store arbitrary key-value pairs on-chain. This opens up their usage for a broad variety of applications, such as comments & ratings, attestations, and privately sharing data (when the value is encrypted). - -Let's see how! - -Here are the steps: - -1. Setup -2. Publish data NFT -3. Add key-value pair to data NFT -4. Retrieve value from data NFT - -## 1. Setup - -Ensure that you've already (a) [installed Ocean](install.md), and (b) [set up locally](setup-local.md) or [remotely](setup-remote.md). - -## 2. Publish data NFT - -```javascript -// from ocean_lib.models.arguments import DataNFTArguments -// data_nft = ocean.data_nft_factory.create(DataNFTArguments('NFT1', 'NFT1'), alice) -``` - -## 3. Add key-value pair to data NFT - -```javascript -// # Key-value pair -// key = "fav_color" -// value = b"blue" - -// # prep key for setter -// from web3.main import Web3 -// key_hash = Web3.keccak(text=key) # Contract/ERC725 requires keccak256 hash - -// # set -// data_nft.setNewData(key_hash, value, {"from": alice}) -``` - -## 4. Retrieve value from data NFT - -```javascript -// value2_hex = data_nft.getData(key_hash) -// value2 = value2_hex.decode('ascii') -// print(f"Found that {key} = {value2}") -``` - -That's it! Note the simplicity. All data was stored and retrieved from on-chain. We don't need Ocean Provider or Ocean Aquarius for these use cases (though the latter can help for fast querying & retrieval). - -We can also encrypt the data. Other quickstarts explore this. - -Under the hood, it uses [ERC725](https://erc725alliance.org/), which augments ERC721 with a well-defined way to set and get key-value pairs. diff --git a/test/integration/CodeExamples.test.ts b/test/integration/CodeExamples.test.ts index c40a8d2c..c7ab5738 100644 --- a/test/integration/CodeExamples.test.ts +++ b/test/integration/CodeExamples.test.ts @@ -678,20 +678,33 @@ describe('Marketplace flow tests', async () => { /// ``` it('8.1 Add key-value pair to data NFT', async () => { + /// Data NFTs can store arbitrary key-value pairs on-chain. This opens up their usage for a broad variety of applications, such as comments & ratings, attestations, and privately sharing data (when the value is encrypted). + + /// Let's see how! + + /// Here are the steps: + + /// 1. Setup (same as above) + /// 2. Publish data NFT (same as above) + /// 3. Add key-value pair to data NFT (use the `setData` method) + /// 4. Retrieve value from data NFT (use the `getData` method) + + /// Let's start by using the `setData` method to update the nft key value store with some data /// ```Typescript const nft = new Nft(web3) const data = 'SomeData' try { - /// Use the `setData` method to update the nft key value store with some data await nft.setData(freNftAddress, publisherAccount, '1', data) } catch (e) { console.error(e) assert.fail('Download failed') } /// ``` + + /// Under the hood, this uses [ERC725](https://erc725alliance.org/), which augments ERC721 with a well-defined way to set and get key-value pairs. }) /// - it('8.2 Add key-value pair to data NFT', async () => { + it('8.2 get the key-value pair data from the NFT', async () => { /// ```Typescript const nft = new Nft(web3) try { @@ -703,8 +716,9 @@ describe('Marketplace flow tests', async () => { assert.fail('Download failed') } /// ``` + + /// That's it! Note the simplicity. All data was stored and retrieved from on-chain. We don't need Ocean Provider or Ocean Aquarius for these use cases (though the latter can help for fast querying & retrieval). }) /// - /// ``` }) /// /// ## Editing this file