2021-02-17 11:04:21 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
2021-04-04 14:40:59 +02:00
|
|
|
# external imports
|
2021-02-17 11:04:21 +01:00
|
|
|
import pytest
|
2021-04-04 14:40:59 +02:00
|
|
|
from chainqueue.db.models.otx import Otx
|
|
|
|
from chainqueue.db.models.tx import TxCache
|
2021-02-17 11:04:21 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_eth.queue.balance import (
|
|
|
|
balance_outgoing,
|
|
|
|
balance_incoming,
|
|
|
|
assemble_balances,
|
|
|
|
)
|
2021-08-28 13:10:18 +02:00
|
|
|
from cic_eth.encode import tx_normalize
|
2021-02-17 11:04:21 +01:00
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
def test_assemble():
|
|
|
|
|
|
|
|
token_foo = '0x' + os.urandom(20).hex()
|
|
|
|
token_bar = '0x' + os.urandom(20).hex()
|
|
|
|
b = [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'address': token_foo,
|
|
|
|
'converters': [],
|
|
|
|
'balance_foo': 42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'address': token_bar,
|
|
|
|
'converters': [],
|
|
|
|
'balance_baz': 666,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'address': token_foo,
|
|
|
|
'converters': [],
|
|
|
|
'balance_bar': 13,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
'address': token_bar,
|
|
|
|
'converters': [],
|
|
|
|
'balance_xyzzy': 1337,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
r = assemble_balances(b)
|
|
|
|
logg.debug('r {}'.format(r))
|
|
|
|
|
2021-08-28 13:10:18 +02:00
|
|
|
assert r[0]['address'] == tx_normalize.executable_address(token_foo)
|
|
|
|
assert r[1]['address'] == tx_normalize.executable_address(token_bar)
|
2021-02-17 11:04:21 +01:00
|
|
|
assert r[0].get('balance_foo') != None
|
|
|
|
assert r[0].get('balance_bar') != None
|
|
|
|
assert r[1].get('balance_baz') != None
|
|
|
|
assert r[1].get('balance_xyzzy') != None
|
|
|
|
|
|
|
|
|
|
|
|
def test_outgoing_balance(
|
|
|
|
default_chain_spec,
|
|
|
|
init_database,
|
|
|
|
):
|
|
|
|
|
|
|
|
recipient = '0x' + os.urandom(20).hex()
|
|
|
|
tx_hash = '0x' + os.urandom(32).hex()
|
|
|
|
signed_tx = '0x' + os.urandom(128).hex()
|
2021-04-04 14:40:59 +02:00
|
|
|
otx = Otx.add(0, tx_hash, signed_tx, session=init_database)
|
2021-02-17 11:04:21 +01:00
|
|
|
init_database.add(otx)
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
token_address = '0x' + os.urandom(20).hex()
|
|
|
|
sender = '0x' + os.urandom(20).hex()
|
|
|
|
txc = TxCache(
|
2021-08-28 13:10:18 +02:00
|
|
|
tx_normalize.tx_hash(tx_hash),
|
|
|
|
tx_normalize.wallet_address(sender),
|
|
|
|
tx_normalize.wallet_address(recipient),
|
|
|
|
tx_normalize.executable_address(token_address),
|
|
|
|
tx_normalize.executable_address(token_address),
|
2021-02-17 11:04:21 +01:00
|
|
|
1000,
|
|
|
|
1000,
|
2021-04-04 14:40:59 +02:00
|
|
|
session=init_database,
|
2021-02-17 11:04:21 +01:00
|
|
|
)
|
|
|
|
init_database.add(txc)
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
token_data = {
|
|
|
|
'address': token_address,
|
|
|
|
'converters': [],
|
|
|
|
}
|
2021-04-04 14:40:59 +02:00
|
|
|
b = balance_outgoing([token_data], sender, default_chain_spec.asdict())
|
2021-02-17 11:04:21 +01:00
|
|
|
assert b[0]['balance_outgoing'] == 1000
|
|
|
|
|
2021-04-04 14:40:59 +02:00
|
|
|
otx.readysend(session=init_database)
|
|
|
|
init_database.flush()
|
|
|
|
otx.reserve(session=init_database)
|
|
|
|
init_database.flush()
|
2021-02-17 11:04:21 +01:00
|
|
|
otx.sent(session=init_database)
|
|
|
|
init_database.commit()
|
|
|
|
|
2021-04-04 14:40:59 +02:00
|
|
|
b = balance_outgoing([token_data], sender, default_chain_spec.asdict())
|
2021-02-17 11:04:21 +01:00
|
|
|
assert b[0]['balance_outgoing'] == 1000
|
|
|
|
|
|
|
|
otx.success(block=1024, session=init_database)
|
|
|
|
init_database.commit()
|
|
|
|
|
2021-04-04 14:40:59 +02:00
|
|
|
b = balance_outgoing([token_data], sender, default_chain_spec.asdict())
|
2021-02-17 11:04:21 +01:00
|
|
|
assert b[0]['balance_outgoing'] == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_incoming_balance(
|
|
|
|
default_chain_spec,
|
|
|
|
init_database,
|
|
|
|
):
|
|
|
|
|
|
|
|
recipient = '0x' + os.urandom(20).hex()
|
|
|
|
tx_hash = '0x' + os.urandom(32).hex()
|
|
|
|
signed_tx = '0x' + os.urandom(128).hex()
|
2021-04-04 14:40:59 +02:00
|
|
|
otx = Otx.add(0, tx_hash, signed_tx, session=init_database)
|
2021-02-17 11:04:21 +01:00
|
|
|
init_database.add(otx)
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
token_address = '0x' + os.urandom(20).hex()
|
|
|
|
sender = '0x' + os.urandom(20).hex()
|
|
|
|
txc = TxCache(
|
2021-08-28 13:10:18 +02:00
|
|
|
tx_normalize.tx_hash(tx_hash),
|
|
|
|
tx_normalize.wallet_address(sender),
|
|
|
|
tx_normalize.wallet_address(recipient),
|
|
|
|
tx_normalize.executable_address(token_address),
|
|
|
|
tx_normalize.executable_address(token_address),
|
2021-02-17 11:04:21 +01:00
|
|
|
1000,
|
|
|
|
1000,
|
2021-04-04 14:40:59 +02:00
|
|
|
session=init_database,
|
2021-02-17 11:04:21 +01:00
|
|
|
)
|
|
|
|
init_database.add(txc)
|
|
|
|
init_database.commit()
|
|
|
|
|
|
|
|
token_data = {
|
|
|
|
'address': token_address,
|
|
|
|
'converters': [],
|
|
|
|
}
|
2021-04-04 14:40:59 +02:00
|
|
|
b = balance_incoming([token_data], recipient, default_chain_spec.asdict())
|
2022-01-04 17:01:01 +01:00
|
|
|
assert b[0]['balance_incoming'] == 1000
|
2021-02-17 11:04:21 +01:00
|
|
|
|
2021-04-04 14:40:59 +02:00
|
|
|
otx.readysend(session=init_database)
|
|
|
|
init_database.flush()
|
|
|
|
otx.reserve(session=init_database)
|
|
|
|
init_database.flush()
|
2021-02-17 11:04:21 +01:00
|
|
|
otx.sent(session=init_database)
|
|
|
|
init_database.commit()
|
|
|
|
|
2022-01-04 17:01:01 +01:00
|
|
|
#b = balance_incoming([token_data], recipient, default_chain_spec.asdict())
|
|
|
|
#assert b[0]['balance_incoming'] == 1000
|
2021-02-17 11:04:21 +01:00
|
|
|
|
|
|
|
otx.success(block=1024, session=init_database)
|
|
|
|
init_database.commit()
|
|
|
|
|
2021-04-04 14:40:59 +02:00
|
|
|
b = balance_incoming([token_data], recipient, default_chain_spec.asdict())
|
2021-02-17 11:04:21 +01:00
|
|
|
assert b[0]['balance_incoming'] == 0
|
|
|
|
|
|
|
|
|
|
|
|
|