-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVRF.py
72 lines (54 loc) · 1.87 KB
/
VRF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import json
import time
from web3 import Web3
with open("contracts/VRFv2Consumer_abi.json") as f:
info_json = json.load(f)
ABI = info_json["output"]["abi"]
CONTRACT = os.environ["CONTRACT_VRF"]
WALLET = os.environ["WALLET"]
PRIV_KEY = os.environ["PRIV_KEY"]
sepolia_rpc_url = 'https://rpc.sepolia.org/'
def oracle_random_number():
"""
this funtion call a random number from chainlink
:return:
"""
w3 = Web3(Web3.HTTPProvider(sepolia_rpc_url))
if w3.is_connected():
print("-" * 50)
print("Connection Successful")
print("-" * 50)
else:
print("Connection Failed")
contract_address = CONTRACT
contract_abi = ABI
queries = 20
contract = w3.eth.contract(address=contract_address, abi=contract_abi)
account_address = WALLET
private_key = PRIV_KEY
function_data = contract.functions.requestRandomWords().build_transaction({
'from': account_address,
'gas': 3000000,
'gasPrice': w3.to_wei('10', 'gwei'),
'nonce': w3.eth.get_transaction_count(account_address),
'chainId': 11155111,
})
signed_transaction = w3.eth.account.sign_transaction(function_data, private_key)
transaction_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print("Hash send transaction: ", transaction_hash.hex())
time.sleep(10)
result = contract.functions.lastRequestId().call()
print("lastRequestId:", result)
for i in range(queries):
request_status = contract.functions.s_requests(result).call()
if request_status[0]:
raw_number = contract.functions.getRequestStatus(result)
print("Request OK")
break
else:
print("Processing request")
time.sleep(10)
print("VRF random number: ")
print(raw_number.arguments[0])
return raw_number.arguments[0]