Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| #! /usr/bin/env python | |
| import json | |
| import jsonrpc | |
| import sys, os | |
| from math import exp, log | |
| _blockspan = 1440 # about one day | |
| _maxinterest = 0.02 # 2% interest per _blockspan | |
| _targetfunds = 10.0 # amount of NBT we want to have allocated | |
| _address = 'BCuSTodiAnAddR3SSXXXX' # custodian grant address | |
| _nuconfig = '%s/.nu/nu.conf'%os.getenv("HOME") # path to nu.conf | |
| _passphrase = "" # possibly passphrase (unsafe) | |
| _fee = 0.01 # fixed fee of 0.01 NBT | |
| # calculates the payout for the amount provided at the balance of the corresponding block | |
| def payout(balance, amount): | |
| # integrate over decreasing logistic sigmoid centered at _targetfunds with a maximum value of _maxinterest | |
| return _maxinterest * (amount - (log(exp(_targetfunds) + exp(balance + amount)) - log(exp(_targetfunds) + exp(balance)))) | |
| def main(): | |
| # rpc connection | |
| opts = dict(tuple(line.strip().replace(' ','').split('=')) for line in open(_nuconfig).readlines()) | |
| assert 'rpcuser' in opts.keys() and 'rpcpassword' in opts.keys(), "RPC parameters could not be read" | |
| rpc = jsonrpc.ServiceProxy("http://%s:%s@127.0.0.1:%s"%( | |
| opts['rpcuser'],opts['rpcpassword'],opts.pop('rpcport', '14002'))) | |
| txout = {} | |
| try: | |
| hblock = rpc.getblockhash(rpc.getblockcount() - _blockspan) | |
| for tx in rpc.listsinceblock(hblock)['transactions']: # scan through previous transactions | |
| if tx['address'] == _address and tx['confirmations'] == _blockspan and tx['category'] == 'receive': | |
| # this is a transaction which should receive a payout now, so create the outgoing transaction | |
| txid = rpc.decoderawtransaction(rpc.getrawtransaction(tx['txid'],0))['vin'][0]['txid'] | |
| addr = rpc.decoderawtransaction(rpc.getrawtransaction(txid,0))['vout'][0]['scriptPubKey']['addresses'][0] | |
| if not addr in txout.keys(): txout[addr] = 0.0 | |
| txout[addr] += payout(rpc.getbalance("", tx['confirmations']), tx['amount']) | |
| except: | |
| print "failed to parse transactions" | |
| else: | |
| txout = {k : v for k,v in txout.items() if v > _fee} # payouts must at least cover fee | |
| if txout: | |
| if _passphrase: # unlock wallet if required | |
| try: rpc.walletpassphrase(_passphrase, 30, False) | |
| except:pass | |
| # send the transactions | |
| try: rpc.sendmany("", txout) | |
| except: print "failed to send transactions at block %d:"%rpc.getblockcount(), txout | |
| if __name__ == "__main__": | |
| main() |