Skip to content

Instantly share code, notes, and snippets.

@WendySanarwanto
Last active March 5, 2019 23:18
Show Gist options
  • Save WendySanarwanto/129a09cb0a9a3f15985f493f47feb933 to your computer and use it in GitHub Desktop.
Save WendySanarwanto/129a09cb0a9a3f15985f493f47feb933 to your computer and use it in GitHub Desktop.
eosio smartcontract - addresses
#include <eosiolib/eosio.hpp>
// #include "includes/person.hpp"
using namespace eosio;
using namespace std;
/**
* Assymptions:
* 1. The only account authorized to modify the address book is the user.
* 2. the primary_key of our table is unique, based on username
* 3. For usability, the contract should have the ability to both create and modify a table row with a single action.
*/
class [[eosio::contract("addressbook")]] addressbook : public contract {
public:
using contract::contract;
addressbook(name receiver, name code, datastream<const char*> ds) : contract(receiver, code, ds) {}
[[eosio::action]]
void upsert(name user, string first_name, string last_name, string street, string city, string state) {
// The only account authorized to modify the address book is the user.
require_auth(user);
/*
Instantiate the table. Previously, a multi_index table was configured, and declared it as address_index.
To instantiate a table, consider it's two required arguments:
1. The "code", which represents the contract's account. This value is accessible through the scoped _code variable.
2. The "scope" which ensures the uniqueness of the contract. In this case, since we only have one table we can use "_code" as well
*/
address_index addresses( _code, _code.value );
// Next, query the iterator, setting it to a variable since this iterator will be used several times
auto iterator = addresses.find(user.value);
/*
Detect whether or not the particular user already exists. To do this, use table's find method by passing the user parameter.
The find method will return an iterator. Use that iterator to test it against the end method.
The "end" method is an alias for "null".
*/
if (iterator == addresses.end()) {
// The user IS NOT in the table
addresses.emplace(user, [&](auto& row) {
row.key = user;
row.first_name = first_name;
row.last_name = last_name;
row.street = street;
row.city = city;
row.state = state;
});
} else {
// The user is in the table
addresses.modify(iterator, user, [&](auto& row) {
row.key = user;
row.first_name = first_name;
row.last_name = last_name;
row.street = street;
row.city = city;
row.state = state;
});
}
}
[[eosio::action]]
void erase(name user) {
require_auth(user);
address_index addresses(_code, _code.value);
auto iterator = addresses.find(user.value);
eosio_assert(iterator != addresses.end(), "Record does not exist");
addresses.erase(iterator);
}
private:
struct [[eosio::table]] person {
name key;
string first_name;
string last_name;
string street;
string city;
string state;
uint64_t primary_key() const { return key.value; }
};
typedef multi_index<"people"_n, person> address_index;
};
EOSIO_DISPATCH( addressbook, (upsert) (erase) )
// Command to build: rm -rf ./build && mkdir build && eosio-cpp -o ./build/addressbook.wasm ./src/addressbook.cpp --abigen;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment