Esempio n. 1
0
 def setUp(self):
     self.FROM = 'Mx31e61a05adbd13c6b625262704bc305bf7725026'
     self.PRIVATE_KEY = '07bc17abdcee8b971bb8723e36fe9d2523306d5ab2d683631693238e0f9df142'
     self.SIGNED_TX = 'f8830102018a4d4e540000000000000002a9e88a4d4e5400000000000000880de0b6b3a76400008a54455354000000000000880de0b6b3a7640000808001b845f8431ba0e34be907a18acb5a1aed263ef419f32f5adc6e772b92f949906b497bba557df3a0291d7704980994f7a6f5950ca84720746b5928f21c3cfc5a5fbca2a9f4d35db0'
     self.TX = MinterSellCoinTx(
         **{
             'nonce': 1,
             'chain_id': MinterTx.TESTNET_CHAIN_ID,
             'gas_coin': 'MNT',
             'coin_to_sell': 'MNT',
             'value_to_sell': 1,
             'coin_to_buy': 'TEST',
             'min_value_to_buy': 1
         })
Esempio n. 2
0
class TestMinterSellCoinTx(unittest.TestCase):
    def setUp(self):
        self.FROM = 'Mx31e61a05adbd13c6b625262704bc305bf7725026'
        self.PRIVATE_KEY = '07bc17abdcee8b971bb8723e36fe9d2523306d5ab2d683631693238e0f9df142'
        self.SIGNED_TX = 'f8830102018a4d4e540000000000000002a9e88a4d4e5400000000000000880de0b6b3a76400008a54455354000000000000880de0b6b3a7640000808001b845f8431ba0e34be907a18acb5a1aed263ef419f32f5adc6e772b92f949906b497bba557df3a0291d7704980994f7a6f5950ca84720746b5928f21c3cfc5a5fbca2a9f4d35db0'
        self.TX = MinterSellCoinTx(
            **{
                'nonce': 1,
                'chain_id': MinterTx.TESTNET_CHAIN_ID,
                'gas_coin': 'MNT',
                'coin_to_sell': 'MNT',
                'value_to_sell': 1,
                'coin_to_buy': 'TEST',
                'min_value_to_buy': 1
            })

    def test_valid_tx(self):
        """
        Is tx instance of needed TX class.
        """

        self.assertIsInstance(self.TX, MinterSellCoinTx)

    def test_sign_tx(self):
        """
        Sign transaction and check signed transaction
        """
        self.TX.sign(self.PRIVATE_KEY)

        self.assertEqual(self.TX.signed_tx, self.SIGNED_TX)

    def test_sign_with_signature(self):
        self.TX.signature_type = MinterTx.SIGNATURE_SINGLE_TYPE
        signature = self.TX.generate_signature(self.PRIVATE_KEY)
        self.TX.sign(signature=signature)

        self.assertEqual(self.TX.signed_tx, self.SIGNED_TX)

    def test_from_raw(self):
        tx = MinterTx.from_raw(raw_tx=self.SIGNED_TX)

        self.assertEqual(tx.from_mx, self.FROM)
        self.assertEqual(tx.coin_to_sell, self.TX.coin_to_sell)
        self.assertEqual(tx.value_to_sell, self.TX.value_to_sell)
        self.assertEqual(tx.coin_to_buy, self.TX.coin_to_buy)
        self.assertEqual(tx.min_value_to_buy, self.TX.min_value_to_buy)
Esempio n. 3
0
    def convert(self, value, from_symbol, to_symbol, gas_coin=None):
        """
        Конвертирует одну монету в другую
        :param gas_coin: str 'SYMBOL' - монета для оплаты комиссии
        :param value: int/float
        :param from_symbol: str (Тикер монеты)
        :param to_symbol: str (Тикер монеты)
        :return:
        """

        from_symbol = from_symbol.upper()
        to_symbol = to_symbol.upper()
        value = Decimal(str(value))
        if gas_coin is None:
            gas_coin = from_symbol

        balances = self.get_balance(in_bip=True)

        if balances[from_symbol] < value:
            print(
                f"На кошельке недостаточно {from_symbol}. Нужно {value}, а есть {balances[from_symbol]}"
            )
            return

        # Генерируем транзакцию
        nonce = self.API.get_nonce(self.address)
        tx = MinterSellCoinTx(coin_to_sell=from_symbol,
                              value_to_sell=value,
                              coin_to_buy=to_symbol,
                              min_value_to_buy=0,
                              nonce=nonce,
                              gas_coin=gas_coin)

        # Проверяем достаточно ли баланса на оплату комиссии
        commission = to_bip(tx.get_fee())
        if gas_coin == from_symbol and balances[from_symbol] < (value +
                                                                commission):
            print(
                f"На кошельке недостаточно {from_symbol} для оплаты комиссии {commission}\n"
                f"Баланс: {round(balances[from_symbol], 2)}\n"
                f"Нужно:  {value + commission} (+{value + commission - round(balances[from_symbol], 2)})"
            )
            return
        elif balances[gas_coin] < commission:
            print(
                f"На кошельке недостаточно {gas_coin} для оплаты комиссии {commission}\n"
                f"Баланс: {round(balances[gas_coin], 2)}\n"
                f"Нужно:  {commission}")
            return

        # Отправляем транзакицю
        tx.sign(private_key=self.private_key)
        r = self.API.send_transaction(tx.signed_tx)

        try:
            if r['result']['code'] == 0:
                print(f'{from_symbol} сконвертирован в {to_symbol}')
                self._wait_for_nonce(
                    nonce
                )  # Ждем nonce, чтобы предотвратить отправку нескольких транзакций в блоке
        except Exception:
            print(
                f'Не удалось сконвертировать {from_symbol} в {to_symbol}\nServer response: {r}'
            )

        return r