more work on trade command

This commit is contained in:
Crypto City 2019-08-25 01:55:44 +00:00
parent c2812b6793
commit 2c88df3028
2 changed files with 57 additions and 2 deletions

View File

@ -182,6 +182,7 @@ bool cc_command_handler_trade::check(const cryptonote::BlockchainDB &db, const c
if (trade.bid)
{
// we pay, they sell
CHECK_AND_ASSERT_MES(trade.price >= matched_trade.price, false, "Matching with non matching price");
const uint64_t trade_cost_per_unit = matched_trade.price;
uint64_t trade_balance;
CHECK_AND_ASSERT_MES(get_account_balance(db, trade.cc_account, trade_balance), false, "Failed to get balance");
@ -227,6 +228,7 @@ bool cc_command_handler_trade::check(const cryptonote::BlockchainDB &db, const c
else
{
// we sell, they pay
CHECK_AND_ASSERT_MES(trade.price <= matched_trade.price, false, "Matching with non matching price");
const uint64_t matched_cost_per_unit = matched_trade.price;
uint64_t matched_balance;
CHECK_AND_ASSERT_MES(get_account_balance(db, matched_trade.cc_account, matched_balance), false, "Failed to get balance");

View File

@ -618,6 +618,10 @@ class CCTest():
assert res.balance == expected_balances[i]
assert res.block_balances == expected_block_balances[i]
# current orders
# 2 sells 3 at 80
# 1 buys 2 at 80
# matching to a non existing tx
ok = False
try: self.wallet[0].cc_trade_blocks(True, 1, 1, 80, 1500, matches = [{'txid': "4"*64, 'amount': 1}], cost = 80)
@ -634,16 +638,65 @@ class CCTest():
# matching with non matching price
ok = False
try: self.wallet[0].cc_trade_blocks(False, 1, 1, 81, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 1}], cost = 80)
try: self.wallet[0].cc_trade_blocks(True, 1, 1, 79, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 1}], cost = 80)
except: ok = True
assert ok
# matching too much
ok = False
try: self.wallet[0].cc_trade_blocks(False, 1, 10, 80, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 1}], cost = 800)
try: self.wallet[0].cc_trade_blocks(True, 1, 5, 80, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 5}], cost = 800)
except: ok = True
assert ok
# matching nothing
ok = False
try: self.wallet[0].cc_trade_blocks(True, 1, 1, 80, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 0}], cost = 1)
except: ok = True
assert ok
# matching the same tx twice
ok = False
try: self.wallet[0].cc_trade_blocks(True, 1, 2, 80, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 1}, {'txid': offer_1_5_80_txid, 'amount': 1}], cost = 160)
except: ok = True
assert ok
# matching with oneself's order
ok = False
try: self.wallet[2].cc_trade_blocks(True, 1, 1, 80, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 1}], cost = 80)
except: ok = True
assert ok
# matching with a better price uses the matched trade's price
res = self.wallet[0].cc_trade_blocks(True, 1, 1, 140, 1500, matches = [{'txid': offer_1_5_80_txid, 'amount': 1}], cost = 80)
assert len(res.tx_hash) == 64
expected_balances[0] -= res.fee
expected_balances[0] -= 80
expected_balances[2] += 80
expected_block_balances[0][1] += 1
expected_block_balances[2][1] -= 1
res = self.wallet[2].cc_trade_blocks(False, 1, 1, 40, 1500, matches = [{'txid': bid_1_2_80_a_txid, 'amount': 1}], cost = 0)
assert len(res.tx_hash) == 64
expected_balances[2] -= res.fee
expected_balances[2] += 80
expected_balances[1] -= 80
expected_block_balances[2][1] -= 1
expected_block_balances[1][1] += 1
daemon.generateblocks('CC1MM8HqWBathu8hS5mwMNHm1da3cZCzg2rkqWLKCxpUarKtPszP3MjiocrJeLvph4AghgYu1AXonCmckfEuyE8Q2FFm8jNdiz3', 1)
expected_balances[1] -= bid_1_2_80_a_fee # finally!
daemon.generateblocks('CC1MM8HqWBathu8hS5mwMNHm1da3cZCzg2rkqWLKCxpUarKtPszP3MjiocrJeLvph4AghgYu1AXonCmckfEuyE8Q2FFm8jNdiz3', 1)
# check balances
for i in range(3):
self.wallet[i].refresh()
res = self.wallet[i].cc_get_info()
account_id = res.account_id
res = daemon.cc_get_account(account_id)
assert res.balance == expected_balances[i]
assert res.block_balances == expected_block_balances[i]
if __name__ == '__main__':
CCTest().run_test()