-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmego.cpp
More file actions
49 lines (38 loc) · 1.47 KB
/
mego.cpp
File metadata and controls
49 lines (38 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT mego : public eosio::contract {
private:
TABLE ridestruct {
uint64_t prim_key; // primary key
name user; // account name for the user
std::string scooter; // id of scooter to be ridden
// primary key
auto primary_key() const { return prim_key; }
// secondary key
// only supports uint64_t, uint128_t, uint256_t, double or long double
uint64_t get_by_user() const { return user.value; }
};
// create a multi-index table and support secondary key
typedef eosio::multi_index< name("ridestruct"), ridestruct,
indexed_by< name("getbyuser"), const_mem_fun<ridestruct, uint64_t, &ridestruct::get_by_user> >
> ride_table;
ride_table _rides;
public:
using contract::contract;
// constructor
mego( name receiver, name code, datastream<const char*> ds ):
contract( receiver, code, ds ),
_rides( receiver, receiver.value ) {}
ACTION ride( name user, std::string& scooter ) {
// to sign the action with the given account
require_auth( user );
// insert new ride
_rides.emplace( _self, [&]( auto& new_ride ) {
new_ride.prim_key = _rides.available_primary_key();
new_ride.user = user;
new_ride.scooter = scooter;
});
}
};
// specify the contract name, and export a public action: ride
EOSIO_DISPATCH( mego, (ride) )