web3 部署合约
代码:
let Web3 = require('web3');
let web3;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
let from = web3.eth.accounts[0];
//编译合约
let transferCompiled = [
{
"constant": false,
"inputs": [],
"name": "deposit",
"outputs": [],
"payable": true,
"type": "function",
"stateMutability": "payable"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_from",
"type": "address"
},
{
"indexed": true,
"name": "_to",
"type": "address"
},
{
"indexed": true,
"name": "value",
"type": "uint256"
}
],
"name": "transfer",
"type": "event"
},
{
"payable": false,
"type": "fallback",
"stateMutability": "nonpayable"
},
{
"constant": true,
"inputs": [],
"name": "getBanlance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function",
"stateMutability": "view"
}
]
//得到合约对象
//let abiDefinition = transferCompiled["info"]["abiDefinition"];
let transferContract = web3.eth.contract(transferCompiled);
//2. 部署合约
//2.1 获取合约的代码,部署时传递的就是合约编译后的二进制码
let deployCode = "0x608060405234801561001057600080fd5b50610143806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063d0e30db01461005b578063d228d48414610065575b34801561005857600080fd5b50005b610063610090565b005b34801561007157600080fd5b5061007a6100f8565b6040518082815260200191505060405180910390f35b600080309150349050808273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fbeabacc8ffedac16e9a60acdb2ca743d80c2ebb44977a93fa8e483c74d2b35a860405160405180910390a45050565b60003073ffffffffffffffffffffffffffffffffffffffff16319050905600a165627a7a72305820e5a2991b09af8f2c7b994339693559b0142a9b3461f28d6b0b88887a707a07a50029";
//2.2 部署者的地址,当前取默认账户的第一个地址。
let deployeAddr = web3.eth.accounts[0];
console.log(deployeAddr);
//2.3 异步方式,部署合约
//警告,你不应该每次都部署合约,这里只是为了提供一个可以完全跑通的例子!!!
transferContract.new({
data: deployCode,
from: deployeAddr,
gas: 1000000
}, function(err, myContract) {
if (!err) {
// 注意:这个回调会触发两次
//一次是合约的交易哈希属性完成
//另一次是在某个地址上完成部署
// 通过判断是否有地址,来确认是第一次调用,还是第二次调用。
if (!myContract.address) {
console.log("contract deploy transaction hash: " + myContract.transactionHash) //部署合约的交易哈希值
// 合约发布成功后,才能调用后续的方法
} else {
console.log("contract deploy address: " + myContract.address) // 合约的部署地址
console.log("Current balance: " + myContract.getBanlance());
var event = myContract.transfer();
// 监听
event.watch(function(error, result){
console.log("Event are as following:-------");
for(let key in result){
console.log(key + " : " + result[key]);
}
console.log("Event ending-------");
});
//使用transaction方式调用,写入到区块链上
myContract.deposit.sendTransaction({
from: deployeAddr,
value: 100,
gas: 1000000
}, function(err, result){
console.log("Deposit status: " + err + " result: " + result);
console.log("After deposit balance: " + myContract.getBanlance());
//终止监听,注意这里要在回调里面,因为是异步执行的。
event.stopWatching();
});
}
}
else
console.log(err);
});
结果:
此处的code 是手动到命令行上编译生成的,web3的编译语句不能用
部署完成后,在私有链上调用代码操作可查看在geth客户端调用已部署的智能合约
调用过程如下:
1)获取合约地址:
合约地址即为上图的 contract deploy address
2)获取abi:abi在remix复制
3)获取合约
4)调用
此处的getBalance只是获取状态,因此不需要消耗gas:
这次调用的是deposite
函数,因为会导致区块链的数据发送改变,所以需要使用sendTransaction
:
结果如下:调用了四次,但因为出块需要时间,而call不需要对区块进行改写,执行时间很短,所以此时getBalance()得到的是上一次交易后的结果
最终结果为 500:
若想实时获得交易结果,需要使用 solidity 的 event。event 可以实现web3 js与交易执行同步,交易一完成,web3即可获得交易结果。
solidity 源码为:
pragma solidity ^0.4.0;
contract Transfer{
event transfer(address indexed _from, address indexed _to, uint indexed value);
function deposit() payable {
address current = this;
uint value = msg.value;
transfer(msg.sender, current, value);
current.transfer(10);
}
function getBanlance() constant returns(uint)
{
return this.balance;
}
/* fallback function */
function(){}
}
部署完后的调用代码(diaoyong.js)
//部署完后的调用代码
let Web3 = require('web3');
let web3;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
console.log("good");
//abi,直接在remix 里面复制
var abi = [{"constant": false,"inputs": [],"name": "deposit","outputs": [],"payable": true,"type": "function","stateMutability": "payable"},{"constant": true,"inputs": [],"name": "getBanlance","outputs": [{"name": "","type": "uint256"}],"payable": false,"type": "function","stateMutability": "view"},{"payable": false,"type": "fallback","stateMutability": "nonpayable"},{"anonymous": false,"inputs": [{"indexed": true,"name": "_from","type": "address"},{"indexed": true,"name": "_to","type": "address"},{"indexed": true,"name": "value","type": "uint256"}],"name": "transfer","type": "event"}];
//合约地址
var address = web3.eth.accounts[0];
var metacoin = web3.eth.contract(abi).at(address);
var t = metacoin.getBanlance.call();
console.log(t['c']);
let a = web3.eth.accounts[0];
metacoin.deposit.sendTransaction({from:a,value:100,gas:100000
},function(err,result){
console.log(err + "result :" + result);
});
console.log(metacoin.getBanlance.call());