Beispiel #1
0
    def exists(cls, founder, partner):
        """

        :param founder: URL of channel founder
        :param partner: URL of channel partner
        :return:
        """
        # judge whether the channel exist or not
        if Channel.get_channel(founder, partner, EnumChannelState.OPENED):
            console_log.warning('OPENED channel already exists.')
            return False
        else:
            # creating channel is ongoing
            # channel = Channel.get_channel(founder, partner, EnumChannelState.OPENING)
            # if :
            #     console.warning('Channel {} is on the way. Please try later if failed')
            #     return
            pass

        return True
Beispiel #2
0
    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
Beispiel #3
0
    def create(cls,
               wallet,
               founder,
               partner,
               asset_type,
               deposit,
               partner_deposit=None,
               comments=None,
               trigger=None,
               cli=True):
        """
            Provide one method to be called by the wallet prompt console.
        :param wallet:
        :param partner:
        :param asset_type:
        :param deposit:
        :param partner_deposit:
        :param comments:
        :param cli:
        :return:
        """
        if not (wallet and partner and asset_type and deposit):
            LOG.error('Invalid parameters:wallet<{}>, founder<{}>, partner<{}>, asset_type<{}>, deposit<{}>' \
                      .format(wallet, founder, partner, asset_type, deposit))
            # here we could use some hooks to register event to handle output console ????
            console_log.error(
                'Illegal mandatory parameters. Please check in your command.')
            return False

        # use deposit as default value for both partners if partner's deposit is not set:
        if not partner_deposit:
            partner_deposit = deposit

        # judge whether the channel exist or not
        if Channel.get_channel(founder, partner, EnumChannelState.OPENED):
            console_log.warning('OPENED channel already exists.')
            return False
        else:
            # creating channel is ongoing
            # channel = Channel.get_channel(founder, partner, EnumChannelState.OPENING)
            # if :
            #     console.warning('Channel {} is on the way. Please try later if failed')
            #     return
            pass

        channel_name = cls.new_channel_name(founder, partner)
        if cli:
            deposit = int(deposit)
            partner_deposit = int(partner_deposit)
            if 0 >= deposit or 0 >= partner_deposit:
                LOG.error('Could not register channel because of illegal deposit<{}:{}>.'\
                          .format(deposit, partner_deposit))
                return False

            try:
                trigger(wallet, channel_name, asset_type, founder, deposit,
                        partner, partner_deposit, comments)
            except Exception as error:
                LOG.info('Create channel<{}> failed. Exception: {}'.format(
                    channel_name, error))
                return False

        return True