initial commit

This commit is contained in:
nolash 2021-06-02 16:34:55 +02:00
commit fa658f3fab
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 176 additions and 0 deletions

5
config/config.ini Normal file
View File

@ -0,0 +1,5 @@
[session]
chain_spec=
[rpc]
endpoint=

3
example.csv Normal file
View File

@ -0,0 +1,3 @@
0x7Ad7a61539De44a2Cf296dE0aEfe37F6c4bbdA7E,0xb708175e3f6Cd850643aAF7B32212AFad50e2549,100
0x6336e1b77b7106C7F63aE870df846619AFEe51C4,0xb708175e3f6Cd850643aAF7B32212AFad50e2549,12012
0xDfB7B9DbdC9398ff95738aa27ba180aD03dDa024,0xb708175e3f6Cd850643aAF7B32212AFad50e2549,25530.939
1 0x7Ad7a61539De44a2Cf296dE0aEfe37F6c4bbdA7E 0xb708175e3f6Cd850643aAF7B32212AFad50e2549 100
2 0x6336e1b77b7106C7F63aE870df846619AFEe51C4 0xb708175e3f6Cd850643aAF7B32212AFad50e2549 12012
3 0xDfB7B9DbdC9398ff95738aa27ba180aD03dDa024 0xb708175e3f6Cd850643aAF7B32212AFad50e2549 25530.939

6
requirements.txt Normal file
View File

@ -0,0 +1,6 @@
jsonschema~=3.2.0
chainlib~=0.0.3a1
confini~=0.3.6rc3
cic-eth-registry~=0.5.5a7
eth-erc20~=0.0.9a3
pycryptodome==3.10.1

162
scripts/eth.py Normal file
View File

@ -0,0 +1,162 @@
# standard imports
import csv
import os
import logging
import argparse
import uuid
import sys
import tempfile
import stat
# external imports
import csv
import confini
from chainlib.eth.address import is_checksum_address
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import OverrideGasOracle
from chainlib.eth.tx import TxFormat
from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore
from eth_erc20 import ERC20
from cic_eth_registry.erc20 import ERC20Token
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
required_fields = [
'to',
'token',
'value',
]
config_dir = os.environ.get('CONFINI_DIR', '.')
argparser = argparse.ArgumentParser('chainqueue transaction submission and trigger server')
argparser.add_argument('-c', '--config', dest='c', type=str, default=config_dir, help='configuration directory')
argparser.add_argument('-p', type=str, help='rpc endpoint')
argparser.add_argument('-i', type=str, help='chain spec')
argparser.add_argument('--session-id', dest='session_id', type=str, default=str(uuid.uuid4()), help='session id to use for session')
argparser.add_argument('--gas-limit', dest='gas_limit', type=int, default=8000000, help='override gas limit')
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
argparser.add_argument('-v', action='store_true', help='be verbose')
argparser.add_argument('-vv', action='store_true', help='be very verbose')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('-o', '--output', type=str, help='output directory')
argparser.add_argument('input_data', type=str, help='input file')
args = argparser.parse_args(sys.argv[1:])
if args.vv:
logg.setLevel(logging.DEBUG)
elif args.v:
logg.setLevel(logging.INFO)
config = confini.Config(args.c)
config.process()
args_override = {
'SESSION_CHAIN_SPEC': getattr(args, 'i'),
'RPC_ENDPOINT': getattr(args, 'p'),
}
config.dict_override(args_override, 'cli args')
config.add(getattr(args, 'input_data'), '_INPUT_FILE', True)
config.add(getattr(args, 'session_id'), '_SESSION_ID', True)
config.add(getattr(args, 'gas_limit'), '_GAS_LIMIT', True)
output_dir = getattr(args, 'output')
if not output_dir:
output_dir = os.path.join('.', 'output', config.get('_SESSION_ID'))
config.add(output_dir, '_OUTPUT_DIR', True)
logg.debug('config loaded:\n{}'.format(config))
logg.info('using session id {}'.format(config.get('_SESSION_ID')))
passphrase_env = 'ETH_PASSPHRASE'
if args.env_prefix != None:
passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
signer_address = None
keystore = DictKeystore()
if args.y == None:
sys.stderr.write('keyfile missing\n')
sys.exit(1)
logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_keystore_file(args.y, password=passphrase)
logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
rpc = EthHTTPConnection(config.get('RPC_ENDPOINT'))
nonce_oracle = RPCNonceOracle(signer_address, conn=rpc)
gas_oracle = OverrideGasOracle(limit=config.get('_GAS_LIMIT'), conn=rpc)
chain_spec = ChainSpec.from_chain_str(config.get('SESSION_CHAIN_SPEC'))
try:
os.makedirs(config.get('_OUTPUT_DIR'))
logg.info('output dir {} created'.format(config.get('_OUTPUT_DIR')))
except FileExistsError:
sys.stderr.write('output directory {} already exists\n'.format(config.get('_OUTPUT_DIR')))
sys.exit(1)
class InvalidData(Exception):
pass
token_cache = {}
erc20 = ERC20(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
def validate(row):
if len(row) != 3:
raise InvalidData('row length {} found, need {}'.format(len(row), 3))
if not is_checksum_address(row[0]):
raise InvalidData('invalid recipient address {}'.format(row[0]))
if not is_checksum_address(row[1]):
raise InvalidData('invalid token address {}'.format(row[1]))
if token_cache.get(row[1]) == None:
token_cache[row[1]] = ERC20Token(chain_spec, rpc, row[1])
try:
value = float(row[2])
except ValueError:
raise InvalidData('invalid token address {}'.format(row[1]))
return token_cache[row[1]]
def main():
tmp_out = tempfile.mkdtemp()
f = open(config.get('_INPUT_FILE'), 'r')
cr = csv.reader(f)
i = 1
for row in cr:
token_symbol = None
try:
token = validate(row)
except InvalidData as e:
sys.stderr.write(str(e) + ' in line {}\n'.format(i))
sys.exit(1)
multiplier = 10 ** token.decimals
value = int(multiplier * float(row[2]))
(tx_hash_hex, o) = erc20.transfer(row[1], signer_address, row[0], value, tx_format=TxFormat.RLP_SIGNED)
fname = '{}_{}'.format(i, row[0])
fpath = os.path.join(config.get('_OUTPUT_DIR'), fname)
f = open(fpath, 'x')
f.write(o)
f.close()
logg.info('tx {}: {} ({} * 10^{}) {} {} -> {}'.format(tx_hash_hex, value, row[2], token.decimals, token.symbol, signer_address, row[0]))
i += 1
f.close()
if __name__ == '__main__':
main()