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

Commit

Permalink
Finished cost pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
alepacheco committed Jun 27, 2018
1 parent 360c506 commit 97d05fd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
46 changes: 37 additions & 9 deletions resource_exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ void resource_exchange::apply(account_name contract, account_name act) {

switch (act) {
case N(transfer): {
eosio_assert(contract == N(eosio.token),
"invalid contract, use eosio.token");
auto tx = unpack_action_data<currency::transfer>();
if (tx.from != _contract) {
deposit(tx);
Expand All @@ -29,6 +31,11 @@ void resource_exchange::apply(account_name contract, account_name act) {
buystake(tx.from, tx.to, tx.net, tx.cpu);
break;
}
case N(calcost): {
auto tx = unpack_action_data<asset>();
calcost(tx);
break;
}
}
}

Expand All @@ -41,7 +48,7 @@ void resource_exchange::deposit(currency::transfer tx) {
auto itr = accounts.find(tx.from);
// Create new account if it doesn't exist
if (itr == accounts.end()) {
itr = accounts.emplace(_self, [&](auto& acnt) { acnt.owner = tx.from; });
itr = accounts.emplace(tx.from, [&](auto& acnt) { acnt.owner = tx.from; });
}

// Update account
Expand Down Expand Up @@ -113,9 +120,10 @@ void resource_exchange::buystake(account_name from, account_name to, asset net,
auto itr = accounts.find(from);
eosio_assert(itr != accounts.end(), "account not found");
// calculate cost based on supply
asset cost = calculate_cost(net, cpu);
asset cost = calcost(net + cpu);
print("Buying: ", net + cpu, " in stake for: ", cost, "\n");
eosio_assert(itr->balance >= cost, "not enough funds on account");

// TODO do this on the next cycle
// deduce account
accounts.modify(itr, 0, [&](auto& acnt) {
Expand All @@ -138,18 +146,38 @@ void resource_exchange::sellstake(account_name from, account_name to, asset net,
// TODO
}

asset resource_exchange::calculate_cost(asset stake_net_quantity,
asset stake_cpu_quantity) {
void resource_exchange::cycle() {
// TODO
// remove sold stake from users (deduce, undelegate)
// finish stake purchase orders (deduce, delegate)
// bill accounts with stake
// pay hodlers
}

// TODO find optimal function
asset resource_exchange::calcost(asset resources) {
eosio_assert(contract_state.exists(), "No contract state available");
int PURCHASE_STEP = 10000; // the lower the more persice but more cpu
int PRICE_TUNE = 100;
auto state = contract_state.get();

uint64_t purchase = stake_net_quantity.amount + stake_cpu_quantity.amount;
uint64_t purchase = resources.amount;
double_t liquid = state.liquid_funds.amount;
double_t total = state.get_total().amount;
double_t cost_per_token =
((total * total / liquid) / (total * 10)); // TODO find optimal function

return asset(cost_per_token * purchase);
// purchases of 1 EOS at a time, to get fair price as the resources get
// scarcer
uint32_t steps = purchase / PURCHASE_STEP;
double_t cost_per_token = 0;
for (int i = 0; i < steps; i++) {
cost_per_token += ((total * total / liquid) /
(total * PRICE_TUNE)); // TODO find optimal function
liquid -= PURCHASE_STEP;
}
cost_per_token = cost_per_token / steps;

asset price = asset(cost_per_token * purchase);
print("price: ", price, "\n");
return price;
}
} // namespace eosio

Expand Down
5 changes: 3 additions & 2 deletions resource_exchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class resource_exchange : public eosio::contract {
void undelegatebw(account_name receiver, asset stake_net_quantity,
asset stake_cpu_quantity);

asset calculate_cost(asset stake_net_quantity, asset stake_cpu_quantity);

public:
resource_exchange(account_name self)
: _contract(self),
Expand Down Expand Up @@ -75,5 +73,8 @@ class resource_exchange : public eosio::contract {

/// @abi action
void withdraw(account_name to, asset quantity);

/// @abi action
asset calcost(asset res);
};
} // namespace eosio

0 comments on commit 97d05fd

Please sign in to comment.