def WithdrawDelegatorOneReward(self, delegator, validator, fees, gas, gasAdjust): self.IncrHandingCount() self.IncrCalledCount() now = now_timestamp() try: rewards, err = self.QueryDelegatorRewardWithValidator( delegator, validator) if err is not None: return None, DistributorError('WithdrawDelegatorOneReward: ' + err.msg) memo = '%s withdraw reward from %s' % (delegator.getAddress(), validator.operatorAddr) if rewards: if len(rewards) == 0: return None, DistributorError( 'WithdrawDelegatorOneReward: ' + 'reward is empty') for reward in rewards: if reward['denom'] == 'hsn': if Decimal(reward['amount']) == Decimal('0'): return None, DistributorError( 'WithdrawDelegatorOneReward: ' + 'reward is 0') delegator = QueryAccountInfo(delegator) # 获取账户最新信息 if delegator is None: return None, DistributorError( 'WithdrawDelegatorOneReward: ' + 'delegator is invalid!') baseReqJson, err = GenBaseReqJson(delegator, HSN_CHAIN_ID, fees, False, memo, gas, gasAdjust) if err is not None: return None, DistributorError( 'WithdrawDelegatorOneReward: ' + err.msg) withdrawTxJson, err = GenWithdrawDelegatorOneRewardTxJson( baseReqJson) if err is not None: return None, DistributorError( 'WithdrawDelegatorOneReward: ' + err.msg) withdrawnTxJson, err = postWithdrawDelegatorOneReward( withdrawTxJson, delegator.getAddress(), validator.operatorAddr) if err is not None: return None, DistributorError( 'WithdrawDelegatorOneReward: ' + err.msg) # 写入到文件中 unSignJsonFileName = '[withdrawreward]--' + delegator.getAddress( ) + '|' + str(int(round(time.time() * 1000))) + '.json' unSignJsonPath, err = WriteToFile(UNSIGN_JSON_DIR, unSignJsonFileName, withdrawnTxJson) if err is not None: return None, DistributorError(err.msg) return unSignJsonPath, None return None, DistributorError('WithdrawDelegatorOneReward: ' + 'reward is invalid!') finally: self.SetCalculateCost(now_timestamp() - now) self.DecrHandingCount()
def Sign(self, account, unSignTxJsonPath, node): self.IncrHandingCount() self.IncrCalledCount() now = now_timestamp() try: signedJson, err = sign(account, unSignTxJsonPath, node) if err is not None: return None, err self.IncrCompletedCount() return signedJson, None finally: self.SetCalculateCost(now_timestamp() - now) self.DecrHandingCount()
def SendCoins(self, srcAccount, dstAccount, coins, fees, gas, gasAdjust): self.IncrHandingCount() self.IncrCalledCount() now = now_timestamp() try: srcCoins, err = GetBalance(srcAccount) if err is not None: return None, BankerError(err.msg) for srcKey, srcVal in srcCoins.items(): for coin in coins: if srcKey == coin['denom']: if int(srcVal) < int(coin['amount']): return None, BankerError( 'src account balances are insufficient!') memo = '%s send %s to %s' % (srcAccount.getAddress(), json.dumps(coins), dstAccount.getAddress()) srAccount = QueryAccountInfo(srcAccount) # 获取账户最新信息 if srAccount is None: return None, BankerError('srcAccount is invalid!') baseReqJson, err = GenBaseReqJson(srcAccount, HSN_CHAIN_ID, fees, False, memo, gas, gasAdjust) if err is not None: return None, err sendTxJson, err = GenSendTxJson(baseReqJson, coins) if err is not None: return None, err sendedTxJson, err = postSendTxJson(sendTxJson, dstAccount.getAddress()) if err is not None: return None, err # 写入到文件中 unSignJsonFileName = '[sendCoins]--' + srcAccount.getAddress( ) + '|' + str(int(round(time.time() * 1000))) + '.json' unSignJsonPath, err = WriteToFile(UNSIGN_JSON_DIR, unSignJsonFileName, sendedTxJson) if err is not None: return None, BankerError(err.msg) return unSignJsonPath, None finally: self.SetCalculateCost(now_timestamp() - now) self.DecrHandingCount()
def Delegate(self, delegator, validator, coin, fees, gas, gasAdjust): self.IncrHandingCount() self.IncrCalledCount() now = now_timestamp() try: avaliableBalance, err = GetAvailableBalance(delegator) if err is not None: return None, DelegatorError(err.msg) if avaliableBalance < int(coin['amount']): return None, DelegatorError( 'avaliable hsn token is insufficient!') memo = '%s delegate %s hsn to %s' % (delegator.getAddress(), coin['amount'], validator.operatorAddr) delegator = QueryAccountInfo(delegator) # 获取账户最新信息 if delegator is None: return None, DelegatorError('delegator is invalid!') baseReqJson, err = GenBaseReqJson(delegator, HSN_CHAIN_ID, fees, False, memo, gas, gasAdjust) if err is not None: return None, err delegateTxJson, err = GenDelegateTxJson(baseReqJson, delegator, validator, coin) if err is not None: return None, err delegatedTxJson, err = postDelegate(delegateTxJson, delegator.getAddress()) if err is not None: return None, err # 写入到文件中 unSignJsonFileName = '[delegate]--' + delegator.getAddress() + '|' + str(int(round(time.time() * 1000))) + '.json' unSignJsonPath, err = WriteToFile(UNSIGN_JSON_DIR, unSignJsonFileName, delegatedTxJson) if err is not None: return None, DelegatorError(err.msg) return unSignJsonPath, None finally: self.SetCalculateCost(now_timestamp() - now) self.DecrHandingCount()
def func(srcAccount, body): delay = 1 while True: time.sleep(delay) txResult, err = checkTx(body) if txResult is not None: with open(BROADCASTED_TX_DIR + '/' + srcAccount.getAddress() + '|' + str(now_timestamp()) + '.json', 'w', encoding='utf-8') as broadcastedTxFile: if broadcastedTxFile.writable(): broadcastedTxFile.write(txResult) return if err is not None: if delay == 6: break else: delay += 1 print( Logger(time.strftime( LOG_TIME_FOEMAT, time.localtime())).Warn( 'checkTx recycle : %s' % (err.msg)))