Back to projects
Mar 01, 2025
5 min read

Near Strawpoll: Decentralized Voting on NEAR

A deep dive into the Near Strawpoll project—a smart contract built with Rust to enable secure, decentralized voting on the NEAR blockchain. This project highlights efficient state management, secure storage, and the simplicity of on-chain voting mechanisms.

Near Strawpoll: Bringing Trustless Voting to the NEAR Blockchain

The Near Strawpoll project represents my foray into the decentralized world of blockchain voting. Designed as a simple yet robust smart contract, it allows a poll creator to set up a secure voting session where only a predefined list of voters can participate. In this post, I’ll walk you through the project’s design, key features, and the lessons learned while building a decentralized voting solution with Rust on the NEAR platform.

Project Overview

Near Strawpoll is a smart contract that implements a decentralized voting poll on the NEAR blockchain. Its core objective is to ensure that:

  • Restricted Voting: Only voters on an approved list can cast their vote.
  • One Vote Per Voter: The contract maintains state to prevent double voting.
  • Candidate Tallying: Votes are securely counted for each candidate, and results can be queried at any time.
  • Secure Storage: Unique storage prefixes guarantee data integrity across the blockchain.

By leveraging Rust and the NEAR ecosystem, the project demonstrates how to build secure, efficient, and decentralized applications.

Smart Contract Architecture

The contract’s architecture is streamlined to handle voting securely and efficiently:

  • Poll Creator: The account that initializes the poll becomes the authority to manage the voting session.
  • Allowed Voters List: A vector of account IDs is used to specify who can vote, ensuring that only authorized participants take part.
  • Vote Tracking: A separate set tracks voters who have already cast their vote, enforcing the rule of one vote per voter.
  • Candidate Map: A mapping between candidate names and their respective vote counts, which gets updated with each valid vote.

The simplicity of this design not only secures the voting process but also makes it easy to audit and extend for future features.

Key Features

  • Restricted Voting: The contract verifies that only approved voters can participate.
  • Single Vote Enforcement: Each voter is allowed to vote only once, maintaining fairness.
  • Real-Time Tallying: Candidates’ vote counts are updated in real time, and anyone can query the results.
  • Secure State Management: Utilizing unique storage prefixes ensures that all data remains tamper-proof on the blockchain.

Tech Stack & Implementation

  • Programming Language: Rust is used for its performance, safety, and powerful concurrency features, making it an ideal choice for blockchain development.

  • Blockchain SDK: near-sdk-rs simplifies interactions with the NEAR blockchain, allowing for straightforward smart contract development.

  • Deployment Tools: The contract is compiled to WebAssembly using Rust’s standard toolchain and deployed via the NEAR CLI, ensuring a smooth deployment process.

Building & Deploying the Contract

  1. Build the Contract:

    cargo build --target wasm32-unknown-unknown --release
  2. Deploy Using NEAR CLI:

    near deploy --wasmFile target/wasm32-unknown-unknown/release/your_contract.wasm --accountId your_account.testnet

These steps highlight the streamlined process of moving from code to a live decentralized application on the NEAR blockchain.

Lessons Learned

  1. State Management in Decentralized Systems Building the voting contract deepened my understanding of managing state on-chain, particularly ensuring that each voter can cast only one vote while preserving data integrity.

  2. Security by Design Emphasizing secure storage through unique prefixes was essential. This approach mitigates risks related to data collision or unauthorized data manipulation.

  3. Developer Experience with NEAR Working with near-sdk-rs and the NEAR CLI provided a smooth developer experience, demonstrating that blockchain development can be both efficient and enjoyable with the right tools.

  4. Scalability & Simplicity The project’s simplicity proved to be its strength. By focusing on core functionalities, the contract remains scalable and easier to audit, laying a robust foundation for future enhancements.

Future Directions

Looking ahead, there are several exciting avenues to explore:

  • Extending Poll Functionality: Incorporating features like multiple polls, time-based voting sessions, or even delegation of votes.
  • Enhanced Analytics: Developing on-chain analytics to provide deeper insights into voting patterns and participation.
  • User Interface Integration: Building a decentralized front-end that interacts seamlessly with the smart contract to offer an intuitive voting experience.

Final Thoughts

The Near Strawpoll project has been a rewarding challenge, blending secure blockchain development with the powerful features of Rust. By addressing key concerns such as vote integrity and secure state management, this project not only serves as a functional decentralized voting solution but also as a valuable learning experience in the realm of smart contract engineering.

I’m excited about the future of decentralized applications and look forward to applying these learnings to more complex blockchain solutions. If you’re curious to explore the project further, check out the repository on GitHub and feel free to reach out for a discussion or potential collaboration.