0

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);

};
  • Are you testing against testrpc? Did you restart testrpc? Can you share what are you doing between ob = TestObject.deployed() and ob.deposit(23)? How do you get myValue from the contract? It should be const val = ob.myValue(). – Ismael Oct 10 '17 at 1:12
  • Yeah I restarted testrpc. I don't do anything between those calls. const val = ob.myValue() gives the same error: ob.myValue is not a function. Is there any other information I can give that could help? – Richard Garfield Oct 10 '17 at 2:27
  • That is the problem, testrpc by default will start a new blockchain when it is restarted. You have to re-deploy your contracts with truffle migrate --reset. Or start testrpc in persistent mode testrpc --db mydir/ -d (First create an empty dir mkdir 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:43
  • Truffle stores the deployed address in the contract artifacts inside the build/ 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
  • @Ismael I restarted testrpc, then truffle migrate --reset. There are in fact .json artifacts present in /build. But unfortunately still no luck. – Richard Garfield Oct 10 '17 at 16:09
1

It may be that the contract was not deployed properly or that you don't have the contract instance object and there isn't much information to be able tell what exactly went wrong in the process. Try to re-deploy your contract using the following steps (in the node console) and see what happens:

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
code = fs.readFileSync('/path/to/contract/TestObject.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)
abiDefinition = JSON.parse(compiledCode.contracts[':TestObject'].interface)
TestContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':TestObject'].bytecode
deployedContract = TestContract.new({data: byteCode, from: web3.eth.accounts[0], gas: 45000000})
contractInstance = TestContract.at(deployedContract.address)

Obviously, make sure that you have either testrpc/geth running on localhost, port 8545.

Lastly, you can play around with the contractInstance object to invoke functions.

contractInstance.deposit(200, {from: web.eth.accounts[0]});
contractInstance.myValue.call() // public variables get getter functions by default
  • Thanks! I executed all the commands fine except the last one. I get Error: Invalid address passed to TestObject.at(): undefined. When examining deployedContract it appears to have a valid address... is there any information I can give you that would help? Im still pretty confused... – Richard Garfield Oct 10 '17 at 1:15
  • Notice that you are naming the contract as TestObject but the constructor's name is TeXtObject. This might not be the cause of your error, but anyways you'll have to fix it. Otherwise myValue will never be initialized at 1000. – pabloruiz55 Oct 10 '17 at 2:24
  • Oh good catch. Unfortunately I seem to have the same issue... – Richard Garfield Oct 10 '17 at 2:45
  • @RichardGarfield I think I had a bug in my code, too! TestObject.at() should be TestContract.at(). I have made the changes in my code above. Can you try now and see what happens? Can you see if deployedContract.address and contractInstance objects are finely constructed? – Manan Mehta Oct 10 '17 at 3:09
  • @MananMehta I was able to get your code to work, but I'm not sure what the next step is? Thank you again. – Richard Garfield Oct 10 '17 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.