1

I have followed this tutorial to create a cross-contract calling in ink. There are two contracts in the example accumulator and delegator. The accumulator contract has two functions and the delegator contract can access those functions. But if I do an operation using increasing a value using the accumulator contract, then the delegator contract cannot access that value. The delegator contract can access only those values that are created by its functions though and not the values of the Accumulator. How can the delegator contract access the values generated by the accumulator contract?

The contracts of the accumulator and delegator can be found here

3
  • When a smart contract call function of another smart contract, then it can read the state of another contract but modify state is not possible because smart contract operates on sandbox environment.
    – Ganesh11
    Feb 28, 2023 at 6:40
  • what do you mean by access the values? you can not access contract's fields directly, in this case you can get it with get() and increase it with inc() methods. The contract's state is encapsulated and only the contract can change its state via external calls
    – kriko.eth
    Feb 28, 2023 at 12:29
  • what i meant by accessing values is as follows: The accumulator contract increase the value using the inc function by 5. Hence the value is 5 if you use get function in accumulator. When i access the get function from delegator, it retruns zero and not 5. Is it possible to get the value set by accumulator contract in delegator contract? Feb 28, 2023 at 13:43

1 Answer 1

1

The reason why you are getting a different value is because the accumulator contract is instantiated twice. First, when you uploaded and instantiated the accumulator contract. Second, when you uploaded and instantiated the delegator contract.

In other words, when you increment the accumulator value directly through the accumulator contract, you are calling the first instance you created of the contract. When you call the get() message of the accumulator contract through the delegator contract, you are calling the second instance you created of the accumulator contract. This is because when you instantiated the delegator contract it also instantiated a new accumulator contract (see code).

So if you want to call the get() message of the first instance of the accumulator contract through the delegator contract you need the Contract in stead of the Code hash, aka the AccountId of the contract. With the fn build_call() you then can build & execute a call to another contract. With the selector you can specify which message to call within a contract.

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.