I am trying to build a simple double escrow contract. I have an enum for the state:

enum State { Created, Locked, Inactive }
State public state;

When I try to interact with the contract through the truffle console, I can get the enum type to return:

enter image description here

but I don't understand how to make comparisons with the returned value. Can someone tell me how to compare the returned value?


It's a BigNumber: https://github.com/ethereum/wiki/wiki/JavaScript-API#a-note-on-big-numbers-in-web3js

Try this to get the idea:

> var stateReadable = state.toString(10);
> stateReadable

Hope it helps.

Update ... this will be a little rough because it's a picture and not code I can play with. You're returning a promise with this part. return instance.state(). Now, you have to do something with that promise.

var state = is a mistake, because you don't want it to be a promise.

I'm going to break it down a little bit for readability and illustration.


> var state;
> var contract;
> Escrow.deployed().then(function(instance) { contract = instance; });
> contract.state().then(function(value) { state = value) });
> state.toString(10);

As a scripted thing, I would probably chain these so things don't thunder ahead too fast. Wait for the instance, then query the function.

var state;
var escrow; // more conventional naming
.then(function(instance) { 
   escrow = instance; 
   return escrow.state(); // this way we know it won't get called until the instance returns
.then(function(response) { // now we do something with the promise we returned.
   state = response;

See how the promises are starting to form a chain? You can keep returning promises, then close up the block and append .then(... to pass the results into the next step. In other words, if you need another interaction with the contract after my console.log(), you would return the function, and then carry on at }); with another .then().

Just going by sound here, so I hope I didn't flub the syntax.

Hope it helps.

  • Thank you for the answer, but instead of returning an int, it is returning a promise. – Nepoche Jul 24 '17 at 3:12
  • I see what you mean. Updating. – Rob Hitchens - B9lab Jul 24 '17 at 3:18
  • Wow thank you so much! The explanation really helps as well. – Nepoche Jul 24 '17 at 3:39

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.