-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (81 loc) · 3.09 KB
/
app.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
import json
import os
import streamlit as st
from dotenv import load_dotenv
from pathlib import Path
from web3 import Web3
load_dotenv()
w3 = Web3(Web3.HTTPProvider(os.getenv("WEB3_PROVIDER_URI")))
@st.cache_resource
def load_contract():
with open(Path('./abi/PollDAppABI.json')) as f:
abi = json.load(f)
contract_address = os.getenv("SMART_CONTRACT_ADDRESS")
contract = w3.eth.contract(
address=w3.to_checksum_address(contract_address),
abi=abi
)
return contract
contract = load_contract()
if 'poll_count' not in st.session_state:
st.session_state['poll_count'] = contract.functions.getPollCount().call()
def refresh_poll_count():
st.session_state['poll_count'] = contract.functions.getPollCount().call()
def add_field():
st.session_state['option_count'] += 1
st.title("PollD")
st.text(f"Total Polls: {st.session_state.poll_count}")
with st.form("create_poll_form"):
question = st.text_input("Enter your poll question...")
st.divider()
if 'option_count' not in st.session_state:
st.session_state['option_count'] = 1
for i in range(st.session_state['option_count']):
st.text_input(f"Option {i+1}", key=f"option_{i+1}")
st.form_submit_button("Add another field", on_click=add_field)
st.divider()
html_code = """
<button onclick="connectWallet()">Connect to MetaMask</button>
<p id="status"></p>
<script>
async function connectWallet() {
if (typeof window.ethereum !== 'undefined') {
try {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
document.getElementById('status').innerText = 'Connected: ' + accounts[0];
// Add more JavaScript here to handle transactions
} catch (error) {
document.getElementById('status').innerText = 'Error connecting: ' + error.message;
}
} else {
document.getElementById('status').innerText = 'MetaMask is not available.';
}
}
</script>
"""
st.markdown(html_code, unsafe_allow_html=True)
if st.form_submit_button("Create Poll"):
wallet_address = os.getenv("WALLET_ADDRESS")
options = []
for i in range(st.session_state['option_count']):
options.append(st.session_state[f"option_{i+1}"])
tx = contract.functions.createPoll(
question,
options,
0
).build_transaction({
"from": wallet_address,
"nonce": w3.eth.get_transaction_count(wallet_address)
})
signed_tx = w3.eth.account.sign_transaction(
tx,
private_key=os.getenv("WALLET_PRIVATE_KEY")
)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
w3.eth.wait_for_transaction_receipt(tx_hash)
st.session_state['last_transaction'] = w3.eth.get_transaction(tx_hash)
refresh_poll_count()
if 'last_transaction' in st.session_state:
st.success("Poll Created")
with st.expander("Transaction Details"):
st.write(st.session_state['last_transaction'])