コード例 #1
0
class BTCCurrencyTransfer(AbstractDigitalCurrencyTransfer):
    def __init__(self):

        self.yml_config = ConfigFileReader()
        self.fee = self.yml_config.get_reserved_fee_for_transferring(
            currency='btc')
        self.confirms = self.yml_config.get_min_transfer_confirmations(
            currency='btc')
        self.btc_rpc_call = BTCRPCCall(wallet='receive', currency='btc')
        self.coin_to_be_send_dict = self.__init_dict_of_accounts()

    def __get_total_amount_in_wallet(self):

        if self.coin_to_be_send_dict.values():
            return reduce(lambda (coin_value), y: coin_value + y,
                          self.coin_to_be_send_dict.values())
        else:
            return 0

    def __init_dict_of_accounts(self):

        lists_received_by_account = self.btc_rpc_call.list_accounts(
            self.confirms)

        dict_coin_to_be_send = {}

        for received_account, amount in lists_received_by_account.iteritems():

            amount_balance = self.btc_rpc_call.get_balance(
                received_account, self.confirms)

            if amount_balance > 0:
                dict_coin_to_be_send[received_account] = amount_balance

            if amount_balance < 0:
                logger_file.error("Minus value is in the account %s ",
                                  received_account)
                raise SystemExit("Minus value is in an account!!")

        return dict_coin_to_be_send

    def __create_an_address_with_account_assigned(self):

        new_address = self.btc_rpc_call.do_get_new_address()
        self.btc_rpc_call.do_set_account(new_address, new_address)
        return new_address

    def main(self):

        min_transfer = self.yml_config.get_min_transfer_amount(currency='btc')

        total_amount = self.__get_total_amount_in_wallet()
        balance_amount = (self.btc_rpc_call.do_getinfo())['balance']

        amount_thresh = abs(balance_amount - total_amount)

        if amount_thresh > 0.0001:
            logger_file.error(
                "%d confirmed amount %s  != the total receiving balance %s, need more confirms",
                int(self.confirms), total_amount, balance_amount)

        logger_file.info("Total amount of coins to be transfer: %f" %
                         total_amount)

        if total_amount >= min_transfer:
            logger_file.info("Init transferring...")
            logger_file.info(
                "Creating a temporary address for moving coins...")
            btc_account = self.__create_an_address_with_account_assigned()
            logger_file.info("Starting to move coins to %s", btc_account)

            for received_account, amount in self.coin_to_be_send_dict.iteritems(
            ):
                """
                logger_file.info("%s, %s, %f", received_account,
                                 self.btc_rpc_call.get_addresses_by_account(received_account), amount)
                """
                if self.btc_rpc_call.move(received_account, btc_account,
                                          float(amount)):
                    pass
                else:
                    logger_file.error("Fail to move coins to from %s to %s!",
                                      received_account, btc_account)

            send_to_address = self.yml_config.get_safe_address_to_be_transferred(
                currency='btc')

            amount_to_transfer = float(total_amount) - float(self.fee)

            logger_file.info(
                "Starting transferring %f coins to address: %s from account: %s",
                amount_to_transfer, send_to_address, btc_account)

            self.btc_rpc_call.send_from(btc_account, send_to_address,
                                        amount_to_transfer)
            logger_file.info("Transfer is done")
        else:
            logger_file.info("It is not ready to do the coin transferring!")

    def main_test(self):

        total_amount = self.__get_total_amount_in_wallet()
        logger_file.info(total_amount)

        balance_amount = (self.btc_rpc_call.do_getinfo())['balance']
        logger_file.info(balance_amount)

        logger_file.info(len(self.coin_to_be_send_dict))

        for received_account, amount in self.coin_to_be_send_dict.iteritems():
            logger_file.info("account: %s, amount: %f", received_account,
                             amount)
