0x8e056cb8…8b04sent to0xb7278a61…e575·#24,709,203·0x783234a7…e3a496
# v0.1.1
# { "Depends": "py-genlayer:latest" }
from genlayer import *
import json
import typing
class NeuralDuel(gl.Contract):
# Game State
round_count: u256
round_data: TreeMap[u256, str]
# Player Stats
player_balance: TreeMap[Address, u256]
player_wins: TreeMap[Address, u256]
player_rounds: TreeMap[Address, u256]
# Platform fee (5%)
deployer: Address
total_fees: u256
def __init__(self, deployer_address: str):
self.round_count = u256(0)
self.deployer = Address(deployer_address)
self.total_fees = u256(0)
@gl.public.write
def join_round(self, bet_amount: int):
"""Player bets and AI generates trivia question"""
player = gl.message.sender_address
bet = u256(bet_amount)
self.round_count += 1
round_id = self.round_count
def generate_question() -> typing.Any:
prompt = """Generate a trivia question with 4 answer choices.
Return ONLY this JSON format:
{
"question": "What is 2+2?",
"choices": ["A. 3", "B. 4", "C. 5", "D. 6"],
"correct": 1
}
Return ONLY valid JSON, no markdown, no extra text."""
result = gl.nondet.exec_prompt(prompt).replace("```json", "").replace("```", "").strip()
return json.loads(result)
question = gl.eq_principle.strict_eq(generate_question)
round_obj = {
"id": int(round_id),
"player": player.as_hex,
"bet": int(bet),
"question": question["question"],
"choices": question["choices"],
"correct": question["correct"],
"answer": None,
"is_correct": None,
"finalized": False
}
self.round_data[round_id] = json.dumps(round_obj)
rounds = self.player_rounds.get(player, u256(0))
self.player_rounds[player] = rounds + 1
@gl.public.write
def submit_answer(self, round_id: int, answer: int):
"""Player submits answer choice (0-3)"""
player = gl.message.sender_address
rid = u256(round_id)
data = json.loads(self.round_data[rid])
data["answer"] = answer
data["is_correct"] = (answer == data["correct"])
self.round_data[rid] = json.dumps(data)
@gl.public.write
def finalize_round(self, round_id: int):
"""Distribute winnings: 5% fee, rest to player if correct"""
player = gl.message.sender_address
rid = u256(round_id)
data = json.loads(self.round_data[rid])
bet = u256(data["bet"])
player_addr = Address(data["player"])
# 5% platform fee
fee = (bet * u256(5)) / u256(100)
player_share = bet - fee
# Platform gets fee
self.total_fees += fee
deployer_bal = self.player_balance.get(self.deployer, u256(0))
self.player_balance[self.deployer] = deployer_bal + fee
# Player gets winnings if correct
if data["is_correct"]:
player_bal = self.player_balance.get(player_addr, u256(0))
self.player_balance[player_addr] = player_bal + player_share
wins = self.player_wins.get(player_addr, u256(0))
self.player_wins[player_addr] = wins + 1
data["finalized"] = True
self.round_data[rid] = json.dumps(data)
@gl.public.view
def get_round_count(self) -> int:
return int(self.round_count)
@gl.public.view
def get_round(self, round_id: int) -> str:
rid = u256(round_id)
if rid not in self.round_data:
return json.dumps({"error": "Not found"})
return self.round_data[rid]
@gl.public.view
def get_balance(self, address: str) -> int:
addr = Address(address)
return int(self.player_balance.get(addr, u256(0)))
@gl.public.view
def get_stats(self, address: str) -> str:
addr = Address(address)
rounds = int(self.player_rounds.get(addr, u256(0)))
wins = int(self.player_wins.get(addr, u256(0)))
balance = int(self.player_balance.get(addr, u256(0)))
win_rate = (wins / rounds * 100) if rounds > 0 else 0
return json.dumps({
"address": address,
"rounds": rounds,
"wins": wins,
"win_rate": round(win_rate, 2),
"balance": balance
})
@gl.public.view
def get_leaderboard(self) -> str:
leaders = []
for addr in self.player_wins.keys():
wins = int(self.player_wins[addr])
rounds = int(self.player_rounds.get(addr, u256(0)))
win_rate = (wins / rounds * 100) if rounds > 0 else 0
leaders.append({
"address": addr.as_hex,
"wins": wins,
"rounds": rounds,
"win_rate": round(win_rate, 2)
})
leaders.sort(key=lambda x: x["wins"], reverse=True)
return json.dumps(leaders[:10])0x8E056cb829788507641fFBC066246Fd6B1D08b04