from datetime import datetime
from decimal import Decimal
import responses
import requests

try:
    from unittest.mock import patch, Mock
except ImportError:
    from mock import patch, Mock

from monero.wallet import Wallet
from monero.address import BaseAddress, Address, SubAddress
from monero.seed import Seed
from monero.transaction import IncomingPayment, OutgoingPayment, Transaction
from monero.backends.jsonrpc import JSONRPCWallet

from .base import JSONTestCase


class JSONRPCWalletTestCase(JSONTestCase):
    data_subdir = "test_jsonrpcwallet"
    accounts_result = {
        "id": 0,
        "jsonrpc": "2.0",
        "result": {
            "subaddress_accounts": [
                {
                    "account_index": 0,
                    "balance": 224916129245183,
                    "base_address": "9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag",
                    "label": "Primary account",
                    "unlocked_balance": 224916129245183,
                },
                {
                    "account_index": 1,
                    "balance": 3981420960933,
                    "base_address": "BaCBwYSK9BGSuKxb2msXEj4mmpvZYJexYHfqx7kNPDrXDePVXSfoofxGquhXxpA4uxawcnVnouusMDgP74CACa7e9siimpj",
                    "label": "Untitled account",
                    "unlocked_balance": 3981420960933,
                },
                {
                    "account_index": 2,
                    "balance": 7256159239955,
                    "base_address": "BgCseuY3jFJAZS7kt9mrNg7fEG3bo5BV91CTyKbYu9GFiU6hUZhvdNWCTUdQNPNcA4PyFApsFr3EsQDEDfT3tQSY1mVZeP2",
                    "label": "Untitled account",
                    "unlocked_balance": 7256159239955,
                },
            ],
            "total_balance": 236153709446071,
            "total_unlocked_balance": 236153709446071,
        },
    }

    @patch.object(requests.Session, "post")
    def test_seed(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        phrase = (
            "phrases petals speedy fuming ascend weird duplex identity "
            "yearbook masterful elope omission height empty react hope "
            "left iceberg leisure bobsled pyramid ammo sorry tiers "
            "pyramid"
        )
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {"key": phrase},
        }
        seed = self.wallet.seed()
        self.assertIsInstance(seed, Seed)
        self.assertEqual(seed.phrase, phrase)

    @patch.object(requests.Session, "post")
    def test_balance(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "balance": 224916129245183,
                "per_subaddress": [
                    {
                        "address": "9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag",
                        "address_index": 0,
                        "balance": 189656129245183,
                        "label": "Primary account",
                        "num_unspent_outputs": 2,
                        "unlocked_balance": 189656129245183,
                    },
                    {
                        "address": "BfJ5W7dZGaYih6J63YvhiDSKpVUUZbVrEhLRCY6L6TdnEfzJmwP6aUJZQQnzLQ2NMTKMAC8hiJsoiNC7jbEUZ8tmBoJcnN1",
                        "address_index": 5,
                        "balance": 260000000000,
                        "label": "(Untitled address)",
                        "num_unspent_outputs": 1,
                        "unlocked_balance": 260000000000,
                    },
                    {
                        "address": "BbkS4mn6gcgUidn2znLa2J6eSBkbGjGX4doeDCKAzT2A3t1cjbquQGjhYgiMHiKTrY8ojk6Zjqi1ufvfuPwyKv4hNnMruro",
                        "address_index": 7,
                        "balance": 35000000000000,
                        "label": "(Untitled address)",
                        "num_unspent_outputs": 5,
                        "unlocked_balance": 35000000000000,
                    },
                ],
                "unlocked_balance": 224916129245183,
            },
        }
        locked = self.wallet.balance()
        unlocked = self.wallet.balance(unlocked=True)
        balances = self.wallet.balances()
        self.assertEqual(balances[0], locked)
        self.assertEqual(balances[1], unlocked)
        self.assertIsInstance(locked, Decimal)
        self.assertIsInstance(unlocked, Decimal)
        self.assertIsInstance(balances[0], Decimal)
        self.assertIsInstance(balances[1], Decimal)
        self.assertEqual(locked, Decimal("224.916129245183"))

    @responses.activate
    def test_address(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_address-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_address-10-getaddress.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_address-10-getaddress.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        waddr = self.wallet.address()
        a0addr = self.wallet.accounts[0].address()
        self.assertEqual(len(responses.calls), 3)
        self.assertEqual(waddr, a0addr)
        self.assertIsInstance(waddr, Address)
        self.assertEqual(
            waddr,
            "596ETuuDVZSNox73YLctrHaAv72fBboxy3atbEMnP3QtdnGFS9KWuHYGuy831SKWLUVCgrRfWLCxuCZ2fbVGh14X7mFrefy",
        )
        self.assertEqual(waddr.label, "Primary account")
        self.assertEqual(a0addr.label, "Primary account")

    @responses.activate
    def test_account_creation(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_account_creation-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_account_creation-10-create_account.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_account_creation-20-getbalance.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_account_creation-30-get_accounts.json"),
            status=200,
        )
        w = Wallet(JSONRPCWallet())
        self.assertEqual(1, len(w.accounts))
        w.new_account("account 1")
        self.assertEqual(2, len(w.accounts))
        self.assertEqual("account 1", w.accounts[1].label)
        self.assertEqual(0, w.accounts[1].balance())
        acc0, acc1 = w.accounts
        w.refresh()
        self.assertEqual(2, len(w.accounts))
        self.assertEqual("account 1", w.accounts[1].label)
        self.assertEqual([acc0, acc1], w.accounts)

    @responses.activate
    def test_new_address(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_new_address-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_new_address-10-new_address_account_0.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_new_address-20-new_address_account_1.json"),
            status=200,
        )
        w = Wallet(JSONRPCWallet())
        subaddr, index = w.new_address()
        self.assertIsInstance(subaddr, SubAddress)
        self.assertIsInstance(index, int)
        subaddr, index = w.accounts[1].new_address()
        self.assertIsInstance(subaddr, SubAddress)
        self.assertIsInstance(index, int)

    @patch.object(requests.Session, "post")
    def test_incoming_confirmed(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "in": [
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 3000000000000,
                        "double_spend_seen": False,
                        "fee": 8661870000,
                        "height": 1087601,
                        "note": "",
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "timestamp": 1517234267,
                        "txid": "f34b495cec77822a70f829ec8a5a7f1e727128d62e6b1438e9cb7799654d610e",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 10000000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "5c3ab739346e9d98d38dc7b8d36a4b7b1e4b6a16276946485a69797dbf887cd8",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 4000000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 7000000000000,
                        "double_spend_seen": False,
                        "fee": 962430000,
                        "height": 1087601,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "timestamp": 1517234267,
                        "txid": "5ef7ead6a041101ed326568fbb59c128403cba46076c3f353cd110d969dac808",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 10000000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "cc44568337a186c2e1ccc080b43b4ae9db26a07b7afd7edeed60ce2fc4a6477f",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKDUeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV",
                        "amount": 8000000000000,
                        "double_spend_seen": False,
                        "fee": 960990000,
                        "height": 1088394,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 2},
                        "timestamp": 1517335388,
                        "txid": "bc8b7ef53552c2d4bce713f513418894d0e2c8dcaf72e681e1d4d5a202f1eb62",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2120000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "cb248105ea6a9189",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "e9a71c01875bec20812f71d155bfabf42024fde3ec82475562b817dcc8cbf8dc",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKDUeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV",
                        "amount": 1000000000000,
                        "double_spend_seen": False,
                        "fee": 3528910000,
                        "height": 1087606,
                        "note": "",
                        "payment_id": "0166d8da6c0045c51273dd65d6f63734beb8a84e0545a185b2cfd053fced9f5d",
                        "subaddr_index": {"major": 0, "minor": 2},
                        "timestamp": 1517234425,
                        "txid": "a0b876ebcf7c1d499712d84cedec836f9d50b608bb22d6cb49fd2feae3ffed14",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 3140000000000,
                        "double_spend_seen": False,
                        "fee": 961950000,
                        "height": 1087858,
                        "note": "",
                        "payment_id": "03f6649304ea4cb2",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517256811,
                        "txid": "d29264ad317e8fdb55ea04484c00420430c35be7b3fe6dd663f99aebf41a786c",
                        "type": "in",
                        "unlock_time": 0,
                    },
                ]
            },
        }
        pay_in = self.wallet.incoming()
        self.assertEqual(len(list(pay_in)), 9)
        for pmt in pay_in:
            self.assertIsInstance(pmt, IncomingPayment)
            self.assertIsInstance(pmt.local_address, BaseAddress)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.transaction, Transaction)
            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIsInstance(pmt.transaction.height, int)

    @patch.object(requests.Session, "post")
    def test_incoming_confirmed_and_unconfirmed(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "in": [
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 3000000000000,
                        "double_spend_seen": False,
                        "fee": 8661870000,
                        "height": 1087601,
                        "note": "",
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "timestamp": 1517234267,
                        "txid": "f34b495cec77822a70f829ec8a5a7f1e727128d62e6b1438e9cb7799654d610e",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 10000000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "5c3ab739346e9d98d38dc7b8d36a4b7b1e4b6a16276946485a69797dbf887cd8",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 4000000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 7000000000000,
                        "double_spend_seen": False,
                        "fee": 962430000,
                        "height": 1087601,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "timestamp": 1517234267,
                        "txid": "5ef7ead6a041101ed326568fbb59c128403cba46076c3f353cd110d969dac808",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 10000000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "cc44568337a186c2e1ccc080b43b4ae9db26a07b7afd7edeed60ce2fc4a6477f",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKDUeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV",
                        "amount": 8000000000000,
                        "double_spend_seen": False,
                        "fee": 960990000,
                        "height": 1088394,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 2},
                        "timestamp": 1517335388,
                        "txid": "bc8b7ef53552c2d4bce713f513418894d0e2c8dcaf72e681e1d4d5a202f1eb62",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2120000000000,
                        "double_spend_seen": False,
                        "fee": 962550000,
                        "height": 1087530,
                        "note": "",
                        "payment_id": "cb248105ea6a9189",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517228238,
                        "txid": "e9a71c01875bec20812f71d155bfabf42024fde3ec82475562b817dcc8cbf8dc",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKDUeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV",
                        "amount": 1000000000000,
                        "double_spend_seen": False,
                        "fee": 3528910000,
                        "height": 1087606,
                        "note": "",
                        "payment_id": "0166d8da6c0045c51273dd65d6f63734beb8a84e0545a185b2cfd053fced9f5d",
                        "subaddr_index": {"major": 0, "minor": 2},
                        "timestamp": 1517234425,
                        "txid": "a0b876ebcf7c1d499712d84cedec836f9d50b608bb22d6cb49fd2feae3ffed14",
                        "type": "in",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 3140000000000,
                        "double_spend_seen": False,
                        "fee": 961950000,
                        "height": 1087858,
                        "note": "",
                        "payment_id": "03f6649304ea4cb2",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517256811,
                        "txid": "d29264ad317e8fdb55ea04484c00420430c35be7b3fe6dd663f99aebf41a786c",
                        "type": "in",
                        "unlock_time": 0,
                    },
                ],
                "pool": [
                    {
                        "address": "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKDUeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV",
                        "amount": 1000000000000,
                        "double_spend_seen": False,
                        "fee": 960960000,
                        "height": 0,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 2},
                        "timestamp": 1517336242,
                        "txid": "f349c6badfa7f6e46666db3996b569a05c6ac4e85417551ec208d96f8a37294a",
                        "type": "pool",
                        "unlock_time": 0,
                    },
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 1000000000000,
                        "double_spend_seen": False,
                        "fee": 960960000,
                        "height": 0,
                        "note": "",
                        "payment_id": "1f2510a597bd634bbd130cf21e63b4ad01f4565faf0d3eb21589f496bf28f7f2",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "timestamp": 1517336242,
                        "txid": "41304bbb514d1abdfdb0704bf70f8d2ec4e753c57aa34b6d0525631d79113b87",
                        "type": "pool",
                        "unlock_time": 0,
                    },
                ],
            },
        }
        pay_in = self.wallet.incoming(unconfirmed=True)
        self.assertEqual(len(list(pay_in)), 11)
        for pmt in pay_in:
            self.assertIsInstance(pmt, IncomingPayment)
            self.assertIsInstance(pmt.local_address, BaseAddress)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.transaction, Transaction)
            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIsInstance(pmt.transaction.height, (int, type(None)))

    @patch.object(requests.Session, "post")
    def test_incoming_unconfirmed(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "pool": [
                    {
                        "address": "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKDUeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV",
                        "amount": 1000000000000,
                        "double_spend_seen": False,
                        "fee": 960960000,
                        "height": 0,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 2},
                        "timestamp": 1517336242,
                        "txid": "f349c6badfa7f6e46666db3996b569a05c6ac4e85417551ec208d96f8a37294a",
                        "type": "pool",
                        "unlock_time": 0,
                    },
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 1000000000000,
                        "double_spend_seen": False,
                        "fee": 960960000,
                        "height": 0,
                        "note": "",
                        "payment_id": "1f2510a597bd634bbd130cf21e63b4ad01f4565faf0d3eb21589f496bf28f7f2",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "timestamp": 1517336242,
                        "txid": "41304bbb514d1abdfdb0704bf70f8d2ec4e753c57aa34b6d0525631d79113b87",
                        "type": "pool",
                        "unlock_time": 0,
                    },
                ]
            },
        }
        pay_in = self.wallet.incoming(unconfirmed=True, confirmed=False)
        self.assertEqual(len(list(pay_in)), 2)
        for pmt in pay_in:
            self.assertIsInstance(pmt, IncomingPayment)
            self.assertIsInstance(pmt.local_address, BaseAddress)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.transaction, Transaction)
            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIs(pmt.transaction.height, None)

    @responses.activate
    def test_incoming_by_tx_id(self):
        # 3 payments in one transaction
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-7ab84-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            tx_id="7ab84fe2fb34467c590cde2f7d6ba7de5928a2db6c84c6ccfff8962eca0ad99c"
        )
        self.assertEqual(len(pmts), 3)
        self.assertEqual(pmts[0].amount, Decimal(1))
        self.assertEqual(pmts[1].amount, Decimal(1))
        self.assertEqual(pmts[2].amount, Decimal(2))

    @responses.activate
    def test_incoming_by_tx_id__with_min_height(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-7ab84-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            min_height=409223,
            tx_id="7ab84fe2fb34467c590cde2f7d6ba7de5928a2db6c84c6ccfff8962eca0ad99c",
        )
        self.assertEqual(len(pmts), 3)
        self.assertEqual(pmts[0].amount, Decimal(1))
        self.assertEqual(pmts[1].amount, Decimal(1))
        self.assertEqual(pmts[2].amount, Decimal(2))

    @responses.activate
    def test_incoming_by_tx_id__with_max_height(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-7ab84-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            max_height=409223,
            tx_id="7ab84fe2fb34467c590cde2f7d6ba7de5928a2db6c84c6ccfff8962eca0ad99c",
        )
        self.assertEqual(len(pmts), 0)

    @responses.activate
    def test_incoming_by_tx_id__not_found(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-e0b15-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            tx_id="e0b15ac819c94ed9ba81edb955a98c696f3216335960ccf90018d76a8dcb0e7e"
        )
        self.assertEqual(len(pmts), 0)

    @responses.activate
    def test_incoming_by_tx_id__multiple_ids(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-7ab84-get_transfer_by_txid.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-55e75-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            tx_id=[
                "7ab84fe2fb34467c590cde2f7d6ba7de5928a2db6c84c6ccfff8962eca0ad99c",
                "55e758d7d259bb316551ddcdd4808711de99c30b8b5c52de3e95e563fd92d156",
            ]
        )
        self.assertEqual(len(pmts), 4)
        self.assertEqual(pmts[0].amount, Decimal(4))
        self.assertEqual(pmts[1].amount, Decimal(1))
        self.assertEqual(pmts[2].amount, Decimal(1))
        self.assertEqual(pmts[3].amount, Decimal(2))

    @responses.activate
    def test_incoming_by_tx_id__mempool(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-31b52-get_transfer_by_txid.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-31b52-get_transfer_by_txid.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-31b52-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            tx_id="31b527fb9c27e759d56892fef93136df1057186c5cf4e3c93c5298b70160f562"
        )
        self.assertEqual(len(pmts), 0)
        pmts = self.wallet.incoming(
            tx_id="31b527fb9c27e759d56892fef93136df1057186c5cf4e3c93c5298b70160f562",
            unconfirmed=True,
        )
        self.assertEqual(len(pmts), 1)
        pmts = self.wallet.incoming(
            tx_id="31b527fb9c27e759d56892fef93136df1057186c5cf4e3c93c5298b70160f562",
            confirmed=False,
        )
        self.assertEqual(len(pmts), 0)

    @responses.activate
    def test_outgoing_by_tx_id(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-362c3-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        acc = self.wallet.accounts[1]
        pmts = acc.outgoing(
            tx_id="362c3a4e601d5847b3882c3debfd28a0ee31654e433c38498539677199c304c2"
        )
        self.assertEqual(len(pmts), 1)
        self.assertEqual(pmts[0].amount, Decimal("0.52"))

    @responses.activate
    def test_outgoing_by_tx_id__mempool(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-afaf0-get_transfer_by_txid.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-afaf0-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        acc = self.wallet.accounts[1]
        pmts = acc.outgoing(
            tx_id="afaf04e5e40c6b60fc7cc928a88843fc96031ec2b567c310ee61abf3d00020da"
        )
        self.assertEqual(len(pmts), 0)
        pmts = acc.outgoing(
            tx_id="afaf04e5e40c6b60fc7cc928a88843fc96031ec2b567c310ee61abf3d00020da",
            unconfirmed=True,
        )
        self.assertEqual(len(pmts), 1)

    @responses.activate
    def test_outgoing_by_tx_id__multiple_ids(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-362c3-get_transfer_by_txid.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_outgoing_by_tx_id-eda89-get_transfer_by_txid.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        acc = self.wallet.accounts[1]
        pmts = acc.outgoing(
            tx_id=[
                "362c3a4e601d5847b3882c3debfd28a0ee31654e433c38498539677199c304c2",
                "eda891adf76993f9066abd56a8a5aa5c51a7618298cab59ec37739f1c960596d",
            ]
        )
        self.assertEqual(len(pmts), 2)
        self.assertEqual(pmts[0].amount, Decimal("0.52"))
        self.assertEqual(pmts[1].amount, Decimal("0.0212"))

    @patch.object(requests.Session, "post")
    def test_incoming_by_payment_ids(self, mock_post):
        # These queries will use get_bulk_payments RPC method instead of get_transfers
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "payments": [
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 3000000000000,
                        "block_height": 1087601,
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "tx_hash": "f34b495cec77822a70f829ec8a5a7f1e727128d62e6b1438e9cb7799654d610e",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 10000000000000,
                        "block_height": 1087530,
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "tx_hash": "5c3ab739346e9d98d38dc7b8d36a4b7b1e4b6a16276946485a69797dbf887cd8",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 4000000000000,
                        "block_height": 1087530,
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "tx_hash": "4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3",
                        "unlock_time": 0,
                    },
                ]
            },
        }
        pay_in = self.wallet.incoming(payment_id="f75ad90e25d71a12")
        self.assertEqual(len(list(pay_in)), 3)
        for pmt in pay_in:
            self.assertIsInstance(pmt, IncomingPayment)
            self.assertIsInstance(pmt.local_address, BaseAddress)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.transaction, Transaction)
            # Fee is not returned by this RPC method!
            #            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIsInstance(pmt.transaction.height, int)
            self.assertEqual(pmt.payment_id, "f75ad90e25d71a12")

        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "payments": [
                    {
                        "address": "BhE3cQvB7VF2uuXcpXp28Wbadez6GgjypdRS1F1Mzqn8Advd6q8VfaX8ZoEDobjejrMfpHeNXoX8MjY8q8prW1PEALgr1En",
                        "amount": 3000000000000,
                        "block_height": 1087601,
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 1},
                        "tx_hash": "f34b495cec77822a70f829ec8a5a7f1e727128d62e6b1438e9cb7799654d610e",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 10000000000000,
                        "block_height": 1087530,
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "tx_hash": "5c3ab739346e9d98d38dc7b8d36a4b7b1e4b6a16276946485a69797dbf887cd8",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 4000000000000,
                        "block_height": 1087530,
                        "payment_id": "f75ad90e25d71a12",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "tx_hash": "4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2120000000000,
                        "block_height": 1087530,
                        "payment_id": "cb248105ea6a9189",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "tx_hash": "e9a71c01875bec20812f71d155bfabf42024fde3ec82475562b817dcc8cbf8dc",
                        "unlock_time": 0,
                    },
                ]
            },
        }
        ids = ("f75ad90e25d71a12", "cb248105ea6a9189")
        pay_in = self.wallet.incoming(payment_id=ids)
        self.assertEqual(len(list(pay_in)), 4)
        for pmt in pay_in:
            self.assertIsInstance(pmt, IncomingPayment)
            self.assertIsInstance(pmt.local_address, BaseAddress)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.transaction, Transaction)
            # Fee is not returned by this RPC method!
            #            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIsInstance(pmt.transaction.height, int)
            self.assertIn(pmt.payment_id, ids)

    @patch.object(requests.Session, "post")
    def test_outgoing(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "out": [
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 70000000000,
                        "destinations": [
                            {
                                "address": "BaB4Xsi9fBPDutsdNWTzNN5qgmTE2WUwnMGm5ukkb9TXUJHQLyZhQikekhHX8zhmRn3VTJeniuMXJHuCHfKyPk1XQDfk9bw",
                                "amount": 70000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960900000,
                        "height": 1088441,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517342302,
                        "txid": "e32cccd7522e760b1c8a571fd08c75e7a1d822874380edc9656f58284eeb2fe5",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 21000000000000,
                        "destinations": [
                            {
                                "address": "BczAN2ZV5MENFAqSBdJJpzW9CMqURR9XTCjihzEGHzCV58YFRZHYuhk2huFsBrbPtDN2dGWHgiY1CRTUAPeDJg1ZFCzCssT",
                                "amount": 21000000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960990000,
                        "height": 1088394,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517335388,
                        "txid": "edc7c28e7b07486be48dac0a178f25a3505114267ddaf3e62ab00d9a6e996044",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 210000000000,
                        "destinations": [
                            {
                                "address": "Bg2iRGq5f6iXf3gXYps9AJFWuhhtL74o8GE4JrBPFN3ViuyuTDcPSVqckGeNSU1brSFbdYJ5jjVerJ8Cb4aGJwJTSvzzHwf",
                                "amount": 210000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960810000,
                        "height": 1088479,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517344830,
                        "txid": "d09666238129a1e2a71a9b7c6b30564a95baef926556bb658785cb9c38d9b25a",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 1111111111000,
                        "double_spend_seen": False,
                        "fee": 960750000,
                        "height": 1088516,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517347450,
                        "txid": "551721b5358b02565d6a9862867e3806b9a2e0d5aa5158d4d30940251d27bbdd",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 70000000000,
                        "destinations": [
                            {
                                "address": "BaB4Xsi9fBPDutsdNWTzNN5qgmTE2WUwnMGm5ukkb9TXUJHQLyZhQikekhHX8zhmRn3VTJeniuMXJHuCHfKyPk1XQDfk9bw",
                                "amount": 70000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960900000,
                        "height": 1088438,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517341824,
                        "txid": "21e7eb651e8a2bc7661975e965ac6b30a6f4033c6a02e96320e41139ad3e307c",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 3333333333330,
                        "double_spend_seen": False,
                        "fee": 960750000,
                        "height": 1088521,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517347785,
                        "txid": "5d15fef66fe8de715bcbf2c181f97b9932f9f843aef4724f3026fa3cd1082c68",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 1000000000000,
                        "destinations": [
                            {
                                "address": "Bg2iRGq5f6iXf3gXYps9AJFWuhhtL74o8GE4JrBPFN3ViuyuTDcPSVqckGeNSU1brSFbdYJ5jjVerJ8Cb4aGJwJTSvzzHwf",
                                "amount": 1000000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960990000,
                        "height": 1088394,
                        "note": "",
                        "payment_id": "6cc9350927868849",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517335388,
                        "txid": "5e8f392a42899294e6b679ddac13cfe1dfe7f034b1e347424218af06c3dfdc85",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2450000000000,
                        "destinations": [
                            {
                                "address": "9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag",
                                "amount": 2450000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 961350000,
                        "height": 1088184,
                        "note": "",
                        "payment_id": "6cc9350927868849",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517299877,
                        "txid": "2b41226d45edb875634694fccd95f3c174daa5062763eee619ed4475a7b9207a",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 1000000000000,
                        "destinations": [
                            {
                                "address": "BZHa2Fm9yn3by1CWTBruWxEuZ5TqyMLiBQAFrHM6aniLfjTBWkA4E4kif63YS7wmbFW5UBwrFSKNoiSQ2mE8SS5S6uzgvoc",
                                "amount": 1000000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 961350000,
                        "height": 1088184,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517299877,
                        "txid": "40de45db57eb87eb8395baf5c1dc705602938317d043f463e68ed85b7108f9f3",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 123000000000,
                        "destinations": [
                            {
                                "address": "BYeK7sZ9DhkASZpMrhGw6yFynaDy5PJ8T2CeogvGTFfT2kMQdTNJFQpDXpoHNBPTyNfrxERdRje9bBJh7LTqN4oDUVVvzAe",
                                "amount": 123000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 3843000000,
                        "height": 1088523,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517347908,
                        "txid": "e291fe40c6102a6193c82ac33227c08e5b30a863dba1bc63e13043a25abbb97a",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2220000000000,
                        "double_spend_seen": False,
                        "fee": 3843840000,
                        "height": 1088411,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517339862,
                        "txid": "a8829744952facbfdaab21ca193298edb1fa16f688cd5dbcdff3ed3968155f28",
                        "type": "out",
                        "unlock_time": 0,
                    },
                ]
            },
        }
        pay_out = self.wallet.outgoing()
        self.assertEqual(len(list(pay_out)), 11)
        for pmt in pay_out:
            self.assertIsInstance(pmt, OutgoingPayment)
            self.assertIsInstance(pmt.local_address, Address)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.timestamp, datetime)
            self.assertIsInstance(pmt.transaction, Transaction)
            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIsInstance(pmt.transaction.height, int)

    @patch.object(requests.Session, "post")
    def test_outgoing_confirmed_and_unconfirmed(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "out": [
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 70000000000,
                        "destinations": [
                            {
                                "address": "BaB4Xsi9fBPDutsdNWTzNN5qgmTE2WUwnMGm5ukkb9TXUJHQLyZhQikekhHX8zhmRn3VTJeniuMXJHuCHfKyPk1XQDfk9bw",
                                "amount": 70000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960900000,
                        "height": 1088441,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517342302,
                        "txid": "e32cccd7522e760b1c8a571fd08c75e7a1d822874380edc9656f58284eeb2fe5",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 21000000000000,
                        "destinations": [
                            {
                                "address": "BczAN2ZV5MENFAqSBdJJpzW9CMqURR9XTCjihzEGHzCV58YFRZHYuhk2huFsBrbPtDN2dGWHgiY1CRTUAPeDJg1ZFCzCssT",
                                "amount": 21000000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960990000,
                        "height": 1088394,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517335388,
                        "txid": "edc7c28e7b07486be48dac0a178f25a3505114267ddaf3e62ab00d9a6e996044",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 210000000000,
                        "destinations": [
                            {
                                "address": "Bg2iRGq5f6iXf3gXYps9AJFWuhhtL74o8GE4JrBPFN3ViuyuTDcPSVqckGeNSU1brSFbdYJ5jjVerJ8Cb4aGJwJTSvzzHwf",
                                "amount": 210000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960810000,
                        "height": 1088479,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517344830,
                        "txid": "d09666238129a1e2a71a9b7c6b30564a95baef926556bb658785cb9c38d9b25a",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 1111111111000,
                        "double_spend_seen": False,
                        "fee": 960750000,
                        "height": 1088516,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517347450,
                        "txid": "551721b5358b02565d6a9862867e3806b9a2e0d5aa5158d4d30940251d27bbdd",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 70000000000,
                        "destinations": [
                            {
                                "address": "BaB4Xsi9fBPDutsdNWTzNN5qgmTE2WUwnMGm5ukkb9TXUJHQLyZhQikekhHX8zhmRn3VTJeniuMXJHuCHfKyPk1XQDfk9bw",
                                "amount": 70000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960900000,
                        "height": 1088438,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517341824,
                        "txid": "21e7eb651e8a2bc7661975e965ac6b30a6f4033c6a02e96320e41139ad3e307c",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 3333333333330,
                        "double_spend_seen": False,
                        "fee": 960750000,
                        "height": 1088521,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517347785,
                        "txid": "5d15fef66fe8de715bcbf2c181f97b9932f9f843aef4724f3026fa3cd1082c68",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 1000000000000,
                        "destinations": [
                            {
                                "address": "Bg2iRGq5f6iXf3gXYps9AJFWuhhtL74o8GE4JrBPFN3ViuyuTDcPSVqckGeNSU1brSFbdYJ5jjVerJ8Cb4aGJwJTSvzzHwf",
                                "amount": 1000000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 960990000,
                        "height": 1088394,
                        "note": "",
                        "payment_id": "6cc9350927868849",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517335388,
                        "txid": "5e8f392a42899294e6b679ddac13cfe1dfe7f034b1e347424218af06c3dfdc85",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2450000000000,
                        "destinations": [
                            {
                                "address": "9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag",
                                "amount": 2450000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 961350000,
                        "height": 1088184,
                        "note": "",
                        "payment_id": "6cc9350927868849",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517299877,
                        "txid": "2b41226d45edb875634694fccd95f3c174daa5062763eee619ed4475a7b9207a",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 1000000000000,
                        "destinations": [
                            {
                                "address": "BZHa2Fm9yn3by1CWTBruWxEuZ5TqyMLiBQAFrHM6aniLfjTBWkA4E4kif63YS7wmbFW5UBwrFSKNoiSQ2mE8SS5S6uzgvoc",
                                "amount": 1000000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 961350000,
                        "height": 1088184,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517299877,
                        "txid": "40de45db57eb87eb8395baf5c1dc705602938317d043f463e68ed85b7108f9f3",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 123000000000,
                        "destinations": [
                            {
                                "address": "BYeK7sZ9DhkASZpMrhGw6yFynaDy5PJ8T2CeogvGTFfT2kMQdTNJFQpDXpoHNBPTyNfrxERdRje9bBJh7LTqN4oDUVVvzAe",
                                "amount": 123000000000,
                            }
                        ],
                        "double_spend_seen": False,
                        "fee": 3843000000,
                        "height": 1088523,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517347908,
                        "txid": "e291fe40c6102a6193c82ac33227c08e5b30a863dba1bc63e13043a25abbb97a",
                        "type": "out",
                        "unlock_time": 0,
                    },
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 2220000000000,
                        "double_spend_seen": False,
                        "fee": 3843840000,
                        "height": 1088411,
                        "note": "",
                        "payment_id": "0000000000000000",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517339862,
                        "txid": "a8829744952facbfdaab21ca193298edb1fa16f688cd5dbcdff3ed3968155f28",
                        "type": "out",
                        "unlock_time": 0,
                    },
                ],
                "pending": [
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 3141592653589,
                        "double_spend_seen": False,
                        "fee": 3842880000,
                        "height": 0,
                        "note": "",
                        "payment_id": "0000000000000000000000000000000079323846264338327950288419716939",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517348994,
                        "txid": "34833fef78c7b7c15383a78912344ecfb3ace479d27c4bd6f3e3f136ddc1d6a9",
                        "type": "pending",
                        "unlock_time": 0,
                    }
                ],
            },
        }
        pay_out = self.wallet.outgoing(unconfirmed=True)
        self.assertEqual(len(list(pay_out)), 12)
        for pmt in pay_out:
            self.assertIsInstance(pmt, OutgoingPayment)
            self.assertIsInstance(pmt.local_address, Address)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.timestamp, datetime)
            self.assertIsInstance(pmt.transaction, Transaction)
            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIsInstance(pmt.transaction.height, (int, type(None)))

    @patch.object(requests.Session, "post")
    def test_outgoing_unconfirmed_only(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "pending": [
                    {
                        "address": "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC",
                        "amount": 3141592653589,
                        "double_spend_seen": False,
                        "fee": 3842880000,
                        "height": 0,
                        "note": "",
                        "payment_id": "0000000000000000000000000000000079323846264338327950288419716939",
                        "subaddr_index": {"major": 0, "minor": 0},
                        "timestamp": 1517348994,
                        "txid": "34833fef78c7b7c15383a78912344ecfb3ace479d27c4bd6f3e3f136ddc1d6a9",
                        "type": "pending",
                        "unlock_time": 0,
                    }
                ]
            },
        }
        pay_out = self.wallet.outgoing(unconfirmed=True, confirmed=False)
        self.assertEqual(len(list(pay_out)), 1)
        for pmt in pay_out:
            self.assertIsInstance(pmt, OutgoingPayment)
            self.assertIsInstance(pmt.local_address, Address)
            self.assertIsInstance(pmt.amount, Decimal)
            self.assertIsInstance(pmt.timestamp, datetime)
            self.assertIsInstance(pmt.transaction, Transaction)
            self.assertIsInstance(pmt.transaction.fee, Decimal)
            self.assertIs(pmt.transaction.height, None)

    @responses.activate
    def test_multiple_destinations(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_multiple_destinations-accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_multiple_destinations-incoming.json"),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        out = self.wallet.outgoing()
        self.assertEqual(len(out), 1)
        pmt = out[0]
        self.assertEqual(pmt.amount, 1)
        self.assertEqual(len(pmt.destinations), 2)
        self.assertEqual(pmt.destinations[0][1] + pmt.destinations[1][1], pmt.amount)

    @patch.object(requests.Session, "post")
    def test_send_transfer(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "amount_list": [3000000000000],
                "fee_list": [3866640000],
                "multisig_txset": "",
                "tx_blob_list": [
                    " "
                ],
                "tx_hash_list": [
                    "401d8021975a0fee16fe84acbfc4d8ba6312e563fa245baba2aac382e787fb60"
                ],
                "tx_key_list": [
                    "7061d4d939b563a11e344c60938410e2e63ea72c43741fae81b8805cebe5570a"
                ],
            },
        }
        txns = self.wallet.transfer(
            "9wFuzNoQDck1pnS9ZhG47kDdLD1BUszSbWpGfWcSRy9m6Npq9NoHWd141KvGag8hu2gajEwzRXJ4iJwmxruv9ofc2CwnYCE",
            3,
        )
        self.assertEqual(len(txns), 1)
        txn = txns[0]
        self.assertIsInstance(txn, Transaction)
        self.assertIsInstance(txn.fee, Decimal)
        self.assertEqual(
            txn.hash, "401d8021975a0fee16fe84acbfc4d8ba6312e563fa245baba2aac382e787fb60"
        )
        self.assertEqual(
            txn.key, "7061d4d939b563a11e344c60938410e2e63ea72c43741fae81b8805cebe5570a"
        )

    @responses.activate
    def test_sweep_all(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_sweep_all-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_sweep_all-10-getbalance.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_sweep_all-20-sweep_all.json"),
            status=200,
        )
        w = Wallet(JSONRPCWallet())
        result = w.sweep_all(
            "55LTR8KniP4LQGJSPtbYDacR7dz8RBFnsfAKMaMuwUNYX6aQbBcovzDPyrQF9KXF9tVU6Xk3K8no1BywnJX6GvZX8yJsXvt",
            relay=False,
        )
        self.assertEqual(len(result), 1)
        result = result[0]
        self.assertIsInstance(result[0], Transaction)
        self.assertEqual(Decimal("111.086545699972"), result[1])

    @patch.object(requests.Session, "post")
    def test_dynamic_ring_size_deprecation(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "amount_list": [3000000000000],
                "fee_list": [3866640000],
                "multisig_txset": "",
                "tx_blob_list": [
                    " "
                ],
                "tx_hash_list": [
                    "401d8021975a0fee16fe84acbfc4d8ba6312e563fa245baba2aac382e787fb60"
                ],
                "tx_key_list": [
                    "7061d4d939b563a11e344c60938410e2e63ea72c43741fae81b8805cebe5570a"
                ],
            },
        }

    @patch.object(requests.Session, "post")
    def test_export_import_outputs(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            "id": 0,
            "jsonrpc": "2.0",
            "result": {
                "outputs_data_hex": " "
            },
        }
        outs_hex = self.wallet.export_outputs()
        mock_post.return_value.json.return_value = {
            u"id": 0,
            u"jsonrpc": u"2.0",
            u"result": {u"num_imported": 9},
        }
        self.assertEqual(self.wallet.import_outputs(outs_hex), 9)

    @patch.object(requests.Session, "post")
    def test_export_import_key_images(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.accounts_result
        self.wallet = Wallet(JSONRPCWallet())
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = {
            u"id": 0,
            u"jsonrpc": u"2.0",
            u"result": {
                u"signed_key_images": [
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"ffe0d3216dc7a9f44dd997e7412ecb4a5e03641bba27119bbc0d5b6a145955048f4a8f301826c0d5999264cbc67817a6643143c49195a56fd135936667db0e08",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"0ed0b9752eb5d2466985174c50453f3fc9bd271b1f95bfd50d484b958976c40e8e1a7584cde52a85d40edc4a1bf9684003436492364af0ad9e1d4add1e158108",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"b54632f4e95f70eb1af6459c94694c65ff1b8c822b40a62e6ef7cc1c6a055b0bb4b3832d48330c863993bf46bf87e1ed2971bf6a5d94a9a333a59017fb83630e",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"57f85fd761ecb780a877034cef57449df7c3ad492d803711d1b2a47bf192d00af2f2341388120364dba0d7496c0a5d041032602812995c11268253618713040a",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"9d22b52da3b835afbf29de7ba2dd86fb805435492691799debd7d37dd932dc0e4b07e9b4744da38e2b38fac63475522112804627227756bc37dbc57ea7e59701",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"664c7ac1c1f4c86e56ebc5298ef4acf46c2c154263c5dbc69b5d4d08b9f31a001db8845dbe37a81e7289e33f24ab5b0f17bbfe00612e4624c7b27ca0c27e5c0c",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"a6bf6c5248b00f946ced5d21d03311fa6335b0ddf37f8e46d89d4b0a73de7b040d7ceee31da144693a8f86b1e61908014783e139061c0a4015b0ef13a967060c",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"2d6792c9c438657a5a2d7068f16fe80ca7995144ec1f12f1a37c091690cf260323bbe866e88d725dc791344e6af29b0f22d0792b9f60a5020c6e54bad7fe1404",
                    },
                    {
                        u"key_image": u"0100000000000000000000000000000000000000000000000000000000000000",
                        u"signature": u"a5fe1c57e83442856b1e3cefc67814c908f2485269adbe808c013c7ddb489c0592278fec3c7cc32b2d16135e627fe7bf42ab896e0c7a17333f176ff8bf267f04",
                    },
                ]
            },
        }
        kimgs = self.wallet.export_key_images()
        mock_post.return_value.json.return_value = {
            u"id": 0,
            u"jsonrpc": u"2.0",
            u"result": {u"height": 125578, u"spent": 322437994530000, u"unspent": 0},
        }
        self.assertEqual(
            self.wallet.import_key_images(kimgs),
            (125578, Decimal("322.437994530000"), Decimal("0")),
        )

    @responses.activate
    def test_incoming_from_self__issue_71(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_from_self__issue_71-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read(
                "test_incoming_from_self__issue_71-1a75f-get_transfer_by_txid.json"
            ),
            status=200,
        )
        self.wallet = Wallet(JSONRPCWallet())
        pmts = self.wallet.incoming(
            tx_id="1a75f3aa57f7912313e90ab1188b7a102dbb619a324c3db51bb856a2f40503f1"
        )
        self.assertEqual(len(pmts), 1)

    @responses.activate
    def test_init_default_backend(self):
        """Test default backend initialization.
        Also test for bug #88 where invalid backend value was passed to PaymentManager."""
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_from_self__issue_71-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-7ab84-get_transfer_by_txid.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_from_self__issue_71-00-get_accounts.json"),
            status=200,
        )
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read("test_incoming_by_tx_id-7ab84-get_transfer_by_txid.json"),
            status=200,
        )

        wallet1 = Wallet(host="127.0.0.1")
        self.assertEqual(type(wallet1._backend), JSONRPCWallet)
        pmts = wallet1.incoming(
            tx_id="7ab84fe2fb34467c590cde2f7d6ba7de5928a2db6c84c6ccfff8962eca0ad99c"
        )
        self.assertEqual(len(pmts), 3)

        wallet2 = Wallet()
        self.assertEqual(type(wallet2._backend), JSONRPCWallet)
        pmts = wallet2.incoming(
            tx_id="7ab84fe2fb34467c590cde2f7d6ba7de5928a2db6c84c6ccfff8962eca0ad99c"
        )
        self.assertEqual(len(pmts), 3)

        with self.assertRaises(ValueError):
            wallet3 = Wallet(backend=JSONRPCWallet(), port=18089)
