Help doing opt-in of ASA to smart contract

Greetings Algofam!

I want to opt-in my smart contract into an ASA through an inner transaction. Here is my code:

    @Subroutine(TealType.none)
    def opt_in_smart_contract_to_asa(asa_id):
     
        return Seq([
        
        InnerTxnBuilder.Begin(),

            InnerTxnBuilder.SetFields({
                TxnField.type_enum : TxnType.AssetTransfer,
                TxnField.xfer_asset : asa_id,
                TxnField.asset_amount : Int(0),
                TxnField.asset_sender : Global.current_application_address(),
                TxnField.asset_receiver: Global.current_application_address(),
                TxnField.fee : Int(6_000)
                }),

        InnerTxnBuilder.Submit()
            
        ])


    @Subroutine(TealType.none)
    def opt_in_transaction():

        asa_id = Btoi( Txn.application_args[1] )
        smart_contract_asset_balance = AssetHolding.balance( Global.current_application_address(), asa_id ) 
        

        # if smart contract is not opted in to the asset, 
        # call smart_contract_opt_in_to_asa()
        return Seq([
            smart_contract_asset_balance,

            # below if statement returns 1 if the smart contract is not opted in to asset
            If( Not(smart_contract_asset_balance.hasValue()) ).
                # if smart contract is not opted in to the asset, 
                # send an opt in transaction for the asset
                Then( Seq( opt_in_smart_contract_to_asa(asa_id) ) )
                # transfer the asset to the smart contract with a rekey back to the sender
                #Then( Seq( asset_transfer(asa_id, amount)) )
            ])

I keep getting the following error:

TransactionPool.Remember: transaction F5PPWKTO3IJKGQMQPN2QBRHOHPDF7LUGPHRD6XJGKSZQARDQS7RQ: logic eval error: clawback not allowed: sender CCVIIUUVP74CYJJWNPKBP4DIVFQKBS37R2U3VNOHEZUMTGE5VOEZD4IN24, clawback AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ. Details: pc=144, opcodes=pushint 6000
itxn_field Fee
itxn_submit

Any ideas as to what may be causing it?

Changing AssetSender to Sender did the trick. What is the difference between AssetReceiver and Receiver? The documentation does not say too much about it.

See Why have both txn.sender() and txn.asset_sender() in PyTEAL? - #2 by jannotti

In short:

1 Like

In other words

  • In AssetTransfer, use: Sender, AssetReceiver
  • In Payment, use: Sender , Receiver

Right?

Yes, unless you want clawback for AssetTransfer, which is not usual.