|
@@ -0,0 +1,334 @@
|
|
|
+import web3.exceptions
|
|
|
+from web3 import Web3
|
|
|
+import service
|
|
|
+
|
|
|
+current_user_address = ''
|
|
|
+current_user_data = ''
|
|
|
+
|
|
|
+contract_address = '0x041a4dAAD14c913aE81B70BF03599c3568Ccf8D3'
|
|
|
+
|
|
|
+contract_abi = [
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Name",
|
|
|
+ "type": "string"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "uint256",
|
|
|
+ "name": "Price",
|
|
|
+ "type": "uint256"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "AddNewProduct",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Name",
|
|
|
+ "type": "string"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "BuyProduct",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "address",
|
|
|
+ "name": "addressUser",
|
|
|
+ "type": "address"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "DemotionSellerToBuyer",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Name",
|
|
|
+ "type": "string"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "uint256",
|
|
|
+ "name": "Price",
|
|
|
+ "type": "uint256"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "EditProduct",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "address",
|
|
|
+ "name": "addressUser",
|
|
|
+ "type": "address"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "PromotionToAdmin",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "address",
|
|
|
+ "name": "addressUser",
|
|
|
+ "type": "address"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "PromotionToSeller",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Name",
|
|
|
+ "type": "string"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Password",
|
|
|
+ "type": "string"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "RegUser",
|
|
|
+ "outputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [],
|
|
|
+ "stateMutability": "nonpayable",
|
|
|
+ "type": "constructor"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Password",
|
|
|
+ "type": "string"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "AuthUser",
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "internalType": "bool",
|
|
|
+ "name": "",
|
|
|
+ "type": "bool"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "stateMutability": "view",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [],
|
|
|
+ "name": "GetProducts",
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string[]",
|
|
|
+ "name": "",
|
|
|
+ "type": "string[]"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "stateMutability": "view",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "",
|
|
|
+ "type": "string"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "products",
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "internalType": "uint256",
|
|
|
+ "name": "",
|
|
|
+ "type": "uint256"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "stateMutability": "view",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [
|
|
|
+ {
|
|
|
+ "internalType": "address",
|
|
|
+ "name": "",
|
|
|
+ "type": "address"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "name": "users",
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "internalType": "string",
|
|
|
+ "name": "Name",
|
|
|
+ "type": "string"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "bytes32",
|
|
|
+ "name": "PasswordHash",
|
|
|
+ "type": "bytes32"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "uint256",
|
|
|
+ "name": "Balance",
|
|
|
+ "type": "uint256"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "uint256",
|
|
|
+ "name": "Role",
|
|
|
+ "type": "uint256"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "internalType": "bool",
|
|
|
+ "name": "isUserExist",
|
|
|
+ "type": "bool"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "stateMutability": "view",
|
|
|
+ "type": "function"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "inputs": [],
|
|
|
+ "name": "ViewBalance",
|
|
|
+ "outputs": [
|
|
|
+ {
|
|
|
+ "internalType": "uint256",
|
|
|
+ "name": "",
|
|
|
+ "type": "uint256"
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "stateMutability": "view",
|
|
|
+ "type": "function"
|
|
|
+ }
|
|
|
+]
|
|
|
+
|
|
|
+#
|
|
|
+w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
|
|
|
+store_contract = w3.eth.contract(address=service.contract_address,
|
|
|
+ abi=service.contract_abi)
|
|
|
+
|
|
|
+
|
|
|
+def auth():
|
|
|
+ print('Enter address:')
|
|
|
+ address = input()
|
|
|
+ print('Enter password:')
|
|
|
+ password = input()
|
|
|
+
|
|
|
+ if address != '' and password != '':
|
|
|
+ try:
|
|
|
+ auth_user = store_contract.functions.AuthUser(password).call({'from': address})
|
|
|
+ if auth_user:
|
|
|
+ print('You auth')
|
|
|
+ service.current_user_address = address
|
|
|
+ service.current_user_data = store_contract.functions.users(service.current_user_address).call()
|
|
|
+ if service.current_user_data[3] == 0:
|
|
|
+ admin_menu()
|
|
|
+ elif service.current_user_data[3] == 1:
|
|
|
+ seller_menu()
|
|
|
+ elif service.current_user_data[3] == 2:
|
|
|
+ buyer_menu()
|
|
|
+ else:
|
|
|
+ print('No auth')
|
|
|
+ return
|
|
|
+ except web3.exceptions.ContractLogicError as err:
|
|
|
+ print(err)
|
|
|
+
|
|
|
+
|
|
|
+def reg():
|
|
|
+ print("Enter address: ")
|
|
|
+ address = input()
|
|
|
+ print('Enter name: ')
|
|
|
+ name = input()
|
|
|
+ print('Enter password: ')
|
|
|
+ password = input()
|
|
|
+ if address != '' and password != '' and name != '':
|
|
|
+ try:
|
|
|
+ store_contract.functions.RegUser(name, password).transact({'from': address})
|
|
|
+ except web3.exceptions.ContractLogicError as err:
|
|
|
+ print(err)
|
|
|
+
|
|
|
+
|
|
|
+def admin_menu():
|
|
|
+ while True:
|
|
|
+ print("Главное меню")
|
|
|
+ print("1 - Узнать баланс")
|
|
|
+ print("2 - Повысить пользователя")
|
|
|
+ print("3 - Повысить до продавца")
|
|
|
+ print("4 - Понизить продавца до покупателя")
|
|
|
+ print("0 - Close")
|
|
|
+
|
|
|
+ user_choice = int(input())
|
|
|
+ if user_choice == 1:
|
|
|
+ print(store_contract.functions.ViewBalance().call({'from': service.current_user_address}))
|
|
|
+ elif user_choice == 2:
|
|
|
+ print('Enter address: ')
|
|
|
+ address = input()
|
|
|
+ try:
|
|
|
+ store_contract.functions.PromotionToAdmin(address).transact({'from': service.current_user_address})
|
|
|
+ except web3.exceptions.ContractLogicError as err:
|
|
|
+ print(err)
|
|
|
+ elif user_choice == 3:
|
|
|
+ print('Enter address: ')
|
|
|
+ address = input()
|
|
|
+ try:
|
|
|
+ store_contract.functions.PromotionToSeller(address).transact({'from': service.current_user_address})
|
|
|
+ except web3.exceptions.ContractLogicError as err:
|
|
|
+ print(err)
|
|
|
+ elif user_choice == 4:
|
|
|
+ print('Enter address: ')
|
|
|
+ address = input()
|
|
|
+ try:
|
|
|
+ store_contract.functions.DemotionSellerToBuyer(address).transact({'from': service.current_user_address})
|
|
|
+ except web3.exceptions.ContractLogicError as err:
|
|
|
+ print(err)
|
|
|
+ elif user_choice == 0:
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ break
|
|
|
+
|
|
|
+
|
|
|
+def seller_menu():
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+def buyer_menu():
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+while True:
|
|
|
+ print("Hi")
|
|
|
+ print("1 - Auth")
|
|
|
+ print("2 - Reg")
|
|
|
+ print("0 - Close")
|
|
|
+ user_choice = int(input())
|
|
|
+
|
|
|
+ if user_choice == 1:
|
|
|
+ auth()
|
|
|
+ elif user_choice == 2:
|
|
|
+ reg()
|
|
|
+ elif user_choice == 0:
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ break
|