From 5b6aa6c50ebe5c97100ab243146c713a2bab36f5 Mon Sep 17 00:00:00 2001 From: ZhaoYu Date: Sat, 20 Oct 2018 21:56:51 +0900 Subject: [PATCH 1/2] deferred check --- src/bankofstaked.cpp | 18 ++++++++++++++++++ src/utils.cpp | 17 +++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/bankofstaked.cpp b/src/bankofstaked.cpp index 11d2cfb..63f9a17 100644 --- a/src/bankofstaked.cpp +++ b/src/bankofstaked.cpp @@ -442,6 +442,23 @@ class bankofstaked : contract out.send((uint128_t(code_account) << 64) | current_time() | nonce, code_account, true); } + //deferred check + void delayed_check(uint64_t nonce) + { + eosio::transaction out; + action act1 = action( + permission_level{ code_account, N(bankperm) }, + code_account, N(check), + std::make_tuple() + ); + out.actions.emplace_back(act1); + + out.delay_sec = 1 * SECONDS_PER_MIN; + out.send((uint128_t(code_account) << 64) | current_time() | nonce, code_account, true); + } + + + //token received void received_token(const currency::transfer &t) { @@ -545,6 +562,7 @@ class bankofstaked : contract std::vector order_ids; order_ids.emplace_back(order_id); undelegate(order_ids, plan->duration); + delayed_check(current_time()); } } }; diff --git a/src/utils.cpp b/src/utils.cpp index 3fbb89d..1eb1ea3 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -44,9 +44,22 @@ namespace utils //get account EOS balance asset get_balance(account_name owner) { + print("creditor get balance called!"); auto symbol = symbol_type(system_token_symbol); eosio::token t(N(eosio.token)); - return t.get_balance(owner, symbol.name()); + auto balance = t.get_balance(owner, symbol.name()); + // update creditor if balance is outdated + creditor_table c(code_account, SCOPE_CREDITOR>>1); + auto creditor_itr = c.find(owner); + print(" | balance in table:", creditor_itr->balance); + print(" | balance queried:", balance); + if(creditor_itr->balance != balance) { + c.modify(creditor_itr, ram_payer, [&](auto &i) { + i.balance = balance; + i.updated_at = now(); + }); + } + return balance; } //rotate active creditor @@ -59,7 +72,7 @@ namespace utils auto idx = c.get_index(); auto itr = idx.begin(); asset free_balance = get_balance(free_creditor); - asset paid_balance = get_balance(free_creditor); + asset paid_balance = get_balance(paid_creditor); auto free_rotated = free_balance.amount > MIN_CREDITOR_BALANCE ?TRUE:FALSE; auto paid_rotated = paid_balance.amount > MIN_CREDITOR_BALANCE ?TRUE:FALSE; From d8548561f1554b823dae05b1472831b480886416 Mon Sep 17 00:00:00 2001 From: ZhaoYu Date: Sat, 20 Oct 2018 22:13:05 +0900 Subject: [PATCH 2/2] fix creditor balance incorrect issue --- src/bankofstaked.cpp | 18 ------------------ src/utils.cpp | 3 --- 2 files changed, 21 deletions(-) diff --git a/src/bankofstaked.cpp b/src/bankofstaked.cpp index 63f9a17..11d2cfb 100644 --- a/src/bankofstaked.cpp +++ b/src/bankofstaked.cpp @@ -442,23 +442,6 @@ class bankofstaked : contract out.send((uint128_t(code_account) << 64) | current_time() | nonce, code_account, true); } - //deferred check - void delayed_check(uint64_t nonce) - { - eosio::transaction out; - action act1 = action( - permission_level{ code_account, N(bankperm) }, - code_account, N(check), - std::make_tuple() - ); - out.actions.emplace_back(act1); - - out.delay_sec = 1 * SECONDS_PER_MIN; - out.send((uint128_t(code_account) << 64) | current_time() | nonce, code_account, true); - } - - - //token received void received_token(const currency::transfer &t) { @@ -562,7 +545,6 @@ class bankofstaked : contract std::vector order_ids; order_ids.emplace_back(order_id); undelegate(order_ids, plan->duration); - delayed_check(current_time()); } } }; diff --git a/src/utils.cpp b/src/utils.cpp index 1eb1ea3..45c7292 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -44,15 +44,12 @@ namespace utils //get account EOS balance asset get_balance(account_name owner) { - print("creditor get balance called!"); auto symbol = symbol_type(system_token_symbol); eosio::token t(N(eosio.token)); auto balance = t.get_balance(owner, symbol.name()); // update creditor if balance is outdated creditor_table c(code_account, SCOPE_CREDITOR>>1); auto creditor_itr = c.find(owner); - print(" | balance in table:", creditor_itr->balance); - print(" | balance queried:", balance); if(creditor_itr->balance != balance) { c.modify(creditor_itr, ram_payer, [&](auto &i) { i.balance = balance;