cic-cli/tests/eth/test_eth_token.py

119 lines
3.8 KiB
Python
Raw Normal View History

2021-10-10 15:37:26 +02:00
# standard imports
import os
import unittest
import logging
# external imports
from chainlib.eth.tx import (
TxFormat,
receipt,
)
2021-10-10 19:59:52 +02:00
from chainlib.eth.contract import (
abi_decode_single,
ABIContractEncoder,
ABIContractType,
)
from chainlib.jsonrpc import JSONRPCRequest
from hexathon import add_0x
2021-10-10 15:37:26 +02:00
# local imports
from cic.ext.eth import CICEth
# tests imports
from tests.eth.base_eth import TestCICEthBase
from tests.base_cic import test_data_dir
from giftable_erc20_token import GiftableToken
2021-10-10 19:59:52 +02:00
from eth_erc20 import ERC20
2021-10-10 15:37:26 +02:00
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestCICEthToken(TestCICEthBase):
def setUp(self):
super(TestCICEthToken, self).setUp()
self.resources['token']['reference'] = None
self.adapter = CICEth(self.chain_spec, self.resources, self.proofs)
2021-10-10 19:59:52 +02:00
self.token_name = 'FOotoken'
self.token_symbol = 'FOO'
self.token_precision = 8
2021-10-10 15:37:26 +02:00
def test_token_nobackend(self):
2021-10-10 19:59:52 +02:00
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode())
2021-10-10 15:37:26 +02:00
v = self.adapter.process_token()
def test_token_sign(self):
self.adapter.signer = self.signer
self.adapter.tx_format = TxFormat.RLP_SIGNED
self.adapter.prepare_token('FOoToken', 'FOO', 8, GiftableToken.bytecode())
v = self.adapter.process_token()
def test_token_rpc(self):
self.adapter.signer = self.signer
self.adapter.rpc = self.rpc
self.adapter.tx_format = TxFormat.JSONRPC
self.adapter.prepare_token('FOoToken', 'FOO', 8, GiftableToken.bytecode())
v = self.adapter.process_token()
o = receipt(v)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
2021-10-10 19:59:52 +02:00
def test_token_weirdorder(self):
weird_contract_bytecode_path = os.path.join(test_data_dir, 'contracts', 'WeirdPosToken.bin')
f = open(weird_contract_bytecode_path, 'r')
weird_bytecode = f.read()
f.close()
try:
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, extra=['0xdeadbeef'], extra_types=['bytes32'])
except ValueError:
v = self.adapter.process_token()
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, extra=['0xdeadbeef'], extra_types=['bytes32'], positions=[1, 2, 3, 0])
self.adapter.signer = self.signer
self.adapter.rpc = self.rpc
self.adapter.tx_format = TxFormat.JSONRPC
v = self.adapter.process_token()
logg.debug('v {}'.format(v))
o = receipt(v)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
contract_address = r['contract_address']
c = ERC20(self.chain_spec)
o = c.name(contract_address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(c.parse_name(r), self.token_name)
o = c.symbol(contract_address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(c.parse_symbol(r), self.token_symbol)
o = c.decimals(contract_address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(c.parse_decimals(r), self.token_precision)
j = JSONRPCRequest()
o = j.template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('foo')
data = add_0x(enc.get())
tx = c.template(self.accounts[0], contract_address)
tx = c.set_code(tx, data)
o['params'].append(c.normalize(tx))
o['params'].append('latest')
o = j.finalize(o)
r = self.rpc.do(o)
v = abi_decode_single(ABIContractType.BYTES32, r)
self.assertEqual(v[-8:], 'deadbeef')
2021-10-10 15:37:26 +02:00
if __name__ == '__main__':
unittest.main()