智能合约遇到的三个大坑
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]
轉載于:https://www.cnblogs.com/xiaocongcong888/p/9497853.html
總結
以上是生活随笔為你收集整理的智能合约遇到的三个大坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3 常用属性
- 下一篇: fatal: Unable to fin