diff --git a/examples/create_modify_cancel_skip_nonce.py b/examples/create_modify_cancel_skip_nonce.py index f0a20e8..0bacde7 100644 --- a/examples/create_modify_cancel_skip_nonce.py +++ b/examples/create_modify_cancel_skip_nonce.py @@ -1,17 +1,20 @@ import asyncio + +from lighter.nonce_manager import NonceManagerType from utils import default_example_setup async def main(): - client, api_client, _ = default_example_setup() + # noop nonce manager + client, api_client, _ = default_example_setup(nonce_management_type=NonceManagerType.NONE) client.check_client() # Note: change this to 2048 to trade spot ETH. Make sure you have at least 0.1 ETH to trade spot. market_index = 0 # create order - api_key_index, base_nonce = client.nonce_manager.next_nonce() - nonce_interval = 1000 + api_key_index, base_nonce = 4, 22 # can't use client.nonce_manager.next_nonce() + nonce_interval = 3 tx, tx_hash, err = await client.create_order( market_index=market_index, client_order_index=123, diff --git a/examples/utils.py b/examples/utils.py index 8ae2f29..3c63a41 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -30,7 +30,7 @@ def get_api_key_config(config_file="./api_key_config.json"): return cfg["baseUrl"], cfg["accountIndex"], private_key -def default_example_setup(config_file="./api_key_config.json") -> Optional[Tuple[lighter.SignerClient, lighter.ApiClient, websockets.connect]]: +def default_example_setup(config_file="./api_key_config.json", nonce_management_type=lighter.nonce_manager.NonceManagerType.OPTIMISTIC) -> Optional[Tuple[lighter.SignerClient, lighter.ApiClient, websockets.connect]]: logging.basicConfig(level=logging.DEBUG) base_url, account_index, private_keys = get_api_key_config(config_file) @@ -39,6 +39,7 @@ def default_example_setup(config_file="./api_key_config.json") -> Optional[Tuple url=base_url, account_index=account_index, api_private_keys=private_keys, + nonce_management_type=nonce_management_type, ) err = client.check_client() diff --git a/lighter/__init__.py b/lighter/__init__.py index b00a28b..fd7a3d5 100644 --- a/lighter/__init__.py +++ b/lighter/__init__.py @@ -225,3 +225,4 @@ PaperPosition, PaperTrade, ) +from lighter.nonce_manager import NonceManagerType diff --git a/lighter/nonce_manager.py b/lighter/nonce_manager.py index c2c6db6..aedb260 100644 --- a/lighter/nonce_manager.py +++ b/lighter/nonce_manager.py @@ -97,9 +97,36 @@ def next_nonce(self, api_key: Optional[int] = None) -> Tuple[int, int]: return api_key, nonce +class NoOpNonceManager(NonceManager): + """For users who provide their own nonces (skip_nonce mode).""" + # noinspection PyMissingConstructor + def __init__(self, account_index, api_client, api_keys_list): + # Skip super().__init__() to avoid the HTTP call + self.account_index = account_index + self.api_client = api_client + self.api_keys_list = api_keys_list + self.nonce = {} + self.current = 0 + + def next_nonce(self, api_key=None): + raise ValidationError( + "NoOpNonceManager does not manage nonces. " + "You must provide nonce and api_key_index explicitly." + ) + + def acknowledge_failure(self, api_key): + pass # no-op + + def refresh_nonce(self, api_key): + pass # no-op + + def hard_refresh_nonce(self, api_key): + pass # no-op + class NonceManagerType(enum.Enum): OPTIMISTIC = 1 API = 2 + NONE = 3 def nonce_manager_factory( @@ -120,4 +147,10 @@ def nonce_manager_factory( api_client=api_client, api_keys_list=api_keys_list, ) + elif nonce_manager_type == NonceManagerType.NONE: + return NoOpNonceManager( + account_index=account_index, + api_client=api_client, + api_keys_list=api_keys_list, + ) raise ValidationError("invalid nonce manager type") diff --git a/lighter/signers/lighter-signer-darwin-arm64.dylib b/lighter/signers/lighter-signer-darwin-arm64.dylib index 20b2830..8e22156 100644 Binary files a/lighter/signers/lighter-signer-darwin-arm64.dylib and b/lighter/signers/lighter-signer-darwin-arm64.dylib differ diff --git a/lighter/signers/lighter-signer-linux-amd64.so b/lighter/signers/lighter-signer-linux-amd64.so index 3ddaff4..33612f7 100644 Binary files a/lighter/signers/lighter-signer-linux-amd64.so and b/lighter/signers/lighter-signer-linux-amd64.so differ diff --git a/lighter/signers/lighter-signer-linux-arm64.so b/lighter/signers/lighter-signer-linux-arm64.so index 88e9694..685a1ba 100644 Binary files a/lighter/signers/lighter-signer-linux-arm64.so and b/lighter/signers/lighter-signer-linux-arm64.so differ diff --git a/lighter/signers/lighter-signer-windows-amd64.dll b/lighter/signers/lighter-signer-windows-amd64.dll index a2a54e3..8beb35b 100644 Binary files a/lighter/signers/lighter-signer-windows-amd64.dll and b/lighter/signers/lighter-signer-windows-amd64.dll differ