Skip to content

Commit

Permalink
Implement OFAC list address check
Browse files Browse the repository at this point in the history
  • Loading branch information
shibaeff committed Sep 6, 2024
1 parent 5541211 commit 75433cf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
34 changes: 24 additions & 10 deletions pyinjective/core/broadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from decimal import Decimal
from typing import List, Optional

import requests
from google.protobuf import any_pb2
from grpc import RpcError

Expand All @@ -12,27 +13,26 @@
from pyinjective.constant import GAS_PRICE
from pyinjective.core.gas_limit_estimator import GasLimitEstimator
from pyinjective.core.network import Network
from pyinjective.exceptions import BannedAddressError, OfacListFetchError

OFAC_LIST_URL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/master/wallets/ofac.json"


class BroadcasterAccountConfig(ABC):
@property
@abstractmethod
def trading_injective_address(self) -> str:
...
def trading_injective_address(self) -> str: ...

@property
@abstractmethod
def trading_private_key(self) -> PrivateKey:
...
def trading_private_key(self) -> PrivateKey: ...

@property
@abstractmethod
def trading_public_key(self) -> PublicKey:
...
def trading_public_key(self) -> PublicKey: ...

@abstractmethod
def messages_prepared_for_transaction(self, messages: List[any_pb2.Any]) -> List[any_pb2.Any]:
...
def messages_prepared_for_transaction(self, messages: List[any_pb2.Any]) -> List[any_pb2.Any]: ...


class TransactionFeeCalculator(ABC):
Expand All @@ -44,8 +44,7 @@ async def configure_gas_fee_for_transaction(
transaction: Transaction,
private_key: PrivateKey,
public_key: PublicKey,
):
...
): ...


class MsgBroadcasterWithPk:
Expand All @@ -62,6 +61,17 @@ def __init__(
self._client = client
self._composer = composer
self._fee_calculator = fee_calculator
self._ofac_list = self.load_ofac_list()

@classmethod
def load_ofac_list(cls) -> List[str]:
try:
response = requests.get(OFAC_LIST_URL)
response.raise_for_status()
ofac_list = response.json()
return ofac_list
except requests.exceptions.RequestException as e:
raise OfacListFetchError(f"Error fetching OFAC list: {e}")

@classmethod
def new_using_simulation(
Expand Down Expand Up @@ -157,6 +167,10 @@ async def broadcast(self, messages: List[any_pb2.Any]):

messages_for_transaction = self._account_config.messages_prepared_for_transaction(messages=messages)

# before contraucting the transaction, check if sender address is in the OFAC list
if self._account_config.trading_injective_address in self._ofac_list:
raise BannedAddressError(f"Address {self._account_config.trading_injective_address} is in the OFAC list")

transaction = Transaction()
transaction.with_messages(*messages_for_transaction)
transaction.with_sequence(self._client.get_sequence())
Expand Down
6 changes: 6 additions & 0 deletions pyinjective/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ class ConvertError(PyInjectiveError):

class SchemaError(PyInjectiveError):
pass

class BannedAddressError(PyInjectiveError):
pass

class OfacListFetchError(PyInjectiveError):
pass
48 changes: 25 additions & 23 deletions pyinjective/proto/google/api/client_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75433cf

Please sign in to comment.