"""
DONATION 16RdQZ86NUWQbE6rM86JbxnnaCDcAKqa9
AUTHOR http://steal1982.altervista.org/hashnest.php

"""
def main(autobuy):
    ####PACMIC PROFIT CALCULATOR####



    #####EDIT THIS VALUES#########

    #number of BTC/CONTRACTS/UNPAID BALANCE
    principal=10

    #AutoReBuy option values True/False
    

    #autobuy=False

    #how many days AutoReBuy will be active 0=Entire Contract Duration(infinite)
    autobuy_days=0

    #difficulty increase https://bitcoinwisdom.com/bitcoin/difficulty
    diff_increase=1.02

    #################################################


    sat=1*10**-8
    yeld=0.45*sat
    ghs=1000*principal

    balance=0
    profit=0 
    blocks=0


    """
    code to get btc per block
    """
    import urllib2
    import math
    import json

    def get_btc_block(difficulty,ghs):
     #assuming 100% luck antpool will find 144block daily
     return (25/((difficulty*49710.2696)/((ghs*10**9))))/144

    def openjsonurl(url):
     res = urllib2.urlopen(url)
     r=res.read()
     return json.loads(r)

    def get_diff():
      data=openjsonurl('http://btc.blockr.io/api/v1/coin/info')['data']
      return float(data['last_block']['difficulty'])
    ##########################################################################

    difficulty=get_diff()
    btc_block=get_btc_block(difficulty,ghs)
    start=principal  #principal become unpaid balance (sorry)

    while principal>=0:
     blocks+=1
     #difficulty retarget each 2600 blocks
     if blocks %2600==0:
      difficulty*=diff_increase #constant 
      #calculate new amount of btc per block
      btc_block=get_btc_block(difficulty,ghs) 
     

     """
     FROM HASHNEST DOC https://www.hashnest.com/contracts/new?contract_id=2
      Profit Calculation Method

      Unpaid principal (BTC) * 0.45 (satoshis per BTC per second) * time to find a block (seconds)

      For every block found, the remaining payout after the profit is paid will count towards the principal payment. After each principal payment is made, the amount is subtracted from the amount of remaining principal.

      Please note: information on blocks found will only be transfered to Hashnest.com from AntPool after receiving six network confirmations. Because of this, profit and principal payments will be on a slight delay from the network.


     """
     #temp var to store profit reward  
     t=principal*yeld*10*60
     #store to cumulative profit(it is only for stats purposes as principal and balance are really important for the workflow)
     profit+=t
     #temp var to store principal reward
     p=(btc_block-t)
     #I will subtract principal reward from unpaid principal
     principal -=p
     #total balance paid to my wallet
     balance+= btc_block
     #days gone block found/144 assuming luck 100%
     days=blocks/144

#     print blocks,days,balance,principal,profit,ghs,btc_block,t,p

     #if autobuy is enabled
     if autobuy:
      #rebuy= number of ghs... I can see from screenshots that hashnest also sell fractions of ghs in autorebuy contracts so no round
      # 1BTC=1THS => 0.001BTC=1GHS
      rebuy=t/0.001
      #I HOPE THAT THAT'S FUNDAMENTAL. WHEN I PERFORM AUTOREBUY AUTOREBUY AMOUNT GO TO UNPAID PRINCIPAL
      principal+=t
      #IM WASTING EVERYTHING IN AUTOREBUY SO BALANCE BECOME 0
      balance-=t
      #INCREASE GHS AMOUNT
      ghs=ghs+rebuy
      #CALCULATE NEW AMOUNT OF BTC PER BLOCK AFTER REBUY
      btc_block=get_btc_block(difficulty,ghs)

      #EXIT FROM AUTOBUY AFTER autobuy_days DAYS
      if autobuy_days>0 and blocks>autobuy_days*144:
       autobuy=False 

    #THAT'S ONLY FOR STATS PURPOSE
    profit=balance-start
    if autobuy:print 'AUTOBUY: ON'
    else:print 'AUTOBUY: OFF'
    print "Blocks:",blocks
    print "Days:",days
    print "Bitcoins in wallet:",round(balance,8)
    print "Profit:",round(profit,8)
    print "Average Daily Profit:",round(profit/days,8)
    print "APR:",round((365/float(days))*(profit/start)*100,2),'%'
    print "TOTAL CONTRACTS BOUGHT:",float(ghs)/float(1000)
    print "last 10minute reward:",btc_block
    print "Last daily profit:",btc_block*144
    print "Unpaid principal:",principal
main(True)
print '-----------------------'
main(False)