-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfanaticfi.py
247 lines (188 loc) · 9.84 KB
/
fanaticfi.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Imports
import json
import math
from web3 import Web3
import streamlit as st
from investor_wallet import generate_account, get_balance
def write_supply():
# Get how many tokens are left in crowdsale
cap_goal = crowdsale_contract.functions.cap().call() * 10 ** (-18)
total_raised = crowdsale_contract.functions.weiRaised().call() * 10 ** (-18)
supply_left = cap_goal - total_raised
# Display total tokens left in crowdsale
st.sidebar.markdown("## Tokens Left")
st.sidebar.write(f"{supply_left} Tokens")
#----------------------------------------------------------------------------------------------------------------------
# Preparations
#----------------------------------------------------------------------------------------------------------------------
# Link app with ganache
w3 = Web3(Web3.HTTPProvider('HTTP://127.0.0.1:8545'))
# Load all abi files
# abi_database = {
# "BillGates_abis": ["abi_files/BillGates_abi.json", "token_abi","BillGates_abi",
# "abi_files/BillGates_Crowdsale_abi.json", "crowdsale_abi", "BillGates_Crowdsale_abi",
# "abi_files/BillGates_deployer_abi.json", "deployer_abi", "BillGates_deployer_abi"]
# }
# abi_db_list = list(abi_database.values())
# abi_owners = ["BillGates_abis"]
# with open(abi_database[abi_owners][0]) as abi_database[abi_owners][1]:
# abi_database[abi_owners][2] = json.load(abi_database[abi_owners][1])
# with open(abi_database[abi_owners][3]) as abi_database[abi_owners][4]:
# abi_database[abi_owners][5] = json.load(abi_database[abi_owners][4])
# with open(abi_database[abi_owners][6]) as abi_database[abi_owners][7]:
# abi_database[abi_owners][8] = json.load(abi_database[abi_owners][7])
#########
######### The method above was attemp to keep the code DRY, but we realized the unhashable list error. Below method is our final choice.
#########
with open("abi_files/BillGates_abi.json") as token_abi:
BillGates_abi = json.load(token_abi)
with open("abi_files/BillGates_Crowdsale_abi.json") as crowdsale_abi:
BillGates_Crowdsale_abi = json.load(crowdsale_abi)
with open("abi_files/BillGates_deployer_abi.json") as deployer_abi:
BillGates_deployer_abi = json.load(deployer_abi)
with open("abi_files/ElonMusk_abi.json") as elon_token_abi:
ElonMusk_abi = json.load(elon_token_abi)
with open("abi_files/ElonMusk_Crowdsale_abi.json") as elon_crowdsale_abi:
ElonMusk_Crowdsale_abi = json.load(elon_crowdsale_abi)
with open("abi_files/ElonMusk_deployer_abi.json") as elon_deployer_abi:
ElonMusk_deployer_abi = json.load(elon_deployer_abi)
with open("abi_files/DonaldTrump_abi.json") as trump_token_abi:
DonaldTrump_abi = json.load(trump_token_abi)
with open("abi_files/DonaldTrump_Crowdsale_abi.json") as trump_crowdsale_abi:
DonaldTrump_Crowdsale_abi = json.load(trump_crowdsale_abi)
with open("abi_files/DonaldTrump_deployer_abi.json") as trump_deployer_abi:
DonaldTrump_deployer_abi = json.load(trump_deployer_abi)
with open("abi_files/Kimchi_abi.json") as kim_token_abi:
Kimchi_abi = json.load(kim_token_abi)
with open("abi_files/Kimchi_Crowdsale_abi.json") as kim_crowdsale_abi:
Kimchi_Crowdsale_abi = json.load(kim_crowdsale_abi)
with open("abi_files/Kimchi_deployer_abi.json") as kim_deployer_abi:
Kimchi_deployer_abi = json.load(kim_deployer_abi)
# Establish celebrity database
celebrity_database = {
"BILLYG": ["6 pack Bill", "0xd6a8b63AAFF4789D4156ec84Bc2B9030EaB87eed", 0.0066, "pics/bill.jpeg",
'0x6Cf44E3d171eC73f6A12D923e5eb30B901E7bf5a', '0x8d21d57d5D8bd214C54C61417C2Ee74a2Ff172Ed', BillGates_deployer_abi,
BillGates_Crowdsale_abi, BillGates_abi],
"ELON": ["Get lit with Elon Musk", "0xd9BA61598720508C1A7FCC403EA87E6de762e0C6", 0.0042, "pics/elon.jpeg",
'0xDfE15b50803748BB523df696a972E2bCd12C0d2f', '0x3F62D19Db825A0aa73A491239E3e41dF46BbdafE', ElonMusk_deployer_abi,
ElonMusk_Crowdsale_abi, ElonMusk_abi],
"TRUMP": ["Save Donald Trump", "0x0E68C98676DbEfE6A1a53e304E8D0FA8027Fa122", 0.0099, "pics/trump.jpeg",
'0xaba4711771e0B7AFC30237F3D3E41876B9D64538', '0xf7D5201Aa9Ed749A8573BcdF7D89b53534B394EB', DonaldTrump_deployer_abi,
DonaldTrump_Crowdsale_abi, DonaldTrump_abi],
"KIMCHI": ["Kim owns all blockchains", "0x28CF16cd57cb03AA25B3A8a7F4e4eA25A778D385", 0.0011, "pics/kim.jpeg",
'0x583cfC469CcF2a14bc8B77b1740a6b2333E6F721','0xD86f7e5fECA055e6B3B5d93C9Dc6EEC0C95097d7', Kimchi_deployer_abi,
Kimchi_Crowdsale_abi, Kimchi_abi]
}
db_list = list(celebrity_database.values())
# A list of public figures' token names
token_names = ["BILLYG", "ELON", "TRUMP", "KIMCHI"]
#----------------------------------------------------------------------------------------------------------------------
# Main page functions and designs
#----------------------------------------------------------------------------------------------------------------------
# Streamlit application headings
col1, col2 = st.columns((1,2))
with col1:
st.text(" \n")
st.image("pics/fanaticfi.jpeg", width=150)
# st.text(" \n")
with col2:
st.text(" \n")
st.title("Welcome to FanaticFi!")
st.text("WHERE FANS AND IDOLS CONNECT!")
st.text(" \n")
# Display the database of celebrities and their tokens information
def get_people(w3):
count=0
max_cols=2
max_rows=math.ceil(len(token_names)/max_cols)
for each_row in range(max_rows):
columns=st.columns(max_cols)
for each_col in range(max_cols):
if count <= len(token_names):
with columns[each_col]:
st.image(db_list[count][3], width=250)
st.write("#### ", db_list[count][0])
st.write("Ethereum Account Address: ", db_list[count][1])
st.write("#### Token Price: ", db_list[count][2], "ETH per token")
st.text(" \n")
count +=1
#----------------------------------------------------------------------------------------------------------------------
# Sidebar functions and designs
#----------------------------------------------------------------------------------------------------------------------
st.sidebar.markdown("## Purchase Celebrity Tokens HERE!")
# Allow investors to create an account to buy tokens
investor_account = generate_account()
# Write investor Ethereum account address and ballance to the sidebar
st.sidebar.markdown("## My account and balance")
st.sidebar.write(investor_account.address)
st.sidebar.write(get_balance(w3, investor_account.address), "ETH")
#Allow investors to select tokens to invest
token_name = st.sidebar.selectbox('Select Token to Invest', token_names)
number_of_tokens = st.sidebar.number_input("Number of Tokens Purchasing", step = 1)
st.sidebar.write('---')
st.sidebar.markdown("## Token Name, Token Price, and Celebrity Ethereum Address")
# Identify the public figure that investing to
celebrity = celebrity_database[token_name][0]
# Write the Fintech Finder candidate's name to the sidebar
st.sidebar.write(celebrity)
# Clarify token price of selected celebrity
token_price = celebrity_database[token_name][2]
# Write the token price of selected celebrity to the sidebar
st.sidebar.write(token_price)
# Identify selected celebrity's Ethereum Address
celebrity_address = celebrity_database[token_name][1]
# Write selected celebrity's Ethereum Address to the sidebar
st.sidebar.write(celebrity_address)
# Write total cost of tokens
st.sidebar.markdown("## Total Token Cost in Ether")
total_cost = token_price * number_of_tokens
st.sidebar.write(total_cost)
st.sidebar.write(celebrity_address)
st.sidebar.write(investor_account)
# Load deployer, crowdsale, and token contracts with abi files
deployer_contract=w3.eth.contract(address=celebrity_address, abi=celebrity_database[token_name][6])
crowdsale_contract=w3.eth.contract(address=celebrity_database[token_name][4], abi=celebrity_database[token_name][7])
token_contract=w3.eth.contract(address=celebrity_database[token_name][5], abi=celebrity_database[token_name][8])
write_supply()
#----------------------------------------------------------------------------------------------------------------------
# Transaction functionality designs
#----------------------------------------------------------------------------------------------------------------------
from web3.gas_strategies.time_based import medium_gas_price_strategy
def buy_token(w3, beneficiary, total_price):
# Set gas price strategy
w3.eth.setGasPriceStrategy(medium_gas_price_strategy)
# Convert eth amount to Wei
value = w3.toWei(total_price, "ether")
# Calculate gas estimate
gasEstimate = w3.eth.estimateGas(
{"from": beneficiary.address, "value": value})
# Construct a raw transaction
txn={
'from': beneficiary.address,
'value': value,
'gas': gasEstimate,
'gasPrice': 0,
'nonce': w3.eth.get_transaction_count(beneficiary.address)
}
# Sign the raw transaction with ethereum account
signed_txn=beneficiary.signTransaction(txn)
crowdsale_contract.functions.buyTokens(beneficiary.address).buildTransaction(txn)
# Send the signed transactions
return w3.eth.sendRawTransaction(signed_txn.rawTransaction)
# Click button to buy tokens
if st.sidebar.button("Buy Token"):
transaction_hash = buy_token(w3, investor_account, total_cost)
# Calculate remaining number of tokens available for sale
# total_supply = token_contract.functions.totalSupply().call()
# cap = crowdsale_contract.functions.cap().call()
# remaining_tokens = cap - total_supply
write_supply()
# Write available token balance
# st.sidebar.write("#### Only ", remaining_tokens, "amount of tokens are available")
# Markdown for the transaction hash
st.sidebar.markdown("#### Validated Transaction Hash")
# Write the returned transaction hash to the screen
st.sidebar.write(transaction_hash)
# Celebrate your successful payment
st.balloons()
get_people(w3)