This commit is contained in:
jcheca
2024-08-08 16:28:16 +02:00
parent b4dfc443a7
commit ff50d6d054
4 changed files with 148 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ 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('players/', player_list, name='player_list'),
path('matches/', match_list, name='match_list'),
path('tournois/', tournoi_list, name='tournoi_list'),

View File

@@ -202,3 +202,73 @@ def tournoi_list(request):
return render(request, 'pong/tournoi_list.html', {'tournois': tournois})
####################### THEOUCHE PART ############################
####################### jcheca PART ############################
from web3 import Web3
provider = Web3.HTTPProvider("https://sepolia.infura.io/v3/60e51df7c97c4f4c8ab41605a4eb9907")
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 = web3.eth.contract(address=contract_address, abi=contract_abi)
def read_data(request):
# Créer une instance du contrat
# Appeler une fonction du contrat pour obtenir tous les tournois
tournaments = contract.functions.getAllTournaments().call()
# 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)
# 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)
def write_data(request):
# addTournament(string,uint256,uint256,string[],string[])
# # 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("-----------------------------")