Post
Share your knowledge.
Apr 10, 2025
Article
Building a Next-Gen NFT Lottery DApp with Sui Move & a Modern UI
🧩 Building a Next-Gen NFT Lottery DApp with Sui Move & a Modern UI
This is your ultimate guide to building a gamified, NFT-powered lottery DApp using Sui Move, with multi-round support, referral systems, DAO governance, and a design system Gen Z will love. From contract architecture to UI flow—let's go all in.
📦 Phase Breakdown
Phase 1 – Core Lottery
- Multi-round gameplay
- NFT ticketing
- Referral reward system
- Basic DAO voting
Phase 2 – Marketplace & Gamification
- NFT marketplace integration
- Boosters (increase win chance)
- Jackpot system
- Hidden airdrops
Phase 3 – DAO & Multichain
- Cross-chain compatibility
- DAO with advanced proposals
- Dynamic pricing
- On-chain analytics
🧠 Smart Contract Deep Dive on Sui Move
Contract Structure
module nft_lottery_x::nft_lottery_x {
use sui::object;
use sui::balance::{Balance, zero};
use sui::coin::{Self, Coin};
use sui::clock::Clock;
use sui::random::Random;
use sui::event::emit;
use sui::transfer;
use sui::tx_context::TxContext;
use std::option;
use std::signer;
const EGameNotStarted: u64 = 1000;
const EGameAlreadyFinished: u64 = 1001;
const EInvalidPayment: u64 = 1002;
const ENoTickets: u64 = 1003;
const EWinnerAlreadyChosen: u64 = 1004;
const ENotWinner: u64 = 1005;
public struct Game has key {
id: UID,
ticket_price: u64,
start_time: u64,
end_time: u64,
total_tickets: u32,
round: u32,
winner: Option<u32>,
balance: Balance<FAUCET_COIN>,
referral_bonus: u64,
}
public struct Ticket has key {
id: UID,
game_id: ID,
ticket_number: u32,
buyer: address,
referrer: Option<address>,
}
public struct GameCreated has copy, drop {
game_id: ID,
start_time: u64,
end_time: u64,
ticket_price: u64,
}
public struct TicketBought has copy, drop {
game_id: ID,
ticket_number: u32,
buyer: address,
referrer: Option<address>,
}
public struct WinnerAnnounced has copy, drop {
game_id: ID,
winner_ticket: u32,
round: u32,
}
public struct RewardClaimed has copy, drop {
game_id: ID,
ticket_number: u32,
amount: u64,
}
public fun create_game(
start_time: u64,
end_time: u64,
ticket_price: u64,
referral_bonus: u64,
ctx: &mut TxContext
) {
let game = Game {
id: object::new(ctx),
ticket_price,
start_time,
end_time,
total_tickets: 0,
round: 1,
winner: option::none(),
balance: zero(),
referral_bonus,
};
emit<GameCreated>(GameCreated {
game_id: object::id(&game),
start_time,
end_time,
ticket_price,
});
transfer::share_object(game);
}
public fun buy_ticket(
game: &mut Game,
coin: Coin<FAUCET_COIN>,
clock: &Clock,
referrer: Option<address>,
ctx: &mut TxContext
): Ticket {
assert!(clock.timestamp_ms() >= game.start_time, EGameNotStarted);
assert!(clock.timestamp_ms() < game.end_time, EGameAlreadyFinished);
assert!(coin.value() == game.ticket_price, EInvalidPayment);
game.total_tickets = game.total_tickets + 1;
coin::put(&mut game.balance, coin);
let ticket = Ticket {
id: object::new(ctx),
game_id: object::id(game),
ticket_number: game.total_tickets,
buyer: signer::address_of(ctx.sender()),
referrer,
};
emit<TicketBought>(TicketBought {
game_id: object::id(game),
ticket_number: ticket.ticket_number,
buyer: ticket.buyer,
referrer: ticket.referrer,
});
ticket
}
public entry fun determine_winner(
game: &mut Game,
rand: &Random,
clock: &Clock,
ctx: &mut TxContext
) {
assert!(clock.timestamp_ms() >= game.end_time, EGameNotStarted);
assert!(game.winner.is_none(), EWinnerAlreadyChosen);
assert!(game.total_tickets > 0, ENoTickets);
let mut generator = rand.new_generator(ctx);
let winning_ticket = generator.generate_u32_in_range(1, game.total_tickets);
game.winner = option::some(winning_ticket);
emit<WinnerAnnounced>(WinnerAnnounced {
game_id: object::id(game),
winner_ticket: winning_ticket,
round: game.round,
});
}
public fun claim_reward(
ticket: Ticket,
game: Game,
ctx: &mut TxContext
): Coin<FAUCET_COIN> {
assert!(object::id(&game) == ticket.game_id, EInvalidPayment);
let ticket_num = ticket.ticket_number;
assert!(game.winner.contains(&ticket_num), ENotWinner);
let amount = game.balance.value();
let reward = game.balance.into_coin(ctx);
emit<RewardClaimed>(RewardClaimed {
game_id: object::id(&game),
ticket_number: ticket.ticket_number,
amount,
});
object::delete(object::id(&game));
reward
}
}
Key Takeaways:
- ✅
Balance<FAUCET_COIN>
ensures type-safety and proper coin handling - ✅
Option<u32>
clearly signals if a winner has been chosen - ✅ Events offer traceability for frontends and explorers
🛠 Sui CLI Commands
sui client call --package <PACKAGE_ID> --module nft_lottery_x --function create_game --args <START_TIME> <END_TIME> <TICKET_PRICE> <REFERRAL_BONUS> --gas-budget 10000000
To buy ticket, determine winner, or claim reward, follow similar CLI flows.
🔮 Future Additions
- Auto-reset logic for next round in
claim_reward
- Emit more events like
ReferralRewardDistributed
- Refactor jackpots and referrals into submodules
Let me know if you want a part 2 to build UI and integrate on Sui testnet!
- Sui
3
Share
Comments
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
204Posts300Answers
Trending posts