This NEAR smart contract implements a weighted raffle system. Participants can enter the raffle with different weights, giving them proportionally different chances of winning.
Based off the contract from https://github.com/NEAR-DevHub/race-of-sloths/
The contract consists of two main structures:
Contract
: Stores the owner of the contract.Participant
: Represents a raffle participant with a name and a weight.
The main function weighted_raffle
selects winners based on the weights of the participants.
Initializes the contract with the given owner.
Conducts a weighted raffle and returns the names of the winners.
participants
: A list ofParticipant
objects, each with a name and weight.winners
: The number of winners to select.
- Deploy the contract to a NEAR account.
- Call the
weighted_raffle
function with a list of participants and the number of winners.
Example using NEAR CLI:
near call <contract_account_id> weighted_raffle '{"participants": [{"name": "Alice", "weight": 5}, {"name": "Bob", "weight": 3}, {"name": "Charlie", "weight": 2}], "winners": 2}' --accountId <your_account_id>
Replace <contract_account_id>
with the account where the contract is deployed, and <your_account_id>
with your NEAR account.
- Ensure you have Rust and the NEAR CLI installed.
- Clone this repository.
- Build the contract:
cargo build --target wasm32-unknown-unknown --release
- Deploy the contract using NEAR CLI.
- Run tests:
cargo test
- The contract uses a random number generator seeded with
env::random_seed()
for fairness. - Participants with higher weights have a higher chance of winning.
- The number of winners must not exceed the number of participants.
MIT License