web3 language

This commit is contained in:
jcheca
2024-09-12 17:53:58 +02:00
parent 7fd8d73127
commit bebd993109
7 changed files with 119 additions and 62 deletions

View File

@@ -9,6 +9,7 @@ from .game import Game
from .models import Tournoi
from .utils import create_tournament, update_tournament, getlen
from asgiref.sync import sync_to_async
from .views import write_data
TOURNAMENT_NAMES = [
@@ -208,6 +209,10 @@ class TournamentMatchMaker:
})
await sync_to_async(update_tournament)(self.final_name, winner_username)
player_list = [player.user.username for player in self.waiting_players]
write_data(player_list, winner_username)
# Reset tournament state
self.waiting_players = []
self.matches = []

View File

@@ -11,7 +11,6 @@ urlpatterns = [
path('check_user_exists/', views.check_user_exists, name='check_user_exists'),
path('register_user/', views.register_user, name='register_user'),
path('authenticate_user/', views.authenticate_user, name='authenticate_user'),
path('web3/', views.read_data, name='read_data'),
path('api/match_list/', match_list_json, name='match_list_json'),
path('api/player_list/', player_list_json, name='player_list_json'),
path('api/tournoi_list/', tournoi_list_json, name='tournoi_list_json')

View File

@@ -102,68 +102,46 @@ def tournoi_list_json(request):
return JsonResponse(data)
import os
from web3 import Web3
import time
provider = Web3.HTTPProvider("https://sepolia.infura.io/v3/60e51df7c97c4f4c8ab41605a4eb9907")
provider = Web3.HTTPProvider(os.getenv('WEB3_PROVIDER'))
web3 = Web3(provider)
eth_gas_price = web3.eth.gas_price/1000000000
print(eth_gas_price)
contract_address = "0x078D04Eb6fb97Cd863361FC86000647DC876441B"
contract_abi = [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"uint256","name":"_timecode","type":"uint256"},{"internalType":"uint256","name":"_participantCount","type":"uint256"},{"internalType":"string[]","name":"_playerPseudonyms","type":"string[]"},{"internalType":"string[]","name":"_finalOrder","type":"string[]"}],"name":"addTournament","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getAllTournaments","outputs":[{"components":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"string","name":"name","type":"string"},{"internalType":"uint256","name":"timecode","type":"uint256"},{"internalType":"uint256","name":"participantCount","type":"uint256"},{"internalType":"string[]","name":"playerPseudonyms","type":"string[]"},{"internalType":"string[]","name":"finalOrder","type":"string[]"}],"internalType":"struct PongTournament.Tournament[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_id","type":"uint256"}],"name":"getTournament","outputs":[{"components":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"string","name":"name","type":"string"},{"internalType":"uint256","name":"timecode","type":"uint256"},{"internalType":"uint256","name":"participantCount","type":"uint256"},{"internalType":"string[]","name":"playerPseudonyms","type":"string[]"},{"internalType":"string[]","name":"finalOrder","type":"string[]"}],"internalType":"struct PongTournament.Tournament","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tournamentCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tournaments","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"string","name":"name","type":"string"},{"internalType":"uint256","name":"timecode","type":"uint256"},{"internalType":"uint256","name":"participantCount","type":"uint256"}],"stateMutability":"view","type":"function"}]
contract_address = os.getenv('CONTRACT_ADDRESS')
contract_abi = [{"inputs": [{"internalType": "uint256","name": "_timecode","type": "uint256"},{"internalType": "string[]","name": "_player_list","type": "string[]"},{"internalType": "string","name": "_winner","type": "string"}],"name": "addTournament","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [],"stateMutability": "nonpayable","type": "constructor"},{"inputs": [],"name": "getAllTournaments","outputs": [{"components": [{"internalType": "uint256","name": "id","type": "uint256"},{"internalType": "uint256","name": "timecode","type": "uint256"},{"internalType": "string[]","name": "player_list","type": "string[]"},{"internalType": "string","name": "winner","type": "string"}],"internalType": "struct PongTournament.Tournament[]","name": "","type": "tuple[]"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "uint256","name": "_id","type": "uint256"}],"name": "getTournament","outputs": [{"components": [{"internalType": "uint256","name": "id","type": "uint256"},{"internalType": "uint256","name": "timecode","type": "uint256"},{"internalType": "string[]","name": "player_list","type": "string[]"},{"internalType": "string","name": "winner","type": "string"}],"internalType": "struct PongTournament.Tournament","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "owner","outputs": [{"internalType": "address","name": "","type": "address"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "tournamentCount","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "uint256","name": "","type": "uint256"}],"name": "tournaments","outputs": [{"internalType": "uint256","name": "id","type": "uint256"},{"internalType": "uint256","name": "timecode","type": "uint256"},{"internalType": "string","name": "winner","type": "string"}],"stateMutability": "view","type": "function"}]
contract = web3.eth.contract(address=contract_address, abi=contract_abi)
def read_data(request):
# Créer une instance du contrat
def write_data(player_list, winner):
# Configuration de la transaction pour la fonction store
timecode = int(time.time())
account = os.getenv('WEB3_ACCOUNT')
private_key = os.getenv('PRIVATE_KEY')
# Appeler une fonction du contrat pour obtenir tous les tournois
tournaments = contract.functions.getAllTournaments().call()
print(contract_address, account, private_key)
print(timecode, player_list, winner)
# Afficher les résultats
json_data = []
for tournament in tournaments:
tournament_data = []
for item in tournament:
print(f"{item}")
tournament_data.append(item)
json_data.append(tournament_data)
nonce = web3.eth.get_transaction_count(account)
transaction = contract.functions.addTournament(timecode, player_list, winner).build_transaction({
'chainId': 11155111, # ID de la chaîne Sepolia
'gas': 2000000,
'gasPrice': web3.to_wei(eth_gas_price, 'gwei'),
'nonce': nonce
})
# Retourner le JSON comme réponse HTTP
# print(f"Tournament ID: {tournament[0]}")
# print(f"Name: {tournament[1]}")
# print(f"Timecode: {tournament[2]}")
# print(f"Participant Count: {tournament[3]}")
# print(f"Player Pseudonyms: {', '.join(tournament[4])}")
# print(f"Final Order: {', '.join(tournament[5])}")
print("-----------------------------")
return JsonResponse(json_data, safe=False)
# Signature de la transaction
signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
def write_data(request):
# addTournament(string,uint256,uint256,string[],string[])
# Envoi de la transaction
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("Transaction hash:", web3.to_hex(tx_hash))
# # Configuration de la transaction pour la fonction store
# account = "0x66CeBE2A1F7dae0F6AdBAad2c15A56A9121abfEf"
# private_key = "beb16ee3434ec5abec8b799549846cc04443c967b8d3643b943e2e969e7d25be"
# nonce = web3.eth.get_transaction_count(account)
# transaction = contract.functions.addTournament("test",1721830559,6,["aaudeber", "tlorne", "ocassany", "yestello", "jcheca", "toto"],["toto", "jcheca", "yestello", "tlorne", "ocassany", "aaudeber"]).build_transaction({
# 'chainId': 11155111, # ID de la chaîne Sepolia
# 'gas': 2000000,
# 'gasPrice': web3.to_wei(eth_gas_price, 'gwei'),
# 'nonce': nonce
# })
# # Signature de la transaction
# signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
# # Envoi de la transaction
# tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
# print("Transaction hash:", web3.to_hex(tx_hash))
# # Attente de la confirmation de la transaction
# tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
# print("Transaction receipt:", tx_receipt)
print("-----------------------------")
# Attente de la confirmation de la transaction
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
print("Transaction receipt:", tx_receipt)
print("-----------------------------")