|
39 | 39 | "stateMutability": "nonpayable", |
40 | 40 | "type": "function", |
41 | 41 | }, |
| 42 | + { |
| 43 | + "inputs": [{"name": "account", "type": "address"}], |
| 44 | + "name": "balanceOf", |
| 45 | + "outputs": [{"name": "", "type": "uint256"}], |
| 46 | + "stateMutability": "view", |
| 47 | + "type": "function", |
| 48 | + }, |
42 | 49 | ] |
43 | 50 |
|
44 | 51 |
|
@@ -137,35 +144,6 @@ def _get_web3_and_contract(): |
137 | 144 | return w3, token, spender |
138 | 145 |
|
139 | 146 |
|
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 | | - |
169 | 147 | def ensure_opg_allowance( |
170 | 148 | wallet_account: LocalAccount, |
171 | 149 | min_allowance: float, |
@@ -222,6 +200,15 @@ def ensure_opg_allowance( |
222 | 200 | allowance_after=allowance_before, |
223 | 201 | ) |
224 | 202 |
|
| 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 | + |
225 | 212 | logger.info( |
226 | 213 | "Permit2 allowance below minimum threshold (%s < %s), approving %s base units", |
227 | 214 | allowance_before, |
|
0 commit comments