Smart contracts

hellocoffe contract

#include <eosio/eosio.hpp>
#include <eosio/asset.hpp>
#include <eosio/transaction.hpp>
#include <eosio/system.hpp>
#include <eosio/crypto.hpp>
#include <eosio/action.hpp>
#include <string>

using namespace eosio;
using namespace std;
using std::string;

CONTRACT hellocoffe : public contract {
  public:
    using contract::contract;
    hellocoffe(eosio::name receiver, eosio::name code, datastream<const char*> ds):contract(receiver, code, ds) {}

    TABLE testtable {
        uint64_t id;
        name account;
        string message;
        uint64_t primary_key()const { return id; }
    };
    typedef eosio::multi_index< "testtable"_n, testtable> db_testtable;

    ACTION injectdata( name from, string value );
    ACTION cleandata( name from );
};

void hellocoffe::injectdata( name from, string value ){
    db_testtable testtabl( get_self(), get_self().value );
    testtabl.emplace( get_self(), [&]( auto& t ){
        t.id = testtabl.available_primary_key();
        t.account = from;
        t.message = value;
    });
}

void hellocoffe::cleandata( name from ){
    int on = 0;
    db_testtable testtabl(get_self(), get_self().value);
    auto test = testtabl.begin();
    while(test != testtabl.end()){
        test = testtabl.erase(test);
        on++;
        if(on >= 20){ break; }
    }
}

extern "C" void apply(uint64_t receiver, uint64_t code, uint64_t action)
{
    if (code == receiver)
    {
        switch (action)
        {
            EOSIO_DISPATCH_HELPER( hellocoffe, (injectdata)(cleandata))
        }
    }
}

Development Tools

Compilation and deployment of hellocoffe smart contract to COFFE account

A smart contract is an executable code written in C. Before deploying it to the network, it must be compiled into a binary code. When compiling, you will get 2 *.wasm and *.abi files. They need to be uploaded to the network to your account. The smart contract is written to the account. Each account can contain only one smart contract. When it comes to the smart contract address, in the case of the COFFE network, the smart contract address is the name of your account, for example, https://coffe.bloks.io/account/accountname1 You can download an example of a smart contract that is used in the sample at the link: https://coffe.io/files/samples/ue4sample.zip There are 3 contracts in the archive as Examples: hellocoffe, which is the contract that is used in the ue4 sample. testdapp, which is the contract that demonstrates the capabilities of smart contracts. clean, which is the cleanup contract of smart contract. Let's deploy the hellocoffe contract to our account. Go to https://dev.coffe.io. Click "select source file" and select "hellocoffe.cpp".

Compilation

In the sourcename field enter ‘hellocoffe’ and click ‘compile’. The server will compile the files. You will have a minute to download the binaries from the link, then they will be removed.

Compilation

Enter your account and a PRIVATE key. Click ‘select wasm file’ > ‘deploy wasm’, then ‘select abi file’ > ‘deploy abi’.

Deployment

After the binaries are deployed, you must also give permission to the contract for accessing the EOSIO.CODE code. Otherwise, some functions in smart contracts will be unavailable. Click ‘set EOSIO.CODE permission’ to install. Go to bot> ‘account’> ‘add eosio.code permission’.

After successfully loading the contract in the explorer, you can see that a new contract tab has appeared

In a smart contract, data is stored in tables. These tables are publicly available. Any user on the network can get data from these tables, therefor if you need to place private data, encrypt it before writing it to the table.

The ‘table’ tab in the explorer contains the testtable. Now this table is empty. The Actions tab contains the actions available for accounts. Their execution is to be signed by any user interacting with the contract.

Still need help? Get in touch!
Last updated on 31st Dec 2021