This repository provides a comprehensive guide to creating, building, deploying, and interacting with a simple voting smart contract on the MultiversX blockchain.
MultiversX is a high-performance blockchain designed for scalability, efficiency, and low-cost transactions, making it ideal for smart contract development. Here's why:
- High Throughput: Handles up to 15,000 transactions per second (TPS) with transaction costs as low as $0.001.
- Scalability: Adaptive State Sharding enables the network to scale beyond 100,000 TPS as it grows.
- Tested Efficiency: Reached up to 263,000 TPS on the testnet.
- Comprehensive Tools: Includes an IDE, a Rust-based framework, and debugging utilities.
- Incentives: Developers earn 30% of gas fees for contract execution.
- WASM Compatibility: Allows the use of WebAssembly (WASM) for diverse programming languages.
Rust offers a robust environment for developing efficient and secure smart contracts with minimal gas costs.
pipx
is the recommended tool for installing the MultiversX CLI (mxpy
).
sudo apt update
sudo apt install pipx
pipx ensurepath
brew install pipx
pipx ensurepath
Verify installation:
pipx --version
Install the MultiversX CLI for blockchain interactions and smart contract management:
pipx install multiversx-sdk-cli --force
Rust is required to compile smart contracts.
sudo apt install build-essential pkg-config libssl-dev
xcode-select --install
Install Rust via mxpy
:
mxpy deps install rust --overwrite
Verify installation:
mxpy --version
rustup show
Initialize a new smart contract project:
sc-meta new --template empty --name simple-voting
Verify the project setup:
cargo check
Compile the smart contract into WebAssembly (WASM) bytecode:
sc-meta all build
This generates an output directory containing:
output/
├── simple-voting.abi.json
├── simple-voting.imports.json
├── simple-voting.mxsc.json
└── simple-voting.wasm
simple-voting.wasm
: Compiled bytecode for deployment.simple-voting.abi.json
: ABI for interacting with the contract.
Define the deployment arguments in deploy_arguments.json
:
[
"What is your favorite programming language?",
["Rust", "Python", "JavaScript", "C++"]
]
Deploy the contract:
mxpy --verbose contract deploy \
--recall-nonce \
--bytecode="./output/simple-voting.wasm" \
--proxy=https://devnet-gateway.multiversx.com \
--abi ./output/simple-voting.abi.json \
--arguments-file ./deploy_arguments.json \
--gas-limit 500000000 \
--keyfile="./<your-wallet-keyfile>.json" \
--send
Replace <your-wallet-keyfile>.json
with your wallet keyfile name. Note the contract address after successful deployment.
To upgrade a deployed contract:
-
Build the updated contract:
sc-meta all build
-
Deploy the upgraded bytecode:
mxpy contract upgrade erd1<your-contract-address> \ --bytecode ./output/simple-voting.wasm \ --proxy=https://devnet-gateway.multiversx.com \ --chain D \ --recall-nonce \ --gas-limit 5000000 \ --keyfile="./<your-wallet-keyfile>.json" \ --send
Example: Fetch the current poll question.
mxpy contract query erd1<your-contract-address> \
--proxy https://devnet-gateway.multiversx.com \
--function getPollQuestion
Cast a vote for an option (e.g., Rust
):
mxpy contract call erd1<your-contract-address> \
--function vote \
--arguments str:Rust \
--proxy https://devnet-gateway.multiversx.com \
--keyfile "./<your-wallet-keyfile>.json" \
--send
- Secure Your Wallet: Keep your wallet keyfile safe and backed up.
- Optimize Gas Usage: Write efficient smart contract code to reduce gas costs.
- Test Thoroughly: Test your contract extensively on the devnet before deploying to the mainnet.