Skip to content

Commit

Permalink
Merge pull request #27 from madnesspie/fix-send-transaction
Browse files Browse the repository at this point in the history
Fixes wrong amount in bitcoin send_transaction formatting
  • Loading branch information
madnesspie committed Jun 23, 2020
2 parents 249076f + 0f23f0f commit f281c3b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 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.26"
__version__ = "0.0.27"
6 changes: 1 addition & 5 deletions obm/connectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ async def close(self):
@_catch_network_errors
async def call(self, payload: dict) -> dict:
await self.open()
async with self.session.post( # type: ignore
url=self.url,
json=payload,
raise_for_status=True,
) as response:
async with self.session.post(url=self.url, json=payload) as response:
return await response.json(
loads=functools.partial(json.loads, parse_float=Decimal)
)
Expand Down
24 changes: 17 additions & 7 deletions obm/connectors/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,30 @@ async def wrapper(self, *args, method: str = None) -> Union[dict, list]:

@staticmethod
def format_transaction(tx, latest_block_number):

def get_amount(details, category):
if category in ["send", "oneself"]:
return details.get("send", {}).get("amount")
elif category == "receive":
return details.get("receive", {}).get("amount")

def get_category(from_address, to_address):
if from_address and to_address:
return "send" if from_address != to_address else "oneself"
elif from_address:
return "send"
elif to_address:
return "receive"

category = tx.get("category")
if category is None:
# Tx is send_transaction output.
details = {detail["category"]: detail for detail in tx["details"]}
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"
category = get_category(from_address, to_address)
amount = get_amount(details, category)
else:
# Tx is list_transactions output.
if category == "oneself":
Expand Down
2 changes: 1 addition & 1 deletion obm/connectors/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def format_transaction(self, tx, addresses):
elif tx["to"] in addresses:
category = "receive"
else:
assert False, "Unrecognized category"
raise RuntimeError("Unrecognized category")

if tx["blockNumber"] is None:
block_number = None
Expand Down
1 change: 1 addition & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ disable=
too-many-ancestors,
bad-continuation,
inconsistent-return-statements,
no-else-return,
R0801, # Stupid similar lines in 2 files :)


Expand Down
8 changes: 8 additions & 0 deletions tests/connectors/test_bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ async def test_send_transaction(bitcoin_core):
)
assert isinstance(fee, dict)

@staticmethod
async def test_send_transaction_on_outside_wallet(bitcoin_core):
fee = await bitcoin_core.send_transaction(
amount=0.00001,
to_address=os.environ.get("BITCOIN_CORE_OUT_WALLET_ADDRESS"),
)
assert isinstance(fee, dict)

@staticmethod
async def test_fetch_recent_transactions(bitcoin_core):
txs = await bitcoin_core.fetch_recent_transactions(limit=30)
Expand Down

0 comments on commit f281c3b

Please sign in to comment.