0

Say I have a published smart contract with a variable:

bool public myvariable = true;

Which is used in the contract later on by way of if's which check whether myvariable is true or false.

How do I then change that variable, how do I communicate with the contract and what on earth do I tell it, in order to flip myvariable to false, say?

I suspect that this is a very basic question but the lack (absence?) of true beginners solidity information means I have to ask stupid questions. So far I've been using MEW to publish the contracts, and that has an "interact with contract" facility, offering me an ABI/JSON field to enter, presumably, the communication I wish to have with the contract?

Also, how do I stop just anyone triggering the same change?

Really not sure how to proceed from here. One idea I had was to code it so that the true/false state is flipped when a certain precise value of eth is sent to the contract, by my particular address. The precise amount of eth simply serving as a message of sorts. But I don't think that would "permanently" change the true/false, anyway.

Thanks for any suggestions.

1

You need to write a setter function in your contract.

something like

function set_myVariable(bool newVal) public{
    myVariable = newVal;
} 

To prevent anyone from calling this, you need to use "require" in your function, to make sure msg.sender is the contract owner.

If you aren't already, I suggest using Remix to play with, test and deploy your code.

  • Thank you - that's very useful. But then how do I go about triggering that function in the deployed smart contract? What code do I send to the contract in order to do that, and will the state change (from false to true, say) be "permanent", i.e. remain that way until I later flip it back, perhaps? – John Mar 14 '18 at 11:13
0

AnAllergy is correct. The only way to interact with a smart contract is through a function call. So you will need to write a setter function which allows your boolean variable to be changed based on the conditions you define.

Here is AnAllergy's code with the extra require statement. I would suggest adding a member variable of type address called owner to your contract. And set owner = msg.sender in your contract's constructor function.

function set_myVariable(bool newVal) public{
    require(msg.sender == owner);
    myVariable = newVal;
} 
  • Thank you - that's very useful. But then how do I go about triggering that function in the deployed smart contract? What code do I send to the contract in order to do that, and will the state change (from false to true, say) be "permanent", i.e. remain that way until I later flip it back, perhaps? – John Mar 14 '18 at 11:13
  • You can interact w contracts via web3. Basically, connect to the node running the blockchain (local or remote) and then use the web3 library to write scripts that interact w the contract. – Ryan Miller Mar 16 '18 at 2:33

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.