Categories
Crypto

EOS with examples

EOS is a blockchain platform that was created to support the development and execution of decentralized applications (dApps). It uses a Delegated Proof of Stake (DPoS) consensus algorithm, which allows for high scalability and fast transaction processing.

EOS has its own native cryptocurrency called EOS tokens, which are used to pay for resources such as network bandwidth and storage on the EOS blockchain. The EOS platform also includes a web assembly (WASM) virtual machine, which allows for the execution of smart contracts written in multiple programming languages.

One of the key features of EOS is its ability to handle a high number of transactions per second (TPS). While most blockchain platforms are limited to a few TPS, EOS is designed to handle millions of TPS. This is achieved through a combination of a highly efficient consensus algorithm and a unique system of resource allocation.

Another key feature of EOS is its governance structure. The EOS community elects 21 block producers (BPs) through a voting process, who are responsible for maintaining the network and producing new blocks. These BPs are incentivized to act in the best interest of the community, as they are rewarded with EOS tokens for their contributions to the network.

Smart Contracts

EOS smart contracts are written in web assembly (WASM) and can be written in multiple programming languages such as C++, Rust and others.

EOS smart contracts are similar to those on other blockchain platforms, such as Ethereum, in that they allow for the execution of complex logic and the creation of decentralized applications (dApps). However, EOS smart contracts have some unique features that set them apart from other platforms.

One of the key features of EOS smart contracts is their ability to use a “parallel processing” model. This means that multiple smart contracts can be executed simultaneously, which increases the overall speed and efficiency of the network.

Another key feature of EOS smart contracts is the ability to use a “multi-threading” model. This means that a single smart contract can be executed by multiple threads, which increases the overall performance and scalability of the network.

EOS smart contracts also have the ability to access and use the resources of the EOS blockchain, such as network bandwidth and storage. This is achieved through a unique system of resource allocation, where the developer must hold a certain amount of EOS tokens to access and use these resources.

There are also other features such as token creation, peer-to-peer transactions, and more…

Example: token.cpp

#include <eosio/eosio.hpp>

using namespace eosio;

CONTRACT token
: public contract {
public:
using contract::contract;

ACTION create(name issuer, asset maximum_supply);

ACTION issue(name to, asset quantity, string memo);

ACTION transfer(name from, name to, asset quantity, string memo);

private:
TABLE account{
	asset    balance;
	uint64_t primary_key()const { return balance.symbol.code().raw(); }
};

TABLE currency_stats{
	asset          supply;
	asset          max_supply;
	name           issuer;
	uint64_t primary_key()const { return supply.symbol.code().raw(); }
};

typedef eosio::multi_index<"accounts"_n, account> accounts;
typedef eosio::multi_index<"stat"_n, currency_stats> stats;

void sub_balance(name owner, asset value);

void add_balance(name owner, asset value, name ram_payer);

};

ACTION token::create(name issuer, asset maximum_supply)
{
	require_auth(issuer);
	auto sym = maximum_supply.symbol;
	eosio::check(sym.is_valid(), "invalid symbol name");
	eosio::check(maximum_supply.is_valid(), "invalid supply");
	eosio::check(maximum_supply.amount > 0, "max-supply must be positive");

	stats statstable(get_self(), sym.code().raw());
	auto existing = statstable.find(sym.code().raw());
	eosio::check(existing == statstable.end(), "token with symbol already exists");

	statstable.emplace(issuer, [&](auto& s)
	{
		s.supply = asset{ 0, sym };
		s.max_supply = maximum_supply;
		s.issuer = issuer;
	});
}

ACTION token::issue(name to, asset quantity, string memo)
{
	auto sym = quantity.symbol;
	eosio::check(sym.is_valid(), "invalid symbol name");
	eosio::check(memo.size() <= 256, "memo has more than 256 bytes");

	stats statstable(get_self(), sym.code().raw());
	auto existing = statstable.find(sym.code().raw());
	eosio::check(existing != statstable.end(), "token with symbol does not exist, create token before issue");
	const auto& st = *existing;

	require_auth(st.issuer);
	eosio::check(quantity.is_valid(), "invalid quantity");
	eosio::check(quantity.amount > 0, "must issue positive quantity");

	eosio::check(quantity.symbol == st.supply.symbol, "symbol precision mismatch");
	eosio::check(quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply");
	statstable.modify(st, same_payer, [&](auto& s)
	{
		s.supply += quantity;
	});

	add_balance(to, quantity, st.issuer);
}

ACTION token::transfer(name from, name to, asset quantity, string memo)
{
	eosio::check(from != to, "cannot transfer to self");
	require_auth(from);
	eosio::check(is_account(to), "to account does not exist");
	auto sym = quantity.symbol.code();
	stats statstable(get_self(), sym.raw());
	const auto& st = statstable.get(sym.raw());
	require_recipient(from);
	require_recipient(to);

	eosio::check(quantity.is_valid(), "invalid quantity");
	eosio::check(quantity.amount > 0, "must transfer positive quantity");
	eosio::check(quantity.symbol == st.supply.symbol, "symbol precision mismatch");
	eosio::check(memo.size() <= 256, "memo has more than 256 bytes");

	sub_balance(from, quantity);
	add_balance(to, quantity, from);
}

void token::sub_balance(name owner, asset value)
{
	accounts from_acnts(get_self(), owner.value);

	const auto& from = from_acnts.get(value.symbol.code().raw(), "no balance object found");
	eosio::check(from.balance.amount >= value.amount, "overdrawn balance");

	from_acnts.modify(from, owner, [&](auto& a)
	{
		a.balance -= value;
	});
}

void token::add_balance(name owner, asset value, name ram_payer)
{
	accounts to_acnts(get_self(), owner.value);
	auto to = to_acnts.find(value.symbol.code().raw());
	if (to == to_acnts.end())
	{
		to_acnts.emplace(ram_payer, [&](auto& a)
		{
			a.balance = value;
		});
	}
	else
	{
		to_acnts.modify(to, same_payer, [&](auto& a)
		{
			a.balance += value;
		});
	}
}

This contract is a template for creating, issuing, and transferring a custom ERC-20-like token on the EOS blockchain.

It inherits from the contract class provided by the EOSIO library, which allows it to interact with the EOS blockchain.

The contract defines three actions:

  • create: allows the issuer to create a new token with a specified maximum supply.
  • issue: allows the issuer to issue a certain amount of the token to a specified recipient.
  • transfer: allows a user to transfer a certain amount of the token to another user.

It also defines two tables:

  • account: stores the balance of each user for a specific token.
  • currency_stats: stores the overall supply, maximum supply, and issuer of the token.

And helper functions:

  • sub_balance: subtracts a certain amount of the token from a user’s balance.
  • add_balance: adds a certain amount of the token to a user’s balance.

Overall, this contract provides the basic functionality for creating and managing a custom token on the EOS blockchain, and can be adapted to the specific requirements of a given application.

Full source code here https://github.com/michaldrozd/eos-smart-contracts/blob/main/token_contract.cpp

One reply on “EOS with examples”

Leave a Reply

Your email address will not be published. Required fields are marked *