cic-cli/tests/eth/test_eth_token.py

116 lines
4.1 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-15 12:48:14 +02:00
Tx,
2021-10-10 15:37:26 +02:00
)
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-11 08:57:03 +02:00
from giftable_erc20_token import GiftableToken
from eth_erc20 import ERC20
2021-10-10 15:37:26 +02:00
# local imports
from cic.ext.eth import CICEth
# tests imports
from tests.eth.base_eth import TestCICEthTokenBase
2021-10-10 15:37:26 +02:00
from tests.base_cic import test_data_dir
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
2021-10-15 12:48:14 +02:00
logg.setLevel(logging.DEBUG)
2021-10-10 15:37:26 +02:00
2021-10-11 08:57:03 +02:00
class TestCICEthToken(TestCICEthTokenBase):
2021-10-10 15:37:26 +02:00
def test_token_nobackend(self):
2021-10-15 12:48:14 +02:00
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), 0)
self.adapter.token_address = self.token_address
v = self.adapter.process_token(writer=self.outputs_writer)
2021-10-10 15:37:26 +02:00
def test_token_sign(self):
2021-10-15 12:48:14 +02:00
self.adapter.token_address = self.token_address
2021-10-10 15:37:26 +02:00
self.adapter.signer = self.signer
self.adapter.tx_format = TxFormat.RLP_SIGNED
2021-10-15 12:48:14 +02:00
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), self.token_supply)
v = self.adapter.process_token(writer=self.outputs_writer)
2021-10-10 15:37:26 +02:00
def test_token_rpc(self):
self.adapter.signer = self.signer
self.adapter.rpc = self.rpc
self.adapter.tx_format = TxFormat.JSONRPC
2021-10-15 12:48:14 +02:00
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, GiftableToken.bytecode(), self.token_supply)
v = self.adapter.process_token(writer=self.outputs_writer)
2021-10-10 15:37:26 +02:00
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:
2021-10-15 12:48:14 +02:00
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, self.token_supply, extra=['0xdeadbeef'], extra_types=['bytes32'])
2021-10-10 19:59:52 +02:00
except ValueError:
2021-10-15 12:48:14 +02:00
v = self.adapter.process_token(writer=self.outputs_writer)
self.adapter.prepare_token(self.token_name, self.token_symbol, self.token_precision, weird_bytecode, self.token_supply, extra=['0xdeadbeef'], extra_types=['bytes32'], positions=[1, 2, 3, 0])
self.adapter.load_code()
2021-10-10 19:59:52 +02:00
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)
2021-10-15 12:48:14 +02:00
Tx.src_normalize(r)
2021-10-10 19:59:52 +02:00
contract_address = r['contract_address']
2021-10-15 12:48:14 +02:00
logg.debug('contract address {}'.format(contract_address))
2021-10-10 19:59:52 +02:00
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()