From aef17e96d41d55d89ee5671f0164c2845fbb25ae Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Wed, 21 Dec 2022 10:51:50 +0000 Subject: [PATCH] Creating Key-value example readme --- KeyValueExamples.md | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 KeyValueExamples.md diff --git a/KeyValueExamples.md b/KeyValueExamples.md new file mode 100644 index 00000000..400b2d02 --- /dev/null +++ b/KeyValueExamples.md @@ -0,0 +1,57 @@ + + +# 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.