Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
By: Jesse AbramowitzBlockchain Developer
A little over a week ago, Parity Ink was released and substrate was updated to handle smart contracts! After much excitement we are now able to get our hands wet with some rust smart contracts on Substrate!
Quickly after that an amazing Demo was released here by shawntabrizi and can be found here. There are some great demos and examples in there and I totally recommend going through them and trying them out for yourself.
After going through Shawntabriziâs Demo, I was hungry for more so I decided to create my own contracts, write unit tests for them, then erase the code. The subsequent tests have been put up on github and are available here for anyone to try.
Furthermore any contribution would be super cool. For contribution make sure to put the answers in the answers folder and try to put your exercise in a position of incrementing difficulty.
A quick primer on Substrate
Substrate consists of a bunch of rust modules called Substrate Runtime Modules. Substrate chains in their core modules have connectivity to the main Polkadot chain. Thus, any Substrate chain can be interconnected to the Polkadot Network.
This means that developers can choose to launch customizable blockchains for their DApps instead of smart contracts. This will inevitably lead to greater scalability and use cases.
However, the first implementation of Polkadot will only have 100 parachainsâŠalthough this will scale infinitely because you can have relay chains off of relay chains, there is still some barrier of entry to creating a parachain.
Smart Contracts Vs SRMLs
Last year I created a DApp that was for an event hosted by BlockX Labs called Blockchain For Peace. The DApp was a one off contract for one night so people could donate to the charity and write a message of peace on the blockchain forever.
To get a parachain set up, I need to pay some amount of Dots and get approved by Dot holders. This would be illogical for my one off use case.
However, letâs say someone like Facebook wanted to create a DApp. With their user base, the need for a parachain would be a no brainer.
Thus there is no doubt that the need for smart contracts on Substrate will still be an active and integral part of the polkadot experience for a long time to come.
Conclusion
From my knowledge, this is one of the first major blockchains to be able to offer Rust smart contracts with a testing suit and front end. With the move of a lot of blockchains VMs to WASM. Rust smart contracts seem to be the way of the future.
So do the tutorial, try to pass my tests and have some fun with it!
Oh you are still Here?
If you are, I want to go over some code and run through one example if you are having trouble.
If you are serious I recommend going through the tutorial above and then trying my tests yourself, then if you are having trouble, read this section:
fn set_works() {
let mut contract = Incrementer::deploy_mock(3);
assert_eq!(contract.get(), 3);
contract.set(4);
assert_eq!(contract.get(), 7);
contract.set(0);
assert_eq!(contract.get(), 7)
}
My first test makes two contract calls, get and set and takes in one argument for the constructor. Each time you run this it will mock deploy the smart contract and run through the get and set functions. Letâs go line by line to get this to pass.
First:
let mut contract = Incrementer::deploy_mock(3);
assert_eq!(contract.get(), 3);
We have to create a variable to be stored by the smart contract:
struct Incrementer {
value: storage::Value<u64>,
}
Now that we can store a value, we need to build the constructor that will set that value:
impl Deploy for Incrementer {
fn deploy(&mut self, init_value: u64) {
self.value.set(init_value)
}
Then we also need a getter function to read that variable:
pub(external) fn get(&self) -> u64 {
println!(âCounter value: {:?}â, *self.value);
*self.value
}
Finally from the test we only need a setter function now and the tests should pass. That can be done like so:
pub (external) fn set(&mut self, amount: u64) {
self.value += amount;
}
All that, when run together would pass the tests. As first, it would
- deploy the contract with a constructor of 3
- Then it would check it
- Then it would add 4 and check it
- Then it will add 0 and check it
- Each time the contract would return the correct amount 3, 7, 7 respectively
The tests get harder from here so good luck and enjoy.
Bonus!!!!!
Bunz has generously donated 20 000 BTZ for the first person to contribute to the: Pass the Test repo (foRust). To win you must:
- Create a PR with a legitimate test that follows the contribution rules in the article above
- Include their Bunz ID in the PR so I can send them the funds
Bunz is an application that allows you to trade things and earn BTZ for your data. BTZ can be spent at stores in Toronto or converted to an ERC223Â token
Bonus 2!!!!!
A general bounty for Bunz that will be posted on all of our articles until it is claimed. 20 000 ERC223 Bunz tokens to:
- Create utility function to stake BTZs in a smart contract that pays a benefit to a user
- This can be interest base, time based, or based on the number of transactions in the contract
________________
Jesse Abramowitz is a Blockchain Developer at BlockX Labs. He has worked on multiple DApps, projects, and Blockchain Networks. Currently, he is also a professor at George Brown College in Toronto. He is always looking to help, teach and build on the blockchain. You can reach him at: jesse@blockxlabs.com
BlockX Labs specializes in building developer tools and solutions for blockchain ecosystems.We aim to sift through the noise to bring some sense and clarity into the Blockchain space. Our accomplishments include AIWAâââa wallet and DApp interaction tool for the Aion Network, and Universal Faucetâââa test token faucet for Ethereum, Aion, and Tron.
Follow Us on Twitter: @BlockXLabs
Rust Smart Contracts: Demos & Challenges was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.