Skip to content
This repository has been archived by the owner on Dec 11, 2021. It is now read-only.

Commit

Permalink
Fixing 3 day delay
Browse files Browse the repository at this point in the history
  • Loading branch information
alepacheco committed Jul 6, 2018
1 parent 02ef92e commit 0dd00b1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 42 deletions.
30 changes: 16 additions & 14 deletions src/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@ void resource_exchange::deposit(currency::transfer tx) {
void resource_exchange::withdraw(account_name to, asset quantity) {
eosio_assert(quantity.is_valid(), "invalid quantity");
eosio_assert(quantity.amount > 0, "must withdraw positive quantity");
asset liquid_funds = contract_balance.find(asset().symbol.name())->balance;
if (quantity > liquid_funds) {
// TODO check if funds on refund, else force overdraft
// retry next cycle
eosio::transaction out;
out.actions.emplace_back(
permission_level(to, N(active)), _self, N(withdraw),
std::make_tuple(to, quantity));
auto state = contract_state.get();

if (quantity > state.liquid_funds) {
if (quantity > state.get_unstaked()) {
// force overdraft
} else {
// retry next cycle
eosio::transaction out;
out.actions.emplace_back(permission_level(to, N(active)), _self,
N(withdraw), std::make_tuple(to, quantity));

out.delay_sec = CYCLE_TIME + 100;
out.send(to, _contract);
out.delay_sec = CYCLE_TIME + 100;
out.send(to, _contract);
return;
}
}

auto state = contract_state.get();
auto itr = accounts.find(to);
eosio_assert(itr != accounts.end(), "unknown account");

Expand All @@ -57,9 +60,8 @@ void resource_exchange::withdraw(account_name to, asset quantity) {
state_on_withdraw(quantity);

action(permission_level(_contract, N(active)), N(eosio.token), N(transfer),
std::make_tuple(_contract, to, quantity, std::string("")))
.send();

std::make_tuple(_contract, to, quantity, std::string("")))
.send();

if (itr->is_empty()) {
accounts.erase(itr);
Expand Down
1 change: 0 additions & 1 deletion src/pricing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void resource_exchange::payreward(account_name user, asset fee_collected) {
}
}


asset resource_exchange::billaccount(account_name owner,
double cost_per_token) {
auto acnt = accounts.find(owner);
Expand Down
4 changes: 3 additions & 1 deletion src/resource_exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void resource_exchange::cycle() {

// if ((state.timestamp.utc_seconds + secs_to_next.utc_seconds) >
// this_time.utc_seconds) {
// out.delay_sec = (state.timestamp.utc_seconds + secs_to_next.utc_seconds) -
// out.delay_sec = (state.timestamp.utc_seconds + secs_to_next.utc_seconds)
// -
// (this_time.utc_seconds + secs_flexibility.utc_seconds);
// out.send(this_time.utc_seconds, _contract);
// return;
Expand All @@ -89,6 +90,7 @@ void resource_exchange::docycle() {
payreward(acnt->owner, fees_collected);
}
unstakeunknown();
state_cycle();

print("Total fees: ", fees_collected, " ");
}
Expand Down
16 changes: 13 additions & 3 deletions src/resource_exchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ class resource_exchange : public eosio::contract {
asset liquid_funds;
asset total_stacked;
time_point_sec timestamp;
asset to_be_refunding;
asset refunding;

asset get_total() const { return liquid_funds + total_stacked; }
EOSLIB_SERIALIZE(state_t, (liquid_funds)(total_stacked)(timestamp))
asset get_total() const {
return liquid_funds + total_stacked + to_be_refunding + refunding;
}
asset get_unstaked() const {
return liquid_funds + to_be_refunding + refunding;
}
EOSLIB_SERIALIZE(state_t, (liquid_funds)(total_stacked)(timestamp)(
to_be_refunding)(refunding))
};

//@abi table account i64
Expand Down Expand Up @@ -108,11 +116,13 @@ class resource_exchange : public eosio::contract {
void state_on_deposit(asset quantity);
void state_on_withdraw(asset quantity);
void state_set_timestamp(time_point_sec this_time);
void state_on_sellstake(asset stake);
void state_on_sellstake(asset stake_from_account, asset stake_from_tx);
void state_on_buystake(asset stake);
void state_on_reset_account(asset account_res);
void state_on_undelegate_unknown(asset delegated);
void state_unstake_delayed(asset amount);
void state_change(asset liquid, asset staked);
void state_cycle();
void state_init();

void docycle();
Expand Down
13 changes: 8 additions & 5 deletions src/stake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void resource_exchange::sellstake(account_name user, asset net, asset cpu) {
acnt.resource_cpu -= usr_cpu;
});
net_from_account += usr_net;
cpu_from_account += cpu_net;
cpu_from_account += usr_cpu;
} else {
auto amount_tx_net = pending_itr->net - net;
auto amount_tx_cpu = pending_itr->cpu - cpu;
Expand All @@ -101,7 +101,7 @@ void resource_exchange::sellstake(account_name user, asset net, asset cpu) {
cpu_from_tx += pending_itr->cpu;
accounts.modify(itr, 0,
[&](auto& acnt) { acnt.resource_cpu -= -amount_tx_cpu; });
cpu_from_account += -amount_tx_cpu;
cpu_from_account += -amount_tx_cpu;
}

if (amount_tx_net >= asset(0)) {
Expand All @@ -113,16 +113,19 @@ void resource_exchange::sellstake(account_name user, asset net, asset cpu) {
net_from_tx += pending_itr->net;
accounts.modify(itr, 0,
[&](auto& acnt) { acnt.resource_net -= -amount_tx_net; });
net_from_account += -amount_tx_net;
net_from_account += -amount_tx_net;
}

if (pending_itr->is_empty()) {
pendingtxs.erase(pending_itr);
}
}

eosio_assert((net+cpu) == (net_from_account+cpu_from_account+net_from_tx+cpu_from_tx), "sold stake calculation error");
state_on_sellstake(net_from_account+cpu_from_account, net_from_tx+cpu_from_tx);
eosio_assert((net + cpu) == (net_from_account + cpu_from_account +
net_from_tx + cpu_from_tx),
"sold stake calculation error");
state_on_sellstake(net_from_account + cpu_from_account,
net_from_tx + cpu_from_tx);
}

} // namespace eosio
50 changes: 32 additions & 18 deletions src/state_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@
namespace eosio {
void resource_exchange::state_init() {
if (!contract_state.exists()) {
contract_state.set(state_t{asset(0), asset(0), time_point_sec(0)},
_contract);
contract_state.set(
state_t{asset(0), asset(0), time_point_sec(0), asset(0), asset(0)},
_contract);
}
}

void resource_exchange::state_change(asset liquid, asset staked) {
auto state = contract_state.get();
contract_state.set(state_t{state.liquid_funds + liquid,
state.total_stacked + staked, state.timestamp},
_self);
contract_state.set(
state_t{state.liquid_funds + liquid, state.total_stacked + staked,
state.timestamp, state.to_be_refunding, state.refunding},
_self);
}

void resource_exchange::state_unstake_delayed(asset amount) {
eosio_assert(amount >= asset(0), "must use positive amount");
auto state = contract_state.get();
contract_state.set(
state_t{state.liquid_funds, state.total_stacked - amount, state.timestamp,
state.to_be_refunding + amount, state.refunding},
_self);
}

void resource_exchange::state_cycle() {
auto state = contract_state.get();
contract_state.set(
state_t{state.liquid_funds + state.refunding, state.total_stacked,
state.timestamp, asset(0), state.to_be_refunding},
_self);
}

void resource_exchange::state_on_deposit(asset quantity) {
Expand All @@ -31,32 +50,27 @@ void resource_exchange::reset_delayed_tx(pendingtx tx) {

void resource_exchange::state_set_timestamp(time_point_sec this_time) {
auto state = contract_state.get();
contract_state.set(
state_t{state.liquid_funds, state.total_stacked, this_time}, _self);
contract_state.set(state_t{state.liquid_funds, state.total_stacked, this_time,
state.to_be_refunding, state.refunding},
_self);
}


// TODO 3 days delay
void resource_exchange::state_on_undelegate_unknown(asset delegated) {
state_change(delegated, -delegated);
state_unstake_delayed(delegated);
}

// TODO 3 days delay
void resource_exchange::state_on_reset_account(asset account_res) {
state_change(account_res, -account_res);
state_unstake_delayed(account_res);
}

void resource_exchange::state_on_buystake(asset stake) {
// Mark as staked, but it can later fail if not enought funds
state_change(-stake, stake);
}


void resource_exchange::state_on_sellstake(asset stake_from_account, asset stake_from_tx) {
void resource_exchange::state_on_sellstake(asset stake_from_account,
asset stake_from_tx) {
state_change(stake_from_tx, -stake_from_tx);

// TODO 3 days delay (to count on next cycle)
state_change(stake_from_account, -stake_from_account);
state_unstake_delayed(stake_from_account);
}

} // namespace eosio

0 comments on commit 0dd00b1

Please sign in to comment.