I was wondering if, when using the FA2 library, there is a way to have the contract as a default operator so that users don't have to call update_operators ?
Tezos Stack Exchange is a question and answer site for users, experts, and developers of the Tezos blockchain project. It only takes a minute to sign up.Sign up to join this community
Yes, you can do this by creating a new transfer policy.
Warning ⚠️ : having the contract be operator by default may introduce a security flaw or be totally fair depending on your contract's logic.
Here how to define a new policy:
import smartpy as sp FA2 = sp.io.import_script_from_url("https://smartpy.io/templates/fa2_lib.py") # Inherits from the OwnerOrOperatorTransfer policy # but you can choose another base policy. class MyPolicy(FA2.OwnerOrOperatorTransfer): # Explicitely change the policy name. # This whole method can be remove if you want to keep the base policy name. def init_policy(self, contract): FA2.OwnerOrOperatorTransfer.init_policy(self, contract) self.name = "this-contract-or-owner-or-operator-transfer" # Redefine the transaction transfer's permissions. def check_tx_transfer_permissions(self, contract, from_, to_, token_id): sp.verify( (sp.sender == sp.self_address) | (sp.sender == from_) | contract.data.operators.contains( sp.record(owner=from_, operator=sp.sender, token_id=token_id) ), message="FA2_NOT_OPERATOR", ) def is_operator(self, contract, operator_permission): # Add self_address to is_operator (used in offchain view) return (sp.sender == sp.self_address) | contract.data.operators.contains(operator_permission)
Here how you set the policy as a named argument when instanciating your FA2.
c1 = FA2.Fa2<BaseClass>(..., policy=MyPolicy()).
Here is an example.