script to doposit to home modified to use utils api

This commit is contained in:
Alexander Kolotov 2018-05-28 10:43:01 +03:00
parent 07416424aa
commit 56962ff6e3
5 changed files with 124 additions and 124 deletions

View File

@ -1,74 +0,0 @@
#!/opt/anaconda3/bin/python
from web3 import Web3
import json
from toml import load
import sys
from random import randint
test_env_db = '/home/koal/parity/bridge/test_env_db.toml'
try:
test_env = load(test_env_db)
except:
sys.exit(1)
bridge_config = load(test_env['bridge_config'])
bridge_db = load(test_env['bridge_db'])
keystore = test_env['actor_keystore']
_IPC_file = bridge_config['home']['ipc']
web3 = Web3(Web3.IPCProvider(_IPC_file))
gasPrice = bridge_config['transactions']['withdraw_relay']['gas_price']
gasLimit = 50000
bridgeContractAddress = web3.toChecksumAddress(bridge_db['home_contract_address'])
if (len(sys.argv) == 2):
txNum = int(sys.argv[1])
else:
sys.exit(1)
net_id = int(web3.version.network)
txTmpl = {
'to': bridgeContractAddress,
'gas': gasLimit,
'gasPrice': gasPrice,
'chainId': net_id
}
with open(keystore) as keyfile:
encrypted_key = keyfile.read()
private_key = web3.eth.account.decrypt(encrypted_key, '11')
actor = web3.eth.account.privateKeyToAccount(private_key)
op_num = web3.eth.getTransactionCount(actor.address)
################################################################################
# Preparing batch of transactions to the bridge contract with ether sending
################################################################################
tx_signed = []
for i in range(0, txNum):
tx = txTmpl.copy()
tx['nonce'] = op_num + i
value = web3.toWei(10, 'szabo')
tx['value'] = value
signed = actor.signTransaction(tx)
tx_signed.append(signed)
print('NOnce:', tx['nonce'], 'value:', value)
################################################################################
# Sending batch of transactions
################################################################################
for tx in tx_signed:
txHash = web3.eth.sendRawTransaction(tx.rawTransaction)
print('TX:', txHash.hex())
sys.exit(0)

View File

@ -1,45 +0,0 @@
#!/opt/anaconda3/bin/python
from web3 import Web3
from web3.utils.transactions import wait_for_transaction_receipt
import json
from toml import load
import sys
from random import randint
test_env_db = '/home/koal/parity/bridge/test_env_db.toml'
try:
test_env = load(test_env_db)
except:
sys.exit(1)
bridge_config = load(test_env['bridge_config'])
bridge_db = load(test_env['bridge_db'])
_IPC_file = bridge_config['home']['ipc']
web3 = Web3(Web3.IPCProvider(_IPC_file))
#web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:38545"))
_gasPrice = bridge_config['transactions']['withdraw_relay']['gas_price']
bridgeContractAddress = web3.toChecksumAddress(bridge_db['home_contract_address'])
if 'actor_address' in test_env:
actor = web3.toChecksumAddress(test_env['actor_address'])
else:
sys.exit("actor is not set in testenv DB")
################################################################################
# Sending ether to the bridge contract
################################################################################
value = web3.toWei(randint(700, 5000), 'szabo')
tx = {'from': actor, 'to': bridgeContractAddress, 'value': value ,'gasPrice': _gasPrice}
print("Sending", value, "to Home bridge")
txHash = web3.personal.sendTransaction(tx, "11")
wait_for_transaction_receipt(web3, txHash)
print("TX:", txHash.hex())
sys.exit(0)

71
erc20/home_deposit.py Executable file
View File

