Skip to content

Commit

Permalink
Merge pull request #422 from GolosChain/379-add-events-to-vesting-con…
Browse files Browse the repository at this point in the history
…tract

Add sending events in vesting smart-contract #379
  • Loading branch information
afalaleev committed Jan 25, 2019
2 parents ee8efd6 + 56ddbdd commit 8c1efca
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions golos.vesting/golos.vesting.abi
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@
}
]
},
{
"name": "balance_event",
"base": "",
"fields": [
{
"name": "account",
"type": "name"
},
{
"name": "vesting",
"type": "asset"
},
{
"name": "delegate_vesting",
"type": "asset"
},
{
"name": "received_vesting",
"type": "asset"
}
]
},
{
"name": "delegate_record",
"base": "",
Expand Down Expand Up @@ -295,6 +317,10 @@
]
}
],
"events": [
{"name": "balance", "type": "balance_event"},
{"name": "vesting", "type": "asset"}
],
"actions": [
{
"name": "setparams",
Expand Down
20 changes: 20 additions & 0 deletions golos.vesting/golos.vesting.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "golos.vesting.hpp"
#include "config.hpp"
#include <eosiolib/transaction.hpp>
#include <eosiolib/event.hpp>
#include <cyber.token/cyber.token.hpp>
#include <golos.charge/golos.charge.hpp>

Expand Down Expand Up @@ -101,6 +102,7 @@ void vesting::on_transfer(name from, name to, asset quantity, std::string memo)
asset converted = convert_to_vesting(quantity, *vesting);
table_vesting.modify(vesting, name(), [&](auto& item) {
item.supply += converted;
// TODO Add notify about supply changes
});

add_balance(recipient != name() ? recipient : from, converted, has_auth(to) ? to : from);
Expand Down Expand Up @@ -129,6 +131,7 @@ void vesting::retire(asset quantity, name user) {
sub_balance(user, quantity, true);
table_vesting.modify(vesting, name(), [&](auto& item) {
item.supply -= quantity;
// TODO Add notify about supply changes
});
}

Expand Down Expand Up @@ -234,6 +237,7 @@ void vesting::delegate_vesting(name sender, name recipient, asset quantity, uint

account_sender.modify(balance_sender, sender, [&](auto& item){
item.delegate_vesting += quantity;
// TODO Add notify about vesting changed
});

tables::delegate_table table(_self, sname);
Expand Down Expand Up @@ -265,6 +269,7 @@ void vesting::delegate_vesting(name sender, name recipient, asset quantity, uint
eosio_assert(balance_recipient != account_recipient.end(), "Not found balance token vesting");
account_recipient.modify(balance_recipient, sender, [&](auto& item){
item.received_vesting += quantity;
// TODO Add notify about vesting changed
});

eosio_assert(balance_recipient->received_vesting.amount >= delegation_params.min_remainder, "delegated vesting withdrawn");
Expand Down Expand Up @@ -307,6 +312,7 @@ void vesting::undelegate_vesting(name sender, name recipient, asset quantity) {
eosio_assert(balance != account_recipient.end(), "This token is not on the recipient balance sheet");
account_recipient.modify(balance, sender, [&](auto& item){
item.received_vesting -= quantity;
// TODO Add notify about vesting changed
});

eosio_assert(balance->received_vesting.amount >= delegation_params.min_remainder, "delegated vesting withdrawn");
Expand All @@ -322,6 +328,7 @@ void vesting::create(symbol symbol, name notify_acc) {
table_vesting.emplace(_self, [&](auto& item){
item.supply = asset(0, symbol);
item.notify_acc = notify_acc;
// TODO Add notify about supply changes
});
}

Expand Down Expand Up @@ -363,6 +370,7 @@ void vesting::calculate_convert_vesting() {
eosio_assert(vest != table_vesting.end(), "Vesting not found"); // must not happen at this point
table_vesting.modify(vest, name(), [&](auto& item) {
item.supply -= quantity;
// TODO Add notify about supply change
});
INLINE_ACTION_SENDER(eosio::token, transfer)(config::token_name, {_self, config::active_name},
{_self, obj->recipient, convert_to_token(quantity, *vest), "Convert vesting"});
Expand Down Expand Up @@ -390,6 +398,7 @@ void vesting::calculate_delegate_vesting() {
eosio_assert(balance_recipient != account_recipient.end(), "This token is not on the sender balance sheet");
account_recipient.modify(balance_recipient, name(), [&](auto &item){
item.delegate_vesting -= obj->amount;
// TODO Add event about vesting changed
});

obj = index.erase(obj);
Expand Down Expand Up @@ -446,6 +455,7 @@ void vesting::sub_balance(name owner, asset value, bool retire_mode) {
a.vesting -= value;
if (retire_mode)
a.unlocked_limit -= value;
// Add notify about vesting changed
});
notify_balance_change(owner, -value);
}
Expand All @@ -461,15 +471,25 @@ void vesting::add_balance(name owner, asset value, name ram_payer) {
a.vesting = value;
a.delegate_vesting.symbol = value.symbol;
a.received_vesting.symbol = value.symbol;
// TODO Add notify about vesting changed
});
} else {
account.modify(to, ram_payer, [&](auto& a) {
a.vesting += value;
// TODO Add notify about vesting changed
});
}
notify_balance_change(owner, value);
}

void vesting::send_account_event(name account, const structures::user_balance& balance) {
eosio::event(_self, "balance"_n, std::make_tuple(account, balance)).send();
}

void vesting::send_vesting_event(const structures::vesting_info& info) {
eosio::event(_self, "vesting"_n, info.supply).send();
}

int64_t fix_precision(const asset from, const symbol to) {
int a = from.symbol.precision();
int b = to.precision();
Expand Down
2 changes: 2 additions & 0 deletions golos.vesting/golos.vesting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class vesting : public contract {
void notify_balance_change(name owner, asset diff);
void sub_balance(name owner, asset value, bool retire_mode = false);
void add_balance(name owner, asset value, name ram_payer);
void send_account_event(name account, const structures::user_balance& balance);
void send_vesting_event(const structures::vesting_info& info);

const asset convert_to_token(const asset& vesting, const structures::vesting_info& vinfo) const;
const asset convert_to_vesting(const asset& token, const structures::vesting_info& vinfo) const;
Expand Down

0 comments on commit 8c1efca

Please sign in to comment.