Search code examples
smartcontractsnearprotocol

Why do Env variables like signer_account_id cause error: ProhibitedInView?


I'm getting a ProhibitedInView error for a simple view function that does the following

export function getInfo(): Info {
   const caller = context.sender
   return infos.getSome(caller)
}

and realised that it's because of the context.sender call.

My assumption/understanding is that as long as a tx doesn't change the state, they can be considered ViewFunctions.

I found a warning against this on the Potential Gotchas, but why do the following functions cause view functions to fail? What does "binding methods that are exposed from nearcore" mean?

  • signer_account_id
  • signer_account_pk
  • predecessor_account_id

Solution

  • On the context of a view functions, there is no such thing as:

    • signer_account_id
    • signer_account_pk
    • predecessor_account_id

    It is not required to sign any message to run a view function, and moreover it is not allowed. It is more like inspecting a contract anonymously, nothing should be paid for that, and the result is independent from the caller. Because of this there is no signer_account_id and signer_account_pk.

    On the other hand, it is not possible to make a cross-contract call, if you are initiating this call in view mode. Because of this there is no predecessor_account_id available, since signer_account_id is not available, and it is impossible that this was called from another contract.