Skip to content

Commit cc72532

Browse files
author
balogh.adam@icloud.com
committed
keep 1
1 parent 62e8d43 commit cc72532

6 files changed

Lines changed: 60 additions & 254 deletions

File tree

docs/opengradient/client/llm.md

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Supports both streaming and non-streaming responses.
2121
All request methods (``chat``, ``completion``) are async.
2222

2323
Before making LLM requests, ensure your wallet has approved sufficient
24-
OPG tokens for Permit2 spending by calling ``ensure_opg_allowance`` or ``approve_opg``.
24+
OPG tokens for Permit2 spending by calling ``ensure_opg_allowance``.
2525

2626
#### Constructor
2727

@@ -58,32 +58,6 @@ resolves TEEs from the on-chain registry.
5858

5959
---
6060

61-
#### `approve_opg()`
62-
63-
```python
64-
def approve_opg(self, opg_amountfloat) ‑> [Permit2ApprovalResult](./opg_token)
65-
```
66-
Approve Permit2 to spend ``opg_amount`` OPG tokens.
67-
68-
Always sends an approval transaction regardless of the current allowance.
69-
70-
**Arguments**
71-
72-
* **`opg_amount`**: Number of OPG tokens to approve (e.g. ``0.1``
73-
for 0.1 OPG). Must be at least 0.01 OPG.
74-
75-
**Returns**
76-
77-
Permit2ApprovalResult: Contains ``allowance_before``,
78-
``allowance_after``, and ``tx_hash``.
79-
80-
**Raises**
81-
82-
* **`ValueError`**: If the OPG amount is less than 0.01.
83-
* **`RuntimeError`**: If the approval transaction fails.
84-
85-
---
86-
8761
#### `chat()`
8862

8963
```python

integrationtest/llm/test_llm_chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def llm_client():
9999
print("Account funded with ETH and OPG")
100100

101101
llm = og.LLM(private_key=account.key.hex())
102-
llm.approve_opg(opg_amount=OPG_FUND_AMOUNT)
102+
llm.ensure_opg_allowance(min_allowance=OPG_FUND_AMOUNT, approve_amount=OPG_FUND_AMOUNT)
103103
print("Permit2 approval complete")
104104

105105
# Wait for the approval to propagate on-chain

src/opengradient/client/llm.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from x402.mechanisms.evm.upto.register import register_upto_evm_client
1616

1717
from ..types import TEE_LLM, StreamChoice, StreamChunk, StreamDelta, TextGenerationOutput, x402SettlementMode
18-
from .opg_token import Permit2ApprovalResult, approve_opg, ensure_opg_allowance
18+
from .opg_token import Permit2ApprovalResult, ensure_opg_allowance
1919
from .tee_connection import RegistryTEEConnection, StaticTEEConnection, TEEConnectionInterface
2020
from .tee_registry import TEERegistry
2121

@@ -58,7 +58,7 @@ class LLM:
5858
All request methods (``chat``, ``completion``) are async.
5959
6060
Before making LLM requests, ensure your wallet has approved sufficient
61-
OPG tokens for Permit2 spending by calling ``ensure_opg_allowance`` or ``approve_opg``.
61+
OPG tokens for Permit2 spending by calling ``ensure_opg_allowance``.
6262
6363
Usage:
6464
# Via on-chain registry (default)
@@ -182,27 +182,6 @@ async def _call_with_tee_retry(
182182

183183
# ── Public API ──────────────────────────────────────────────────────
184184

185-
def approve_opg(self, opg_amount: float) -> Permit2ApprovalResult:
186-
"""Approve Permit2 to spend ``opg_amount`` OPG tokens.
187-
188-
Always sends an approval transaction regardless of the current allowance.
189-
190-
Args:
191-
opg_amount: Number of OPG tokens to approve (e.g. ``0.1``
192-
for 0.1 OPG). Must be at least 0.01 OPG.
193-
194-
Returns:
195-
Permit2ApprovalResult: Contains ``allowance_before``,
196-
``allowance_after``, and ``tx_hash``.
197-
198-
Raises:
199-
ValueError: If the OPG amount is less than 0.01.
200-
RuntimeError: If the approval transaction fails.
201-
"""
202-
if opg_amount < 0.01:
203-
raise ValueError("OPG amount must be at least 0.01.")
204-
return approve_opg(self._wallet_account, opg_amount)
205-
206185
def ensure_opg_allowance(
207186
self,
208187
min_allowance: float,

src/opengradient/client/opg_token.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
"stateMutability": "nonpayable",
4040
"type": "function",
4141
},
42+
{
43+
"inputs": [{"name": "account", "type": "address"}],
44+
"name": "balanceOf",
45+
"outputs": [{"name": "", "type": "uint256"}],
46+
"stateMutability": "view",
47+
"type": "function",
48+
},
4249
]
4350

4451

@@ -137,35 +144,6 @@ def _get_web3_and_contract():
137144
return w3, token, spender
138145

139146

140-
def approve_opg(wallet_account: LocalAccount, opg_amount: float) -> Permit2ApprovalResult:
141-
"""Approve Permit2 to spend ``opg_amount`` OPG tokens.
142-
143-
Always sends an approval transaction regardless of the current allowance.
144-
145-
Example::
146-
147-
result = approve_opg(wallet, 5.0)
148-
149-
Args:
150-
wallet_account: The wallet account to approve from.
151-
opg_amount: Number of OPG tokens to approve (e.g. ``5.0`` for 5 OPG).
152-
Converted to base units (18 decimals) internally.
153-
154-
Returns:
155-
Permit2ApprovalResult: Contains ``allowance_before``,
156-
``allowance_after``, and ``tx_hash``.
157-
158-
Raises:
159-
RuntimeError: If the approval transaction fails.
160-
"""
161-
amount_base = int(opg_amount * 10**18)
162-
163-
w3, token, spender = _get_web3_and_contract()
164-
owner = Web3.to_checksum_address(wallet_account.address)
165-
166-
return _send_approve_tx(wallet_account, w3, token, owner, spender, amount_base)
167-
168-
169147
def ensure_opg_allowance(
170148
wallet_account: LocalAccount,
171149
min_allowance: float,
@@ -222,6 +200,15 @@ def ensure_opg_allowance(
222200
allowance_after=allowance_before,
223201
)
224202

203+
balance = token.functions.balanceOf(owner).call()
204+
if approve_base > balance:
205+
logger.warning(
206+
"Requested approve_amount (%.6f OPG) exceeds wallet balance (%.6f OPG), capping approval to wallet balance",
207+
approve_amount,
208+
balance / 10**18,
209+
)
210+
approve_base = balance
211+
225212
logger.info(
226213
"Permit2 allowance below minimum threshold (%s < %s), approving %s base units",
227214
allowance_before,

tests/llm_test.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,17 +488,6 @@ async def test_tools_with_stream_falls_back_to_single_chunk(self, fake_http):
488488
assert chunks[0].choices[0].finish_reason == "tool_calls"
489489

490490

491-
# ── approve_opg tests ────────────────────────────────────────
492-
493-
494-
class TestEnsureOpgApproval:
495-
def test_rejects_amount_below_minimum(self, fake_http):
496-
llm = _make_llm()
497-
498-
with pytest.raises(ValueError, match="at least"):
499-
llm.approve_opg(opg_amount=0.01)
500-
501-
502491
# ── Lifecycle tests ──────────────────────────────────────────────────
503492

504493

0 commit comments

Comments
 (0)