def prepare(self, block_height, address='', deposit=0.0, key=''): super(ChannelDepositEvent, self).prepare(block_height) # if this transaction id is approved, it means successful by eth block chain if self.approved_tx_id: checked = self.check_transaction_success(self.approved_tx_id) if checked: LOG.debug('Approved asset by address<{}:{}>'.format(address, deposit)) Channel.update_channel(self.channel_name, state=EnumChannelState.OPENING.name) LOG.info('Start to create channel<{}>. State: OPENING.'.format(self.channel_name)) console_log.info('Channel<{}> is opening'.format(self.channel_name)) # go to next stage self.next_stage() return True elif checked is False: return False else: # new transaction is pushed to chain self.approved_tx_id = None else: approved_deposit = self.contract_event_api.get_approved_asset(address) if approved_deposit >= int(deposit): LOG.debug('Has already approved: {}'.format(approved_deposit)) # go to next stage self.next_stage() return True # make transaction to chain result = self.contract_event_api.approve(address, int(deposit), key, gwei_coef=self.gwei_coef) if result: self.approved_tx_id = '0x'+result.strip() return False
def configure_gas(self, arguments): """ :param arguments: :return: """ subcommand = get_arg(arguments) if not subcommand: self.help() return False if subcommand == 'configure': coef = get_arg(arguments, 1, True) if coef is None or 0 >= coef: console_log.warn( 'Please use number. Attention: much larger, much more expensive charge.' ) return False Client.set_gas_price(coef) elif subcommand == 'query': console_log.info('Current use {} GWEI'.format( Client.get_gas_price())) else: self.help() return
def channel_deposit_limit(self, arguments): """ :param arguments: :return: """ deposit = DepositAuth.deposit_limit() console_log.info("Current Deposit limit is %s TNC" % deposit) return None
def quit(self): console_log.info( 'Shutting down. This may take about 15 sec to sync the block info') self.go_on = False ws_instance.stop_websocket() EventMonitor.stop_monitor() self.do_close_wallet() CurrentLiveWallet.update_current_wallet(None) reactor.stop()
def terminate(self, block_height, *args, asset_type='TNC'): super(ChannelQuickSettleEvent, self).terminate(block_height) # to check the total deposit of the channel total_deposit = self.contract_event_api.get_channel_total_balance(self.channel_name) if 0 >= total_deposit: Channel.update_channel(self.channel_name, state=EnumChannelState.CLOSED.name) APIStatistics.update_statistics(self.wallet_address, state=EnumChannelState.CLOSED.name) console_log.info('Channel {} state is {}'.format(self.channel_name, EnumChannelState.CLOSED.name)) self.next_stage()
def prepare(self, block_height, *args, **kwargs): super(ChannelQuickSettleEvent, self).prepare(block_height, *args, **kwargs) # update the channel OPENING State after trigger the deposit event, wait for OPENED if self.retry is False: Channel.update_channel(self.channel_name, state=EnumChannelState.CLOSING.name) LOG.info('Start to quick-close channel<{}>. State: CLOSING.'.format(self.channel_name)) console_log.info('Channel<{}> is closing'.format(self.channel_name)) # go to next stage self.next_stage()
def terminate(self, block_height, *args, asset_type='TNC'): super(ChannelDepositEvent, self).terminate(block_height, *args) # check the deposit of the contract address total_deposit = self.contract_event_api.get_channel_total_balance(self.channel_name) if total_deposit >= self.deposit + self.partner_deposit: Channel.update_channel(self.channel_name, state=EnumChannelState.OPENED.name) Channel.update_trade(self.channel_name, self.nonce, state=EnumTradeState.confirmed.name) sync_channel_info_to_gateway(self.channel_name, 'AddChannel', asset_type) console_log.info('Channel {} state is {}'.format(self.channel_name, EnumChannelState.OPENED.name)) # to trigger monitor event for closing channel event_monitor_close_channel(self.channel_name) event_monitor_settle(self.channel_name) # to trigger monitor event for unlocking htlc locked payment event_monitor_withdraw(self.channel_name) event_monitor_withdraw_update(self.channel_name) event_monitor_withdraw_settle(self.channel_name) self.next_stage()
def get_channel_list(address, **kwargs): """ :param address: :param kwargs: :return: """ filter_src = {'src_addr': address, 'magic': get_magic()} filter_dest = {'dest_addr': address, 'magic': get_magic()} output_text = '' for key, value in kwargs.items(): if value: filter_dest.update({key: value}) filter_src.update({key: value}) output_text += ' {} {}'.format(key, value) console_log.info('Get Channels with Address {}{}'.format( address, output_text)) if filter_src.get('peer'): filter_src.update({'dest_addr': filter_src.get('peer')}) filter_src.pop('peer') channels = APIChannel.batch_query_channel(filters=filter_src) for ch in channels: balance = Channel.convert_balance(ch.balance) console_log.console('==' * 10, '\nChannelName:', ch.channel, '\nState:', ch.state, '\nPeer:', ch.dest_addr, '\nBalance:', json.dumps(balance, indent=1)) channels = APIChannel.batch_query_channel(filters=filter_dest) for ch in channels: balance = Channel.convert_balance(ch.balance) console_log.console('==' * 10, '\nChannelName:', ch.channel, '\nState:', ch.state, '\nPeer:', ch.src_addr, '\nBalance:', json.dumps(balance, indent=1))
def channel_qrcode(self, arguments): """ :param arguments: :return: """ enable = get_arg(arguments, 1) if enable.upper() not in ["ON", "OFF"]: console_log.error("should be on or off") self.qrcode = True if enable.upper() == "ON" else False console_log.console( "Qrcode opened") if self.qrcode else console_log.info( "Qrcode closed") return None
def channel_trans(self, arguments): """ :param arguments: :return: """ if len(arguments) == 2: # payment code pay_code = get_arg(arguments, 1) result, info = Payment.decode_payment_code(pay_code) if result: receiver = info.get("uri") net_magic = info.get('net_magic') if not net_magic or net_magic != str(get_magic()): console_log.error("No correct net magic") return None hashcode = info.get("hashcode") asset_type = info.get("asset_type") # asset_type = get_asset_type_name(asset_type) count = info.get("payment") comments = info.get("comments") console_log.info("will pay {} {} to {} comments {}".format( TrinityNumber.convert_to_number(count), asset_type, receiver, comments)) else: console_log.error("The payment code is not correct") return else: receiver = get_arg(arguments, 1) asset_type = get_arg(arguments, 2) count = TrinityNumber(get_arg(arguments, 3).strip()).number hashcode = get_arg(arguments, 4) if not receiver or not asset_type or not count: self.help() return None asset_type = asset_type.upper() if check_support_asset_type( asset_type) else None if not asset_type: console_log.error( "No support asset, current just support {}".format( str(SupportAssetType.SupportAssetType))) return None if 0 >= count: console_log.warn('Not support negative number or zero.') return None # query channels by address channel_set = Channel.get_channel(self.Wallet.url, receiver, EnumChannelState.OPENED) if channel_set and channel_set[0]: Channel.transfer(channel_set[0].channel, self.Wallet, receiver, asset_type, count, cli=True, comments=hashcode, trigger=RsmcMessage.create) else: if not hashcode: console_log.error("No hashcode") return None try: message = { "MessageType": "GetRouterInfo", "Sender": self.Wallet.url, "Receiver": receiver, "AssetType": asset_type, "NetMagic": get_magic(), "MessageBody": { "AssetType": asset_type, "Value": count } } result = gate_way.get_router_info(message) routerinfo = json.loads(result.get("result")) except Exception as error: LOG.error( 'Exception occurred during get route info. Exception: {}'. format(error)) console_log.warning('No router was found.') return else: router = routerinfo.get("RouterInfo") if not router: LOG.error('Router between {} and {} was not found.'.format( self.Wallet.url, receiver)) console_log.error('Router not found for HTLC transfer.') return full_path = router.get("FullPath") LOG.info("Get Router {}".format(str(full_path))) next_jump = router.get("Next") LOG.info("Get Next {}".format(str(next_jump))) fee_router = [ i for i in full_path if i[0] not in (self.Wallet.url, receiver) ] if fee_router: # fee = reduce(lambda x, y:x+y,[TrinityNumber(str(i[1]).strip()).number for i in fee_router]) fee = reduce(lambda x, y: x + y, [float(i[1]) for i in fee_router]) else: fee = 0 fee = TrinityNumber(str(fee)).number count = int(count) + fee fee = fee / pow(10, 8) receiver = full_path[1][0] channel_set = Channel.get_channel(self.Wallet.url, receiver, EnumChannelState.OPENED) if not (channel_set and channel_set[0]): print('No OPENED channel was found for HTLC trade.') return LOG.info("Get Fee {}".format(fee)) answer = prompt( "You will pay extra fee {}. Do you wish continue this transaction? [Yes/No]>" .format(fee)) if answer.upper() in ["YES", "Y"]: channel_name = channel_set[0].channel Channel.transfer(channel_name, self.Wallet, receiver, asset_type, count, hashcode, router=full_path, next_jump=full_path[2][0], cli=True, trigger=HtlcMessage.create) else: return