Resumen
QuickLotteryTON es un contrato inteligente simple en la blockchain TON. Los participantes ingresan a la lotería mediante la llamada enter, enviando 1 o más TON (cantidades exactas, sin fracciones).
Una vez que 10+ participantes se hayan unido y hayan pasado 24 horas, cualquier participante puede ejecutar la llamada draw. Se selecciona aleatoriamente ~10% de los participantes y se distribuye el fondo entre ellos.
¡Eso es todo!
Puedes ver el código fuente abajo (ir al código).
Cómo participar
Para ingresar a la lotería, simplemente envía 1 TON (o 2, 3, 4, etc.) a la dirección del contrato
EQCsihC1Z6fHRsFbz9N3sCHC6sUfvnlz4RbelethC-6Q34y6
¡Copiado!
con el comentario enter (usa exactamente esa palabra, sin espacios y en minúsculas).

Respuestas
Si envías un valor con decimales (ej. 1.002 TON), recibirás tu dinero de vuelta con el mensaje Can only enter with 1 TON or multiples of 1 TON.
Si ya has participado en esta ronda, recibirás tu dinero de vuelta con el mensaje You have already entered this draw.
Si tu entrada es correcta, recibirás el mensaje Successfully entered the lottery draw with 1 TON!
Sorteo
Sólo un participante necesita ejecutar el sorteo por ronda. Normalmente no hace falta, otro lo hará automáticamente. Sólo entra y espera los resultados en 24 horas.
Si quieres hacer el sorteo, envía 0.05 TON o más (según el número de participantes, suficiente para el gas) a la dirección del contrato
EQCsihC1Z6fHRsFbz9N3sCHC6sUfvnlz4RbelethC-6Q34y6
¡Copiado!
con el comentario exacto draw.
Si el sorteo anterior fue hace menos de 24 horas, recibirás tu dinero con el mensaje Cannot draw yet, X seconds remaining until next draw.
Si han entrado menos de 10 personas en esta ronda, recibirás tu dinero con el mensaje Not enough participants for draw.
En otro caso, el código seleccionará aleatoriamente ~10% de los participantes y repartirá el fondo entre ellos. 1% se retiene como comisión de gas.
Los ganadores recibirán su premio con el mensaje Congratulations! You won 10000000000 nanoTON in the lottery draw!
Métodos
En el enlace de Verifier o TonViewer también puedes llamar a los métodos getter del contrato. Los métodos disponibles son:
getBetPool(): devuelve el total de nanoTON en el fondo actual.getParticipantCount(): devuelve el número total de participantes en la ronda actual.
Los resultados están en formato hex, debes convertirlos a decimal.
Código fuente
Este código fuente también se puede verificar en TON Verifier:
/**QuickLotteryTON contractExplorers:- Tonviewer: https://tonviewer.com/EQCsihC1Z6fHRsFbz9N3sCHC6sUfvnlz4RbelethC-6Q34y6- Verifier: https://verifier.ton.org/EQCsihC1Z6fHRsFbz9N3sCHC6sUfvnlz4RbelethC-6Q34y6Usage:- Enter lottery:• Send ≥ 1 TON (multiples of 1 TON) with comment "enter".• Each address may enter once per draw.- Getters:• balance() → current contract balance (nanoTON).• betPool() → total TON in the current lottery pool (nanoTON).• participantCount() → number of unique participants.- Draw:• Anyone can trigger via sending any amount of TON with comment "draw".• At most once every 24h.• Requires ≥ 10 participants before allowing draw.- Payout:• 1% fee to deployer for transaction fees.• Remaining pool distributed proportionally among ~10% of participants, chosen randomly.*/import "@stdlib/deploy";const NANO_COUNT: Int = 1_000_000_000;message TransferEvent {amount: Int as int64;recipient: Address;}message EntryEvent {sender: Address;amount: Int as int64;}contract QuickLotteryTON with Deployable {const DRAW_EVERY: Int = 24 * 60 * 60; // Once every day at most.const MIN_PARTICIPANTS: Int = 10;lastDrawTime: Int;participants: map<Address, Int>;participantCount: Int;betPool: Int;deployer: Address;init() {nativeReserve(ton("1.0"), ReserveAtMost | ReserveBounceIfActionFail);self.deployer = sender();self.participantCount = 0;self.betPool = 0;self.participants = emptyMap();self.lastDrawTime = now();}receive("enter") {let amount: Int = context().value;if (amount < NANO_COUNT || amount % NANO_COUNT != 0) {self.reply("Can only enter with 1 TON or multiples of 1 TON.".asComment());return;}let sender: Address = sender();if (self.participants.exists(sender)) {self.reply("You have already entered this draw.".asComment());return;}self.participants.set(sender, amount);self.participantCount += 1;self.betPool += amount;// Emit entry eventemit(EntryEvent{sender: sender, amount: amount}.toCell());// Send confirmation message to participant with bet amountlet sb: StringBuilder = beginString();sb.append("Successfully entered the lottery draw with ");sb.append((amount / NANO_COUNT).toString());sb.append(" TON!");send(SendParameters{to: sender,bounce: false,value: 0, // No TON refundedmode: SendIgnoreErrors | SendPayFwdFeesSeparately,body: sb.toString().asComment()});}receive("draw") {// Check time since last drawlet remaining: Int = self.DRAW_EVERY - (now() - self.lastDrawTime);if (remaining > 0) {let sb: StringBuilder = beginString();sb.append("Cannot draw yet, ");sb.append(remaining.toString());sb.append(" seconds remaining until next draw.");self.reply(sb.toString().asComment());return;}if (self.participantCount < self.MIN_PARTICIPANTS) {self.reply("Not enough participants for draw.".asComment());return;}// Send 1% fee to ownerlet ownerFee: Int = self.betPool / 100;if (ownerFee > 0) {emit(TransferEvent{amount: ownerFee, recipient: self.deployer}.toCell());send(SendParameters{to: self.deployer,bounce: true,value: ownerFee,mode: SendIgnoreErrors});}// Determine prize pool and winner countlet pool: Int = self.betPool - ownerFee;let targetWinners: Int = self.participantCount / 10; // 10% winners// Select winnerslet winners: map<Address, Int> = emptyMap();let winnerCount: Int = 0;let totalWinnerWeight: Int = 0;// First pass: try to select winners with 10% chancewhile (winnerCount == 0) {foreach (adr, winnerWeight in self.participants) {if (random(1, 100) <= 10 && winnerCount < targetWinners) {winners.set(adr, winnerWeight);totalWinnerWeight += winnerWeight;winnerCount += 1;}}}// Distribute prizes proportionally to bet amountsforeach (winnerAddress, winnerWeight in winners) {let prize: Int = pool * winnerWeight / totalWinnerWeight;if (prize > 0) {// Create notification message with prize amountlet sb: StringBuilder = beginString();sb.append("Congratulations! You won ");sb.append((prize).toString());sb.append(" nanoTON in the lottery draw!");// Emit transfer eventemit(TransferEvent{amount: prize, recipient: winnerAddress}.toCell());// Send prize with notification message in a single transactionsend(SendParameters{to: winnerAddress,bounce: true,value: prize,mode: SendIgnoreErrors,body: sb.toString().asComment()});}}// Reset lotteryself.participantCount = 0;self.betPool = 0;self.participants = emptyMap();self.lastDrawTime = now();}get fun participantCount(): Int {return self.participantCount;}get fun betPool(): Int {return self.betPool;}get fun balance(): Int {return myBalance();}}





