智能合约所碰到的坑

WINDOWS中安装WEB3始终不成功。

第一个坑,npm install web3  提示需要VC环境,安装VC环境也有问题

网上各种找资料,都是介绍以下方法:

npm install windows-build-tools

没有用。

最终解决方案为

npm install web3@^0.20.0 
第二个坑,truffle部署的地址并不是testrpc中的account[0].其地址应该是truffle migrate时部署的地址。

testrpc中的accounts[0]

truffle migrate中的地址.如果这个地址找错了,会让web3找不到合约。

第三个坑。web3调用truffle部署好的合约后需要定义defaultAccount,否则会报invalid address错误而不能转账。查账是没有问题的。


const Base = require ( './base.js' );
var Web3 = require ( "web3" );
//创建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" ));
}
var abi = [ { "constant" : true , "inputs" : [], "name" : "name" , "outputs" : [ { "name" : "" , "type" : "string" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "constant" : false , "inputs" : [ { "name" : "_spender" , "type" : "address" }, { "name" : "_value" , "type" : "uint256" } ], "name" : "approve" , "outputs" : [ { "name" : "" , "type" : "bool" } ], "payable" : false , "stateMutability" : "nonpayable" , "type" : "function" }, { "constant" : true , "inputs" : [], "name" : "totalSupply" , "outputs" : [ { "name" : "" , "type" : "uint256" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "constant" : false , "inputs" : [ { "name" : "_from" , "type" : "address" }, { "name" : "_to" , "type" : "address" }, { "name" : "_value" , "type" : "uint256" } ], "name" : "transferFrom" , "outputs" : [ { "name" : "" , "type" : "bool" } ], "payable" : false , "stateMutability" : "nonpayable" , "type" : "function" }, { "constant" : true , "inputs" : [], "name" : "INITIAL_SUPPLY" , "outputs" : [ { "name" : "" , "type" : "uint256" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "constant" : true , "inputs" : [], "name" : "decimals" , "outputs" : [ { "name" : "" , "type" : "uint8" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "constant" : false , "inputs" : [ { "name" : "_spender" , "type" : "address" }, { "name" : "_subtractedValue" , "type" : "uint256" } ], "name" : "decreaseApproval" , "outputs" : [ { "name" : "" , "type" : "bool" } ], "payable" : false , "stateMutability" : "nonpayable" , "type" : "function" }, { "constant" : true , "inputs" : [ { "name" : "_owner" , "type" : "address" } ], "name" : "balanceOf" , "outputs" : [ { "name" : "balance" , "type" : "uint256" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "constant" : true , "inputs" : [], "name" : "symbol" , "outputs" : [ { "name" : "" , "type" : "string" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "constant" : false , "inputs" : [ { "name" : "_to" , "type" : "address" }, { "name" : "_value" , "type" : "uint256" } ], "name" : "transfer" , "outputs" : [ { "name" : "" , "type" : "bool" } ], "payable" : false , "stateMutability" : "nonpayable" , "type" : "function" }, { "constant" : false , "inputs" : [ { "name" : "_spender" , "type" : "address" }, { "name" : "_addedValue" , "type" : "uint256" } ], "name" : "increaseApproval" , "outputs" : [ { "name" : "" , "type" : "bool" } ], "payable" : false , "stateMutability" : "nonpayable" , "type" : "function" }, { "constant" : true , "inputs" : [ { "name" : "_owner" , "type" : "address" }, { "name" : "_spender" , "type" : "address" } ], "name" : "allowance" , "outputs" : [ { "name" : "" , "type" : "uint256" } ], "payable" : false , "stateMutability" : "view" , "type" : "function" }, { "inputs" : [], "payable" : false , "stateMutability" : "nonpayable" , "type" : "constructor" }, { "anonymous" : false , "inputs" : [ { "indexed" : true , "name" : "owner" , "type" : "address" }, { "indexed" : true , "name" : "spender" , "type" : "address" }, { "indexed" : false , "name" : "value" , "type" : "uint256" } ], "name" : "Approval" , "type" : "event" }, { "anonymous" : false , "inputs" : [ { "indexed" : true , "name" : "from" , "type" : "address" }, { "indexed" : true , "name" : "to" , "type" : "address" }, { "indexed" : false , "name" : "value" , "type" : "uint256" } ], "name" : "Transfer" , "type" : "event" }];
var address = '0x3be29127330474399a7449b16a1ec01980e71690' ;
//通过abi以及合约部署的地址实例化一个coin
var coin = web3 . eth . contract ( abi ). at ( address );
//必须先定义defaultAccount,不然会报invalid address错
web3 . eth . defaultAccount = web3 . eth . accounts [ 0 ];
module . exports = class extends Base {
async indexAction () {

console . log ( web3 . eth . defaultAccount );
this . assign ( 'title11' , web3 . eth . coinbase );
await this . assign ( 'xianjin' , coin . balanceOf . call ( web3 . eth . coinbase ));
// console.log(coin.balanceOf(web3.eth.accounts[1]));
// coin.transferFrom(web3.eth.accounts[0],web3.eth.accounts[1], 800);
coin . transfer ( web3 . eth . accounts [ 1 ], 200 );
// console.log(web3.eth.accounts[1]);
return this . display ();
}
};

上面是thinkjs的一个controller 调用web3进行查账,转账200个币到accounts[1]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值