[Ethereum] How to return the index value of an Enum

contract-developmentjavascriptsoliditytruffle

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?

Best Answer

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.

Try:

> 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
Escrow.deployed()
.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;
   console.log(state.toString(10);
});

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.

Related Topic