Contract account can opt-in asset?

How a contract account can opt-in an ASA?

Hi @rafmc98,

Yes, Contract Accounts can Opt-In ASAs if in their TEAL logic Opt-In transactions are properly handled. This means that when you build the Stateless ASC1 you have to include Opt-In transaction checks among its approval conditions.

Here you have some best practices to code an Opt-In approval for a Contract Account.

Yes, @rafmc98 have a look at this example HTLC Stateless Smart Contract for ASA.

Code overview

( (Scenario 1) OR  ( (        Scenario 2           ) OR  (      Scenario 3     ) ) AND Fee)
( (  Opt-in  ) bnz ( ( Time Lock: ( ASA || Algos ) ) bnz ( Hash Lock: ASA Only ) ) && Fee )

Ok thanks i understand. However, a problem remains: Once the Opt-in transactions have been handled in the logic of the contract, when I send the Opt-in transaction for the contract account, a wallet is required but i don’t know which wallet to specify

You’ll be applying a LogigSig as the signature for your opt-in transaction to/from the contract account (which is the logic that must pass to approve the transaction).

Using goal (from the link above):

5. (Owner) Sign the opt-in transaction using the asa-htlc.teal file as the logicSig

goal clerk sign -i asa-htlc-opt-in-escrow.utxn -o asa-htlc-opt-in-escrow.stxn -p asa-htlc.teal -d $DATA

Importantly, notice the -p asa-htlc.teal which is the source TEAL program applied as the LogicSig to the unsigned transaction. No “wallet” is required; the program logic passing acts as the private key.

I have to use this instruction first right?
4. (Owner) Create an unsigned opt-in transaction from/to the escrow account for specified ASA
goal asset send -o asa-htlc-opt-in-escrow.utxn -a 0 --assetid $ASSET_ID -f $ESCROW_ACCOUNT -t $ESCROW_ACCOUNT -d $DATA
But when i try to send it i get this
open asa-htlc-opt-in-escrow.utxn: no such file or directory

Yes, Step 4 generates the unsigned transaction file for you. Please confirm you successfully completed Steps 1-3. It seems odd you would get that open error in step 4 (unless you don’t have write access to create the file?). If you got Step 2 to work, I assume you have write access. That will create a new compiled file asa-htlc.teal.tok file.

It’s been awhile since I last ran thru this example, so I’m going to start from the top see if I can reproduce your error. I’m happy to receive some feedback if I missing any steps or can improve the wording.

I tried to compile using

./sandbox goal clerk compile asa-htlc.teal 

but i get the same error:
asa-htlc.teal: open asa-htlc.teal: no such file or directory

So i decided to compile using SDK and i get the contract account address

Modifying my test to use sandbox commands…

See Is possible to compile a smart contract (teal program) using docker sandbox? Or i need to run a node? - #2 by fabrice

1 Like

thanks @fabrice for the reply. Unfortunately I didn’t understand how to transfer a teal program from outside to sandbox, can you help me?

The name of the container indeed changed with the new sandbox version.
It is now algorand-sandbox-algod.

If you want to compile the program example.teal that is in your folder outside of the sandbox into example.teal.tok, you do:

  1. Copy example.teal to the sandbox:
$ docker cp example.teal algorand-sandbox-algod:/opt/
  1. Enter the sandbox, and compile inside the sandbox:
$ ./sandbox enter algod

Entering /bin/bash session in the algod container...
root@e4fd29b8cee4:~/testnetwork/Node# cd
root@e4fd29b8cee4:~# ls
data  example.teal  testnetwork
root@e4fd29b8cee4:~# goal clerk compile example.teal
root@e4fd29b8cee4:~# ls
data  example.teal  example.teal.tok  testnetwork
root@e4fd29b8cee4:~# exit
  1. Retrieve the compiled program:
$ docker cp algorand-sandbox-algod:/opt/example.teal.tok .

Thank you so much, finally everything works correctly !!