Main2 et MaSi2 pour utilisation avec GUI. Tout cela passe par des dictionnaire
parent
120592aaec
commit
9ce7c37ba4
|
|
@ -0,0 +1,139 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import math
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import copy
|
||||
|
||||
|
||||
|
||||
class Populations:
|
||||
"""
|
||||
This class is made of a pd.Series of the clients and the operation that
|
||||
can be made on the population.
|
||||
"""
|
||||
def __init__(self, carac):
|
||||
"""
|
||||
Population constructor, initialises the client population with
|
||||
the caracterictic given to it.
|
||||
"""
|
||||
self.CONFIG_TOKEN = {"BTC":(1,0,0),"ETH":(0,1,0),"MGN":(0,0,1)\
|
||||
,"BTC/ETH":(1,1,0),"ETH/MGN":(0,1,1),"MGN/BTC":(1,0,1)\
|
||||
,"B/E/M":(1,1,1)}
|
||||
self.population = pd.Series()
|
||||
self.nmbr = copy.deepcopy(carac)
|
||||
for token in self.CONFIG_TOKEN:
|
||||
self.add_client(self.nmbr[token], self.CONFIG_TOKEN[token])
|
||||
"""
|
||||
We have to define more caracteristics. We will start with a
|
||||
simple serie of inds and their number
|
||||
"""
|
||||
return
|
||||
|
||||
def add_client(self, nb,token_usage):#Need to add an arg "habits"
|
||||
"""
|
||||
Add 'nb' clients to the population.
|
||||
"""
|
||||
start = len(self.population)
|
||||
end = start + nb
|
||||
inds = []
|
||||
for i in range(nb):
|
||||
inds.append(Individu(token_usage))
|
||||
self.population = self.population.append(pd.Series(inds, index=range(start, end)))
|
||||
#self.nmbr += nb
|
||||
return
|
||||
|
||||
class Individu: #tout OK
|
||||
"""
|
||||
This class define the clients, it's caracteristics being :
|
||||
- User habits :
|
||||
- The habits with the Magna Wallet and the 3 tokens :
|
||||
-self.magna_wallet_btc
|
||||
-self.magna_wallet_eth
|
||||
-self.magna_wallet_mgn
|
||||
They containe statisctical law of the user habits.
|
||||
The methods of this class operate on only one client.
|
||||
"""
|
||||
|
||||
def __init__(self,token_usage):#OK
|
||||
"""
|
||||
Constructeur d'une instance 'individu'.
|
||||
Ce constructeur fait appel aux fonction especes et aleatoire
|
||||
pour initialisé les variables d'un individu aux valeurs propres
|
||||
à son espèce.
|
||||
"""
|
||||
# Those 3 caracteristic are the users habits concerning the client
|
||||
# use of Magna Wallet
|
||||
if token_usage[0] == 0:
|
||||
self.magna_wallet_btc = (0, 0)
|
||||
else:
|
||||
self.magna_wallet_btc = (
|
||||
np.random.normal(
|
||||
loc=0.00001, scale=0.002),
|
||||
abs(np.random.normal(
|
||||
loc=0.002, scale=0.01))
|
||||
)
|
||||
if token_usage[1] == 0:
|
||||
self.magna_wallet_eth = (0, 0)
|
||||
else :
|
||||
self.magna_wallet_eth = (
|
||||
np.random.normal(
|
||||
loc=0.01, scale=0.9),
|
||||
abs(np.random.normal(
|
||||
loc=0.5, scale=5))
|
||||
)
|
||||
if token_usage[2] == 0:
|
||||
self.magna_wallet_mgn = (0, 0)
|
||||
else:
|
||||
self.magna_wallet_mgn = (
|
||||
np.random.normal(
|
||||
loc=1, scale=10),
|
||||
abs(np.random.normal(
|
||||
loc=10, scale=50))
|
||||
)
|
||||
return
|
||||
|
||||
class Transaction:
|
||||
"""
|
||||
This class keeps track of the transactions
|
||||
"""
|
||||
def __init__(self):
|
||||
self.btc=0
|
||||
self.eth=0
|
||||
self.mgn=0
|
||||
return
|
||||
|
||||
def fees_btc(nbr_transaction_btc):
|
||||
"""
|
||||
Cette fonction calcule une version simplifié des frais de transaction
|
||||
"""
|
||||
# Dans un premier temps je vais prendre pour valeur pour valeur de transfere :
|
||||
# 20sat/byte
|
||||
# On simplifie aussi en considerant qu'on a 1 input et 1 output
|
||||
fees_btc = (nbr_transaction_btc*180 + 40 + nbr_transaction_btc)*20
|
||||
return fees_btc
|
||||
|
||||
def fees_eth(nbr_transaction_eth):
|
||||
"""
|
||||
Cette fonction est destinee a calculer les frais de transaction en ETH
|
||||
La premiere approximation faire est de considerer toutes les transactions
|
||||
comme de simple transfere.
|
||||
On fixe donc la valeur d'un transfert simple 21 000 gas et 20Gwei/gas
|
||||
"""
|
||||
gwei_per_gas = 20
|
||||
gas = 21000*nbr_transaction_eth
|
||||
fees_eth = gas*20
|
||||
return fees_eth
|
||||
|
||||
def fees_mgn(nbr_transaction_mgn):
|
||||
"""
|
||||
Cette fonction calcule le cout de transfère des tokens magna
|
||||
basés sur Stellar.
|
||||
There are two special values used to calculate fees:
|
||||
The base fee (currently 100 stroops) is used in transaction fees.
|
||||
The base reserve (currently 0.5 XLM) is used in minimum account balances.
|
||||
100 stroops (0.00001 XLM)
|
||||
"""
|
||||
fees_mgn = nbr_transaction_mgn*100
|
||||
return fees_mgn
|
||||
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
#! /usr/bin/env python3
|
||||
import math
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import MagnaSim1 as MaSi
|
||||
from tkinter import *
|
||||
|
||||
window = Tk()
|
||||
|
||||
window.title("MaSi")
|
||||
|
||||
#window.geometry('600x500')
|
||||
|
||||
lbl_client_txt = ["BTC","ETH","MGN","BTC/ETH","ETH/MGN","MGN/BTC","B/E/M"]
|
||||
|
||||
lbl_client = Label(window, text="Nbr clients:")
|
||||
lbl_client.grid(column=0, row=0)
|
||||
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)}
|
||||
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)}
|
||||
lbl_client_token = {}
|
||||
txt_client_token = {}
|
||||
for token in lbl_client_txt:
|
||||
lbl_client_token[token] = Label(window, text=token)
|
||||
txt_client_token[token] = Entry(window, width=10)
|
||||
txt_client_token[token].insert(0,"0")
|
||||
lbl_client_token[token].grid(row=lbl_client_coord[token][0], column=lbl_client_coord[token][1])
|
||||
txt_client_token[token].grid(row=txt_client_coord[token][0], column=txt_client_coord[token][1])
|
||||
|
||||
lbl_rates = Label(window, text="% Frais:")
|
||||
lbl_rates.grid(column=0, row=3)
|
||||
lbl_rates_coord = {"BTC":(3,1),"ETH":(3,3),"MGN":(3,5)}
|
||||
txt_rates_coord = {"BTC":(3,2),"ETH":(3,4),"MGN":(3,6)}
|
||||
lbl_rates = {}
|
||||
txt_rates = {}
|
||||
for token in lbl_client_txt[0:3]:
|
||||
lbl_rates[token] = Label(window, text=token)
|
||||
txt_rates[token] = Entry(window, width=10)
|
||||
txt_rates[token].insert(0,"0")
|
||||
lbl_rates[token].grid(row=lbl_rates_coord[token][0], column=lbl_rates_coord[token][1])
|
||||
txt_rates[token].grid(row=txt_rates_coord[token][0], column=txt_rates_coord[token][1])
|
||||
|
||||
|
||||
def clicked():
|
||||
# First we get the number of clients
|
||||
# in each category
|
||||
nbr_client = {}
|
||||
for token in lbl_client_txt:
|
||||
nbr_client[token] = int(txt_client_token[token].get())
|
||||
if nbr_client[token] == None:#To avoid erors
|
||||
nbr_client[token] = 0
|
||||
# Then we get the rates for each token
|
||||
rates_fee = {}
|
||||
for token in lbl_client_txt[0:3]:
|
||||
rates_fee[token] = float(txt_rates[token].get())
|
||||
# We print the total of clients
|
||||
lbl_SUM.configure(text=sum(nbr_client.values()))
|
||||
print(sum(nbr_client.values()))
|
||||
# We create a population
|
||||
pop = MaSi.Populations(nbr_client)
|
||||
# We create the list of transaction
|
||||
trans = MaSi.Transaction()
|
||||
trans_value = {"BTC":0,"ETH":0,"MGN":0}
|
||||
trans_fees = {"BTC":0,"ETH":0,"MGN":0}
|
||||
for i in pop.population:
|
||||
x = abs(np.random.normal(i.magna_wallet_btc[0],i.magna_wallet_btc[1]))
|
||||
if x != 0:
|
||||
trans.btc += 1
|
||||
trans_value["BTC"] += x
|
||||
trans_fees["BTC"] += x*rates_fee[token]
|
||||
y = abs(np.random.normal(i.magna_wallet_eth[0],i.magna_wallet_eth[1]))
|
||||
if y != 0:
|
||||
trans.eth += 1
|
||||
trans_value["ETH"] += y
|
||||
trans_fees["ETH"] += y*rates_fee["ETH"]
|
||||
z = abs(np.random.normal(i.magna_wallet_mgn[0],i.magna_wallet_mgn[1]))
|
||||
if z != 0:
|
||||
trans.mgn += 1
|
||||
trans_value["MGN"] += z
|
||||
trans_fees["MGN"] += z*rates_fee["MGN"]
|
||||
|
||||
# print("Nombre de BTC : {:f}".format(MaSi.fees_btc(trans.btc)*0.0000001))
|
||||
print("Pour le BTC:\nLes transfères du aux frais coûtent :{:f} satoshi\n".format(MaSi.fees_btc(trans.btc)))
|
||||
print("Pour le ETH:\nLes transfères du aux frais coûtent :{:f} Gwei\n".format(MaSi.fees_eth(trans.eth)))
|
||||
print("Pour le MGN:\nLes transfères du aux frais coûtent :{:f} stroops\n".format(MaSi.fees_mgn(trans.mgn)))
|
||||
print("frais :", trans_fees)
|
||||
print("total mouvement:", trans_value)
|
||||
|
||||
|
||||
btn = Button(window, text="Launch", command=clicked)
|
||||
lbl_SUM0 = Label(window, text="clients :")
|
||||
lbl_SUM0.grid(column=7, row=21)
|
||||
lbl_SUM = Label(window, text="0")
|
||||
lbl_SUM.grid(column=8, row=21)
|
||||
|
||||
btn.grid(column=8, row=20)
|
||||
|
||||
txt_client_token["BTC"].focus()
|
||||
|
||||
window.mainloop()
|
||||
Loading…
Reference in New Issue