Skip to content

Commit

Permalink
Fixes btc.fetch_in_wallet_transaction with receive category
Browse files Browse the repository at this point in the history
  • Loading branch information
madnesspie committed Jun 17, 2020
1 parent f32fadc commit 2e2958f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion obm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = "0.0.24"
__version__ = "0.0.25"
26 changes: 18 additions & 8 deletions obm/connectors/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ def format_transaction(tx, latest_block_number):
if category is None:
# Tx is send_transaction output.
details = {detail["category"]: detail for detail in tx["details"]}
fee = details["send"]["fee"]
amount = details["send"]["amount"]
to_address = details["receive"]["address"]
from_address = details["send"]["address"]
category = "send" if from_address != to_address else "oneself"
fee = details.get("send", {}).get("fee")
from_address = details.get("send", {}).get("address")
amount = details.get("receive", {}).get("amount")
to_address = details.get("receive", {}).get("address")
if from_address and to_address:
category = "send" if from_address != to_address else "oneself"
elif from_address:
category = "send"
elif to_address:
category = "receive"
else:
# Tx is list_transactions output.
if category == "oneself":
Expand Down Expand Up @@ -107,7 +112,7 @@ def format_transaction(tx, latest_block_number):
"from_address": from_address,
"to_address": to_address,
"amount": abs(amount),
"fee": abs(fee),
"fee": abs(fee) if fee is not None else None,
"block_number": block_number,
"category": category,
"timestamp": tx["time"],
Expand Down Expand Up @@ -211,15 +216,20 @@ def combine_duplicates(txs):
return sorted_txs[:limit]

async def fetch_in_wallet_transaction(
self, txid: str
self,
txid: str,
latest_block_number: Optional[int] = None,
) -> dict:
"""Fetches the transaction by txid from a blockchain.
Args:
txid: Transaction ID to return.
latest_block_number: Num of latest block.
Returns:
Dict that represent the transaction.
"""
tx = await self.rpc_get_transaction(txid)
return self.format_transaction(tx, await self.latest_block_number)
if latest_block_number is None:
latest_block_number = await self.latest_block_number
return self.format_transaction(tx, latest_block_number)
49 changes: 49 additions & 0 deletions tests/connectors/test_bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,55 @@ async def mock(*_, **__):
await bitcoin_core.estimate_fee()
assert exc_info.value.args[0] == [error_1]

@staticmethod
@pytest.mark.parametrize(
"mocked_data, expected_result",
(
(
{
"amount": 0.00015000,
"confirmations": 0,
"trusted": False,
"txid": "5bfe8e66c05bed33432c96b73da22fe6f17cd45f4269d53987cfb774a7e3a0e9",
"walletconflicts": [],
"time":1592413084,
"timereceived":1592413084,
"bip125-replaceable":"no",
"details": [
{
"address": "32A5JFirRRoEz7dhsmqHRNWWe36Z9cmRET",
"category": "receive",
"amount": 0.00015000,
"label": "",
"vout":0
}
],
"hex":"02000000012de8a25f30b4d13101a68a9b92fc58d06130207a80dd5e87a44475513638b964090000006a473044022050a180a78dea52b0d620471dbfbda5853dd3dec356997175387ee692a021d37602203cc0b97058d4521201cc5c7bf827cef98f1ce7b35412ce8ae972fc1b446f09290121030c62d097d1c88b1909df6965576f909b6484398846e9dcb87463cb032ab0a331ffffffff02983a00000000000017a914051e0c36f2daf491ea262e04245c3622e1bdf878877ee50200000000001976a9143ea96e3b0a7f901ebde6a481e3e6677d0e38f37288ac00000000"
},
{
"category": "receive",
},
),
),
ids=["receive"],
)
async def test_in_wallet_transaction(
monkeypatch, bitcoin_core, mocked_data, expected_result,
):
async def mock(*_, **__):
return {"result": mocked_data}

monkeypatch.setattr(
connectors.BitcoinCoreConnector,
"call",
mock,
)
result = await bitcoin_core.fetch_in_wallet_transaction(
txid=mocked_data["txid"],
latest_block_number=100,
)
assert result["category"] == expected_result["category"]


@pytest.mark.integration
class TestBitcoinCoreConnectorIntegration:
Expand Down

0 comments on commit 2e2958f

Please sign in to comment.