cic-cli/cic/ext/eth/__init__.py
2021-10-10 15:37:26 +02:00

134 lines
4.0 KiB
Python

# standard imports
import logging
import copy
# external imports
from chainlib.chain import ChainSpec
from chainlib.eth.tx import (
TxFormat,
TxFactory,
)
from chainlib.eth.connection import RPCConnection
from chainlib.eth.contract import ABIContractEncoder
from eth_token_index import TokenUniqueSymbolIndex
from eth_address_declarator import Declarator
logg = logging.getLogger(__name__)
class CICEth:
def __init__(self, chain_spec, resources, proofs, signer=None, rpc=None, nonce_oracle=None, fee_oracle=None):
"""resources will be modified
"""
self.resources = resources
self.proofs = proofs
self.chain_spec = chain_spec
self.signer = signer
self.rpc = rpc
self.nonce_oracle = nonce_oracle
self.fee_oracle = fee_oracle
self.token_details = None
self.token_address = None
self.token_code = None
self.outputs = []
self.tx_format = TxFormat.RAW_ARGS
if self.rpc != None:
self.tx_format = TxFormat.JSONRPC
elif self.signer != None:
self.tx_format = TxFormat.RLP_SIGNED
def prepare_token(self, name, symbol, precision, code, extra={}, positions=None):
self.token_details = {
'name': name,
'symbol': symbol,
'precision': precision,
'code': code,
'extra': extra,
'positions': positions,
}
def process_token(self):
enc = ABIContractEncoder()
enc.string(self.token_details['name'])
enc.string(self.token_details['symbol'])
enc.uint256(self.token_details['precision'])
code = self.token_details['code'] + enc.get()
signer_address = self.resources['token']['key_address']
c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=self.nonce_oracle, gas_oracle=self.fee_oracle)
tx = c.template(signer_address, None, use_nonce=True)
tx = c.set_code(tx, code)
o = c.finalize(tx, self.tx_format)
r = None
if self.rpc != None:
r = self.rpc.do(o[1])
elif self.signer != None:
r = o[1]
if r == None:
r = code
self.outputs.append(r)
return r
def process_token_index(self):
c = TokenUniqueSymbolIndex(self.chain_spec, signer=self.signer)
contract_address = self.resources['token_index']['reference']
signer_address = self.resources['token_index']['key_address']
o = c.register(contract_address, signer_address, self.token_address, tx_format=self.tx_format)
r = None
if self.rpc != None:
r = self.rpc.do(o[1])
elif self.signer != None:
r = o[1]
else:
r = o
self.outputs.append(r)
return r
def process_address_declarator(self):
c = Declarator(self.chain_spec, signer=self.signer)
contract_address = self.resources['address_declarator']['reference']
signer_address = self.resources['address_declarator']['key_address']
r = []
for proof in self.proofs:
o = c.add_declaration(contract_address, signer_address, self.token_address, proof, tx_format=self.tx_format)
if self.rpc != None:
r.append(self.rpc.do(o[1]))
elif self.signer != None:
r.append(o[1])
else:
r.append(o)
self.outputs += r
return r
def process(self):
self.token_address = self.resources['token']['reference']
if self.token_address == None:
tasks.append('token')
for k in self.resources.keys():
if k == 'token':
continue
if self.resources[k]['reference'] != None:
tasks.append(k)
for task in tasks:
getattr(self, 'process_̈́ ' + task)()
def new(resources, proofs, signer_hint=None):
return CICEth(resources, proofs, signer=None)