I have a simple test contract that I've deployed using truffle:
pragma solidity ^0.4.4;
contract TestObject {
uint public myValue;
function TextObject() {
myValue = 1000;
}
function deposit(uint value) returns(uint) {
myValue += value;
return myValue;
}
}
If I do
var ob = TestObject.deployed();
ob
I can see that my object has been deployed:
.
.
.
myValue:
{ [Function]
call: [Function],
sendTransaction: [Function],
request: [Function: bound ],
estimateGas: [Function] },
TextObject:
{ [Function]
call: [Function],
sendTransaction: [Function],
request: [Function: bound ],
estimateGas: [Function] },
deposit:
{ [Function]
call: [Function],
sendTransaction: [Function],
request: [Function: bound ],
estimateGas: [Function] },
sendTransaction: [Function],
send: [Function],
allEvents: [Function: bound ],
address: '0xe43d2d9d7d0cb6d13100ded5548a78139d479047',
transactionHash: null }
Also myValue seems to be present.
When I call ob.myValue
I get undefined
If I try to set the value:
ob.deposit(23);
I get:
TypeError: ob.deposit is not a function
at evalmachine.<anonymous>:1:4
at ContextifyScript.Script.runInContext (vm.js:35:29)
at Object.runInContext (vm.js:89:6)
at TruffleInterpreter.interpret (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:209587:17)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:538:10)
at emitOne (events.js:96:13)
at REPLServer.emit (events.js:188:7)
at REPLServer.Interface._onLine (readline.js:233:10)
Anyone know whats going on here?
Any help would be much appreciated. Thanks!
EDIT Here's my deployment code in migrations/2_deploy_contracts.js:
var Adoption = artifacts.require("./Adoption.sol");
var TestObject = artifacts.require("./TestObject.sol");
const Storage = artifacts.require("./Storage.sol");
module.exports = function(deployer) {
deployer.deploy(Adoption);
deployer.deploy(TestObject);
deployer.deploy(Storage);
};
ob = TestObject.deployed()
andob.deposit(23)
? How do you getmyValue
from the contract? It should beconst val = ob.myValue()
. – Ismael Oct 10 '17 at 1:12truffle migrate --reset
. Or start testrpc in persistent modetestrpc --db mydir/ -d
(First create an empty dirmkdir mydir
).--db
is where testrpc will store its blockchain, next invocation will start where you left,-d
generates the same address in each invocation more about testrpc options. – Ismael Oct 10 '17 at 4:43build/
directory, and will it use when needed. If the blockchains has been restarted then there will be no contract at that addres, and it will fail like in your case,deployed()
returns undefined because there's no code at the contract address. – Ismael Oct 10 '17 at 4:50