点击上方蓝字关注我们~
1
2
获取测试币
rinkeby
ropsten
3
RPC
w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/*************"))
相当于就是连上这条测试链了。不过你还得在配置里允许一些在进行交易的时候用的方法,不然就都会被ban4
MetaMask
5
合约
remix
脚本交互
部署合约
from web3 import Web3
w3 = Web3(HTTPProvider("http://114.115.157.63:8545/"))
w3.eth.account.from_key('0x6160feb8eb4**********************')
w3.eth.get_balance(account.address)
contract=w3.eth.contract(abi=abi,bytecode=opcode)
construct_txn = contract.constructor("hello").buildTransaction({
'from': account.address,
'nonce': w3.eth.getTransactionCount(account.address),
'gas': 5000000,
'gasPrice': w3.toWei('21', 'gwei')})
signed = account.signTransaction(construct_txn) # 用账户对交易签名
tx_id = w3.eth.sendRawTransaction(signed.rawTransaction)
receipt = w3.eth.waitForTransactionReceipt(tx_id)
address = receipt.contractAddress
contract_instance= w3.eth.contract(address=address, abi=abi)
from web3 import Web3
w3 = Web3() # 提前设置好了环境变量,给好RPC了
# w3 = Web3(HTTPProvider("http://114.115.157.63:8545/"))
account = w3.eth.account.from_key('0x6160feb8eb4*********************') //设定外部账户
abi=[
{
"inputs": [
{
"internalType": "string",
"name": "_greeting",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "greet",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "isSolved",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_greeting",
"type": "string"
}
],
"name": "setGreeting",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
opcode="608060405234...30008070033" #这玩意儿太长了,就不放全了
contract=w3.eth.contract(abi=abi,bytecode=opcode)
construct_txn = contract.constructor("hello").buildTransaction({
'from': account.address,
'nonce': w3.eth.getTransactionCount(account.address),
'gas': 5000000,
'gasPrice': w3.toWei('21', 'gwei')})
signed = account.signTransaction(construct_txn) # 用账户对交易签名
tx_id = w3.eth.sendRawTransaction(signed.rawTransaction)
receipt = w3.eth.waitForTransactionReceipt(tx_id)
address = receipt.contractAddress
contract_instance= w3.eth.contract(address=address, abi=abi)
>> construct_txn
{'value': 0,
'chainId': 3,
'from': '0x5bE571f66D2f98eFf8EB270475375f2D34d47B0B',
'nonce': 5,
'gas': 5000000,
'gasPrice': 21000000000,
'data': '0x608060405234...300080700330000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000',
'to': b''}
signed = account.signTransaction(tx_hash)
tx_id = w3.eth.sendRawTransaction(signed.rawTransaction)
contract_instance.functions.greet().call()
tx_hash = contract_instance.functions.setGreeting("HelloChainFlag").buildTransaction({'gas': 1000000,
'gasPrice': w3.toWei('21', 'gwei'), 'from': account.address,
'nonce': w3.eth.getTransactionCount(account.address)}) # 构造交易
signed = account.signTransaction(tx_hash) # 用账户对交易签名
tx_id = w3.eth.sendRawTransaction(signed.rawTransaction)
print(w3.eth.getTransactionReceipt(tx_id.hex()))
print(w3.eth.getTransaction(tx_id.hex()))
=========================================================================================
AttributeDict({'blockHash': HexBytes('0x0fae6f7b46dd6ac9074183b1b1550b0fa857c7d406fb4b6ea03d3703b6942561'), 'blockNumber': 11681286, 'contractAddress': None, 'cumulativeGasUsed': 267017, 'effectiveGasPrice': 21000000000, 'from': '0x5bE571f66D2f98eFf8EB270475375f2D34d47B0B', 'gasUsed': 27050, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0xbE7a7817Dd15eafeAD7917078D51C30Ca9E13fFf', 'transactionHash': HexBytes('0xcf8c3a5549aea014fe8ac5e5316b4670c91f9b97b43a02ef62414b77aa3c481b'), 'transactionIndex': 2, 'type': '0x0'})
AttributeDict({'blockHash': HexBytes('0x0fae6f7b46dd6ac9074183b1b1550b0fa857c7d406fb4b6ea03d3703b6942561'), 'blockNumber': 11681286, 'from': '0x5bE571f66D2f98eFf8EB270475375f2D34d47B0B', 'gas': 1000000, 'gasPrice': 21000000000, 'hash': HexBytes('0xcf8c3a5549aea014fe8ac5e5316b4670c91f9b97b43a02ef62414b77aa3c481b'), 'input': '0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000e48656c6c6f436861696e466c6167000000000000000000000000000000000000', 'nonce': 5, 'r': HexBytes('0x5cb92f983c3c5d6cf2c1b66781514b9fa790ad135c522b051c841d2a7173cc2d'), 's': HexBytes('0x16abebfd0fa522f54a33f0fe691d81c92a5c7c6ea647534ff7ffd94611f7b4af'), 'to': '0xbE7a7817Dd15eafeAD7917078D51C30Ca9E13fFf', 'transactionIndex': 2, 'type': '0x0', 'v': 41, 'value': 0})
opcode
input数据分为3个部分:
4 字节,是方法名的哈希,例如:a9059cbb
32字节,放以太坊地址,目前以太坊地址是20个字节,高位补0
例如:000000000000000000000000abcabcabcabcabcabcabcabcabcabcabcabcabca
32字节,是需要传输的代币数量,这里是1*10^18 GNT
例如:0000000000000000000000000000000000000000000000000de0b6b3a7640000
所有这些加在一起就是交易数据:
a9059cbb000000000000000000000000abcabcabcabcabcabcabcabcabcabcabcabcabca0000000000000000000000000000000000000000000000000de0b6b3a7640000
0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c68656c6c6f2056616e6973680000000000000000000000000000000000000000
6
总结
原文始发于微信公众号(山石网科安全技术研究院):区块链学习笔记之初探以太坊
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论