Skip to content

Commit

Permalink
Merge pull request #423 from GolosChain/380-add-events-to-charge-cont…
Browse files Browse the repository at this point in the history
…ract

Add sending events into charge smart-contract #380
  • Loading branch information
afalaleev committed Jan 25, 2019
2 parents 8c1efca + 54a1d05 commit ea625ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions golos.charge/golos.charge.abi
Expand Up @@ -192,6 +192,36 @@
}
]
},
{
"name": "chargestate",
"base": "",
"fields": [
{
"name": "user",
"type": "name"
},
{
"name": "charge_symbol",
"type": "uint64"
},
{
"name": "token_code",
"type": "symbol_code"
},
{
"name": "charge_id",
"type": "uint8"
},
{
"name": "last_update",
"type": "uint64"
},
{
"name": "value",
"type": "base_t"
}
]
},
{
"name": "stored",
"base": "",
Expand Down Expand Up @@ -245,6 +275,9 @@
]
},
],
"events": [
{"name": "chargestate", "type": "chargestate"},
],
"actions": [
{
"name": "setrestorer",
Expand Down
8 changes: 8 additions & 0 deletions golos.charge/golos.charge.cpp
@@ -1,4 +1,5 @@
#include "golos.charge.hpp"
#include <eosiolib/event.hpp>

namespace golos {
using namespace eosio;
Expand Down Expand Up @@ -33,13 +34,15 @@ fixp_t charge::consume_charge(name issuer, name user, symbol_code token_code, ui
item.charge_id = charge_id;
item.last_update = current_time();
item.value = new_val.data();
send_charge_event(user, item);
});
return new_val;
}
if (new_val > 0)
balances_table.modify(*itr, name(), [&]( auto &item ) {
item.last_update = current_time();
item.value = new_val.data();
send_charge_event(user, item);
});
else
balances_table.erase(itr);
Expand Down Expand Up @@ -117,6 +120,11 @@ void charge::setrestorer(symbol_code token_code, uint8_t charge_id, std::string
item.max_elapsed = to_fixp(max_elapsed).data();
});
}

void charge::send_charge_event(name user, const balance& state) {
eosio::event(_self, "chargestate"_n, std::make_tuple(user, state)).send();
}

EOSIO_DISPATCH(charge, (use)(useandstore)(removestored)(setrestorer) )

} /// namespace golos
Expand Down
4 changes: 4 additions & 0 deletions golos.charge/golos.charge.hpp
Expand Up @@ -113,6 +113,10 @@ class charge : public contract {

return std::max(fp_cast<fixp_t>((elap_t(FP(user_balance.value)) - elap_t(restored)) + elap_t(price)), fixp_t(0));
}

private:
void send_charge_event(name user, const balance& state);

public:
static inline int64_t get_current_value(name code, name user, symbol_code token_code, uint8_t charge_id = 0) {
balances balances_table(code, user.value);
Expand Down

0 comments on commit ea625ab

Please sign in to comment.