コード例 #2
0
class YAMLTestCase(TestCase):

    def setUp(self):
        server_config = open(config_file)
        self.server_map = yaml.safe_load(server_config)
        self.yml_config = ConfigFileReader()

    def test_read_yml(self):

        log.info(self.server_map)
        expect_url = "http://*****:*****@127.0.0.1:18332"
        btc_servers = self.server_map['btc']
        wallets = btc_servers['wallets']
        receive = btc_servers['receive']
        username = receive['username']
        key = receive['key']
        protocol = receive['protocol']
        host = receive['host']
        port = receive['port']
        url_list = list()
        url_list.append(protocol)
        url_list.append('://')
        url_list.append(username)
        url_list.append(':')
        url_list.append(key)
        url_list.append('@')
        url_list.append(host)
        url_list.append(':')
        url_list.append(str(port))

        url = ''.join(url_list)
        log.info(url)
        log.info("wallets are %s ." % wallets)
        self.assertEquals(url, expect_url)

    def test_singleton_yml_reader(self):

        yml_reader_send = ConfigFileReader()
        send_url = yml_reader_send.get_rpc_server(currency='btc', wallet='send')
        log.info(send_url)
        yml_reader_receive = ConfigFileReader()
        receive_url = yml_reader_receive.get_rpc_server(currency='btc', wallet='receive')
        log.info(receive_url)
        self.assertEqual(id(yml_reader_send), id(yml_reader_receive))

    def test_get_min_transfer_confirmations(self):
        min_transfer_confirmations = self.yml_config.get_min_transfer_confirmations(currency='btc')
        log.info('Minimum transfer confirmations : %d' % min_transfer_confirmations)

    def test_get_min_transfer_amount(self):
        min_transfer_amount = self.yml_config.get_min_transfer_amount(currency='btc')
        log.info('Minimum transfer amount : %f' % min_transfer_amount)

    def test_get_safe_address_to_be_transferred(self):
        safe_address_to_be_transferred = self.yml_config.get_safe_address_to_be_transferred(currency='btc')
        log.info('Safe address to be transferred : %s' % safe_address_to_be_transferred)

    def test_get_risk_confirmations_low(self):
        risk_low_confirmations_from_config = self.yml_config.get_confirmations_mapping_to_risk(currency='btc',
                                                                                               risk='low')
        self.assertEqual(risk_low_confirmations, risk_low_confirmations_from_config)

    def test_get_risk_confirmations_medium(self):
        risk_low_confirmations_from_config = self.yml_config.get_confirmations_mapping_to_risk(currency='btc',
                                                                                               risk='medium')
        self.assertEqual(risk_medium_confirmations, risk_low_confirmations_from_config)

    def test_get_risk_confirmations_high(self):
        risk_low_confirmations_from_config = self.yml_config.get_confirmations_mapping_to_risk(currency='btc',
                                                                                               risk='high')
        self.assertEqual(risk_high_confirmations, risk_low_confirmations_from_config)
コード例 #3
0
class BTCCurrencyTransfer(AbstractDigitalCurrencyTransfer):

    def __init__(self):

        self.yml_config = ConfigFileReader()
        self.fee = self.yml_config.get_reserved_fee_for_transferring(currency='btc')
        self.confirms = self.yml_config.get_min_transfer_confirmations(currency='btc')
        self.btc_rpc_call = BTCRPCCall(wallet='receive', currency='btc')
        self.coin_to_be_send_dict = self.__init_dict_of_accounts()

    def __get_total_amount_in_wallet(self):

        if self.coin_to_be_send_dict.values():
            return reduce(lambda (coin_value), y: coin_value + y, self.coin_to_be_send_dict.values())
        else:
            return 0

    def __init_dict_of_accounts(self):

        lists_received_by_account = self.btc_rpc_call.list_accounts(self.confirms)

        dict_coin_to_be_send = {}

        for received_account, amount in lists_received_by_account.iteritems():

            amount_balance = self.btc_rpc_call.get_balance(received_account, self.confirms)

            if amount_balance > 0:
                dict_coin_to_be_send[received_account] = amount_balance

            if amount_balance < 0:
                logger_file.error("Minus value is in the account %s ", received_account)
                raise SystemExit("Minus value is in an account!!")

        return dict_coin_to_be_send

    def __create_an_address_with_account_assigned(self):

        new_address = self.btc_rpc_call.do_get_new_address()
        self.btc_rpc_call.do_set_account(new_address, new_address)
        return new_address

    def main(self):

        min_transfer = self.yml_config.get_min_transfer_amount(currency='btc')

        total_amount = self.__get_total_amount_in_wallet()
        balance_amount = (self.btc_rpc_call.do_getinfo())['balance']

        amount_thresh = abs(balance_amount - total_amount)

        if amount_thresh > 0.0001:
            logger_file.error("%d confirmed amount %s  != the total receiving balance %s, need more confirms",
                              int(self.confirms), total_amount, balance_amount)

        logger_file.info("Total amount of coins to be transfer: %f" % total_amount)

        if total_amount >= min_transfer:
            logger_file.info("Init transferring...")
            logger_file.info("Creating a temporary address for moving coins...")
            btc_account = self.__create_an_address_with_account_assigned()
            logger_file.info("Starting to move coins to %s", btc_account)

            for received_account, amount in self.coin_to_be_send_dict.iteritems():
                """
                logger_file.info("%s, %s, %f", received_account,
                                 self.btc_rpc_call.get_addresses_by_account(received_account), amount)
                """
                if self.btc_rpc_call.move(received_account, btc_account, float(amount)):
                    pass
                else:
                    logger_file.error("Fail to move coins to from %s to %s!", received_account, btc_account)

            send_to_address = self.yml_config.get_safe_address_to_be_transferred(currency='btc')

            amount_to_transfer = float(total_amount) - float(self.fee)

            logger_file.info("Starting transferring %f coins to address: %s from account: %s", amount_to_transfer,
                             send_to_address, btc_account)

            self.btc_rpc_call.send_from(btc_account, send_to_address, amount_to_transfer)
            logger_file.info("Transfer is done")
        else:
            logger_file.info("It is not ready to do the coin transferring!")

    def main_test(self):

        total_amount = self.__get_total_amount_in_wallet()
        logger_file.info(total_amount)

        balance_amount = (self.btc_rpc_call.do_getinfo())['balance']
        logger_file.info(balance_amount)

        logger_file.info(len(self.coin_to_be_send_dict))

        for received_account, amount in self.coin_to_be_send_dict.iteritems():
            logger_file.info("account: %s, amount: %f", received_account, amount)