Some help understanding Smart Contracts

Hi,
I’m making my way through the Smart Contract docs and have a couple points I hope someone can clarify for me. I’m looking at the Hash Time Lock Contract template (https://developer.algorand.org/docs/reference/teal/templates/htlc/) and the SDK page (https://developer.algorand.org/docs/features/asc1/sdks/).

  1. So after I initialize the client and params, I would do something like this to initialize the HTLC contract account:
    let program = new Uint8Array(Buffer.from(" txn Fee
    int TMPL_FEE
    <=
    txn TypeEnum
    int 1
    ==
    &&
    […] ", “base64”));
    let args = [“placeholder_receiver_addr”, “sha256”, “placeholder_hashimg”, params.lastRound+parseInt(3000), “placeholder_own_addr”, algodclient.suggestedFee()+parseInt(100)];
    let lsig = algosdk.makeLogicSig(program, args);

So the HTLC account is expecting 6 parameters (TMPL_RCV, TMPL_HASHFN, TMPL_HASHIMG, TMPL_TIMEOUT, TMPL_OWN, TMPL_FEE), does it assume based on the order of args I passed that algodclient.suggestedFee()+parseInt(100)] is TMPL_FEE, seems like I never defined the names of the arguments anywhere.

https://developer.algorand.org/docs/reference/teal/templates/htlc/ mentions the contract is checking that TMPL_HASHFN(arg_0) is equal to TMPL_HASHIMG, is arg_0 the first argument I’m passing when I compile the logsig (“placeholder_receiver_addr”), or does it refer to something else?

  1. Since Alg Smart Contract only yield true/false and doesn’t create transactions, for HTLC, when the time expires, the amount is not automatically disbursed to TMPL_OWN. So someone has to create another transaction with Receiver = TMPL_OWN to get it approved by the HTLC account, right? So if I’m using HTLC in an application, I would save the TMPL_TIMEOUT in my server and create a separate transaction if the HTLC address still has balance after TMPL_TIMEOUT.

Regarding 1., algosdk.makeLogicSig takes as input a program already compiled where all the parameters are specified. The program program above is not compiled and cannot be used as argument of algosdk.makeLogicSig.

Note that arguments (e.g., arg_0) are different than parameters (TMPL_RCV). Parameters are chosen when creating the smart contract. Arguments are chosen when executing the smart contract, i.e., making a transaction where the sender is the smart contract account.

Note that HTLC is a template and can be used from the SDK by just specifying the parameters. See https://github.com/algorand/js-algorand-sdk/blob/ec9173150434c0f5cf60f78e2980aaa5bbd16a91/tests/8.LogicSig.js#L189

Regarding 2., yes you need to issue this transaction yourself.

Thanks! Does the follow snippet not compile program?
let program = new Uint8Array(Buffer.from("
txn Fee
int TMPL_FEE
<=
txn TypeEnum
int 1 ==
&&
[…] ", “base64”));

Do we have to compile the program via $goal first? https://developer.algorand.org/docs/features/asc1/goal_teal_walkthrough/

Do you have any docs or example about how someone can compile a smart contract and where the parameters are defined?

Could you explain the difference between arguments and parameters and how they’re used?

Sorry for the barrage of questions. I’m having some trouble understanding (I haven’t really done assembly coding)

Most likely

let program = new Uint8Array(Buffer.from("
txn Fee
int TMPL_FEE
<=
txn TypeEnum
int 1 ==
&&
[…] ", “base64”));

tries to read your string as a base64 string. But this is not a base64 string. So it would not work.

You need to compile using goal first indeed.

When you compile a smart contract using goal, you must have replaced the parameters before compiling. There must be no parameters left. When you create an actual contract, you must specify the parameters beforehand. The parameters are constants in the smart contract. They cannot be changed.

When you call a smart contract, you specify arguments.

There is a tutorial on the HTLC contract there: https://developer.algorand.org/tutorials/hash-time-lock-contract-template-javascript/

More generally, you can look at all tutorials about TEAL there: https://developer.algorand.org/search/?query=teal&sort_by=relevancy&category=all-categories

Got it! I read the docs again and the TEAL is compiled into bytecode first and then hashed into an address. Thanks for your explanation!

By the way, if I’m building a Node.js web app where I will create a contract account on the fly based on some user input, is there anyway to automate it without having to manually type in the command line every time?

That’s an interesting point. I don’t know a perfect solution.

One solution is to call the command line goal from nodejs. You must be very careful to ensure this does not open security breaches. (The input must be very carefully sanitized.)

If the constants you are using are always represented the same way in the bytecode and at the same place, you may just be able to replace certain bytes of the bytecode.