Reputation: 1498
I am new to Blockchain development. To learn purpose, started to develop small contracts using ethereum blockchain concept in Node.js
I have install the packages "solc": "^0.4.24"
and "web3": "^0.20.7"
to build and compile my contracts in Node.
My Grades.sol file:
pragma solidity ^0.4.24;
contract Grades {
mapping (bytes32 => string) public grades;
bytes32[] public studentList;
function Grades(bytes32[] studentNames) public {
studentList = studentNames;
}
function giveGradeToStudent(bytes32 student, string grade) public {
require(validStudent(student));
grades[student] = grade;
}
function validStudent(bytes32 student) view public returns (bool) {
for(uint i = 0; i < studentList.length; i++) {
if (studentList[i] == student) {
return true;
}
}
return false;
}
function getGradeForStudent(bytes32 student) view public returns (string) {
require(validStudent(student));
return grades[student];
}
}
And my compile.js file.
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');
const helloPath = path.resolve(__dirname,'contracts','Grades.sol');
const source = fs.readFileSync(helloPath,'UTF-8');
compiledCode = solc.compile(source);
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abiDefinition = JSON.parse(compiledCode.contracts[':Grades'].interface);
GradesContract = web3.eth.contract(abiDefinition);
byteCode = compiledCode.contracts[':Grades'].bytecode
deployedContract = GradesContract.new(['John','James'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000});
deployedContract.giveGradeToStudent('John', 'A+', {from: web3.eth.accounts[0]});
I tried to compile and run i am getting the exception
TypeError: deployedContract.giveGradeToStudent is not a function
In deployedContract, I am able to see the methods. Can anyone help me on this?
Note: I have installed "ganache-cli": "^6.1.8"
for adding the Transactions and running sepratly. I am able to see the transactions in ganache.
Upvotes: 0
Views: 124
Reputation: 60153
I believe the issue is here:
deployedContract = GradesContract.new(['John','James'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000});
.new()
here won't return the deployed contract instance. (I'm not sure it returns anything?) You need a callback, like so:
deployedContract = GradesContract.new(
['John', 'James'],
{data: byteCode, from: web3.eth.accounts[0], gas: 4700000},
function (err, instance) {
if (instance.address) {
instance.giveGradeToStudent(...);
}
}
);
Upvotes: 1