forked from PatrickAlphaC/nft-mix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpful_scripts.py
179 lines (153 loc) · 5.79 KB
/
helpful_scripts.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from brownie import (
network,
accounts,
config,
interface,
LinkToken,
MockV3Aggregator,
MockOracle,
VRFCoordinatorMock,
Contract,
web3,
chain,
)
import os
import time
# Set a default gas price
from brownie.network import priority_fee
OPENSEA_FORMAT = "https://testnets.opensea.io/assets/{}/{}"
NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS = ["hardhat", "development", "ganache"]
LOCAL_BLOCKCHAIN_ENVIRONMENTS = NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS + [
"mainnet-fork",
"binance-fork",
"matic-fork",
]
contract_to_mock = {
"link_token": LinkToken,
"eth_usd_price_feed": MockV3Aggregator,
"vrf_coordinator": VRFCoordinatorMock,
"oracle": MockOracle,
}
def get_account(index=None, id=None):
if index:
return accounts[index]
if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
return accounts[0]
if id:
return accounts.load(id)
if network.show_active() in config["networks"]:
return accounts.add(config["wallets"]["from_key"])
return None
def get_contract(contract_name):
"""If you want to use this function, go to the brownie config and add a new entry for
the contract that you want to be able to 'get'. Then add an entry in the in the variable 'contract_to_mock'.
You'll see examples like the 'link_token'.
This script will then either:
- Get a address from the config
- Or deploy a mock to use for a network that doesn't have it
Args:
contract_name (string): This is the name that is refered to in the
brownie config and 'contract_to_mock' variable.
Returns:
brownie.network.contract.ProjectContract: The most recently deployed
Contract of the type specificed by the dictonary. This could be either
a mock or the 'real' contract on a live network.
"""
contract_type = contract_to_mock[contract_name]
if network.show_active() in NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS:
if len(contract_type) <= 0:
deploy_mocks()
contract = contract_type[-1]
else:
try:
contract_address = config["networks"][network.show_active()][contract_name]
contract = Contract.from_abi(
contract_type._name, contract_address, contract_type.abi
)
except KeyError:
print(
f"{network.show_active()} address not found, perhaps you should add it to the config or deploy mocks?"
)
print(
f"brownie run scripts/deploy_mocks.py --network {network.show_active()}"
)
return contract
def get_publish_source():
if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS or not os.getenv(
"ETHERSCAN_TOKEN"
):
return False
else:
return True
def get_breed(breed_number):
switch = {0: "PUG", 1: "SHIBA_INU", 2: "ST_BERNARD"}
return switch[breed_number]
def fund_with_link(
contract_address, account=None, link_token=None, amount=1000000000000000000
):
account = account if account else get_account()
link_token = link_token if link_token else get_contract("link_token")
tx = interface.LinkTokenInterface(link_token).transfer(
contract_address, amount, {"from": account}
)
print(f"Funded {contract_address}")
return tx
def get_verify_status():
verify = (
config["networks"][network.show_active()]["verify"]
if config["networks"][network.show_active()].get("verify")
else False
)
return verify
def deploy_mocks(decimals=18, initial_value=2000):
"""
Use this script if you want to deploy mocks to a testnet
"""
# Set a default gas price
priority_fee("1 gwei")
print(f"The active network is {network.show_active()}")
print("Deploying Mocks...")
account = get_account()
print("Deploying Mock Link Token...")
link_token = LinkToken.deploy({"from": account})
print("Deploying Mock Price Feed...")
mock_price_feed = MockV3Aggregator.deploy(
decimals, initial_value, {"from": account}
)
print(f"Deployed to {mock_price_feed.address}")
print("Deploying Mock VRFCoordinator...")
mock_vrf_coordinator = VRFCoordinatorMock.deploy(
link_token.address, {"from": account, "gas_price": chain.base_fee}
)
print(f"Deployed to {mock_vrf_coordinator.address}")
print("Deploying Mock Oracle...")
mock_oracle = MockOracle.deploy(link_token.address, {"from": account})
print(f"Deployed to {mock_oracle.address}")
print("Mocks Deployed!")
def listen_for_event(brownie_contract, event, timeout=200, poll_interval=2):
"""Listen for an event to be fired from a contract.
We are waiting for the event to return, so this function is blocking.
Args:
brownie_contract ([brownie.network.contract.ProjectContract]):
A brownie contract of some kind.
event ([string]): The event you'd like to listen for.
timeout (int, optional): The max amount in seconds you'd like to
wait for that event to fire. Defaults to 200 seconds.
poll_interval ([int]): How often to call your node to check for events.
Defaults to 2 seconds.
"""
web3_contract = web3.eth.contract(
address=brownie_contract.address, abi=brownie_contract.abi
)
start_time = time.time()
current_time = time.time()
event_filter = web3_contract.events[event].createFilter(fromBlock="latest")
while current_time - start_time < timeout:
for event_response in event_filter.get_new_entries():
if event in event_response.event:
print("Found event!")
return event_response
time.sleep(poll_interval)
current_time = time.time()
print("Timeout reached, no event found.")
return {"event": None}