zoukankan      html  css  js  c++  java
  • 智能合约遇到的三个大坑

    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]

  • 相关阅读:
    npm教程2
    02.RIP——CCNP学习笔记
    01.静态路由——CCNP学习笔记
    The 10th SWJTU ACM Online Tutorial
    visual studio 2005 常用按键
    Python垃圾回收机制
    私人网盘系统2.0—全部升级为layUI+PHP(持续更新中)
    Layui框架+PHP打造个人简易版网盘系统
    翻译app的开发全过程---编码+打包+上架
    值得认真学习的6 个 JavaScript 框架
  • 原文地址:https://www.cnblogs.com/xiaocongcong888/p/9497853.html
Copyright © 2011-2022 走看看