日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

智能合约遇到的三个大坑

發(fā)布時(shí)間:2025/7/25 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 智能合约遇到的三个大坑 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

WINDOWS中安裝WEB3始終不成功。

第一個(gè)坑,npm install web3 提示需要VC環(huán)境,安裝VC環(huán)境也有問題

網(wǎng)上各種找資料,都是介紹以下方法:

npm install windows-build-tools
沒有用。

最終解決方案為

npm install web3@^0.20.0
第二個(gè)坑,truffle部署的地址并不是testrpc中的account[0].其地址應(yīng)該是truffle migrate時(shí)部署的地址。
testrpc中的accounts[0]

truffle migrate中的地址.如果這個(gè)地址找錯(cuò)了,會(huì)讓web3找不到合約。

第三個(gè)坑。web3調(diào)用truffle部署好的合約后需要定義defaultAccount,否則會(huì)報(bào)invalid address錯(cuò)誤而不能轉(zhuǎn)賬。查賬是沒有問題的。

const Base = require('./base.js');
var Web3 = require("web3");
//創(chuàng)建web3對(duì)象

// 連接到以太坊節(jié)點(diǎn)
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以及合約部署的地址實(shí)例化一個(gè)coin
var coin = web3.eth.contract(abi).at(address);
//必須先定義defaultAccount,不然會(huì)報(bào)invalid address錯(cuò)
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的一個(gè)controller 調(diào)用web3進(jìn)行查賬,轉(zhuǎn)賬200個(gè)幣到accounts[1]

轉(zhuǎn)載于:https://www.cnblogs.com/xiaocongcong888/p/9497853.html

總結(jié)

以上是生活随笔為你收集整理的智能合约遇到的三个大坑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。