-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_buddy_customer_app.py
137 lines (108 loc) · 4.73 KB
/
streamlit_buddy_customer_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
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
#Importing required libraries
import os
import json
from web3 import Web3
from pathlib import Path
from dotenv import load_dotenv
import streamlit as st
#Load in the .env file
load_dotenv()
#Defining and connecting the Web3 provider
w3 = Web3(Web3.HTTPProvider(os.getenv("WEB3_PROVIDER_URI")))
#Create a function to load the contract into the app
@st.cache(allow_output_mutation=True)
def load_contract():
with open(Path(r'abi/BuddyToken2_abi.json')) as f:
buddy_abi = json.load(f)
contract_address = os.getenv("SMART_CONTRACT_ADDRESS")
contract = w3.eth.contract(
address=contract_address,
abi= buddy_abi
)
return contract
#Setting a variable 'contract' to the smart contract loaded in with Web3
token = load_contract()
#Create a function to load the oracle contract into the app
@st.cache(allow_output_mutation=True)
def load_oracle_contract():
with open(Path(r'abi/Oracle_abi.json')) as f:
oracle_abi = json.load(f)
contract_address = os.getenv("SMART_CONTRACT_ADDRESS")
contract = w3.eth.contract(
address=contract_address,
abi= oracle_abi
)
return contract
#Setting a variable 'contract' to the smart contract loaded in with Web3
oracle_contract = load_oracle_contract()
#Create welcome section for the app
st.image(r'resources\BUDlogo2.png')
st.title('Welcome to the Buddy Token Customer App!')
#Display value of Ethereum on sidebar widget
#eth_price = oracle_contract.functions.getLatestPrice().call()
#st.sidebar.write(eth_price)
#Set up accounts via Web3 connection
accounts = w3.eth.accounts
address = st.sidebar.selectbox('Select Ethereum Account', options = accounts)
st.sidebar.subheader('The value of Ethereum today is:')
st.header('What would you like to do today?')
#Ideally, we would want the following 3 buttons to navigate to a different page for the app
#st.button('Purchase Product')
#st.button('Purchase Buddy Tokens')
#st.button('Return/Exchanges')
#Creating the purchase order button for Streamlit app
st.header('Buddy Token Purchase Order')
#Create a slider widget allowing customers to select the quantity of tokens they want to purchase
token_quantity = st.slider('Select how many Buddy Tokens you want to purchase (per transaction limits: min. is 1; max is 10.):',
min_value = 1, max_value = 10)
if token_quantity == 1:
st.write('You are buying', token_quantity, 'Buddy Token.')
else:
st.write('You are buying', token_quantity, 'Buddy Tokens.')
#Create button to confirm purchase of the tokens
if st.button('Purchase Buddy Tokens'):
tx_hash = token.functions.purchase(token_quantity).transact({'from': address})
st.write('Tokens Purchased!')
st.balloons()
#Create section for customer to see their token balance
if st.button('Show Buddy Token Balance Snapshot'):
token_balance_snapshot = token_quantity
st.write('Your Buddy Token Balance is', token_balance_snapshot, 'Buddy Tokens.')
#Create section for customer to purchase store product
st.header('Product Purchase Order')
st.write('Please select your purchase options below:')
#Create a selection box of the product that the customer wants to purchase
st.selectbox('Product Type', ['Cannabis', 'Accessories'])
#Create a list of product options
product_options = ['1/8 oz - $10', '1/4 oz - $18', '1/2 oz - $26', '1 oz - $34']
#Set variables to the product selection and quantity to calculate the total cost
selection = st.selectbox('Select Product Unit', product_options)
quantity = st.number_input('Quantity - Please use whole numbers only.')
#Create a function to show the customer the total cost of their product purchase
@st.cache(suppress_st_warning=True)
def get_total_cost(selection, quantity):
price = 0
if selection == '1/8 oz - $10':
price = 10
total_cost = price * quantity
st.write('The total cost for your order is $', round(total_cost, 2), '.')
elif selection == '1/4 oz - $18':
price = 18
total_cost = price * quantity
st.write('The total cost for your order is $', round(total_cost, 2), '.')
elif selection == '1/2 oz - $26':
price = 26
total_cost = price * quantity
st.write('The total costs for your order is $', round(total_cost, 2), '.')
elif selection == '1 oz - $34':
price = 34
total_cost = price * quantity
st.write('The total cost for your order is $', round(total_cost, 2), '.')
#Create button to display total cost of product purchase to customer
if st.button('Show Total Cost'):
get_total_cost(selection, quantity)
#This button is still in process, to confirm the store product purchase from the store
confirm_purchase = st.button('Confirm Purchase')
if confirm_purchase:
#Put in order for the product
st.write("Purchase confirmed.")