Add gas transfer script
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
zero_address = '0x{:040x}'.format(0)
|
||||
zero_content = '0x{:064x}'.format(0)
|
||||
ZERO_ADDRESS = '0x{:040x}'.format(0)
|
||||
ZERO_CONTENT = '0x{:064x}'.format(0)
|
||||
MINIMUM_FEE_UNITS = 21000
|
||||
|
||||
56
cic_tools/eth/gas.py
Normal file
56
cic_tools/eth/gas.py
Normal file
@@ -0,0 +1,56 @@
|
||||
# third-party imports
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
from crypto_dev_signer.eth.transaction import EIP155Transaction
|
||||
|
||||
# local imports
|
||||
from cic_tools.eth.method import (
|
||||
jsonrpc_template,
|
||||
)
|
||||
from cic_tools.eth.tx import TxFactory
|
||||
from cic_tools.eth.hash import keccak256_hex_to_hex
|
||||
|
||||
|
||||
def price():
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_gasPrice'
|
||||
return o
|
||||
|
||||
|
||||
def balance(address):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_getBalance'
|
||||
o['params'].append(address)
|
||||
return o
|
||||
|
||||
|
||||
class GasTxFactory(TxFactory):
|
||||
|
||||
def create(self, sender, recipient, value):
|
||||
tx = self.template(sender, recipient)
|
||||
txe = EIP155Transaction(tx, tx['nonce'], tx['chainId'])
|
||||
self.signer.signTransaction(txe)
|
||||
tx_raw = txe.rlp_serialize()
|
||||
tx_raw_hex = add_0x(tx_raw.hex())
|
||||
tx_hash_hex = add_0x(keccak256_hex_to_hex(tx_raw_hex))
|
||||
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_sendRawTransaction'
|
||||
o['params'].append(tx_raw_hex)
|
||||
|
||||
return (tx_hash_hex, o)
|
||||
|
||||
|
||||
class DefaultGasOracle:
|
||||
|
||||
def __init__(self, conn):
|
||||
self.conn = conn
|
||||
|
||||
|
||||
def get(self):
|
||||
o = price()
|
||||
r = self.conn.do(o)
|
||||
n = strip_0x(r)
|
||||
return int(n, 16)
|
||||
@@ -1,7 +1,29 @@
|
||||
# third-party imports
|
||||
import sha3
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
|
||||
|
||||
def keccak256_hex(s):
|
||||
h = sha3.keccak_256()
|
||||
h.update(s.encode('utf-8'))
|
||||
return h.digest().hex()
|
||||
|
||||
|
||||
def keccak256_string_to_hex(s):
|
||||
return keccak256_hex(s)
|
||||
|
||||
|
||||
def keecak256_bytes_to_hex(b):
|
||||
h = sha3.keccak_256()
|
||||
h.update(b)
|
||||
return h.digest().hex()
|
||||
|
||||
|
||||
def keccak256_hex_to_hex(hx):
|
||||
h = sha3.keccak_256()
|
||||
b = bytes.fromhex(strip_0x(hx))
|
||||
h.update(b)
|
||||
return h.digest().hex()
|
||||
|
||||
@@ -4,13 +4,13 @@ import uuid
|
||||
from hexathon import add_0x
|
||||
from eth_abi import encode_single
|
||||
|
||||
from .hash import keccak256_hex
|
||||
from .constant import zero_address
|
||||
from .hash import keccak256_string_to_hex
|
||||
from .constant import ZERO_ADDRESS
|
||||
|
||||
|
||||
# TODO: move to cic-contracts
|
||||
erc20_balance_signature = keccak256_hex('balanceOf(address)')[:8]
|
||||
erc20_decimals_signature = keccak256_hex('decimals()')[:8]
|
||||
erc20_balance_signature = keccak256_string_to_hex('balanceOf(address)')[:8]
|
||||
erc20_decimals_signature = keccak256_string_to_hex('decimals()')[:8]
|
||||
|
||||
|
||||
def jsonrpc_template():
|
||||
@@ -22,7 +22,7 @@ def jsonrpc_template():
|
||||
}
|
||||
|
||||
|
||||
def erc20_balance(contract_address, address, sender_address=zero_address):
|
||||
def erc20_balance(contract_address, address, sender_address=ZERO_ADDRESS):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_call'
|
||||
data = erc20_balance_signature
|
||||
@@ -34,7 +34,7 @@ def erc20_balance(contract_address, address, sender_address=zero_address):
|
||||
return o
|
||||
|
||||
|
||||
def erc20_decimals(contract_address, sender_address=zero_address):
|
||||
def erc20_decimals(contract_address, sender_address=ZERO_ADDRESS):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_call'
|
||||
arg = add_0x(erc20_decimals_signature)
|
||||
@@ -45,7 +45,7 @@ def erc20_decimals(contract_address, sender_address=zero_address):
|
||||
return o
|
||||
|
||||
|
||||
def call(contract_address, data, sender_address=zero_address):
|
||||
def call(contract_address, data, sender_address=ZERO_ADDRESS):
|
||||
return {
|
||||
'from': sender_address,
|
||||
'to': contract_address,
|
||||
|
||||
32
cic_tools/eth/nonce.py
Normal file
32
cic_tools/eth/nonce.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# third-party imports
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from cic_tools.eth.method import (
|
||||
jsonrpc_template,
|
||||
)
|
||||
|
||||
|
||||
def nonce(address):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_getTransactionCount'
|
||||
o['params'].append(address)
|
||||
o['params'].append('pending')
|
||||
return o
|
||||
|
||||
|
||||
class DefaultNonceOracle:
|
||||
|
||||
def __init__(self, address, conn):
|
||||
self.address = address
|
||||
self.conn = conn
|
||||
|
||||
|
||||
def next(self):
|
||||
o = nonce(self.address)
|
||||
r = self.conn.do(o)
|
||||
n = strip_0x(r)
|
||||
return int(n, 16)
|
||||
@@ -57,9 +57,6 @@ if args.v:
|
||||
conn = HTTPConnection(args.p)
|
||||
|
||||
def main():
|
||||
# w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
|
||||
# REPLACE WITH URLLIB
|
||||
|
||||
account = to_checksum(args.account)
|
||||
if not args.u and account != add_0x(args.account):
|
||||
raise ValueError('invalid checksum address')
|
||||
|
||||
112
cic_tools/eth/runnable/gas.py
Normal file
112
cic_tools/eth/runnable/gas.py
Normal file
@@ -0,0 +1,112 @@
|
||||
#!python3
|
||||
|
||||
"""Gas transfer script
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
# standard imports
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
# third-party imports
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
|
||||
from crypto_dev_signer.keystore import DictKeystore
|
||||
from crypto_dev_signer.eth.helper import EthTxExecutor
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from cic_tools.eth.address import to_checksum
|
||||
from cic_tools.eth.connection import HTTPConnection
|
||||
from cic_tools.eth.method import (
|
||||
jsonrpc_template,
|
||||
)
|
||||
from cic_tools.eth.nonce import DefaultNonceOracle
|
||||
from cic_tools.eth.gas import (
|
||||
DefaultGasOracle,
|
||||
GasTxFactory,
|
||||
)
|
||||
from cic_tools.eth.gas import balance as gas_balance
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
default_abi_dir = '/usr/share/local/cic/solidity/abi'
|
||||
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
|
||||
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
|
||||
argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Signing address')
|
||||
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('recipient', type=str, help='Ethereum address of recipient')
|
||||
argparser.add_argument('amount', type=int, help='Amount of tokens to mint and gift')
|
||||
args = argparser.parse_args()
|
||||
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
block_last = args.w
|
||||
block_all = args.ww
|
||||
|
||||
signer_address = None
|
||||
keystore = DictKeystore()
|
||||
if args.y != None:
|
||||
logg.debug('loading keystore file {}'.format(args.y))
|
||||
signer_address = keystore.import_keystore_file(args.y)
|
||||
logg.debug('now have key for signer address {}'.format(signer_address))
|
||||
signer = EIP155Signer(keystore)
|
||||
|
||||
conn = HTTPConnection(args.p)
|
||||
nonce_oracle = DefaultNonceOracle(signer_address, conn)
|
||||
gas_oracle = DefaultGasOracle(conn)
|
||||
|
||||
chain_pair = args.i.split(':')
|
||||
chain_id = int(chain_pair[1])
|
||||
|
||||
|
||||
def balance(address):
|
||||
o = gas_balance(address)
|
||||
r = conn.do(o)
|
||||
hx = strip_0x(r)
|
||||
return int(hx, 16)
|
||||
|
||||
|
||||
def main():
|
||||
recipient = to_checksum(args.recipient)
|
||||
if not args.u and recipient != add_0x(args.recipient):
|
||||
raise ValueError('invalid checksum address')
|
||||
|
||||
value = args.amount
|
||||
|
||||
logg.debug('sender {} balance before: {}'.format(signer_address, balance(signer_address)))
|
||||
logg.debug('recipient {} balance before: {}'.format(recipient, balance(recipient)))
|
||||
|
||||
g = GasTxFactory(signer, gas_oracle, nonce_oracle, chain_id=chain_id)
|
||||
(tx_hash_hex, o) = g.create(signer_address, recipient, value)
|
||||
conn.do(o)
|
||||
|
||||
print(tx_hash_hex)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -10,6 +10,7 @@ from rlp import encode as rlp_encode
|
||||
|
||||
# local imports
|
||||
from .address import to_checksum
|
||||
from .constant import MINIMUM_FEE_UNITS
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
@@ -81,3 +82,27 @@ def unpack_signed(tx_raw_bytes, chain_id=1):
|
||||
}
|
||||
|
||||
|
||||
class TxFactory:
|
||||
|
||||
def __init__(self, signer, gas_oracle, nonce_oracle, chain_id=1):
|
||||
self.gas_oracle = gas_oracle
|
||||
self.nonce_oracle = nonce_oracle
|
||||
self.chain_id = chain_id
|
||||
self.signer = signer
|
||||
|
||||
|
||||
def template(self, sender, recipient):
|
||||
gas_price = self.gas_oracle.get()
|
||||
logg.debug('using gas price {}'.format(gas_price))
|
||||
nonce = self.nonce_oracle.next()
|
||||
logg.debug('using nonce {} for address {}'.format(nonce, sender))
|
||||
return {
|
||||
'from': sender,
|
||||
'to': recipient,
|
||||
'value': 0,
|
||||
'data': '0x',
|
||||
'nonce': nonce,
|
||||
'gasPrice': gas_price,
|
||||
'gas': MINIMUM_FEE_UNITS,
|
||||
'chainId': self.chain_id,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user