How to Write Smart Contracts
Last week we talked about one way you can create your own Smart Contracts with Python. You don’t just need to know Python. This article summarizes the three primary ways you can build Algorand Smart Contracts. Each has its pros and cons depending on your skillset and what you are trying to accomplish.
Here is a side-by-side comparison of the three methods followed by detailed explanations of each.
Comparison of 3 Ways to Build Smart Contracts
The SDKs wrap some of the known and reviewed TEAL templates into intuitive SDK method calls. So instead of writing your own contract from scratch, you can take an existing template and inject values for your use case. The method calls do the work of creating the TEAL code. They also supply complementary method calls to issue transactions against the relevant contract. These templates are discussed in more detail on the developer site.
- (DEPRECATED) Delegate Key Registration - This template is used to delegate the authority to sign a participation key transaction for some other key.
- (DEPRECATED) Dynamic Fee - Allows transaction fees, for a transaction between two parties, to be paid by a 3rd party.
- (DEPRECATED) Hash Time Lock Contract - Escrow contract account that can be unlocked with a passcode.
- (DEPRECATED) Limit Order - Contract account that can disburse Algos for Algorand Assets in a defined ratio.
- (DEPRECATED) Split - Contract account that allows withdrawals to two parties at a specific ratio.
let owner = "AJNNFQN7DSR7QEY766V7JDG35OPM53ZSNF7CU264AWOOUGSZBMLMSKCRIU"; let ratn = parseInt(1); let ratd = parseInt(3000); let assetID = 316084; let expiryRound = 5000000; let minTrade = 2999; let maxFee = 2000; // Instantiate the template let limit = new limitTemplate.LimitOrder(owner, assetID, ratn,ratd, expiryRound, minTrade, maxFee);
As the limit order is meant to be used with two transactions, one for the Asset transfer and one for the Algo transfer, the SDK provides a helper method to create the two transactions which need to be submitted at the same time as an Atomic Transfer.
let program = limit.getProgram(); let params = await algodclient.getTransactionParams(); let endRound = params.lastRound + parseInt(1000); let assetAmount = parseInt(1); let microAlgoAmount = parseInt(3000); let secretKey = assetOwner.sk; // generate the two transactions let txnBytes = limitTemplate.getSwapAssetsTransaction(program, assetAmount, microAlgoAmount, secretKey, params.fee, params.lastRound, endRound, params.genesishashb64); // Submit the Atomic Transfer transaction let tx = (await algodclient.sendRawTransaction(txnBytes));
Note that the SDKs provide API calls for all the templates and also provide a mechanism for submitting custom TEAL as described in the developer documentation.
- LimitOrder Contract with Python
- LimitOrder Contract with Java
- LimitOrder Contract with Go
Many other tutorials exist for current TEAL Templates. Use the search function and supply the template name to locate additional tutorials.
In case you missed it, check out this article from last week that summarizes how to get started with PyTeal. In short, PyTeal allows you to write contracts from scratch in Python. The language binding then compiles it into TEAL code. Those who prefer working in a higher-level language may enjoy using PyTeal. Several PyTeal tutorials are available as described in last week’s article.
If you enjoy stack programming, we recommend just working straight in TEAL. There are many resources available for getting up to speed on TEAL. The developer documentation covers an overview of Algorand Smart Contract with a small walkthrough of the different features or you can watch this video for the details.
The reference documentation also covers many other aspects of TEAL including a full list of Opcodes available for use in your code.
See the simple TEAL contract tutorial for an example walkthrough explaining how to build a smart contract.
Community Explorers that Display TEAL
If you use TEAL for your transactions and want to have a look at the decompiled TEAL using an explorer, check out these two block explorers.
The Future of TEAL
TEAL currently provides a suite of opcodes that provide a great deal of power to the application developer and we have even more in store. We are currently developing a stateful mechanism where TEAL programs will have the ability to add application state both at the global and local user level. This will enable applications built with TEAL the ability to live on the Algorand network, which opens even more usage scenarios for developers. In addition, the team has been hard at work to make TEAL development much easier and will be providing developers with a TEAL debugger to allow you to step through your code and verify that it is working the way you expect it to. Stay tuned for more details on these new features and be sure to subscribe to our developer newsletter!