From bf663049d01db235ab2681969f38f2dc32facf55 Mon Sep 17 00:00:00 2001 From: Alexander Kolotov Date: Thu, 15 Mar 2018 00:49:17 +0300 Subject: [PATCH] script to withdraw exact amount of tokens --- erc20/bridge/token/token_exact_withdraw.py | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 erc20/bridge/token/token_exact_withdraw.py diff --git a/erc20/bridge/token/token_exact_withdraw.py b/erc20/bridge/token/token_exact_withdraw.py new file mode 100755 index 0000000..3f62b50 --- /dev/null +++ b/erc20/bridge/token/token_exact_withdraw.py @@ -0,0 +1,74 @@ +#!/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 + +_tokenName = 'BridgeableToken' +_abiFile = _tokenName+".abi" + +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['foreign']['ipc'] +web3 = Web3(Web3.IPCProvider(_IPC_file)) + +_gasPrice = bridge_config['transactions']['withdraw_confirm']['gas_price'] + +tokenContractAddress = web3.toChecksumAddress(test_env['token_contract_address']) + +bridgeContractAddress = web3.toChecksumAddress(bridge_db['foreign_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") + +#---------------------------------------------------------------------------- +# Read ABI +#---------------------------------------------------------------------------- +with open(_abiFile) as f: + _contractABI=json.load(f) +f.close() +#print(_contractABI[0]) + +ContractFactory = web3.eth.contract( + abi = _contractABI, +) + +if (len(sys.argv) == 2): + value = int(sys.argv[1]) +else: + sys.exit(1) + +############################################################################# +# MAIN PART STARTED HERE +############################################################################# + +TokenContract = ContractFactory(tokenContractAddress) + +print("Withdraw", value, "from Foreign bridge") + +txTmpl = {'from': actor, + 'gasPrice': _gasPrice} + +txToSend = TokenContract.functions.approveAndCall(bridgeContractAddress, value, b'').buildTransaction(txTmpl) + +# This is needed since sendTransaction does not expect this argument parameter and does not skip it by some reason +txToSend.pop('chainId', None) + +txHash = web3.personal.sendTransaction(txToSend, "11") +wait_for_transaction_receipt(web3, txHash) + +print("TX:", txHash.hex()) + +sys.exit(0)