Key value storage on the Ethereum network
By: Will Hay Jr.
Tags: Blockchain Development, Solidity, Blockchain, Ethereum, Bitcoin

Over the past few weeks I’ve been learning the ins and outs of the Truffle Ethereum development framework and Ethereum’s scripting language, Solidity.

My first project is a tool which stores any data on the Ethereum blockchain specific to your Ethereum address. Only you may alter the data associated with your address.

Using the contract (Remix IDE)

These instructions will help you connect to the KVStore contract on the main Ethereum network using Remix, a web based Solidity IDE.

Prerequisites

Download MetaMask

Open up MetaMask once you add the extension and create a new account.

Send some ether to your new account. Depending on how much data you want to store, I’d recommend at least 0.001 Ether, the set function at the current (4/18) gas prices costs about 0.0002 Ether (5–10 cents) to store a string sentence.

Running the contract

Go to remix.ethereum.org.

2. Exit out of the browser/ballot.sol file and create a new file called KVStore.sol by clicking the plus button in the top left corner.

3. Copy and paste the contract code from here into the text editor.

4. In the top right, click the “Start to Compile” button or check “Auto Compile.”

5. Click on the Run tab in the top right.

6. Your “Environment” should be set to Injected Web3 (Main (1))

7. The Account should be set to your MetaMask address that you sent Ether to earlier.

8. Now paste this address:

0xd5598441fb71cbe19c16d25864cb8a3e3f0b77e5

into the “Load Contract From Address” field farther down the page on the right.

9. Then click the “At Address” button to load the contract.

Now you should see an element popup with a “Set” and “Get” field.

10. Put something along the lines of

“key”, “some data I want to store on the blockchain”

in the input field and then click “Set”.

11. You’ll then get a popup detailing the transaction you’re about to create. Click confirm and then open MetaMask and submit the transaction.

Your data is now written to the Ethereum blockchain and you can access it using the Get function.

12. In the Get function input, put your MetaMask address in the *address spot and then the key value as the second argument of the Get function.

Note the *address used in should NOT be the contract address, but your MetaMask account address that you sent Ether to which can be found in the Account input in the top right, or if you click the MetaMask extension you can find the address there.

*address, “key”

You may have to wait a minute or two for the data you just set to be propagated across the network and included in the next block before Get() will return the value correctly.

If you put in the parameters correctly, the Get function will return the data you just set earlier!

Congrats, you just stored data to the Ethereum Blockchain, a distributed network of thousands of computers worldwide. Only you may change or delete the data, and it can be accessed basically anywhere!

Stress Test Costs

I tried storing the max amount of data in one “Set” call, a string of length 1000, and it cost 0.00618 Ether ($2.56) which isn’t bad in my opinion. Obviously it is not ideal for storing large amounts of data, which wasn’t what Ethereum was really built for.