0

There are 3 contracts; A, B, and C. C has a state variable with a default value. A changes that state variable to Yerevan. After that, B sees that variable. Evaluating by A is successful but B sees the default value again. It appears that B cannot call a updated smart contract. How can i change a state variable by an other smart contract permanently? is there any related work? please guide a beginner man. Thank you.

C:

pragma solidity ^0.5.12;

contract C{
   bytes public name;
   function rep(bytes memory pseudonym)  public returns(string memory){
       name=pseudonym;
       return string(name);
   }
   }

A:

pragma solidity ^0.5.12;
import "./C.sol";
contract A{
   bytes public fname="Yerevan";
   function naming() public returns(string memory){
      return C(0x8fcECc5B8d42EcF099E1cF032B1F505fbDA230A3).rep(fname);
  }
  }

B:

pragma solidity ^0.5.12;
import "./C.sol";
contract B{
   function rep() public returns(string memory){
      return string(C(0x8fcECc5B8d42EcF099E1cF032B1F505fbDA230A3).name());
   }
   }
3
  • So what exactly is C c=new C() used for in here??? Commented Feb 14, 2020 at 19:04
  • It has to be deleted. Sorry for that.
    – Alireza
    Commented Feb 14, 2020 at 19:04
  • It was deleted.
    – Alireza
    Commented Feb 14, 2020 at 19:05

3 Answers 3

3

Note that, in Truffle console, you should try naming() just like:

A_instance.naming.sendTransaction()

or

A_instance.naming()

This sends a transaction to network that causes your desired code be run. This process mutates ledger and contracts' states. Using .call() instead .sendTransaction() just makes an interaction between two contracts without mutating their states. It causes their previous state don't mutate and you don't see the new values.

1

There are some cases maybe you didn't pay attention:

  1. You are executing naming() from A by .call() not by .sendTransaction(). Obviously, it never mutates the state. I did guess that because of truffle-contract that there is in the tags list of your question.
  2. Maybe, you made a mistake in inserting the real address of C when you call it in both A and B. I mean here: C(0x8fcECc5B8d42EcF099E1cF032B1F505fbDA230A3).rep(...). That can be address of another deployed C.
  3. The same case @Rob Hitchens answered.
0

Not enough is known about your tool stack and testing methodology to be sure. The comment about new C() suggests a situation that might have been enough to create unexpected results.

Intuitively, it may be about waiting for confirmation. It is almost a rite of passage to get confused about ti.

  • A is mutating the state, and that means nothing really happens until the transaction is mined. Mining/confirmation might take some time.

  • B is inspecting the state, and the Ethereum node will inspect its local copy of the blockchain without waiting. In particular, without waiting for A to update. It's entirely possible you might think A failed to do its job because you peeked too soon.

The asynchronous nature of this platform takes some getting used to.

Have a look at this explainer to see if any insights or oversights jump out. https://blog.b9lab.com/calls-vs-transactions-in-ethereum-smart-contracts-62d6b17d0bc2

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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