adding cic-eth as sub dir

This commit is contained in:
2021-02-01 09:12:51 -08:00
parent ed3991e997
commit a4587deac5
317 changed files with 819441 additions and 0 deletions

View File

@@ -0,0 +1,129 @@
# standard imports
import logging
# third-party imports
import pytest
from cic_registry import CICRegistry
# local imports
from cic_eth.eth.bancor import BancorTxFactory, unpack_convert
from cic_eth.eth.bancor import resolve_converters_by_tokens
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.queue.tx import create as queue_create
from cic_eth.eth.bancor import otx_cache_convert
from cic_eth.db.models.otx import Otx
from cic_eth.db.models.tx import TxCache
logg = logging.getLogger()
def test_resolve_converters_by_tokens(
cic_registry,
init_w3,
bancor_tokens,
bancor_registry,
default_chain_spec,
):
r = resolve_converters_by_tokens(
[
{
'address': bancor_tokens[0],
},
{
'address': bancor_tokens[1],
},
],
str(default_chain_spec),
)
logg.warning('this test should be hardened by verifying the converters')
for t in r:
assert t['converters'] != None
assert len(t['converters']) == 1
def test_unpack_convert(
default_chain_spec,
cic_registry,
init_w3,
init_rpc,
bancor_tokens,
bancor_registry,
):
txf = BancorTxFactory(init_w3.eth.accounts[0], init_rpc)
default_reserve = CICRegistry.get_contract(default_chain_spec, 'BNTToken')
convert_tx = txf.convert(
bancor_tokens[0],
bancor_tokens[1],
default_reserve.address(),
42,
13,
default_chain_spec,
)
s = init_w3.eth.sign_transaction(convert_tx)
s_bytes = bytes.fromhex(s['raw'][2:])
tx_dict = unpack_signed_raw_tx(s_bytes, default_chain_spec.chain_id())
convert_contract = CICRegistry.get_contract(default_chain_spec, 'BancorNetwork')
assert tx_dict['from'] == init_w3.eth.accounts[0]
assert tx_dict['to'] == convert_contract.address()
assert tx_dict['value'] == 0
convert_data = unpack_convert(tx_dict['data'])
assert convert_data['amount'] == 42
assert convert_data['min_return'] == 13
assert convert_data['source_token'] == bancor_tokens[0]
assert convert_data['destination_token'] == bancor_tokens[1]
assert convert_data['fee_recipient'] == '0000000000000000000000000000000000000000000000000000000000000000'
assert convert_data['fee'] == 0
def test_queue_cache_convert(
default_chain_spec,
init_w3,
init_rpc,
init_database,
cic_registry,
bancor_registry,
bancor_tokens,
):
txf = BancorTxFactory(init_w3.eth.accounts[0], init_rpc)
amount = 42
min_return = 13
default_reserve = CICRegistry.get_contract(default_chain_spec, 'BNTToken', 'ERC20')
transfer_tx = txf.convert(
bancor_tokens[0],
bancor_tokens[1],
default_reserve.address(),
amount,
min_return,
default_chain_spec,
)
tx_signed = init_w3.eth.sign_transaction(transfer_tx)
tx_hash = init_w3.eth.sendRawTransaction(tx_signed['raw'])
tx_hash_hex = tx_hash.hex()
nonce = int(tx_signed['nonce'][2:], 16)
tx_hash_queue = queue_create(nonce, init_w3.eth.accounts[0], tx_hash_hex, tx_signed['raw'], str(default_chain_spec))
tx_hash_cache = otx_cache_convert(tx_hash_hex, tx_signed['raw'], str(default_chain_spec))
assert tx_hash_hex == tx_hash_queue
assert tx_hash_hex == tx_hash_cache
session = Otx.create_session()
otx = session.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
assert otx.tx_hash == tx_hash_hex
txc = session.query(TxCache).filter(TxCache.otx_id==otx.id).first()
assert txc.sender == init_w3.eth.accounts[0]
assert txc.recipient == init_w3.eth.accounts[0]
assert txc.source_token_address == bancor_tokens[0]
assert txc.destination_token_address == bancor_tokens[1]
assert txc.values() == (amount, amount)

View File

