Smart Contract Examples


Creating Asset and transfer to owner account ownerowner22:

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

name author = get_self();
name category = “weapon”_n;
name owner = “ownerowner22″_n;
string idata = “{\”power\”: 10, \”speed\”: 2.2, \”name\”: \”Magic Sword\” }”;
string mdata = “{\”color\”: \”bluegold\”, \”level\”: 3, \”stamina\”: 5, \”img\”: \”https://bit.ly/2MYh8EA\” }”;

action createAsset = action(
permission_level{author, “active”_n},
SIMPLEASSETSCONTRACT,
“create”_n,
std::make_tuple( author, category, owner, idata, mdata, 0 )
);
createAsset.send();


Creating Asset with requireclaim option for ownerowner22:

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

name author = get_self();
name category = “balls”_n;
name owner = “ownerowner22″_n;
string idata = “{\”radius\”: 2, \”weigh\”: 5, \”material\”: \”rubber\”, \”name\”: \”Baseball\” }”;
string mdata = “{\”color\”: \”white\”, \”decay\”: 99, \”img\”: \”https://i.imgur.com/QoTcosp.png\” }”;

action createAsset = action(
permission_level{author, “active”_n},
SIMPLEASSETSCONTRACT,
“create”_n,
std::make_tuple( author, category, owner, idata, mdata, 1 )
);
createAsset.send();


1. Please add in your hpp file info about assets structure

TABLE account {
uint64_t id;
name author;
asset balance;

uint64_t primary_key()const {
return id;
}
};
typedef eosio::multi_index< "accounts"_n, account > accounts;

TABLE sasset {
uint64_t id;
name owner;
name author;
name category;
string idata;
string mdata;
std::vector container;
std::vector containerf;

auto primary_key() const {
return id;
}

uint64_t by_author() const {
return author.value;
}
};

typedef eosio::multi_index< "sassets"_n, sasset, eosio::indexed_by< "author"_n, eosio::const_mem_fun >> sassets;

2. Searching and using info

name SIMPLEASSETSCONTRACT = “simpleassets”_n;
name author = get_self();
name owner = “lioninjungle”_n;

uint64_t assetid = 100000000000187

sassets assets(SIMPLEASSETSCONTRACT, owner.value);
auto idx = assets.find(assetid);

check(idx != assets.end(), “Asset not found or not yours”);

check (idx->author == author, “Asset is not from this author”);

auto idata = json::parse(idx->idata); // for parsing json here is used nlohmann lib
auto mdata = json::parse(idx->mdata); // https://github.com/nlohmann/json

check(mdata[“cd”] < now(), "Not ready yet for usage");


Update Asset

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

auto mdata = json::parse(idxp->mdata);
mdata[“cd”] = now() + 84600;

name author = get_self();
name owner = “ownerowner22″_n;
uint64_t assetid = 100000000000187;

action saUpdate = action(
permission_level{author, “active”_n},
SIMPLEASSETSCONTRACT,
“update”_n,
std::make_tuple(author, owner, assetid, mdata.dump())
);
saUpdate.send();


Transfer One Asset

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

name author = get_self();
name from = “lioninjungle”_n;
name to = “ohtigertiger”_n;

uint64_t assetid = 100000000000187;

std::vector assetids;
assetids.push_back(assetid);

string memo = “Transfer one asset”;

action saTransfer = action(
permission_level{from, “active”_n},
SIMPLEASSETSCONTRACT,
“transfer”_n,
std::make_tuple(from, to, assetids, memo)
);
saTransfer.send();


Transfer two Asset to same receiver with same memo

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

name author = get_self();
name from = “lioninjungle”_n;
name to = “ohtigertiger”_n;

uint64_t assetid1 = 100000000000187;
uint64_t assetid2 = 100000000000188;

std::vector assetids;
assetids.push_back(assetid1);
assetids.push_back(assetid2);

string memo = “Transfer two asset”

action saTransfer = action(
permission_level{from, “active”_n},
SIMPLEASSETSCONTRACT,
“transfer”_n,
std::make_tuple(from, to, assetids, memo)
);
saTransfer.send();


Burn Assets

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

name owner = “lioninjungle”_n;
uint64_t assetid1 = 100000000000187;
uint64_t assetid2 = 100000000000188;

std::vector assetids;
assetids.push_back(assetid1);
assetids.push_back(assetid2);

string memo = “Transfer two asset”

action saBurn = action(
permission_level{owner, “active”_n},
SIMPLEASSETSCONTRACT,
“transfer”_n,
std::make_tuple(owner, assetids, memo)
);
saBurn.send();


issuef (fungible) issue created token

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

asset wood;
wood.amount = 100;
wood.symbol = symbol(“WOOD”, 0);

name author = get_self();
name to = “lioninjungle”_n;

std::string memo = “WOOD faucet”;
action saRes1 = action(
permission_level{author, “active”_n},
SIMPLEASSETSCONTRACT,
“issuef”_n,
std::make_tuple(to, author, wood, memo)
);
saRes1.send();


transferf (fungible) by author if authorctrl is enabled

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

asset wood;
wood.amount = 20;
wood.symbol = symbol(“WOOD”, 0);

name from = “lioninjungle”_n;
name to = get_self();
name author = get_self();

std::string memo = “best WOOD”;
action saRes1 = action(
permission_level{from, “active”_n},
SIMPLEASSETSCONTRACT,
“transferf”_n,
std::make_tuple(from, to, author, wood, memo)
);
saRes1.send();


burnf (fungible) by author if authorctrl is enabled

name SIMPLEASSETSCONTRACT = “simpleassets”_n;

asset wood;
wood.amount = 20;
wood.symbol = symbol(“WOOD”, 0);

name author = get_self();
name from = “lioninjungle”_n;

std::string memo = “WOOD for oven”;
action saRes1 = action(
permission_level{author, “active”_n},
SIMPLEASSETSCONTRACT,
“burnf”_n,
std::make_tuple(from, author, wood, memo)
);
saRes1.send();