@ -0,0 +1,71 @@
#!/opt/anaconda3/bin/python
from utils.getenv import BridgeEnv
from sys import argv, exit
from utils.web3 import fromEther, toEther
tx_num = 1
tx_value = None
# The command can be used with 2 parameters:
# #1 - number of transactions
# #2 - value to transact
if (len(argv) == 2):
tx_num = int(argv[1])
else:
if (len(argv) == 3):
tx_num = int(argv[1])
tx_value = fromEther(float(argv[2]))
elif (len(argv) != 1):
exit("Incorrect number of input parameters")
b = BridgeEnv()
b.initEnv()
web3 = b.connectionToHome()
b.initHomeBridgeContact()
gas_price = b.home_bridge.functions.gasPrice().call()
gas_limit = 50000
net_id = int(web3.version.network)
op_num = web3.eth.getTransactionCount(b.actor_address)
tx_templ = {
'to': b.home_bridge_address,
'gas': gas_limit,
'gasPrice': gas_price,
'chainId': net_id
}
actor = b.activateActor()
op_num = web3.eth.getTransactionCount(actor.address)
if tx_value == None:
tx_value = b.home_bridge.functions.minPerTx().call()
################################################################################
# Preparing batch of transactions to the bridge contract with ether sending
################################################################################
tx_signed = []
for i in range(0, tx_num):
tx = tx_templ.copy()
tx['nonce'] = op_num + i
tx['value'] = tx_value
signed = actor.signTransaction(tx)
tx_signed.append(signed)
print('NOnce:', tx['nonce'], 'value:', toEther(tx_value))
################################################################################
# Sending batch of transactions
################################################################################
for tx in tx_signed:
txHash = web3.eth.sendRawTransaction(tx.rawTransaction)
print('TX:', txHash.hex())
exit(0)

View File

@ -9,6 +9,8 @@ from json import load as jload
from utils.web3 import (
toChecksumAddress,
connectionToRPCProvider,
decryptPrivateKey,
initContractAtAddress,
)
class BridgeEnv():
@ -16,8 +18,8 @@ class BridgeEnv():
def _fromEnv(self):
self.home_rpc_provider = getenv("HOME_RPC_URL")
self.foreign_rpc_provider = getenv("FOREIGN_RPC_URL")
self.home_bridge = toChecksumAddress(getenv("HOME_BRIDGE_ADDRESS"))
self.foreign_bridge = toChecksumAddress(getenv("FOREIGN_BRIDGE_ADDRESS"))
self.home_bridge_address = toChecksumAddress(getenv("HOME_BRIDGE_ADDRESS"))
self.foreign_bridge_address = toChecksumAddress(getenv("FOREIGN_BRIDGE_ADDRESS"))
self.validator = toChecksumAddress(getenv("VALIDATOR_ADDRESS"))
return True
@ -33,8 +35,8 @@ class BridgeEnv():
except:
return False
self.home_bridge = toChecksumAddress(db["home_contract_address"])
self.foreign_bridge = toChecksumAddress(db["foreign_contract_address"])
self.home_bridge_address = toChecksumAddress(db["home_contract_address"])
self.foreign_bridge_address = toChecksumAddress(db["foreign_contract_address"])
return True
@ -50,6 +52,24 @@ class BridgeEnv():
return True
def activateActor(self):
self.actor = decryptPrivateKey(
self.test_env["actor_keystore"],
self.test_env["actor_pwd"]
)
if not (self.actor_address == self.actor.address):
print("It seems that keystore corrupted")
return self.actor
def initHomeBridgeContact(self):
self.home_bridge = None
if self.home_channel:
self.home_bridge = initContractAtAddress(
self.home_channel,
self.test_env["home_bridge_abi"],
self.home_bridge_address
)
self.home_bridge
def initEnv(self, _environment=None):
if _environment:

View File

@ -1,6 +1,7 @@
# This module is wrapper for web3.py module
from web3 import Web3
from web3 import Web3, Account
from json import load as jload
def toChecksumAddress(_addr):
return Web3.toChecksumAddress(_addr)
@ -12,3 +13,30 @@ def connectionToRPCProvider(_rpc_link):
except e:
pass
return ch
def decryptPrivateKey(_file, _pwd):
with open(_file) as keyfile:
encrypted_key = keyfile.read()
keyfile.close()
ac_obj = Account()
decrypted_key = ac_obj.decrypt(encrypted_key, _pwd)
return ac_obj.privateKeyToAccount(decrypted_key)
def initContractAtAddress(_web3, _file, _address):
with open(_file) as f:
contract_abi = jload(f)
f.close()
contract_factory = _web3.eth.contract(
abi = contract_abi,
)
return contract_factory(_address)
def fromEther(_value):
return Web3.toWei(_value, 'ether')
def toEther(_value):
return Web3.fromWei(_value, 'ether')