【发布时间】:2021-02-24 07:01:38
【问题描述】:
我已经使用 truffle 在以太坊节点上部署了一个简单的“HelloWorld”合约。我正在尝试使用节点 API 访问和调用合约内的函数。
下面是代码:
var Web3 = require('web3');
var express = require('express');
const app = express();
app.use(express.json());
const artifact = require('./../ethereumapp/example/smartContract/build/contracts/HelloWorld.json')
const contract = require('truffle-contract');
const HelloWorldContract = contract(artifact);
var web3 = new Web3('http://localhost:8000');
HelloWorldContract.setProvider(web3.currentProvider);
var contractapp = HelloWorldContract.at('0x3800e97896c6fdb614b9d011c9344dea32e49047').catch((err)={
//Error gets thrown here
console.error("Error in creating instance of HelloWorld :"+err)}
);
app.get('/', (req, res)=>{
res.send("Hey! The app is up and running");
});
app.get('/getMessage',(req,res)=>{
console.log('Invoking smart contract...');
res.send(contractapp.getMessage());
});
app.listen(3000, ()=>{
console.log("App started on 3000");
});
我是以太坊/松露的新手,不知道为什么会抛出这个错误。请提供任何可以帮助我解决此问题的示例 nodejs 代码。
【问题讨论】: