mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2026-02-04 03:30:26 +01:00
add web3
This commit is contained in:
parent
b4dfc443a7
commit
ff50d6d054
@ -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'),
|
||||
|
||||
@ -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("-----------------------------")
|
||||
|
||||
|
||||
@ -67,6 +67,50 @@
|
||||
font-size: 16px;
|
||||
}
|
||||
</style>
|
||||
<style>
|
||||
.modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
z-index: 1;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: auto;
|
||||
background-color: rgb(0,0,0);
|
||||
background-color: rgba(0,0,0,0.4);
|
||||
padding-top: 60px;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background-color: #fefefe;
|
||||
margin: 5% auto;
|
||||
padding: 20px;
|
||||
border: 5px solid #888;
|
||||
width: 80%;
|
||||
max-height: 70vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.close {
|
||||
color: #aaa;
|
||||
float: right;
|
||||
font-size: 28px;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.close:hover,
|
||||
.close:focus {
|
||||
color: black;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
pre {
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
<body>
|
||||
@ -105,6 +149,37 @@
|
||||
</div>
|
||||
|
||||
|
||||
<button id="myBtn">Resultat</button>
|
||||
<div id="myModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<span class="close">×</span>
|
||||
<pre id="jsonContent"></pre>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var modal = document.getElementById("myModal");
|
||||
var btn = document.getElementById("myBtn");
|
||||
var span = document.getElementsByClassName("close")[0];
|
||||
var jsonContent = document.getElementById("jsonContent");
|
||||
btn.onclick = function() {
|
||||
fetch('/web3/')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
jsonContent.textContent = JSON.stringify(data, null, 2);
|
||||
modal.style.display = "block";
|
||||
});
|
||||
}
|
||||
span.onclick = function() {
|
||||
modal.style.display = "none";
|
||||
}
|
||||
window.onclick = function(event) {
|
||||
if (event.target == modal) {
|
||||
modal.style.display = "none";
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<div class="container" id="block-form">
|
||||
<h1 id="welcome">BIENVENUE DANS LE PONG 42</h1>
|
||||
<div class="input-container">
|
||||
|
||||
@ -2,4 +2,5 @@ Django
|
||||
psycopg2
|
||||
python-dotenv
|
||||
channels
|
||||
daphne
|
||||
daphne
|
||||
web3
|
||||
Loading…
x
Reference in New Issue
Block a user