2-How to Generate Pseudo-Random Numbers in Ink! Smart Contract
Prerequisites
Cargo contract installed: https://polkaverse.com/9768/technical-guide-install-cargo-contract-37738
At least on the following node is ready :
- swanky node: https://github.com/AstarNetwork/swanky-node/releases
- substrate contract node: https://github.com/paritytech/substrate-contracts-node/releases
This tutorial uses Ubuntu 22.04, cargo 1.68, cargo contract 2.0 and swanky node
Create a new project
Use the command “cargo contract new“ to create a new rust project (in this tutorial the project name is pseudo_random)
cargo contract new pseudo_random
A new rust project is created with two files:
- Cargo.toml : cargo manifest file
- lib.rs : source code file
More info about these two files in the tutorial 1: https://polkaverse.com/11143/1-an-introduction-to-write-smart-contracts-in-rust-and-ink-37782
Create the struct
Create the PseudoRandom
struct to include the salt
variable. The salt
will be incremented by 1 each time the get_pseudo_random
function is called.
Create the constructors
Create the new
constructor to take an input parameter the salt
variable.
Create the default
constructor with no input parameter. The salt
variable is set with the value 1.
Create the get_pseudo_random function
Then, create the get_pseudo_random
function to take an input parameter for the minimum and maximum value in the range, and to return a number between the min and the max values in the range using the following code:
You can also add a get_salt
function for testing purpose.
Usage
To generate a pseudo-random number within a specified range, simply call the get_pseudo_random
function with the minimum and maximum values in the range as the input parameters. For example, to generate a number between 10 and 50, you would call the function with a min_value
of 10 and a max_value
of 50:
Create the Unit Test
To ensure that the get_pseudo_random
function works as expected, you can write a unit test that calls the function with different min_value
and max_value
parameters and checks that the generated random numbers are within the expected range. Here's an example unit test that you can add to your Ink! smart contract:
Build the contract
Use the command “cargo contract build“ to build the ink! smart contract
cargo contract build
After building, you will have three artifacts :
- pseudo_random.wasm: the contract’s WebAssembly blob (the contract code)
- pseudo_random.json: a JSON file that contains just the contract’s metadata, used by the tools to interact with your contract
- pseudo_random.contract: bundle with the combination of the metadata and the wasm code
Run the Unit Tests
Use the command “cargo test -- --nocapture“ to run the Unit Tests and display the debug messages
Deploy the contract
You can deploy your contract in local node or swanky node or a testnet.
Here we will start a swanky node and use Contracts UI to interact with the contract.
Conclusion
By implementing a pseudo-random function in your Ink! smart contract, you can generate pseudo-random numbers within a specified range in a decentralized and trustless environment. However, it is important to note that the get_pseudo_random
function does not provide the same level of security and trust as a true verifiable random function (VRF).
While the function uses the block timestamp and a salt value to generate a hash value, which is then used to generate a pseudo-random number, it may still be possible for an attacker to predict the output of the function. Additionally, this implementation may not be suitable for applications that require high levels of security, such as gambling or financial applications.
If you require a truly verifiable and secure random function for your smart contract, you may want to consider using an external oracle solution or a specialized random number generator that is specifically designed for use in smart contracts.
Git repository
https://github.com/GuiGou12358/astar-tutorials/tree/main/tuto2
Source
https://docs.astar.network/docs/build/builder-guides/xvm%5Fwasm/pseudo%5Frandom
Crypto-enthusiast, Defi & NFT believer, Dotsam Fam Astar Tech Amb & Phala Amb Web2 builder gradually migrating to Web3
Tutorials to write Smart Contracts in Rust and Ink!
0 comments