Task: Build an Algorand Smart Contract

All, is there a good formula to calculate future round time on the HTLC contract? Rough math got me 20,381 rounds per day. But if a user wants a timeout 30 days in future, is 20381x30 correct or is there any other way to compute?

It’s little difficult for me.I’m not a a développer to write TEAL code.

Can you help me? Please.

Have you looked at using PyTeal to write smart contracts in python?
https://developer.algorand.org/docs/features/asc1/teal/pyteal/

1 Like

You may also take a look on AlgoNim solution, in which you can find an overview of game architecture (there is also a link to a presentation with the original sketch of the prototype) that leverages also ASC1s, this will help you read and understand the how the rules of the game have been translated into TEAL programs using PyTeal.

Feel free to reach me out for questions.

1 Like

I wrote a TEAL contract that I was hoping would be a simple contract that let people send Algo and receive an Asset. However I am getting this error when trying to fund the contract. asset 127494380 missing from

Here is my Teal. Thanks for help.
// - 127494380: ID of the transferred asset
// - 25: exchange rate N/D
// - 1000000: exchange rate N/D
// - 1000: the round at which the account expires
// - 3ELOPICTSZ5Y4W4VKXG533NEN6UBT3G5AGXXYWTA5OSDZ3DEN2ZAGOD24M: the address to refund funds to on timeout
// - 10000: maximum fee used by the limit order transaction
// - 25: the minimum amount (of asset) to be traded away

global GroupSize
int 1
==
bnz oneTx

global GroupSize
int 2
==

txn GroupIndex
int 1
==
&&

txn TypeEnum
int 4 
==
&&

txn AssetAmount
int 25
>
&&

txn XferAsset
int 127494380
==
&&

txn AssetSender
global ZeroAddress
==
&&

txn AssetCloseTo
global ZeroAddress
==
&&

gtxn 0 TypeEnum
int 1 
==
&&

gtxn 0 Receiver
addr 3ELOPICTSZ5Y4W4VKXG533NEN6UBT3G5AGXXYWTA5OSDZ3DEN2ZAGOD24M
==
&&

gtxn 0 Amount
int 1000000
mulw 
store 2 
store 1 

txn AssetAmount
int 25
mulw
store 4 
store 3 

load 1
load 3
>
bnz done

load 1
load 3
==
load 2
load 4
>=
&& 
bnz done
err 

oneTx:

txn TypeEnum
int 1 
==

txn CloseRemainderTo
addr 3ELOPICTSZ5Y4W4VKXG533NEN6UBT3G5AGXXYWTA5OSDZ3DEN2ZAGOD24M
==
&&

txn FirstValid
int 1000
>
&&

txn Receiver
global ZeroAddress
==
&&

txn Amount
int 0
==
&& 

txn TypeEnum
int 4 
==

txn AssetSender
global ZeroAddress
==
&&

txn XferAsset
int 127494380
==
&&

txn AssetAmount
int 0
==
&& 

txn AssetCloseTo
addr 3ELOPICTSZ5Y4W4VKXG533NEN6UBT3G5AGXXYWTA5OSDZ3DEN2ZAGOD24M
==

txn FirstValid
int 1000
>
&&

txn AssetReceiver
global ZeroAddress
==
&& 


txn AssetReceiver
txn Sender
==

txn AssetCloseTo
global ZeroAddress
==
&&

txn LastValid
int 1000
<
&& 

|| 
&& 

|| 

done:

txn Fee
int 10000
<=
&&

Hello and welcome to the Algorand Community!

On a quick look, seems like this may be related to the question asked here: Optins to TEAL Managed Account

Have you opted the contract account into asset 127494380? Add some code to allow it to opt it in, then issue that opt-in transaction before you make a trade. Without that, the contract won’t be able to receive the asset.

Thank you for the reply. I’m handwriting the Teal in AlgoStudio SDK and compiling that way to push to TestNet then I found ALGODesk.io has a teal debugger and compiler also. I think this would be correct but I’m not 100%.

int OptIn
txn OnCompletion
==

The code there is related to opting into a stateful application.

Opting into an asset is just an asset transfer of 0 to and from the account opting in. Here is what the opt-in transaction structure looks like for assets: Algorand Developer Docs

Since I don’t have all context of your app, please take the following suggestions with that in mind as a heavy disclaimer and make sure to do your own security audits…

The asset opt-in scenario could be weaved in under the onetx scenario (because you want to make sure only a single transaction is permitted) and it would look something like:

// asset transaction type
txn TypeEnum
int 4 
==
&&

// opt-ins are transfers of 0 amount
txn AssetAmount
int 0
==
&&

// the sender must be equal to the receiver for opt-ins. And the sender for contract accounts is the contract account.
txn Sender
txn AssetReceiver
==
&&

// make sure it is opting in to the right asset
txn XferAsset
int 127494380
==
&&

// Add any checks to restrict unintended loopholes - this is important; make sure I'm not missing any!
txn AssetSender
global ZeroAddress
==
&&

txn AssetCloseTo
global ZeroAddress
==
&&

// Add an expiration round if you can as a best practice
txn LastValid
int EXPIRATION_ROUND
<
&&

Then next steps would be:

  1. Send algos to contract to initialize it on chain (201,000 microAlgos to cover minimum balance for Algos and one asset holding, plus money for a fee).
  2. Send opt-in transaction from contract account to opt it into asset: 127494380.
  3. If you want, and you’re super paranoid like me, wait until the expiration round has passed and then add more funds to cover fees. That way you can eliminate any unforeseen attack vectors through that additional code :slight_smile:
  4. Fund the account with the asset and let the trades begin!

By the way, I didn’t see any checks for rekeying in your code which is very important. See some more related guidelines here. Relevant line:

Always verify that the RekeyTo property of any transaction is set to the ZeroAddress unless the contract is specifically involved in a rekeying operation.

Edit (2/28): Wrong field name; changed Receiver to AssetReceiver.

1 Like

Thank you very much for the replies. It is starting to be more clear now for me. I was definitely confusing the the docs and got turned around. When I get off work from my day job tonight I will work on my project and try to compile and test again. Hopefully I get it to work and will share the end results with everyone. I need to comment my code better too. Thanks for the excellent explanation too. Big eye opener for me. My lightbulb moment.

2 Likes

Can you point me to the docs for opt-in a transaction from a contract account? With AlgoStudio I have a test optin.json file which I can test sign with to opt-in a wallet account to an asset. But I am having trouble finding docs on how to do this for a contract account. I do not have the mnemonic of the contract account or I could simply add it to algo signer or a mobile wallet and add an asset that way. I’m going to have to retrain my brain some also. Was used to writing and compiling in Solidity for Ethereum. Algo is easier especially with TEAL it is so effiecent I just have to figure out the process Which you have helped me tremendously already. Please also share your algo wallet as I would be happy to send you some algo to say thanks for helping me figure this out.

I think I figured it out thanks to this boiler plate. Algorand Developer Portal

1 Like

Hi @StishSits glad to hear that my solution was helpful! I go through all the process step by step so you will find the whole deployment of an Algorand dApp explained there.

Please note that all the process can be handled using your favorite SDK instead of do it manually with goal.