Skip to content
Snippets Groups Projects
Commit 615b3d66 authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

Mejoras en MasterDistiller

parent 5cdfb88f
No related branches found
No related tags found
No related merge requests found
...@@ -2,13 +2,16 @@ ...@@ -2,13 +2,16 @@
import sys import sys
import os import os
if not os.environ.get('BFAHOME'):
print("$BFAHOME not set. Did you source bfa/bin/env ?", file=sys.stderr)
exit(1)
import json import json
import re import re
import decimal import decimal
os.environ['BFAHOME']='/home/bfa/bfa' import argparse
sys.path.append( os.path.join(os.environ['BFAHOME'],'bin' )) sys.path.append( os.path.join(os.environ['BFAHOME'],'bin' ))
import libbfa import libbfa
bfa = libbfa.Bfa('prod') bfa = None
notation = dict() notation = dict()
janitor = None janitor = None
distillery = None distillery = None
...@@ -16,7 +19,15 @@ distillery = None ...@@ -16,7 +19,15 @@ distillery = None
def distbalance() -> int: def distbalance() -> int:
return bfa.w3.eth.getBalance(distillery.address) return bfa.w3.eth.getBalance(distillery.address)
def editAccount(entry:str, pallet:list): def distribute():
# trigger distribution
beforeBal = distbalance()
rcpt = janitor.transact( web3=bfa.w3, function=distillery.functions.distribute, extragas=4000000)
print('Distribute returned succesfully in block# {} using {} gas.'.format(rcpt.blockNumber, rcpt.gasUsed))
afterBal = distbalance()
print('Distributed {} {}.'.format( int(decimal.Decimal(beforeBal - afterBal) / notation['num']), notation['name']))
def editAccount(entry:str, beneflist:list):
acct = None acct = None
# is it an account address? # is it an account address?
if entry == '': if entry == '':
...@@ -25,15 +36,10 @@ def editAccount(entry:str, pallet:list): ...@@ -25,15 +36,10 @@ def editAccount(entry:str, pallet:list):
elif re.search('^0x[0-9a-fA-F]{40}$', entry): elif re.search('^0x[0-9a-fA-F]{40}$', entry):
acct = entry.lower() acct = entry.lower()
# is it a known account address? # is it a known account address?
elif re.search('^[0-9]+$', entry) and int(entry) < len(pallet): elif re.search('^[0-9]+$', entry) and int(entry) < len(beneflist):
acct = pallet[int(entry)].addr acct = beneflist[int(entry)].addr
elif entry == 'x': elif entry == 'x':
# trigger distribution distribute()
beforeBal = distbalance()
rcpt = janitor.transact( web3=bfa.w3, function=distillery.functions.distribute, extragas=4000000)
print('Distribute returned succesfully in block# {} using {} gas.'.format(rcpt.blockNumber, rcpt.gasUsed))
afterBal = distbalance()
print('Distributed {} {}.'.format( int(decimal.Decimal(beforeBal - afterBal) / notation['num']), notation['name']))
else: else:
print('I do not know what to do with "{}".'.format(entry), file=sys.stderr) print('I do not know what to do with "{}".'.format(entry), file=sys.stderr)
exit(1) exit(1)
...@@ -51,27 +57,27 @@ def editAccount(entry:str, pallet:list): ...@@ -51,27 +57,27 @@ def editAccount(entry:str, pallet:list):
else: else:
print('Update failed.') print('Update failed.')
def getPallet() -> list: def getBeneficiaries() -> list:
count = distillery.functions.numberOfBeneficiaries().call() count = distillery.functions.numberOfBeneficiaries().call()
pallet = list() beneflist = list()
# Fetch addresses from the list in the contract. # Fetch addresses from the list in the contract.
for i in range(count): for i in range(count):
print("Indexing accounts ({}/{})...\x1B[J\r".format(i,count), end='') print("Indexing accounts ({}/{})...\x1B[J\r".format(i,count), end='')
(addr,topuplimit) = distillery.functions.atPosition(i).call() (addr,topuplimit) = distillery.functions.atPosition(i).call()
bal = bfa.w3.eth.getBalance( addr ) bal = bfa.w3.eth.getBalance( addr )
pallet.append( { "addr": addr, "topuplimit": topuplimit, "balance": bal } ) beneflist.append( { "addr": addr, "topuplimit": topuplimit, "balance": bal } )
print("\r\x1B[J".format(i,count), end='') print("\r\x1B[J".format(i,count), end='')
s = lambda x:x['addr'].lower() s = lambda x:x['addr'].lower()
pallet.sort(key=s) beneflist.sort(key=s)
return pallet return beneflist
def printPallet(pallet:list): def print_beneficiary_list(beneflist:list):
# find the length of the longest number-string # find the length of the longest number-string
longestlimit = 1 longestlimit = 1
longestbalance = 1 longestbalance = 1
numformat = notation['strformat'].format numformat = notation['strformat'].format
for i in range(len(pallet)): for i in range(len(beneflist)):
entry = pallet[i] entry = beneflist[i]
numstr = numformat(decimal.Decimal(entry['topuplimit']) / notation['num']) numstr = numformat(decimal.Decimal(entry['topuplimit']) / notation['num'])
thislen = len(numstr) thislen = len(numstr)
if thislen > longestlimit: if thislen > longestlimit:
...@@ -81,9 +87,18 @@ def printPallet(pallet:list): ...@@ -81,9 +87,18 @@ def printPallet(pallet:list):
if thislen > longestbalance: if thislen > longestbalance:
longestbalance = thislen longestbalance = thislen
# print them all # print them all
theformat = '{:' + str(len(str(len(pallet)-1))) + '}: {} fills to {:' + str(longestlimit) + '.' + str(notation['potency']) + 'f} {} (has {:' + str(longestbalance) + '.' + str(notation['potency']) + 'f}).' theformat = '{:' + str(len(str(len(beneflist)-1))) + \
for i in range(len(pallet)): '}: {} fills to {:' + \
entry = pallet[i] str(longestlimit) + \
'.' + \
str(notation['potency']) + \
'f} {} (has {:' + \
str(longestbalance) + \
'.' + \
str(notation['potency']) + \
'f}).'
for i in range(len(beneflist)):
entry = beneflist[i]
numstr = numformat(decimal.Decimal(entry['topuplimit']) / notation['num']) numstr = numformat(decimal.Decimal(entry['topuplimit']) / notation['num'])
while len(numstr) < longestlimit: while len(numstr) < longestlimit:
numstr = ' ' + numstr numstr = ' ' + numstr
...@@ -96,36 +111,29 @@ def printPallet(pallet:list): ...@@ -96,36 +111,29 @@ def printPallet(pallet:list):
)) ))
def overview(): def overview():
print( "The contract's account ({}) has {} {}.".format( while True:
distillery.address, print( "The contract's account ({}) has {} {}.".format(
int(decimal.Decimal(distbalance()) / notation['num']), distillery.address,
notation['name'] int(decimal.Decimal(distbalance()) / notation['num']),
)) notation['name']
pallet = getPallet() ))
printPallet(pallet) beneflist = getBeneficiaries()
answer = input("\n[ Q=quit x=distribute ]\nWhich account to edit (enter index number or full account number)?: ") print_beneficiary_list(beneflist)
if answer is None or answer.upper() == 'Q': answer = input("\n[ Q=quit x=distribute ]\n" +
exit( 0 ) "Which account to edit (enter index number or full account number)?: ")
editAccount(answer, pallet) if answer is None or answer.upper() == 'Q':
exit( 0 )
editAccount(answer, beneflist)
def init(): def init(**kwargs):
global janitor, notation, distillery; global janitor, notation, distillery, bfa;
janitor = libbfa.Account('0xd15dd6dbbe722b451156a013c01b29d91e23c3d6') bfa = libbfa.Bfa(kwargs.get('uri'))
table = dict([ janitor = libbfa.Account(kwargs.get('sender_addr'))
(6, 'Mwei'), table = ('Kwei', 'Mwei', 'Gwei', 'micro', 'finney', 'ether',
(9, 'Gwei'), 'kether', 'grand', 'mether', 'gether', 'tether')
(12, 'micro'),
(15, 'finney'),
(18, 'ether'),
(21, 'kether'),
(24, 'grand'),
(27, 'mether'),
(30, 'gether'),
(33, 'tether'),
])
potency = 18 potency = 18
notation['potency'] = potency notation['potency'] = potency
notation['name'] = table[potency] notation['name'] = table[int(potency/3-1)]
notation['num'] = pow(10, potency) notation['num'] = pow(10, potency)
notation['strformat'] = '{' + ':.{}f'.format(potency) + '}' notation['strformat'] = '{' + ':.{}f'.format(potency) + '}'
abifile = '' abifile = ''
...@@ -137,9 +145,34 @@ def init(): ...@@ -137,9 +145,34 @@ def init():
with open(abifile, 'rt', encoding='utf-8') as infile: with open(abifile, 'rt', encoding='utf-8') as infile:
abitxt = infile.read() abitxt = infile.read()
abi = json.loads(abitxt) abi = json.loads(abitxt)
addr = '0xECB6aFF6e38dC58C4d9AaE2F7927A282bcB77AC2' distillery = bfa.w3.eth.contract(address=kwargs.get('sc_addr'), abi=abi)
distillery = bfa.w3.eth.contract(address=addr, abi=abi)
init() parser = argparse.ArgumentParser(description="Command interface for BFA2018 distillery1.")
while True: parser.add_argument(
'--uri',
metavar='URI',
nargs=1,
default='prod',
help='URI of node to connect to.')
parser.add_argument(
'--sender-addr',
metavar='ADDR',
nargs=1,
default='0xd15dd6dbbe722b451156a013c01b29d91e23c3d6',
help='Address of controller of the smart contract.')
parser.add_argument(
'--sc-addr',
metavar='ADDR',
nargs=1,
default='0xECB6aFF6e38dC58C4d9AaE2F7927A282bcB77AC2',
help='Address of smart contract.')
parser.add_argument(
'--distribute',
action='store_true',
help='Run distribute once and then exit.')
parsed = parser.parse_args()
init(uri=parsed.uri, sc_addr=parsed.sc_addr, sender_addr=parsed.sender_addr)
if parsed.distribute:
distribute()
else:
overview() overview()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment