Rehabilitate transfer, approve

Signed-off-by: nolash <dev@holbrook.no>
This commit is contained in:
Louis Holbrook
2021-03-29 13:27:53 +00:00
parent 299385f320
commit b65ab8a0ca
222 changed files with 3272 additions and 809800 deletions

View File

@@ -1,133 +0,0 @@
# 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()
@pytest.mark.skip()
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
@pytest.mark.skip()
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
@pytest.mark.skip()
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.from_value == amount
assert txc.to_value == amount

View File

@@ -1,58 +0,0 @@
# standard imports
import os
import logging
# third-party imports
import web3
from cic_registry import CICRegistry
# local imports
from cic_eth.eth.token import ExtendedTx
logg = logging.getLogger()
def test_extended_token(
default_chain_spec,
dummy_token,
local_cic_registry,
address_declarator,
init_w3,
):
address_foo = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
label_foo = '0x{:<064s}'.format(b'foo'.hex())
address_bar = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
label_bar = '0x{:<064s}'.format(b'bar'.hex())
label_token = '0x{:<064s}'.format(b'toktoktok'.hex())
# TODO: still need to test results with two different tokens
token_contract = init_w3.eth.contract(abi=CICRegistry.abi('ERC20'), address=dummy_token)
token = CICRegistry.add_token(default_chain_spec, token_contract)
declarator = CICRegistry.get_contract(default_chain_spec, 'AddressDeclarator', 'Declarator')
fn = declarator.function('addDeclaration')
fn(address_foo, label_foo).transact({'from': init_w3.eth.accounts[1]})
fn(address_bar, label_bar).transact({'from': init_w3.eth.accounts[1]})
fn(dummy_token, label_token).transact({'from': init_w3.eth.accounts[1]})
tx_hash = '0x' + os.urandom(32).hex()
xtx = ExtendedTx(tx_hash, default_chain_spec)
xtx.set_actors(address_foo, address_bar, [init_w3.eth.accounts[1]])
xtx.set_tokens(dummy_token, 1024)
tx = xtx.to_dict()
logg.debug('tx {}'.format(tx))
assert tx['hash'] == tx_hash
assert tx['source_token'] == dummy_token
assert tx['destination_token'] == dummy_token
assert tx['source_token_symbol'] == token.symbol()
assert tx['destination_token_symbol'] == token.symbol()
assert tx['source_token_value'] == 1024
assert tx['destination_token_value'] == 1024
assert tx['source_token_decimals'] == token.decimals()
assert tx['destination_token_decimals'] == token.decimals()
assert tx['sender'] == address_foo
assert tx['sender_label'] == 'foo'
assert tx['recipient'] == address_bar
assert tx['recipient_label'] == 'bar'
assert tx['chain'] == str(default_chain_spec)

View File

@@ -1,24 +1,30 @@
from cic_eth.eth.util import unpack_signed_raw_tx
from cic_eth.eth.task import sign_tx
# external imports
from chainlib.eth.gas import (
Gas,
RPCGasOracle,
)
from chainlib.eth.tx import (
TxFormat,
unpack,
)
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.connection import RPCConnection
from hexathon import strip_0x
def test_unpack(
init_rpc,
w3,
default_chain_spec,
eth_rpc,
eth_signer,
agent_roles,
):
tx = {
'from': w3.eth.accounts[1],
'to': w3.eth.accounts[0],
'nonce': 0,
'value': 1024,
'gas': 21000,
'gasPrice': 200000000,
'data': '0x',
'chainId': 42,
}
chain_id = default_chain_spec.chain_id()
rpc = RPCConnection.connect(default_chain_spec, 'default')
nonce_oracle = RPCNonceOracle(agent_roles['ALICE'], eth_rpc)
gas_oracle = RPCGasOracle(eth_rpc)
c = Gas(signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, chain_id=default_chain_spec.chain_id())
(tx_hash_hex, tx_signed_raw_hex) = c.create(agent_roles['ALICE'], agent_roles['BOB'], 100 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED)
(tx_hash, tx_raw) = sign_tx(tx, 'foo:bar:42')
tx = unpack(bytes.fromhex(strip_0x(tx_signed_raw_hex)), chain_id=chain_id)
tx_recovered = unpack_signed_raw_tx(bytes.fromhex(tx_raw[2:]), 42)
assert tx_hash == tx_recovered['hash']
assert tx_hash_hex == tx['hash']

View File

@@ -1,99 +0,0 @@
# 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
from cic_eth.db.models.nonce import NonceReservation
logg = logging.getLogger()
def test_unpack_transfer(
default_chain_spec,
init_database,
init_w3,
init_rpc,
cic_registry,
bancor_tokens,
bancor_registry,
):
NonceReservation.next(init_w3.eth.accounts[0], 'foo', init_database)
init_database.commit()
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,
'foo',
)
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,
):
NonceReservation.next(init_w3.eth.accounts[0], 'foo', init_database)
init_database.commit()
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,
'foo',
)
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.from_value == value
assert txc.to_value == value