Skip to content

Commit

Permalink
Merge pull request #47 from EOSLaoMao/feature/stripe-order
Browse files Browse the repository at this point in the history
implement customized order action
  • Loading branch information
datudou authored Mar 25, 2019
2 parents 3523cf0 + d13931b commit dffdaac
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions scripts/dev/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cleos -u $API system newaccount voter3 stakedincome EOS6MRyAjQq8ud7hVNYcfnVPJqcV
sleep 1
cleos -u $API system newaccount voter3 masktransfer EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV --buy-ram "100 EOS" --stake-cpu "100 EOS" --stake-net "100 EOS"
sleep 1
cleos -u $API system newaccount voter3 fundstostake EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV --buy-ram "100 EOS" --stake-cpu "100 EOS" --stake-net "100 EOS"
sleep 1

cleos -u $API set contract masktransfer proxytoken
sleep 1
Expand Down
78 changes: 76 additions & 2 deletions src/bankofstaked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,81 @@ class [[eosio::contract]] bankofstaked : contract
});
}

[[eosio::action]]
void customorder(name beneficiary, asset quantity, asset cpu, asset net, int64_t duration)
{
require_auth(CODE_ACCOUNT);
bool is_free = false;

//validate plan, is_active should be TRUE
eosio_assert( is_account( beneficiary ), "to account does not exist");

//get active creditor
name creditor = get_active_creditor(is_free);

//plan is not free, make sure creditor has enough balance to delegate
asset to_delegate = cpu + net;
if(get_balance(creditor) < to_delegate) {
creditor = get_qualified_paid_creditor(to_delegate);
}

//make sure creditor is a valid account
eosio_assert( is_account( creditor ), "creditor account does not exist");

//validate beneficiary
//1. beneficiary shouldnt be CODE_ACCOUNT
//2. beneficiary shouldnt be in blacklist
//3. each beneficiary could only have 5 affective orders at most
validate_beneficiary(beneficiary, creditor, is_free);

//INLINE ACTION to delegate CPU&NET for beneficiary account
if (is_safe_creditor(creditor)) {
INLINE_ACTION_SENDER(safedelegatebw, delegatebw)
(creditor, {{creditor, "creditorperm"_n}}, {beneficiary, net, cpu});
} else {
INLINE_ACTION_SENDER(eosiosystem::system_contract, delegatebw)
(EOSIO, {{creditor, "creditorperm"_n}}, {creditor, beneficiary, net, cpu, false});
}

//INLINE ACTION to call check action of `bankofstaked`
INLINE_ACTION_SENDER(bankofstaked, check)
(CODE_ACCOUNT, {{CODE_ACCOUNT, "bankperm"_n}}, {creditor});

// add cpu_staked&net_staked to creditor entry
creditor_table c(CODE_ACCOUNT, SCOPE);
auto creditor_itr = c.find(creditor.value);
c.modify(creditor_itr, RAM_PAYER, [&](auto &i) {
i.cpu_staked += cpu;
i.net_staked += net;
i.balance = get_balance(creditor);
i.updated_at = now();
});

//create Order entry
uint64_t order_id;
order_table o(CODE_ACCOUNT, SCOPE);
o.emplace(RAM_PAYER, [&](auto &i) {
i.id = o.available_primary_key();
i.buyer = CODE_ACCOUNT;
i.price = quantity;
i.creditor = creditor;
i.beneficiary = beneficiary;
i.plan_id = std::numeric_limits<uint64_t>::max();
i.cpu_staked = cpu;
i.net_staked = net;
i.is_free = is_free;
i.created_at = now();
i.expire_at = now() + duration * SECONDS_PER_MIN;

order_id = i.id;
});

//deferred transaction to auto undelegate after expired
std::vector<uint64_t> order_ids;
order_ids.emplace_back(order_id);
undelegate(order_ids, duration);
}

//token received
void received_token(name from, name to, asset quantity, string memo)
{
Expand Down Expand Up @@ -587,8 +662,6 @@ class [[eosio::contract]] bankofstaked : contract
//if order is_free is not free, transfer income to creditor
if (order.is_free == FALSE)
{
auto plan = p.get(order.plan_id);

auto username = get_recipient(order.creditor);
std::string recipient_name = username.to_string();
std::string memo = recipient_name + " bankofstaked income";
Expand Down Expand Up @@ -657,6 +730,7 @@ extern "C" {
(activateplan)
(setrecipient)
(delrecipient)
(customorder)
)
}
}
Expand Down

0 comments on commit dffdaac

Please sign in to comment.