Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/create_modify_cancel_skip_nonce.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions lighter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,4 @@
PaperPosition,
PaperTrade,
)
from lighter.nonce_manager import NonceManagerType
33 changes: 33 additions & 0 deletions lighter/nonce_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Binary file modified lighter/signers/lighter-signer-darwin-arm64.dylib
Binary file not shown.
Binary file modified lighter/signers/lighter-signer-linux-amd64.so
Binary file not shown.
Binary file modified lighter/signers/lighter-signer-linux-arm64.so
Binary file not shown.
Binary file modified lighter/signers/lighter-signer-windows-amd64.dll
Binary file not shown.
Loading