web3.js部署合约

合约的编写

首先在remix上编写Proof.sol智能合约代码

pragma solidity ^0.4.0;

contract Proof{
    struct FileDetails{
        uint timestamp;
        string owner;
    }
    
    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 = new web3.eth.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":"string"},{"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"},{"indexed":false,"name":"fileHash","type":"string"}],"name":"logFileAddedStatus","type":"event"}]);
//部署合约
var proof = proofContract.deploy({
     data: '0x608060405234801561001057600080fd5b506107a6806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063693ec85e14610051578063e942b5161461013a575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101e9565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156100fe5780820151818401526020810190506100e3565b50505050905090810190601f16801561012b5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561014657600080fd5b506101e7600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061036f565b005b600060606000836040518082805190602001908083835b6020831015156102255780518252602082019150602081019050602083039250610200565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600001546000846040518082805190602001908083835b602083101515610294578051825260208201915060208101905060208303925061026f565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600101808054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561035f5780601f106103345761010080835404028352916020019161035f565b820191906000526020600020905b81548152906001019060200180831161034257829003601f168201915b5050505050905091509150915091565b600080826040518082805190602001908083835b6020831015156103a85780518252602082019150602081019050602083039250610383565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390206000015414156105b2576040805190810160405280428152602001838152506000826040518082805190602001908083835b602083101515610432578051825260208201915060208101905060208303925061040d565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008201518160000155602082015181600101908051906020019061048b9291906106d5565b509050507f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600142848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b838110156105095780820151818401526020810190506104ee565b50505050905090810190601f1680156105365780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561056f578082015181840152602081019050610554565b50505050905090810190601f16801561059c5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a16106d1565b7f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600042848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b8381101561062c578082015181840152602081019050610611565b50505050905090810190601f1680156106595780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b83811015610692578082015181840152602081019050610677565b50505050905090810190601f1680156106bf5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a15b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061071657805160ff1916838001178555610744565b82800160010185558215610744579182015b82811115610743578251825591602001919060010190610728565b5b5090506107519190610755565b5090565b61077791905b8082111561077357600081600090555060010161075b565b5090565b905600a165627a7a723058200cc963b5380662be325eaaa537af0e9a306526fff8cc23ece3f445e411517ec00029', 
     arguments: [
     ]
}).send({
     from: web3.eth.accounts[0], 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

这里导出的javascript代码可用来部署智能合约,但是我们下载的是0.19版本的web3,而上述代码是基于1.*版本web3的,因此需要进行一定的修改。主要是如下方面:

第一行new web3.eth.Contract改为web3.eth.contract。 (注意大小写)

第二行proofContract.deploy改为proofContract.new。

proofContract.new的第一个参数需要进行修改,删掉arguments和send,并将send的内容和data放在一起,具体以下面的代码为准。

修改后的部署代码如下:

var proofContract = web3.eth.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":"string"},{"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"},{"indexed":false,"name":"fileHash","type":"string"}],"name":"logFileAddedStatus","type":"event"}]);
var proof = proofContract.new({
    from: web3.eth.accounts[0], 
    data: '0x608060405234801561001057600080fd5b506107a6806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063693ec85e14610051578063e942b5161461013a575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101e9565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156100fe5780820151818401526020810190506100e3565b50505050905090810190601f16801561012b5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561014657600080fd5b506101e7600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061036f565b005b600060606000836040518082805190602001908083835b6020831015156102255780518252602082019150602081019050602083039250610200565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600001546000846040518082805190602001908083835b602083101515610294578051825260208201915060208101905060208303925061026f565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600101808054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561035f5780601f106103345761010080835404028352916020019161035f565b820191906000526020600020905b81548152906001019060200180831161034257829003601f168201915b5050505050905091509150915091565b600080826040518082805190602001908083835b6020831015156103a85780518252602082019150602081019050602083039250610383565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390206000015414156105b2576040805190810160405280428152602001838152506000826040518082805190602001908083835b602083101515610432578051825260208201915060208101905060208303925061040d565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008201518160000155602082015181600101908051906020019061048b9291906106d5565b509050507f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600142848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b838110156105095780820151818401526020810190506104ee565b50505050905090810190601f1680156105365780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561056f578082015181840152602081019050610554565b50505050905090810190601f16801561059c5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a16106d1565b7f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600042848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b8381101561062c578082015181840152602081019050610611565b50505050905090810190601f1680156106595780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b83811015610692578082015181840152602081019050610677565b50505050905090810190601f1680156106bf5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a15b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061071657805160ff1916838001178555610744565b82800160010185558215610744579182015b82811115610743578251825591602001919060010190610728565b5b5090506107519190610755565b5090565b61077791905b8082111561077357600081600090555060010161075b565b5090565b905600a165627a7a723058200cc963b5380662be325eaaa537af0e9a306526fff8cc23ece3f445e411517ec00029', 
    gas: '4700000'
}, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
});

运行后在edge浏览器中按f12可看到

Contract mined! address: 0xaa9c8fa7f44326f739d908b10144875039f1b46e transactionHash: 0x5ea6d40fe39d8cbc6f9ae62cea4718046b8580abfda1ad271b9ae12c441622dc

我们得到了合约部署的地址,以及部署合约这个交易过程的交易号。

使用at方法获取已部署合约的引用
//使用上面部署合约时得到的合约地址
var proof = proofContract.at("0xaa9c8fa7f44326f739d908b10144875039f1b46e");
设置默认账户并调用合约
var proof = proofContract.at("0xaa9c8fa7f44326f739d908b10144875039f1b46e");
//设置默认账户
web3.eth.defaultAccount = web3.eth.accounts[0];
//调用proof合约的set函数
proof.set("League","a1b2c3");
//调用proof合约的get函数(通过异步回调获取结果)
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 = datetime.getFullYear();
    let month = datetime.getMonth()+1;
    let date = datetime.getDate();
    let hour = datetime.getHours();
    let minute = datetime.getMinutes();
    let second = datetime.getSeconds();
    let mseconds = datetime.getMilliseconds();
    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;

你可能感兴趣的:(以太坊智能合约,#,web3.js)