Last active
August 20, 2018 11:16
-
-
Save RomanFro/5a16df720632af22052445df59cda30e to your computer and use it in GitHub Desktop.
Simple voting contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <eosiolib/eosio.hpp> | |
namespace eosio { | |
class voting : public eosio::contract { | |
/// @abi table candidates i64 | |
struct candidate { | |
uint64_t id; | |
account_name name; | |
auto primary_key() const { return id; } | |
}; | |
public: | |
voting(account_name self): contract(self) {} | |
typedef eosio::multi_index<N(candidates), candidate> Candidates; | |
/// @abi action | |
void acandidate( account_name name ) { | |
Candidates candidates( _self, _self); | |
candidates.emplace( _self, [&]( auto& user ) { | |
user.id = candidates.available_primary_key(); | |
user.name = name; | |
}); | |
} | |
/// @abi action | |
void ucandidate( uint64_t id, account_name name ) { | |
Candidates candidates( _self, _self ); | |
auto itr = candidates.find( id ); | |
eosio_assert(itr != candidates.end(), "No candidate found with this id."); | |
candidates.modify( itr, _self, [&]( auto& user ) { | |
user.name = name; | |
}); | |
} | |
/// @abi action | |
void dcandidate( uint64_t id ) { | |
Candidates candidates( _self, _self ); | |
auto itr = candidates.find( id ); | |
eosio_assert(itr != candidates.end(), "No candidate found with this id."); | |
candidates.erase( itr ); | |
eosio_assert(itr != candidates.end(), "Candidate not erased properly."); | |
} | |
/// @abi action | |
void debug( uint64_t id ) { | |
print("Hello, ", name{gcandidate(id)}); | |
} | |
/// @abi action | |
account_name gcandidate( uint64_t id ) const { | |
Candidates candidates( _self, _self ); | |
auto itr = candidates.find( id ); | |
eosio_assert(itr != candidates.end(), "No candidate found with this id."); | |
return itr->name; | |
} | |
}; | |
} /// namespace eosio | |
EOSIO_ABI( eosio::voting, (acandidate)(ucandidate)(dcandidate)(debug) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment