소스 검색

Main2 et MaSi2 pour utilisation avec GUI. Tout cela passe par des dictionnaire

Mushu 6 년 전
부모
커밋
9ce7c37ba4
2개의 변경된 파일238개의 추가작업 그리고 0개의 파일을 삭제
  1. 139 0
      MagnaSim1.py
  2. 99 0
      main1.py

+ 139 - 0
MagnaSim1.py 파일 보기

@@ -0,0 +1,139 @@
1
+#! /usr/bin/env python3
2
+
3
+import math
4
+import numpy as np
5
+import pandas as pd
6
+import copy
7
+
8
+
9
+
10
+class Populations:
11
+    """
12
+    This class is made of a pd.Series of the clients and the operation that 
13
+    can be made on the population.
14
+    """
15
+    def __init__(self, carac):
16
+        """
17
+        Population constructor, initialises the client population with
18
+        the caracterictic given to it.
19
+        """
20
+        self.CONFIG_TOKEN = {"BTC":(1,0,0),"ETH":(0,1,0),"MGN":(0,0,1)\
21
+                ,"BTC/ETH":(1,1,0),"ETH/MGN":(0,1,1),"MGN/BTC":(1,0,1)\
22
+                ,"B/E/M":(1,1,1)}
23
+        self.population = pd.Series()
24
+        self.nmbr = copy.deepcopy(carac)
25
+        for token in self.CONFIG_TOKEN:
26
+            self.add_client(self.nmbr[token], self.CONFIG_TOKEN[token])
27
+        """
28
+        We have to define more caracteristics. We will start with a
29
+        simple serie of inds and their number
30
+        """
31
+        return
32
+
33
+    def add_client(self, nb,token_usage):#Need to add an arg "habits"
34
+        """
35
+        Add 'nb' clients to the population.
36
+        """
37
+        start = len(self.population)
38
+        end = start + nb
39
+        inds = []
40
+        for i in range(nb):
41
+            inds.append(Individu(token_usage))
42
+        self.population = self.population.append(pd.Series(inds, index=range(start, end)))
43
+        #self.nmbr += nb
44
+        return
45
+
46
+class Individu: #tout OK
47
+    """
48
+    This class define the clients, it's caracteristics being :
49
+        - User habits :
50
+            - The habits with the Magna Wallet and the 3 tokens :
51
+                -self.magna_wallet_btc
52
+                -self.magna_wallet_eth
53
+                -self.magna_wallet_mgn
54
+            They containe statisctical law of the user habits.
55
+    The methods of this class operate on only one client.
56
+    """
57
+
58
+    def __init__(self,token_usage):#OK
59
+        """
60
+        Constructeur d'une instance 'individu'.
61
+        Ce constructeur fait appel aux fonction especes et aleatoire
62
+        pour initialisé les variables d'un individu aux valeurs propres
63
+        à son espèce.
64
+        """
65
+        # Those 3 caracteristic are the users habits concerning the client
66
+        # use of Magna Wallet
67
+        if token_usage[0] == 0:
68
+            self.magna_wallet_btc = (0, 0)
69
+        else:
70
+            self.magna_wallet_btc = (
71
+                np.random.normal(
72
+                    loc=0.00001, scale=0.002),
73
+                abs(np.random.normal(
74
+                    loc=0.002, scale=0.01))
75
+            )
76
+        if token_usage[1] == 0:
77
+            self.magna_wallet_eth = (0, 0)
78
+        else :
79
+            self.magna_wallet_eth = (
80
+                np.random.normal(
81
+                    loc=0.01, scale=0.9),
82
+                abs(np.random.normal(
83
+                    loc=0.5, scale=5))
84
+            )
85
+        if token_usage[2] == 0:
86
+            self.magna_wallet_mgn = (0, 0)
87
+        else:
88
+            self.magna_wallet_mgn = (
89
+                np.random.normal(
90
+                    loc=1, scale=10),
91
+                abs(np.random.normal(
92
+                    loc=10, scale=50))
93
+            )
94
+        return
95
+
96
+class Transaction:
97
+    """
98
+    This class keeps track of the transactions
99
+    """
100
+    def __init__(self):
101
+        self.btc=0
102
+        self.eth=0
103
+        self.mgn=0
104
+        return
105
+
106
+def fees_btc(nbr_transaction_btc):
107
+    """
108
+    Cette fonction calcule une version simplifié des frais de transaction
109
+    """
110
+    # Dans un premier temps je vais prendre pour valeur pour valeur de transfere :
111
+    # 20sat/byte
112
+    # On simplifie aussi en considerant qu'on a 1 input et 1 output
113
+    fees_btc = (nbr_transaction_btc*180 + 40 + nbr_transaction_btc)*20
114
+    return fees_btc
115
+
116
+def fees_eth(nbr_transaction_eth):
117
+    """
118
+    Cette fonction est destinee a calculer les frais de transaction en ETH
119
+    La premiere approximation faire est de considerer toutes les transactions
120
+    comme de simple transfere.
121
+    On fixe donc la valeur d'un transfert simple 21 000 gas et 20Gwei/gas
122
+    """
123
+    gwei_per_gas = 20
124
+    gas = 21000*nbr_transaction_eth
125
+    fees_eth = gas*20
126
+    return fees_eth
127
+
128
+def fees_mgn(nbr_transaction_mgn):
129
+    """
130
+    Cette fonction calcule le cout de transfère des tokens magna 
131
+    basés sur Stellar.
132
+    There are two special values used to calculate fees:
133
+        The base fee (currently 100 stroops) is used in transaction fees.
134
+        The base reserve (currently 0.5 XLM) is used in minimum account balances.
135
+    100 stroops (0.00001 XLM)
136
+    """
137
+    fees_mgn = nbr_transaction_mgn*100
138
+    return fees_mgn
139
+

