Skip to content

Commit fa83ed4

Browse files
adambaloghbalogh.adam@icloud.comCopilot
authored
Improved OPG allowance management methods (#209)
* opg management * tests * docs * fix check * fix check * allowance * rm allowance * tests * rm debug * simpple approval * docs * docs * keep 1 * sanity check * sanity check * checks * test * test * test * revert to old name * wait for opg transfer * abi * Update tests/opg_token_test.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: kukac <adambalogh@users.noreply.github.com> * no src import --------- Signed-off-by: kukac <adambalogh@users.noreply.github.com> Co-authored-by: balogh.adam@icloud.com <adambalogh@mac.mynetworksettings.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e5dffc6 commit fa83ed4

26 files changed

Lines changed: 373 additions & 412 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ hub = og.ModelHub(email="you@example.com", password="...")
8888

8989
### OPG Token Approval
9090

91-
Before making LLM requests, your wallet must approve OPG token spending via the [Permit2](https://github.com/Uniswap/permit2) protocol. Call this once (it's idempotent — no transaction is sent if the allowance already covers the requested amount):
91+
Before making LLM requests, your wallet must approve OPG token spending via the [Permit2](https://github.com/Uniswap/permit2) protocol. This only sends an on-chain transaction when the current allowance drops below the threshold:
9292

9393
```python
94-
llm.ensure_opg_approval(opg_amount=5)
94+
llm.ensure_opg_approval(min_allowance=5)
9595
```
9696

9797
See [Payment Settlement](#payment-settlement) for details on settlement modes.

docs/CLAUDE_SDK_USERS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import os
2121
# Create an LLM client
2222
llm = og.LLM(private_key=os.environ["OG_PRIVATE_KEY"])
2323

24-
# One-time OPG token approval (idempotent — skips if allowance already sufficient)
25-
llm.ensure_opg_approval(opg_amount=0.1)
24+
# Ensure sufficient OPG allowance (only sends tx when below threshold)
25+
llm.ensure_opg_approval(min_allowance=0.1)
2626

2727
# LLM Chat (TEE-verified with x402 payments, async)
2828
result = await llm.chat(

docs/opengradient/client/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import opengradient as og
2424

2525
# LLM inference (Base Sepolia OPG tokens)
2626
llm = og.LLM(private_key="0x...")
27-
llm.ensure_opg_approval(opg_amount=5)
27+
llm.ensure_opg_approval(min_allowance=5)
2828
result = await llm.chat(model=og.TEE_LLM.CLAUDE_HAIKU_4_5, messages=[...])
2929

3030
# On-chain model inference (OpenGradient testnet gas tokens)

docs/opengradient/client/llm.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ All request methods (``chat``, ``completion``) are async.
2222

2323
Before making LLM requests, ensure your wallet has approved sufficient
2424
OPG tokens for Permit2 spending by calling ``ensure_opg_approval``.
25-
This only sends an on-chain transaction when the current allowance is
26-
below the requested amount.
2725

2826
#### Constructor
2927

@@ -193,18 +191,30 @@ TextGenerationOutput: Generated text results including:
193191
#### `ensure_opg_approval()`
194192

195193
```python
196-
def ensure_opg_approval(self, opg_amountfloat) ‑> [Permit2ApprovalResult](./opg_token)
194+
def ensure_opg_approval(
195+
self,
196+
min_allowancefloat,
197+
approve_amount: Optional[float= None
198+
) ‑> [Permit2ApprovalResult](./opg_token)
197199
```
198-
Ensure the Permit2 allowance for OPG is at least ``opg_amount``.
200+
Ensure the Permit2 allowance stays above a minimum threshold.
201+
202+
Only sends a transaction when the current allowance drops below
203+
``min_allowance``. When approval is needed, approves ``approve_amount``
204+
(defaults to ``2 * min_allowance``) to create a buffer that survives
205+
multiple service restarts without re-approving.
206+
207+
Best for backend servers that call this on startup::
199208

200-
Checks the current Permit2 allowance for the wallet. If the allowance
201-
is already >= the requested amount, returns immediately without sending
202-
a transaction. Otherwise, sends an ERC-20 approve transaction.
209+
llm.ensure_opg_approval(min_allowance=5.0, approve_amount=100.0)
203210

204211
**Arguments**
205212

206-
* **`opg_amount`**: Minimum number of OPG tokens required (e.g. ``0.1``
207-
for 0.1 OPG). Must be at least 0.1 OPG.
213+
* **`min_allowance`**: The minimum acceptable allowance in OPG. Must be
214+
at least 0.1 OPG.
215+
* **`approve_amount`**: The amount of OPG to approve when a transaction
216+
is needed. Defaults to ``2 * min_allowance``. Must be
217+
>= ``min_allowance``.
208218

209219
**Returns**
210220

@@ -214,5 +224,6 @@ Permit2ApprovalResult: Contains ``allowance_before``,
214224

215225
**Raises**
216226

217-
* **`ValueError`**: If the OPG amount is less than 0.1.
227+
* **`ValueError`**: If ``min_allowance`` is less than 0.1 or
228+
``approve_amount`` is less than ``min_allowance``.
218229
* **`RuntimeError`**: If the approval transaction fails.

docs/opengradient/client/model_hub.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ Create a new model with the given model_name and model_desc, and a specified ver
4343

4444
* **`model_name (str)`**: The name of the model.
4545
* **`model_desc (str)`**: The description of the model.
46-
* **`version (str)`**: The version identifier (default is "1.00").
46+
* **`version (str)`**: A label used in the initial version notes (default is "1.00").
47+
* **`Note`**: the actual version string is assigned by the server.
4748

4849
**Returns**
4950

50-
dict: The server response containing model details.
51+
ModelRepository: Object containing the model name and server-assigned version string.
5152

5253
**Raises**
5354

54-
* **`CreateModelError`**: If the model creation fails.
55+
* **`RuntimeError`**: If the model creation fails.
5556

5657
---
5758

@@ -125,7 +126,7 @@ Upload a model file to the server.
125126

126127
**Returns**
127128

128-
dict: The processed result.
129+
FileUploadResult: The processed result.
129130

130131
**Raises**
131132

docs/opengradient/client/opg_token.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

docs/opengradient/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import opengradient as og
3535
llm = og.LLM(private_key="0x...")
3636

3737
# One-time OPG token approval (idempotent -- skips if allowance is sufficient)
38-
llm.ensure_opg_approval(opg_amount=5)
38+
llm.ensure_opg_approval(min_allowance=5)
3939

4040
# Chat with an LLM (TEE-verified)
4141
response = asyncio.run(llm.chat(

examples/langchain_react_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
private_key = os.environ["OG_PRIVATE_KEY"]
1919

20-
# One-time Permit2 approval for OPG spending (idempotent)
20+
# Ensure sufficient OPG allowance for Permit2 spending
2121
llm_client = og.LLM(private_key=private_key)
22-
llm_client.ensure_opg_approval(opg_amount=5)
22+
llm_client.ensure_opg_approval(min_allowance=5)
2323

2424
# Create the OpenGradient LangChain adapter
2525
llm = og.agents.langchain_adapter(

examples/llm_chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
async def main():
1212
llm = og.LLM(private_key=os.environ.get("OG_PRIVATE_KEY"))
13-
llm.ensure_opg_approval(opg_amount=0.1)
13+
llm.ensure_opg_approval(min_allowance=0.1)
1414

1515
messages = [
1616
{"role": "user", "content": "What is the capital of France?"},

examples/llm_chat_streaming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
async def main():
88
llm = og.LLM(private_key=os.environ.get("OG_PRIVATE_KEY"))
9-
llm.ensure_opg_approval(opg_amount=0.1)
9+
llm.ensure_opg_approval(min_allowance=0.1)
1010

1111
messages = [
1212
{"role": "user", "content": "What is Python?"},

0 commit comments

Comments
 (0)