This is probably a very simple problem to solve, but I am setting up a node.js backend using express for my Dapp and I can now deploy new contracts just fine.
I now need to figure out how to make calls to a function in an existing contract (created previously in Remix). I have the following syntax for setting up the provider. This works, at least for creating a new Insurer:
const Web3 = require("web3");
const HDWalletProvider = require("@truffle/hdwallet-provider");
require('dotenv').config();
const provider = new HDWalletProvider(process.env.MNEMONIC, process.env.INFURA_URL);
var contract = require("@truffle/contract");
const insurerArtifacts = require('./build/contracts/Insurer.json');
var myInsurerContract = contract(insurerArtifacts);
myInsurerContract.setProvider(provider);
const platformArtifacts = require('./build/contracts/Platform.json');
var myPlatformContract = contract(platformArtifacts);
myPlatformContract.setProvider(provider);
I know that this is wrong, but I do not know how to do it properly. The function call I make is as follows:
var insurerAddress = '0x...';
var myClientUIC = req.body.clientUIC;
var myClientName = req.body.clientName;
var myClientGroup = req.body.clientGroup;
var myClientNACE = req.body.clientNACE;
var myCreditLine = parseInt(req.body.creditLine);
var myCreditLineDate = parseInt(req.body.creditLineDate);
var myClientCurrency = req.body.clientCurrency;
var myClientCity = req.body.clientCity;
var myClientCountry = req.body.clientCountry;
myPlatformContract.at("0x...").methods.createClient(myClientUIC, myClientName, myClientGroup,
myClientNACE, insurerAddress, myCreditLine, myCreditLineDate, myClientCurrency, myClientCity, myClientCountry,
{ from:provider.getAddress() })
.once('transactionHash', function(hash) {
console.log("TxHash: " + hash);
}).on('receipt', function(receipt) { console.log("receipt: " + JSON.stringify(receipt)); })
.then(function(result) {
}, function(error) {
console.log(error);
});
The error I get is:
TypeError: Cannot read property 'createClient' of undefined
However, createClient is a function and I have checked that all the parameters have values. Does createClient itself need to be called differently?
I would appreciate any help you an give me with this beginner's issue! Many thanks. Phil