3

I am exploring an FA2 implementation where the storage and the business logic are in different smart contracts (a pattern for upgradability)

This case could see instance of both updating the item in external storage and checking it via view multiple times in the same call– e.g. where a transfer call has a batch of transfer instructions, you would be calling the on-chain view and updating its underlying data (the ledger) multiple times.

For this to be safe, it would be critical that the view is receiving the "new" data that is being updated during execution.

e.g.

  1. Alice has balance of 1 on token_id 0
  2. Alice submits a "double-spend" sending token_id 0 to Bob and Carol
  3. view checks that Alice's balance = 1
  4. transfer to Bob is can be processed
  5. view checks that Alice's balance = 0
  6. transfer to Carol cannot be processed and entire transaction is reverted.

What I am unsure about in the mechanics of the VM is whether the view call in step 5 actually fetches the data that is updated during execution, or the data before the execution

1 Answer 1

2

View calls are executed immediately but the transfers are not (they are delayed until the end of the execution of the script) so in particular, calling the same view of the same contract several times in the same script execution returns the same result.

To achieve what you want you need to either:

  • represent balances using a type that cannot go negative (such as nat or mutez), this is usually the simplest way to do this, or
  • check that the balance of Alice is high enough to honor all the pending transfers, or
  • check that Alice's balance is not negative after all the transfers by performing an extra contract call (typically a recursive call on a specific entrypoint but you can also have a dedicated contract for this).

Your Answer

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

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