Skip to content

Instantly share code, notes, and snippets.

@dskvr
Created September 14, 2018 13:41
Show Gist options
  • Save dskvr/68cc0e5755f4b263c3a712fc93bd8156 to your computer and use it in GitHub Desktop.
Save dskvr/68cc0e5755f4b263c3a712fc93bd8156 to your computer and use it in GitHub Desktop.
Weird contract
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class addressbook : public eosio::contract {
public:
using contract::contract;
addressbook(account_name self): contract(self) {}
[[eosio::action]]
void upsert(account_name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {
require_auth( user );
address_index addresses(_self, user );
auto iterator = addresses.find(user);
if( iterator == addresses.end() )
{
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;
});
INLINE_ACTION_SENDER( addressbook, notify )( N(addressbook), { user, N(active) }, { user, std::string("You added your address to addressbook") } );
}
else {
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;
});
INLINE_ACTION_SENDER( addressbook, notify )( N(addressbook), { user, N(active)}, { user, std::string("You updated your address.") } );
}
}
[[eosio::action]]
void notify(account_name user, std::string msg) {
require_recipient(user);
}
[[eosio::action]]
void remove(account_name user){
require_auth( user );
address_index addresses(_self, user);
auto iterator = addresses.find(user);
eosio_assert(iterator != addresses.end(), "Record does not exist");
addresses.erase( iterator );
eosio_assert(iterator == addresses.end(), "Address not erased properly");
INLINE_ACTION_SENDER( addressbook, notify )( N(addressbook), { user, N(active)}, { user, std::string("You have successfully removed your record from addressbook") } );
}
private:
struct [[eosio::table]] person {
uint64_t key;
std::string first_name;
std::string last_name;
std::string street;
std::string city;
std::string state;
uint64_t primary_key() const { return key; }
EOSLIB_SERIALIZE( person, (key)(first_name)(last_name)(street)(city)(state) )
};
typedef eosio::multi_index<N(person), person> address_index;
};
EOSIO_ABI( addressbook, (upsert) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment