How to Build & Deploy Smart Contracts on Archethic?
Samuel Manzanera
Posted on October 20, 2022
Archethic is a new generation of decentralized and distributed ledger using the concept of TransactionChains to provide fast, scalable and secure network.
Archethic smart contracts are designed to be simple to use by its language, by its concepts and being based on the TransactionChains paradigm.
TransactionChains
Archethic relies on TransactionChain's which reduce to the most atomic form of block: the transaction.
Instead of sharing a block of the transactions with multiple other transactions, a chain is built from a wallet, a service, a smart contract.
The scalability and the speed of the transaction validation is then decoupled.
Smart Contract
Archethic leverage smart contract technology embedded into any transactions. In other words, any transaction can be a smart contract.
The smart contracts are interpreted making it easy to understand by the creators, the auditors, and the network.
They rely on 3 main concepts:
- Triggers: what are the event which will execute some code
- Conditions: what are the conditions met to execute some code
- Actions: The code itself to generate a new transaction on the chain.
Apart from those concepts, the smart contract are easy to use thanks to a domain-specific language reducing most of the complexity and upgradable via the TransactionChains.
Let's build something
Nothing is better to explain sometimes than to show.
So, we will code and create a smart contract to demonstrate the capabilities.
For this example, we will build a crowd-sale smart contract for an ICO.
What the contract will do:
- Accept some cryptocurrency transfers
- Transfers some token in exchange
- Close the sales after a certain duration
Conditions
Transactions
We will define some conditions for the smart contract.
condition transaction: [
uco_transfers: size() > 0,
timestamp: transaction.timestamp < 1665750161
]
We are defining the condition to accept only incoming transactions with some UCO transfers and before a given UNIX timestamp (the closing date of the ICO)
Inherit
Because Archethic relies on TransactionChains, after an executed code, a new transaction might be generated.
But to achieve that, the creator has to delegate its cryptographic keys to the validators, so we could generate new transactions.
However, to prevent a bad usage of the chain, we have to define some constraints making the transactions behave like expected.
condition inherit: [
token_transfers: size() == 1
]
Disclaimer: This is just an example, it should not be taken as production ready. To harden it, you would have to check whether the destination address is within the previous transaction inputs (the UCO transfer)
By having those condition, the next transaction – while being validated – will ensure its integrity by asserting the conditions defined.
Triggers & Actions
The last remaining part is the definition of the code itself and the triggers.
actions triggered_by: transaction do
# Get the amount of uco send to this contract
amount_send = transaction.uco_transfers[contract.address]
if amount_send > 0 do
# Convert UCO to the number of tokens to credit. Each UCO worth 10 token
token_to_credit = amount_send * 10
# Send the new transaction
add_token_transfer to: transaction.address, token_address: contract.address, amount: token_to_credit
end
end
In this snippet, a new incoming transaction would trigger a code which will convert the UCO transferred into a new token transfer, based on the defined rate.
To summarize the contract creation for an ICO fits in the snippet below:
condition inherit: [
token_transfers: size() == 1
]
condition transaction: [
uco_transfers: size() > 0,
timestamp: transaction.timestamp < 1665750161
]
actions triggered_by: transaction do
# Get the amount of uco send to this contract
amount_send = transaction.uco_transfers[contract.address]
if amount_send > 0 do
# Convert UCO to the number of tokens to credit. Each UCO worth 10 token
token_to_credit = amount_send * 10
# Send the new transaction
add_token_transfer to: transaction.address, token_address: contract.address, amount: token_to_credit
end
end
Let's test it
Get some funds
To be able to exchange on the network, you need some funds, you can get some funds from the faucet either using the https://mainnet.archethic.net/faucet or a local node (https://github.com/archethic-foundation/archethic-node)
Build the transaction
Archethic have an SDK to build transactions (https://github.com/archethic-foundation/libjs), which provide a page to build transaction (example/transactionBuilder)
Write the code
We are using the transaction builder to forge the transaction, by passing the chain's seed (previously funded), the code mentioned above.
Mint a token
Moreover, to use an existing token, we will mint a token in the same time.
Just specify the type of transaction as: token
and defines it in the content section. (for more details, please refer to the token standard definition: https://archethic-foundation.github.io/archethic-docs/learn/token)
Authorized the nodes
Now, as explained before, we have to allow nodes to generate transaction in our behalf. Hence, we have to encrypt the chain's seed to be decrypted only by the nodes, by using the Ownerships
section.
The secret being the seed, the public key being the Storage nonce public key
Contract created
After the transaction generated and sent, we have a new transaction on the chain with the tokens minted and the contract deployed
Test the interactions
We will go back to the transaction builder and generate a new transaction from another chain (funded as well), and we will send UCO to this smart contract's address (previously created)
We also have to mention the recipient address to say we want to execute code in the smart contract and not only send UCO to it.
After sending, the chain would have created a new transaction by sending the tokens.
The sender will have now in possession its token, while the contract would have got the UCO.
Conclusion
The way to create smart contract on Archethic are quite innovative to allow users to create simple code but powerful by leveraging triggers and bring confidence/security with well-defined conditions.
Apart from this, other really interesting features are supported such as easy upgrade or few triggers (date, interval, oracles, etc.)
Posted on October 20, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.