@@ -0,0 +1,51 @@
# standard imports
import logging
# local imports
from cic_eth.eth.nonce import NonceOracle
logg = logging.getLogger()
def test_nonce_sequence(
eth_empty_accounts,
init_database,
init_rpc,
):
account= init_rpc.w3.eth.personal.new_account('')
no = NonceOracle(account, 0)
n = no.next()
assert n == 0
n = no.next()
assert n == 1
init_rpc.w3.eth.sendTransaction({
'from': init_rpc.w3.eth.accounts[0],
'to': account,
'value': 200000000,
})
init_rpc.w3.eth.sendTransaction({
'from': account,
'to': eth_empty_accounts[0],
'value': 100,
})
c = init_rpc.w3.eth.getTransactionCount(account, 'pending')
logg.debug('nonce {}'.format(c))
account= init_rpc.w3.eth.personal.new_account('')
no = NonceOracle(account, c)
n = no.next()
assert n == 1
n = no.next()
assert n == 2
# try with bogus value
no = NonceOracle(account, 4)
n = no.next()
assert n == 3

View File

@@ -0,0 +1,24 @@
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.eth.task import sign_tx
def test_unpack(
init_rpc,
w3,
):
tx = {
'from': w3.eth.accounts[1],
'to': w3.eth.accounts[0],
'nonce': 0,
'value': 1024,
'gas': 21000,
'gasPrice': 200000000,
'data': '0x',
'chainId': 8995,
}
(tx_hash, tx_raw) = sign_tx(tx, 'Foo:8995')
tx_recovered = unpack_signed_raw_tx(bytes.fromhex(tx_raw[2:]), 8995)
assert tx_hash == tx_recovered['hash']

View File

@@ -0,0 +1,88 @@
# standard imports
import logging
# third-party imports
import pytest
from cic_registry import CICRegistry
# local imports
from cic_eth.eth.token import TokenTxFactory, unpack_transfer, otx_cache_transfer
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.queue.tx import create as queue_create
from cic_eth.db.models.otx import Otx
from cic_eth.db.models.tx import TxCache
logg = logging.getLogger()
def test_unpack_transfer(
default_chain_spec,
init_w3,
init_rpc,
cic_registry,
bancor_tokens,
bancor_registry,
):
source_token = CICRegistry.get_address(default_chain_spec, bancor_tokens[0])
logg.debug('bancor tokens {} {}'.format(bancor_tokens, source_token))
txf = TokenTxFactory(init_w3.eth.accounts[0], init_rpc)
transfer_tx = txf.transfer(
source_token.address(),
init_w3.eth.accounts[1],
42,
default_chain_spec,
)
s = init_w3.eth.sign_transaction(transfer_tx)
s_bytes = bytes.fromhex(s['raw'][2:])
tx_dict = unpack_signed_raw_tx(s_bytes, default_chain_spec.chain_id())
assert tx_dict['from'] == init_w3.eth.accounts[0]
assert tx_dict['to'] == bancor_tokens[0]
assert tx_dict['value'] == 0
transfer_data = unpack_transfer(tx_dict['data'])
assert transfer_data['to'] == init_w3.eth.accounts[1]
assert transfer_data['amount'] == 42
def test_queue_cache_transfer(
default_chain_spec,
init_w3,
init_rpc,
init_database,
cic_registry,
bancor_tokens,
bancor_registry,
):
source_token = CICRegistry.get_address(default_chain_spec, bancor_tokens[0])
txf = TokenTxFactory(init_w3.eth.accounts[0], init_rpc)
value = 42
transfer_tx = txf.transfer(
source_token.address(),
init_w3.eth.accounts[1],
value,
default_chain_spec,
)
tx_signed = init_w3.eth.sign_transaction(transfer_tx)
tx_hash = init_w3.eth.sendRawTransaction(tx_signed['raw'])
tx_hash_hex = tx_hash.hex()
nonce = int(tx_signed['nonce'][2:], 16)
tx_hash_queue = queue_create(nonce, init_w3.eth.accounts[0], tx_hash_hex, tx_signed['raw'], str(default_chain_spec))
tx_hash_cache = otx_cache_transfer(tx_hash_hex, tx_signed['raw'], str(default_chain_spec))
assert tx_hash_hex == tx_hash_queue
assert tx_hash_hex == tx_hash_cache
session = Otx.create_session()
otx = session.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
assert otx.tx_hash == tx_hash_hex
txc = session.query(TxCache).filter(TxCache.otx_id==otx.id).first()
assert txc.sender == init_w3.eth.accounts[0]
assert txc.recipient == init_w3.eth.accounts[1]
assert txc.source_token_address == bancor_tokens[0]
assert txc.destination_token_address == bancor_tokens[0]
assert txc.values() == (value, value)