-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Implemented OFAC list address check
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
|
||
from pyinjective import PrivateKey | ||
from pyinjective.async_client import AsyncClient | ||
from pyinjective.composer import Composer | ||
from pyinjective.core.broadcaster import BannedAddressError, MsgBroadcasterWithPk, StandardAccountBroadcasterConfig | ||
from pyinjective.core.network import Network | ||
from pyinjective.exceptions import EmptyMsgError | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_broadcast_address_in_ofac_list(): | ||
private_key_banned = PrivateKey.from_mnemonic("test mnemonic never use other places") | ||
public_key_banned = private_key_banned.to_public_key() | ||
address_banned = public_key_banned.to_address() | ||
|
||
ofac_list = [address_banned.to_acc_bech32()] | ||
with patch("pyinjective.core.broadcaster.requests.get") as mock_get: | ||
mock_get.return_value.json.return_value = ofac_list | ||
mock_get.return_value.raise_for_status = lambda: None | ||
|
||
network = Network.local() | ||
client = AsyncClient( | ||
network=Network.local(), | ||
) | ||
composer = AsyncMock(spec=Composer) | ||
|
||
account_config = StandardAccountBroadcasterConfig(private_key=private_key_banned.to_hex()) | ||
broadcaster = MsgBroadcasterWithPk( | ||
network=network, | ||
account_config=account_config, | ||
client=client, | ||
composer=composer, | ||
fee_calculator=AsyncMock(), | ||
) | ||
broadcaster._ofac_list = ofac_list | ||
with pytest.raises(BannedAddressError): | ||
await broadcaster.broadcast(messages=[]) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_broadcast_address_not_in_ofac_list(): | ||
private_key_allowed = PrivateKey.from_mnemonic("another test mnemonic never use other places") | ||
|
||
ofac_list = ["banned_address"] | ||
with patch("pyinjective.core.broadcaster.requests.get") as mock_get: | ||
mock_get.return_value.json.return_value = ofac_list | ||
mock_get.return_value.raise_for_status = lambda: None | ||
|
||
network = Network.local() | ||
client = AsyncClient(network=Network.local()) | ||
composer = AsyncMock(spec=Composer) | ||
|
||
account_config = StandardAccountBroadcasterConfig(private_key=private_key_allowed.to_hex()) | ||
broadcaster = MsgBroadcasterWithPk( | ||
network=network, | ||
account_config=account_config, | ||
client=client, | ||
composer=composer, | ||
fee_calculator=AsyncMock(), | ||
) | ||
broadcaster._ofac_list = ofac_list | ||
|
||
try: | ||
await broadcaster.broadcast(messages=[]) | ||
except BannedAddressError: | ||
pytest.fail("BannedAddressError was raised unexpectedly") | ||
except EmptyMsgError: | ||
# expected failure Exception caught | ||
pass |