Task: Build an Algorand Smart Contract

Ok a couple of things. Are you calling this from Java or Goal or some other SDK. Your arg0 is not 32 bytes long. When you pass parameters in Java they will look something like this:

ArrayList<byte[]> pargs = new ArrayList<byte[]>();
byte[] arg1 = "hunter2".getBytes();
// byte[] arg2 = {4, 5, 6};
pargs.add(arg1);
System.out.println("program: " + result.program.toString());
//lsig = new LogicsigSignature(program, args);
LogicsigSignature lsig = new LogicsigSignature(result.program, pargs);

Notice i am passing a passcode but before I sha256 it I want to check the link I expect it to be. In this case 7.
When you pass parameters using goal you have to pass them as base64 encoded strings, so that same parameter would be passed like:

first get base64 encoded string
$ echo -n hunter2 | base64

aHVudGVyMg==

Then I could call the transaction like:
$ goal clerk send -a 1000 -c J376TKTX4CY5LAXJQKVDHZZBTXTBOYQ7TE562SPGEGCKVOQQDPDKPPMQX4 --to C3MKH24QL3GHSD5CDQ47ZNQZMNZRX4MUTV6LVPAXMWAXMIISYSOWPGH674 --from-program sha.teal --argb64 “aHVudGVyMg==” -d ~/node/data -o ff.stx

You code appears to have a && operator that is not needed. Try:

arg 0
len
int 7
==
arg 0
sha256
byte base64 9S+9MrKzuG/4jvbEkGKChfSCrxXdyylUH5S89Saj9sc=
==
&&

I cover parameter passing in this article:

I hope this helps

ok. Understand now. I am using Java sdk. The length of the argument is 7 in your example and 8 in my example. If I put len int 8 then it works. Agree with &&. That was a typo error. For some reason I was thinking that input arg0 is always 32 (user error…!)

Modified code.

arg 0
len
int 8
==
arg 0
sha256
byte base64 nnhszg2V8PYIlYr+96R2oh9sb7Pc6YHSrAVLXx6cuSE=
==
&&

Result when triggered from Java.

tx[0] cost=17 trace:
1 intcblock =>
4 bytecblock =>
39 arg_0 => 6d65726368616e74
40 len => 8 0x8
41 intc_0 => 8 0x8
42 == => 1 0x1
43 arg_0 => 6d65726368616e74
44 sha256 => 9e786cce0d95f0f608958afef7a476a21f6c6fb3dce981d2ac054b5f1e9cb921
45 bytec_0 => 9e786cce0d95f0f608958afef7a476a21f6c6fb3dce981d2ac054b5f1e9cb921
46 == => 1 0x1
47 && => 1 0x1

pass -

@visybl I see @JasonW provided the info about argument passing and by now we can see the SHA256 implementation does not explicitly check for a 32 byte input. That’s not a bug, so not suggesting it to be changed. Just want to recommend when performing a BIP-199 compatible HTLC in a cross chain swap, ensure both protocols enforce a 32 byte check on the preimage length. My HTLC for ASA template above should retain the explicit check within the TEAL program.

1 Like

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: Structure - Algorand Developer Portal

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.

Hi @liz I hope all is well! I have been working on this for a few weeks without much luck. I created a Repository for my code and to document my progress. Currently, I am stuck hacking through this Issue, which I defined on GitHub.

Command Line Input: goal clerk compile stateless_swap.teal
Output Error: Cannot contact Algorand node: open /Users/brianhaney/node/data/algod.net: no such file or directory

I am struggling to structure a path to solve the Issue. More generally, I am wondering if it is possible to solve the problem I defined with TEAL.

Problem: How to successfully execute an Algorand Smart Contract to transfer value using TEAL?

I know that TEAL is an execution language, and my understanding is that TEAL is mostly used for validating contracts. I started generating .teal scripts using PyTeal, but I have not been able to run or write any TEAL programs. I’m also still struggling to understand the syntactic structure for TEAL software. Alternatively, I may need to change the Problem.

Alternative Problem: How to successfully validate a transaction using TEAL?

I’m wondering if this would be a better problem definition for a TEAL program. However, I am not sure whether I would validate an existing contract or whether I would need to write additional code to first execute a new transaction. For example, would it be possible to use TEAL to validate a LaylaCoin transaction? I was hoping you may be able to provide guidance. I know you are super busy and I really appreciate your time. Thank you!

For the first issue, you need to start the node: goal node start

TEAL can be used in two ways: stateful and stateless.
Have you read Smart Contract Overview - Algorand Developer Portal?
I would also recommend you doing this tutorial which is more hands-on and introduce stateless smart contracts slowly: GitHub - algorandfoundation/buildweb3: Repository for the Algorand class of the "Building with Blockchain for Web 3.0" course (https://buildweb3.org)

In short, for your problem, you need to use a pattern with an escrow account. See Example Crowdfunding Stateful Smart Contract Application | Algorand Developer Portal

For your second problem, what do you mean by “validating a LaylaCoin transaction”?
If you mean, restricting the kind of transactions that can be done with LaylaCoin, see Using a Smart Contract for Advanced Approvals: Hierarchical Thresholds | Algorand Developer Portal
If you mean, selling LaylaCoin for Algos, just read GitHub - algorandfoundation/buildweb3: Repository for the Algorand class of the "Building with Blockchain for Web 3.0" course (https://buildweb3.org)

1 Like

Thank you. This paves a clear path forward. I’m going to work through the resources you provided. I appreciate your guidance here.