Skip to content

Instantly share code, notes, and snippets.

@dixia
Last active May 21, 2019 06:03
Show Gist options
  • Save dixia/7f1589ec8c98d3e06f5d490610b6583c to your computer and use it in GitHub Desktop.
Save dixia/7f1589ec8c98d3e06f5d490610b6583c to your computer and use it in GitHub Desktop.
Techneek meetup technical demo script
Nodeos command:
nodeos -e -p eosio --plugin eosio::producer_plugin --plugin eosio::chain_api_plugin --plugin eosio::http_plugin --plugin eosio::history_plugin --plugin eosio::history_api_plugin --access-control-allow-origin='*' --contracts-console --http-validate-host=false --verbose-http-errors
CDT:
eosio-cpp registry.cpp -o registry.wasm
Cleos commands:
cleos wallet create
cleos wallet create --to-console
cleos wallet open
cleos wallet unlock
cleos wallet list
cleos wallet create_key
cleos create account eosio registry [Public Key]
cleos get account registry
cleos create account eosio han [Public Key]
cleos get account han
cleos set contract registry ./
cleos get abi registry
cleos push action registry upsert '["han","han", 888, 1, 100, "Hollywood Rd","Hong Kong","China"]' -p han@active
cleos get table registry registry registration
cleos push action registry erase '["han"]' -p registry@active
cleos push action registry erase '["han"]' -p han@active
cleos get table registry registry registration
registry.cpp:
#include <eosio/eosio.hpp>
using namespace eosio;
class [[eosio::contract]] registry : public eosio::contract {
private:
struct [[eosio::table]] registration {
name owner;
std::string full_name;
uint32_t house_num;
uint64_t deed_num;
float house_size;
std::string street;
std::string city;
std::string state;
uint64_t primary_key() const { return owner.value; }
};
typedef eosio::multi_index<"registration"_n, registration> registrations_index;
public:
registry(name receiver, name code, datastream<const char*> ds)
:contract(receiver, code, ds) {}
[[eosio::action]]
void upsert(name owner, std::string full_name,
uint32_t house_num, uint64_t deed_num, float house_size,
std::string street, std::string city, std::string state) {
//Verify permission.
//Is the user who sent the transaction is really a property owner?
require_auth(owner);
//instantise a Multi Index table
registrations_index registrations(get_self(), get_first_receiver().value);
auto iterator = registrations.find(owner.value);
if( iterator == registrations.end() ) {
//owner will be the payer for data storage cost
registrations.emplace(owner, [&]( auto& row ) {
row.owner = owner;
row.full_name = full_name;
row.deed_num = deed_num;
row.house_num = house_num;
row.house_size = house_size;
row.street = street;
row.city = city;
row.state = state;
});
}
else {
//iterator points to the location
registrations.modify(iterator, owner, [&]( auto& row ) {
row.owner = owner;
row.full_name = full_name;
row.deed_num = deed_num;
row.house_num = house_num;
row.house_size = house_size;
row.street = street;
row.city = city;
row.state = state;
});
}
}
[[eosio::action]]
void erase(name owner) {
require_auth(owner);
registrations_index registrations( get_self(), get_first_receiver().value);
auto iterator = registrations.find(owner.value);
//check if the record exists
check(iterator != registrations.end(), "Record does not exist");
registrations.erase(iterator);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment