区块链 区块链技术 比特币公众号手机端

web3.js 手工创建合约 不能设置 to 参数

liumuhui 11个月前 (11-12) 阅读数 144 #区块链
文章标签 Web3合约交互

今天调试手工采用node 调用web3.js

1.创建合约失败的 过程:执行后contract address是null的代码:

1.1 下面的code 取值从remix里面拷贝byte code

code = '0x608060405234801561001057600080fd5b5061025d806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80630acc083e1461004657806385d8284c14610076578063bcb4ab0e14610095575b600080fd5b610060600480360381019061005b9190610144565b6100b4565b60405161006d9190610180565b60405180910390f35b61007e6100ca565b60405161008c92919061019b565b60405180910390f35b61009d6100e5565b6040516100ab92919061019b565b60405180910390f35b60006001826100c391906101f3565b9050919050565b60008060016000546100dc91906101f3565b5a915091509091565b60008060016000546100f791906101f3565b6000819055506000545a915091509091565b600080fd5b6000819050919050565b6101218161010e565b811461012c57600080fd5b50565b60008135905061013e81610118565b92915050565b60006020828403121561015a57610159610109565b5b60006101688482850161012f565b91505092915050565b61017a8161010e565b82525050565b60006020820190506101956000830184610171565b92915050565b60006040820190506101b06000830185610171565b6101bd6020830184610171565b9392505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101fe8261010e565b91506102098361010e565b9250828201905080821115610221576102206101c4565b5b9291505056fea26469706673582212204f34bb1b403e445a9b5ec4ca4cbc912cb5614c210931fd57fe93ee7a3b68466264736f6c63430008110033'

1.2 使用nodejs 调用web3.js 的方式

npm init -y
npm i web3
node 

1.2 使用web3.js 来执行


url = 'http://localhost:8545'
{Web3} = require('web3')
web3 = new Web3(url)

accounts  = await  web3.eth.getAccounts()
zero ='0x0000000000000000000000000000000000000000'

tx = {from : accounts[0], to:zero, data: code}
receipt =  await  web3.eth.sendTransaction(tx)

2. 创建成功的代码

tx.to 不赋值


url = 'http://localhost:8545'
{Web3} = require('web3')
web3 = new Web3(url)

accounts  = await  web3.eth.getAccounts()
//zero ='0x0000000000000000000000000000000000000000'
//web3 create transaction, do not set the tx.to zero address, zero address means detroy coin
// undefined tx.to means create transaction
tx = {from : accounts[0], data: code}
receipt =  await  web3.eth.sendTransaction(tx)
版权声明

本文仅代表作者观点,不代表区块链技术网立场。
本文系作者授权本站发表,未经许可,不得转载。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门