+ 99 - 0
main1.py 파일 보기

@@ -0,0 +1,99 @@
1
+#! /usr/bin/env python3
2
+import math
3
+import numpy as np
4
+import pandas as pd
5
+import MagnaSim1 as MaSi
6
+from tkinter import *
7
+
8
+window = Tk()
9
+
10
+window.title("MaSi")
11
+
12
+#window.geometry('600x500')
13
+
14
+lbl_client_txt = ["BTC","ETH","MGN","BTC/ETH","ETH/MGN","MGN/BTC","B/E/M"]
15
+
16
+lbl_client = Label(window, text="Nbr clients:")
17
+lbl_client.grid(column=0, row=0)
18
+lbl_client_coord = {"BTC":(0,1),"ETH":(0,3),"MGN":(0,5),"BTC/ETH":(1,1),"ETH/MGN":(1,3),"MGN/BTC":(1,5),"B/E/M":(2,1)}
19
+txt_client_coord = {"BTC":(0,2),"ETH":(0,4),"MGN":(0,6),"BTC/ETH":(1,2),"ETH/MGN":(1,4),"MGN/BTC":(1,6),"B/E/M":(2,2)}
20
+lbl_client_token = {}
21
+txt_client_token = {}
22
+for token in lbl_client_txt:
23
+    lbl_client_token[token] = Label(window, text=token)
24
+    txt_client_token[token] = Entry(window, width=10)
25
+    txt_client_token[token].insert(0,"0")
26
+    lbl_client_token[token].grid(row=lbl_client_coord[token][0], column=lbl_client_coord[token][1])
27
+    txt_client_token[token].grid(row=txt_client_coord[token][0], column=txt_client_coord[token][1])
28
+
29
+lbl_rates = Label(window, text="% Frais:")
30
+lbl_rates.grid(column=0, row=3)
31
+lbl_rates_coord = {"BTC":(3,1),"ETH":(3,3),"MGN":(3,5)}
32
+txt_rates_coord = {"BTC":(3,2),"ETH":(3,4),"MGN":(3,6)}
33
+lbl_rates = {}
34
+txt_rates = {}
35
+for token in lbl_client_txt[0:3]:
36
+    lbl_rates[token] = Label(window, text=token)
37
+    txt_rates[token] = Entry(window, width=10)
38
+    txt_rates[token].insert(0,"0")
39
+    lbl_rates[token].grid(row=lbl_rates_coord[token][0], column=lbl_rates_coord[token][1])
40
+    txt_rates[token].grid(row=txt_rates_coord[token][0], column=txt_rates_coord[token][1])
41
+
42
+
43
+def clicked():
44
+    # First we get the number of clients
45
+    # in each category
46
+    nbr_client = {}
47
+    for token in lbl_client_txt:
48
+        nbr_client[token] = int(txt_client_token[token].get())
49
+    if nbr_client[token] == None:#To avoid erors
50
+        nbr_client[token] = 0
51
+    # Then we get the rates for each token
52
+    rates_fee = {}
53
+    for token in lbl_client_txt[0:3]:
54
+        rates_fee[token] = float(txt_rates[token].get())
55
+    # We print the total of clients
56
+    lbl_SUM.configure(text=sum(nbr_client.values()))
57
+    print(sum(nbr_client.values()))
58
+    # We create a population
59
+    pop = MaSi.Populations(nbr_client)
60
+    # We create the list of transaction
61
+    trans = MaSi.Transaction()
62
+    trans_value = {"BTC":0,"ETH":0,"MGN":0}
63
+    trans_fees = {"BTC":0,"ETH":0,"MGN":0}
64
+    for i in pop.population:
65
+        x = abs(np.random.normal(i.magna_wallet_btc[0],i.magna_wallet_btc[1]))
66
+        if x != 0:
67
+            trans.btc += 1
68
+            trans_value["BTC"] += x
69
+            trans_fees["BTC"] += x*rates_fee[token]
70
+        y = abs(np.random.normal(i.magna_wallet_eth[0],i.magna_wallet_eth[1]))
71
+        if y != 0:
72
+            trans.eth += 1
73
+            trans_value["ETH"] += y
74
+            trans_fees["ETH"] += y*rates_fee["ETH"]
75
+        z = abs(np.random.normal(i.magna_wallet_mgn[0],i.magna_wallet_mgn[1]))
76
+        if z != 0:
77
+            trans.mgn += 1
78
+            trans_value["MGN"] += z
79
+            trans_fees["MGN"] += z*rates_fee["MGN"]
80
+
81
+#    print("Nombre de BTC : {:f}".format(MaSi.fees_btc(trans.btc)*0.0000001))
82
+    print("Pour le BTC:\nLes transfères du aux frais coûtent :{:f} satoshi\n".format(MaSi.fees_btc(trans.btc)))
83
+    print("Pour le ETH:\nLes transfères du aux frais coûtent :{:f} Gwei\n".format(MaSi.fees_eth(trans.eth)))
84
+    print("Pour le MGN:\nLes transfères du aux frais coûtent :{:f} stroops\n".format(MaSi.fees_mgn(trans.mgn)))
85
+    print("frais :", trans_fees)
86
+    print("total mouvement:", trans_value)
87
+
88
+
89
+btn = Button(window, text="Launch", command=clicked)
90
+lbl_SUM0 = Label(window, text="clients :")
91
+lbl_SUM0.grid(column=7, row=21)
92
+lbl_SUM = Label(window, text="0")
93
+lbl_SUM.grid(column=8, row=21)
94
+
95
+btn.grid(column=8, row=20)
96
+
97
+txt_client_token["BTC"].focus()
98
+
99
+window.mainloop()