I'm a beginner trying to interact with a simple contract via web3, and I'm having some problems that I can't figure out.
I've written a basic contract with a struct and two functions. This is basically copied from here: Returning a struct and reading via Web3
contract MyContract {
struct MyStruct {
address a;
uint b;
}
MyStruct[] public MyStructs;
function getStruct(uint index) public constant returns (address, uint) {
return (MyStructs[index].a, MyStructs[index].b);
}
function addStruct(address _a, uint _b) public returns (uint){
MyStruct memory a = MyStruct(_a, _b);
MyStructs.length++;
MyStructs.push(a);
return MyStructs.length;
}
}
I have two test functions that I'm invoking using truffle's test feature. These are basically copied from http://truffleframework.com/docs/getting_started/javascript-tests
var MyContract = artifacts.require("MyContract");
contract('MyContract', function() {
it("should create a stuct", function() {
return MyContract.deployed().then(function(instance) {
return instance.addStruct.call(0xf17f52151EbEF6C7334FAD080c5704D77216b732, 1000);
}).then(function(length) {
console.log(length);
assert.equal(length, 1, "length is not 1");
});
});
it("get the struct that was previously created", function() {
return MyContract.deployed().then(function(instance) {
return instance.getStruct.call(0);
}).then(function(values) {
console.log(values);
assert.equal(values, (0xf17f52151EbEF6C7334FAD080c5704D77216b732, 1000));
});
});
});
When I run this, I get the following:
Contract: MyContract { [String: '2'] s: 1, e: 0, c: [ 2 ] }
1) should create a stuct
> No events were emitted
2) get the struct that was previously created
> No events were emitted
0 passing (190ms) 2 failing
1) Contract: MyContract should create a stuct:
AssertionError: length is not 1: expected { Object (s, e, ...) } to equal 1
at test/mycontract.js:10:15
at process._tickCallback (internal/process/next_tick.js:103:7)
2) Contract: MyContract get the struct that was previously created:
Error: Invalid JSON RPC response: {"id":21,"jsonrpc":"2.0"}
at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41483:16)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:330353:36
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:326008:9
at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:329052:7)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176427:18)
at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176717:12)
at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176872:12)
at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176832:24)
In other words, when it gets to console.log(length), which should be the response of the addStruct function, it is instead getting { [String: '2'] s: 1, e: 0, c: [ 2 ] }
, and then getStruct errors out with Error: Invalid JSON RPC response: {"id":21,"jsonrpc":"2.0"}
Any idea why this is happening? Both of these things mean nothing to me, so I don't even know what to Google. I've looked at the Truffle and Solidity documentation, and as far as I can tell, I am doing everything correctly.
Perhaps unrelated, but I'm including just in case it isn't -- previously, I had deployed this to Ganache successfully and was playing around in the browser console. I was able to call web3.eth.contract(abi) and get what appears to be the correct response, but when I added the address as an argument, it refused to give me anything except "invalid address." I confirmed in the web3 and ganache documentation that I was doing it right about 10 times, so I'm thinking that maybe something else is wrong. Or maybe not -- feel free to ignore this part.