-
-
Save YunyanDeng/b5066709aa9abd080b6dcb5241ec9840 to your computer and use it in GitHub Desktop.
cashout in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- encoding: utf-8 -*- | |
# Created by deng on 2/22/21. | |
import argparse | |
import sys | |
import time | |
import requests | |
DEBUG_API = 'http://localhost:1635' | |
MINAMOUNT = 100 | |
def getPeers() -> list: | |
rsp = requests.get(DEBUG_API + '/chequebook/cheque').json() | |
if rsp.get('lastcheques') is None: | |
return [] | |
return [cheque['peer'] for cheque in rsp['lastcheques']] | |
def getCumulativePayout(peer: str) -> int: | |
rsp = requests.get(DEBUG_API + '/chequebook/cheque/' + peer).json() | |
if rsp.get('lastreceived') is None \ | |
or rsp['lastreceived'].get('payout') is None: | |
return 0 | |
return rsp['lastreceived']['payout'] | |
def getLastCashedPayout(peer: str) -> int: | |
rsp = requests.get(DEBUG_API + '/chequebook/cashout/' + peer).json() | |
if rsp.get('cumulativePayout') is None: | |
return 0 | |
else: | |
return rsp['cumulativePayout'] | |
def getUncashedAmount(peer: str) -> int: | |
return getCumulativePayout(peer) - getLastCashedPayout(peer) | |
def cashOut(peer: str): | |
rsp = requests.post(DEBUG_API + '/chequebook/cashout/' + peer).json() | |
if rsp.get('transactionHash') is None: | |
print('fail to cashing out cheque for {peer}: {msg}'.format( | |
peer=peer, | |
msg=rsp | |
)) | |
return | |
print('cashing out cheque for {peer} in transaction {txHash}'.format( | |
peer=peer, | |
txHash=rsp['transactionHash'] | |
)) | |
result = requests.get(DEBUG_API + '/chequebook/cashout/' + peer).json().get('result') | |
while result is None: | |
time.sleep(5) | |
result = requests.get(DEBUG_API + '/chequebook/cashout/' + peer).json().get('result') | |
def cashOutOne(args=None): | |
cashOut(args.peer) | |
def cashoutAll(args=None): | |
for peer in getPeers(): | |
uncashedAmount = getUncashedAmount(peer) | |
if uncashedAmount < MINAMOUNT: | |
continue | |
print('uncashed cheque for {peer} ({amount} uncashed)'.format( | |
peer=peer, | |
amount=uncashedAmount | |
)) | |
cashOut(peer) | |
def listAllUncashed(args=None): | |
for peer in getPeers(): | |
uncashedAmount = getUncashedAmount(peer) | |
if uncashedAmount < MINAMOUNT: | |
continue | |
print('{peer} {amount}'.format( | |
peer=peer, | |
amount=uncashedAmount | |
)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(prog='cashout') | |
parser.set_defaults(func=listAllUncashed) | |
parser.add_argument('-s', default='127.0.0.1', help='debug ip of bee to be cashout') | |
parser.add_argument('-p', default='1635', help='debug port') | |
subParsers = parser.add_subparsers() | |
parser_cashout = subParsers.add_parser('cashout', help='cashout a peer') | |
parser_cashout.add_argument('peer', help='peer\'s address') | |
parser_cashout.set_defaults(func=cashOutOne) | |
parser_cashout_all = subParsers.add_parser('cashout-all', help="cashout all uncashed peer") | |
# parser_cashout_all.set_defaults(minAmount=MINAMOUNT) | |
parser_cashout_all.set_defaults(func=cashoutAll) | |
parser_list = subParsers.add_parser('list', help='list all uncashed peer') | |
parser_list.set_defaults(func=listAllUncashed) | |
args = parser.parse_args(sys.argv[1:]) | |
DEBUG_API = 'http://{host}:{port}'.format(host=args.s, port=args.p) | |
args.func(args) |
大神厉害 我只想说 比群里那个技术支持强多了 那个人开了个公众号 工具收费呢 也不知道那个工具是不是他写出来的。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
谢谢大佬