main1.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #! /usr/bin/env python3
  2. """
  3. This is the main script for the Magna Simulation.
  4. It generates a GUI were the user can modify the parameter of the simulation.
  5. Then when user click on the button "Launch" it runs one month.
  6. During one month each generated client will make 1, 2 or 3 transfer,
  7. according to his
  8. """
  9. import math
  10. import time
  11. import tkinter as tk
  12. import numpy as np
  13. import pandas as pd
  14. import MagnaSim1 as MaSi
  15. WINDOW = tk.Tk()
  16. WINDOW.title("MaSi")
  17. # WINDOW.geometry('600x500')
  18. # This is a list we will need to work on the dictionnaries
  19. #that will contains the values the user give
  20. #Consider this like our tokens combinaisons index
  21. LBL_CLIENT_TXT = ["BTC", "ETH", "MGN", "BTC/ETH", "ETH/MGN", "MGN/BTC", "B/E/M"]
  22. # We create the 2 first lines for the number of client
  23. #using each possible combination of tokens
  24. LBL_CLIENT_N = tk.Label(WINDOW, text="Nbr clients:")
  25. LBL_CLIENT_N.grid(column=0, row=0)
  26. LBL_CLIENT_COORD = {"BTC":(0, 1), "ETH":(0, 3), "MGN":(0, 5),
  27. "BTC/ETH":(1, 1), "ETH/MGN":(1, 3),
  28. "MGN/BTC":(1, 5), "B/E/M":(2, 1)}
  29. TXT_CLIENT_COORD = {"BTC":(0, 2), "ETH":(0, 4), "MGN":(0, 6),
  30. "BTC/ETH":(1, 2), "ETH/MGN":(1, 4),
  31. "MGN/BTC":(1, 6), "B/E/M":(2, 2)}
  32. LBL_CLIENT_TOKEN = {}
  33. TXT_CLIENT_TOKEN = {}
  34. for token in LBL_CLIENT_TXT:
  35. LBL_CLIENT_TOKEN[token] = tk.Label(WINDOW, text=token)
  36. TXT_CLIENT_TOKEN[token] = tk.Entry(WINDOW, width=10)
  37. TXT_CLIENT_TOKEN[token].insert(0, "0")
  38. LBL_CLIENT_TOKEN[token].grid(row=LBL_CLIENT_COORD[token][0], column=LBL_CLIENT_COORD[token][1])
  39. TXT_CLIENT_TOKEN[token].grid(row=TXT_CLIENT_COORD[token][0], column=TXT_CLIENT_COORD[token][1])
  40. # We create the third line were the user will input the rate
  41. #of fees for each type of token
  42. LBL_RATES_N = tk.Label(WINDOW, text="% Frais:")
  43. LBL_RATES_N.grid(column=0, row=3)
  44. LBL_RATES_COORD = {"BTC":(3, 1), "ETH":(3, 3), "MGN":(3, 5)}
  45. TXT_RATES_COORD = {"BTC":(3, 2), "ETH":(3, 4), "MGN":(3, 6)}
  46. LBL_RATES = {}
  47. TXT_RATES = {}
  48. for token in LBL_CLIENT_TXT[0:3]:
  49. LBL_RATES[token] = tk.Label(WINDOW, text=token)
  50. TXT_RATES[token] = tk.Entry(WINDOW, width=10)
  51. TXT_RATES[token].insert(0, "0")
  52. LBL_RATES[token].grid(row=LBL_RATES_COORD[token][0], column=LBL_RATES_COORD[token][1])
  53. TXT_RATES[token].grid(row=TXT_RATES_COORD[token][0], column=TXT_RATES_COORD[token][1])
  54. def clicked():
  55. """
  56. This function define the actions occuring when the user launch the simulation.
  57. """
  58. # First we get the number of clients
  59. # in each category
  60. nbr_client = {}
  61. for token in LBL_CLIENT_TXT:
  62. nbr_client[token] = int(TXT_CLIENT_TOKEN[token].get())
  63. if nbr_client[token] == None:#To avoid erors
  64. nbr_client[token] = 0
  65. # Then we get the rates for each token
  66. rates_fee = {}
  67. # The rates are for BTC, ETH or MGN so we use the first
  68. #3 values of our tokens combinations index
  69. for token in LBL_CLIENT_TXT[0:3]:
  70. rates_fee[token] = float(TXT_RATES[token].get())
  71. # We print the total of clients
  72. LBL_SUM.configure(text=sum(nbr_client.values()))
  73. print(sum(nbr_client.values()))
  74. # We create a population
  75. pop = MaSi.Populations(nbr_client)
  76. # We create the list of transaction
  77. trans = MaSi.Transaction()
  78. trans_value = {"BTC":0, "ETH":0, "MGN":0}
  79. trans_fees = {"BTC":0, "ETH":0, "MGN":0}
  80. for i in pop.population:
  81. x = abs(np.random.normal(i.magna_wallet_btc[0], i.magna_wallet_btc[1]))
  82. if x != 0:
  83. trans.btc += 1
  84. trans_value["BTC"] += x
  85. trans_fees["BTC"] += x*rates_fee[token]
  86. y = abs(np.random.normal(i.magna_wallet_eth[0], i.magna_wallet_eth[1]))
  87. if y != 0:
  88. trans.eth += 1
  89. trans_value["ETH"] += y
  90. trans_fees["ETH"] += y*rates_fee["ETH"]
  91. z = abs(np.random.normal(i.magna_wallet_mgn[0], i.magna_wallet_mgn[1]))
  92. if z != 0:
  93. trans.mgn += 1
  94. trans_value["MGN"] += z
  95. trans_fees["MGN"] += z*rates_fee["MGN"]
  96. # print("Nombre de BTC : {:f}".format(MaSi.fees_btc(trans.btc)*0.0000001))
  97. print("""Pour le BTC:\n
  98. Les transfères du aux frais coûtent :{:f} satoshi\n""".format(
  99. MaSi.fees_btc(trans.btc)))
  100. print("""Pour le ETH:\n
  101. Les transfères du aux frais coûtent :{:f} Gwei\n""".format(
  102. MaSi.fees_eth(trans.eth)))
  103. print("""Pour le MGN:\n
  104. Les transfères du aux frais coûtent :{:f} stroops\n""".format(
  105. MaSi.fees_mgn(trans.mgn)))
  106. print("frais :", trans_fees)
  107. print("total mouvement:", trans_value)
  108. btn = tk.Button(WINDOW, text="Launch", command=clicked)
  109. LBL_SUM0 = tk.Label(WINDOW, text="clients :")
  110. LBL_SUM0.grid(column=7, row=21)
  111. LBL_SUM = tk.Label(WINDOW, text="0")
  112. LBL_SUM.grid(column=8, row=21)
  113. btn.grid(column=8, row=20)
  114. #This put the cursor directly in the Entry BTC clients
  115. TXT_CLIENT_TOKEN["BTC"].focus()
  116. WINDOW.mainloop()