INERY Creating Value Contract

INERY Creating Value Contract

By otomatis | pramonoutomo | 2 Nov 2022


A value contract is a smart contract where the contents can be a data set that you can manipulate according to the contract rules that you create, this can be exemplified as a decentralized database where you can view, add, modify and delete data from your database by very easy.

INERY supports creating value contracts very easily, here I will show you how to start and deploy your first simple value contract.

if you don't have inery nodes that you can use to deploy your value contracts, you can check out my tutorial at https://www.publish0x.com/pramonoutomo/inery-testnet-master-node-lite-node-tutorial-xpzpknr  and if you haven't read my article about creating your own token on INERY network, you can see my article https://www.publish0x.com/pramonoutomo/createdeploy-a-token-on-inery-and-transfer-to-other-accounts-xnnkqwm 

If you are looking for a suitable vps for inery, I suggest you can go to https://lihat.info/contabo and buy package L and above. or you can use another vps provider such as vultr, ovh or vpstime.


update your dependencies first

sudo apt update -y && sudo apt upgrade -y
sudo apt install -y make bzip2 automake libbz2-dev libssl-dev doxygen graphviz libgmp3-dev \
autotools-dev libicu-dev python2.7 python2.7-dev python3 python3-dev \
autoconf libtool curl zlib1g-dev sudo ruby libusb-1.0-0-dev \
libcurl4-gnutls-dev pkg-config patch llvm-7-dev clang-7 vim-common jq libncurses5 git

unlock your wallet first, the default name is default

cline wallet unlock -n YourWalletName

get the inery.cdt tools binary from inery github

git clone https://github.com/inery-blockchain/inery.cdt

set as vars for easy access

export PATH=$PATH:$HOME/inery.cdt/bin/

Now we're ready to make our first value contract. let's start writing the contract

sudo nano inrcrud_contract.cpp

copy all text in box below to your terminal client

#include <inery/inery.hpp>
#include <inery/print.hpp>
#include <string>

using namespace inery;

using std::string;

class [[inery::contract]] inrcrud : public inery::contract {
  public:
    using inery::contract::contract;


        [[inery::action]] void create( uint64_t id, name user, string data ) {
            records recordstable( _self, id );
            auto existing = recordstable.find( id );
            check( existing == recordstable.end(), "record with that ID already exists" );
            check( data.size() <= 256, "data has more than 256 bytes" );

            recordstable.emplace( _self, [&]( auto& s ) {
               s.id         = id;
               s.owner      = user;
               s.data       = data;
            });

            print( "Hello, ", name{user} );
            print( "Created with data: ", data );
        }

         [[inery::action]] void read( uint64_t id ) {
            records recordstable( _self, id );
            auto existing = recordstable.find( id );
            check( existing != recordstable.end(), "record with that ID does not exist" );
            const auto& st = *existing;
            print("Data: ", st.data);
        }

        [[inery::action]] void update( uint64_t id, string data ) {
            records recordstable( _self, id );
            auto st = recordstable.find( id );
            check( st != recordstable.end(), "record with that ID does not exist" );


            recordstable.modify( st, get_self(), [&]( auto& s ) {
               s.data = data;
            });

            print("Data: ", data);
        }

            [[inery::action]] void destroy( uint64_t id ) {
            records recordstable( _self, id );
            auto existing = recordstable.find( id );
            check( existing != recordstable.end(), "record with that ID does not exist" );
            const auto& st = *existing;

            recordstable.erase( st );

            print("Record Destroyed: ", id);

        }

  private:
    struct [[inery::table]] record {
       uint64_t        id;
       name     owner;
       string          data;
       uint64_t primary_key()const { return id; }
    };

    typedef inery::multi_index<"records"_n, record> records;
 };

then CTRL + X , then Y, enter.

create new folder and move the file into new folder

mkdir inrcrud
mv inrcrud_contract.cpp inrcrud/inrcrud.cpp
cd inrcrud

compile the contract file

inery-cpp -abigen -o inrcrud.wasm inrcrud.cpp

set contract to our account 

cline set contract AccountName $HOME/inrcrud -p AccountName --json

if the transaction success, then congrats your contract are ready to be used.

now we can play with our first contract, use command below to use your contract.

Create new data

cline push action AccountName create '[1, AccountName, "My first Value Contract Record"]' -p AccountName -j

Read data

cline push action AccountName read [1] -p AccountName -j

Update data

cline push action AccountName update '[ 1,  "My first Record Modified"]' -p AccountName -j

Delete data

cline push action AccountName destroy [1] -p AccountName

 

So, what DApp should we create now? :) 

Follow Inery on twitter Join inery official telegram channel , telegram group and inery official discord here

Join @codeblocklabs for more discussion :) 

also follow @pramonoutomo on twitter and subscribe my channel https://youtube.com/c/pramonoutomo 


#inery #IneryBlockchain #ineryDB #$INR #nodes #database #blockchain #decentralized #smartcontract #contract

How do you rate this article?

4



pramonoutomo
pramonoutomo

Hey it's pram here, join my universe https://youtube.com/c/pramonoutomo and https://twitter.com/pramonoutomo also don't forget to check out my portofolio on https://pramonoutomo.com

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.