web3.js部署合约
合约的编写
⾸先在remix上编写Proof.sol智能合约代码
pragma solidity ^0.4.0;
contract Proof{
struct FileDetails{
uint timestamp;
string owner;
js arguments}
mapping(string => FileDetails) files;
event logFileAddedStatus(bool status,uint timestamp,string owner,string fileHash);
function set(string owner,string fileHash)public{
if(files[fileHash].timestamp ==0){
files[fileHash]=FileDetails(block.timestamp,owner);
emit logFileAddedStatus(true,block.timestamp,owner,fileHash);
}else{
emit logFileAddedStatus(false,block.timestamp,owner,fileHash);
}
}
function get(string fileHash) view public returns(uint timestamp,string owner){
return(files[fileHash].timestamp,files[fileHash].owner);
}
}
点击Compile Proof.sol按钮编译合约,然后点击Compilation Details查看合约编译后的相关信息,到WEB3DEPLOY并复制下来。使⽤合约对象的new⽅法部署
//以ABI为参数创建⼀个合约对象
var proofContract =h.Contract([{"constant":true,"inputs":[{"name":"fileHash","type":"string"}],"name":"get","outputs":[{"name":"timestamp","type" :"uint256"},{"name":"owner","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"owner","type":"strin g"},{"name":"fileHash","type":"string"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs": [{"indexed":false,"name":"status","type":"bool"},{"indexed":false,"name":"timestamp","type":"uint256"},{"indexed":false,"name":"owner","type":"string"},{"inde xed":false,"name":"fileHash","type":"string"}],"name":"logFileAddedStatus","type":"event"}]);
//部署合约
var proof = proofContract.deploy({
data:'0x608060405234801561001057600080fd5b506107a6806100206000396000f30060806040526004361061004c576000357c0100000000000000 000000000000000000000000000000000000000000900463ffffffff168063693ec85e14610051578063e942b5161461013a575b600080fd5b34801561005d5 7600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291 9081815260200183838082843782019150505050505091929192905050506101e9565b6040518083815260200180602001828103825283818151815260 200191508051906020019080838360005b838110156100fe5780820151818401526020810190506100e3565b50505050905090810190601f16801561012 b5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561014657600080fd5b506101e7600480 360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843 78201915050505050509192919290803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152 602001838380828437820191505050505050919291929050505061036f565b005b600060606000836040518082805190602001908083835b60208310151 56102255780518252602082019150602081019050602083039250610200565b6001836020036101000a038019825116818451168082178552505050505 0509050019150509081526020016040518091039020600001546000846040518082805190602001908083835b602083101515610294578051825260208 201915060208101905060208303925061026f565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260 20016040518091039020600101808054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815 26020018280546001816001161561010002031660029004801561035f5780601f106103345761010080835404028352916020019161035f565b820191906 000526020600020905b81548152906001019060200180831161034257829003601f168201915b5050505050905091509150915091565b60008082604051 8082805190602001908083835b6020831015156103a85780518252602082019150602081019050602083039250610383565b6001836020036101000a03 801982511681845116808217855250505050505090500191505090815260200160405180910390206000015414156105b2576040805190810160405280
428152602001838152506000826040518082805190602001908083835b6020831015156104325780518252602082019150602081019050602083039250 61040d565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390206000820151 8160000155602082015181600101908051906020019061048b9291906106d5565b509050507f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e 4637c7b10fb7ba5600142848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020 019080838360005b838110156105095780820151818401526020810190506104ee565b50505050905090810190601f16801561053657808203805160018 36020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561056f578082015181 840152602081019050610554565b50505050905090810190601f16801561059c5780820380516001836020036101000a031916815260200191505b50965 050505050505060405180910390a16106d1565b7f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600042848460405180851 515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b8381101561062c57808 2015181840152602081019050610611565b50505050905090810190601f1680156106595780820380516001836020036101000a03191681526020019150 5b50838103825284818151815260200191508051906020019080838360005b83811015610692578082015181840152602081019050610677565b505050 50905090810190601f1680156106bf5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a15b505 0565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061071657805160ff191683800117855 5610744565b82800160010185558215610744579182015b82811115610743578251825591602001919060010190610728565b5b5090506107519190610 755565b5090565b61077791905b8082111561077357600081600090555060010161075b565b5090565b905600a165627a7a723058200cc963b5380662b e325eaaa537af0e9a306526fff8cc23ece3f445e411517ec00029',
arguments:[
]
}).send({
from: h.accounts[0],
gas:'4700000'
},function(e, contract){
console.log(e, contract);
if(typeof contract.address !=='undefined'){
console.log('Contract mined! address: '+ contract.address +' transactionHash: '+ ansactionHash);
}
})
这⾥导出的javascript代码可⽤来部署智能合约,但是我们下载的是0.19版本的web3,⽽上述代码是基于1.*版本web3的,因此需要进⾏⼀定的修改。主要是如下⽅⾯:
第⼀⾏h.Contract改为act。 (注意⼤⼩写)
第⼆⾏proofContract.deploy改为w。
修改后的部署代码如下:
var proofContract = act([{"constant":true,"inputs":[{"name":"fileHash","type":"string"}],"name":"get","outputs":[{"name":"timestamp","type":"uint 256"},{"name":"owner","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"owner","type":"string"},{" name":"fileHash","type":"string"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"ind exed":false,"name":"status","type":"bool"},{"indexed":false,"name":"timestamp","type":"uint256"},{"ind
exed":false,"name":"owner","type":"string"},{"indexed":f alse,"name":"fileHash","type":"string"}],"name":"logFileAddedStatus","type":"event"}]);
var proof = w({
from: h.accounts[0],
data:'0x608060405234801561001057600080fd5b506107a6806100206000396000f30060806040526004361061004c576000357c01000000000000000 00000000000000000000000000000000000000000900463ffffffff168063693ec85e14610051578063e942b5161461013a575b600080fd5b34801561005d57 600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919 081815260200183838082843782019150505050505091929192905050506101e9565b60405180838152602001806020018281038252838181518152602 00191508051906020019080838360005b838110156100fe5780820151818401526020810190506100e3565b50505050905090810190601f16801561012b 5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561014657600080fd5b506101e76004803 60381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437 8201915050505050509192919290803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526 02001838380828437820191505050505050919291929050505061036f565b005b600060606000836040518082805190602001908083835b602083101515 6102255780518252602082019150602081019050602083039250610200565b6001836020036101000a0380198251168184511680821785525050505050 509050019150509081526020016040518091039020600001546000846040518082805190602001908083835b6020831015156102945780518252602082
01915060208101905060208303925061026f565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602 0016040518091039020600101808054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152 6020018280546001816001161561010002031660029004801561035f5780601f106103345761010080835404028352916020019161035f565b8201919060 00526020600020905b81548152906001019060200180831161034257829003601f168201915b5050505050905091509150915091565b600080826040518 082805190602001908083835b6020831015156103a85780518252602082019150602081019050602083039250610383565b6001836020036101000a038 01982511681845116808217855250505050505090500191505090815260200160405180910390206000015414156105b25760408051908101604052804 28152602001838152506000826040518082805190602001908083835b60208310151561043257805182526020820191506020810190506020830392506 1040d565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008201518 160000155602082015181600101908051906020019061048b9291906106d5565b509050507f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4 637c7b10fb7ba56001428484604051808515151515815260200184815260200180602001806020018381038352858181518152602001915080519060200 19080838360005b838110156105095780820151818401526020810190506104ee565b50505050905090810190601f168015610536578082038051600183 6020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561056f5780820151818 40152602081019050610554565b50505050905090810190601f16801561059c5780820380516001836020036101000a031916815260200191505b509650 50505050505060405180910390a16106d1565b7f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba56000428484604051808515 15151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b8381101561062c578082 015181840152602081019050610611565b50505050905090810190601f1680156106595780820380516001836020036101000a031916815260200191505 b50838103825284818151815260200191508051906020019080838360005b83811015610692578082015181840152602081019050610677565b5050505
0905090810190601f1680156106bf5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a15b5050 565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061071657805160ff1916838001178555 610744565b82800160010185558215610744579182015b82811115610743578251825591602001919060010190610728565b5b50905061075191906107 55565b5090565b61077791905b8082111561077357600081600090555060010161075b565b5090565b905600a165627a7a723058200cc963b5380662be 325eaaa537af0e9a306526fff8cc23ece3f445e411517ec00029',
gas:'4700000'
},function(e, contract){
console.log(e, contract);
if(typeof contract.address !=='undefined'){
console.log('Contract mined! address: '+ contract.address +' transactionHash: '+ ansactionHash);
}
});
运⾏后在edge浏览器中按f12可看到
Contract mined! address:0xaa9c8fa7f44326f739d908b10144875039f1b46e transactionHash:0x5ea6d40fe39d8cbc6f9ae62cea4718046b8580abfda1ad2 71b9ae12c441622dc
我们得到了合约部署的地址,以及部署合约这个交易过程的交易号。
使⽤at⽅法获取已部署合约的引⽤
//使⽤上⾯部署合约时得到的合约地址
var proof = proofContract.at("0xaa9c8fa7f44326f739d908b10144875039f1b46e");
设置默认账户并调⽤合约
var proof = proofContract.at("0xaa9c8fa7f44326f739d908b10144875039f1b46e");
//设置默认账户
//调⽤proof合约的set函数
proof.set("League","a1b2c3");
//调⽤proof合约的get函数(通过异步回调获取结果)
<("a1b2c3",function(error,result){
if(!error){
console.log(TimeToString(result[0]));
console.log("timestamp: "+result[0]+"\towner: "+result[1]);
}
});
//timestamp转换为string表⽰
let TimeToString=function(timestamp){
let datetime =new Date();
datetime.setTime(timestamp);
let year = FullYear();
let month = Month()+1;
let date = Date();
let hour = Hours();
let minute = Minutes();
let second = Seconds();
let mseconds = Milliseconds();
return"date: "+year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second+"."+mseconds;
}
在浏览器中查看log得到如下输出:
date: 1970-1-20 0:51:21.953
index.html:59 timestamp: 1615881953 owner: League
可以看到这⾥的timestamp有些问题,⽽在Ganache中显⽰的是正确的时间,暂时还没到错误原因。事件监听
//通过合约⾥的事件名获取事件对象
var event = proof.logFileAddedStatus();
//开始监听,同时设置回调函数
event.watch(function(error,result){
if(!error){
console.log(result.args.status);
}else{
console.log(error);
}
});
事件所返回的数据通过result.args获取,例如:
result.args.status;
result.args.timestamp;
result.args.owner;
result.args.fileHash;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论