3131from .messages import get_message_status
3232from .payment import (
3333 compute_required_balance ,
34+ compute_required_credit_balance ,
3435 compute_required_flow ,
3536 fetch_balance_of_address ,
37+ fetch_credit_balance_of_address ,
3638 get_stream ,
3739)
3840from .pubsub import PubSub
@@ -187,44 +189,71 @@ async def check_payment(pool: VmPool):
187189 pool .forget_vm (vm_hash )
188190
189191 # Check if the balance held in the wallet is sufficient holder tier resources (Not do it yet)
190- for sender , chains in pool .get_executions_by_sender (payment_type = PaymentType .hold ).items ():
192+ for execution_address , chains in pool .get_executions_by_address (payment_type = PaymentType .hold ).items ():
191193 for chain , executions in chains .items ():
192194 executions = [execution for execution in executions if execution .is_confidential ]
193195 if not executions :
194196 continue
195- balance = await fetch_balance_of_address (sender )
197+ balance = await fetch_balance_of_address (execution_address )
196198
197199 # Stop executions until the required balance is reached
198200 required_balance = await compute_required_balance (executions )
199- logger .debug (f"Required balance for Sender { sender } executions: { required_balance } , { executions } " )
201+ logger .debug (
202+ f"Required balance for Sender { execution_address } executions: { required_balance } , { executions } "
203+ )
200204 # Stop executions until the required balance is reached
201205 while executions and balance < (required_balance + settings .PAYMENT_BUFFER ):
202206 last_execution = executions .pop (- 1 )
203207 logger .debug (f"Stopping { last_execution } due to insufficient balance" )
204208 await pool .stop_vm (last_execution .vm_hash )
205209 required_balance = await compute_required_balance (executions )
210+
206211 community_wallet = await get_community_wallet_address ()
207212 if not community_wallet :
208213 logger .error ("Monitor payment ERROR: No community wallet set. Cannot check community payment" )
209214
215+ # Check if the credit balance held in the wallet is sufficient credit tier resources (Not do it yet)
216+ for execution_address , chains in pool .get_executions_by_address (payment_type = PaymentType .credit ).items ():
217+ for chain , executions in chains .items ():
218+ executions = [execution for execution in executions ]
219+ if not executions :
220+ continue
221+ balance = await fetch_credit_balance_of_address (execution_address )
222+
223+ # Stop executions until the required credits are reached
224+ required_credits = await compute_required_credit_balance (executions )
225+ logger .debug (
226+ f"Required credit balance for Address { execution_address } executions: { required_credits } , { executions } "
227+ )
228+ # Stop executions until the required credits are reached
229+ while executions and balance < (required_credits + settings .PAYMENT_BUFFER ):
230+ last_execution = executions .pop (- 1 )
231+ logger .debug (f"Stopping { last_execution } due to insufficient credit balance" )
232+ await pool .stop_vm (last_execution .vm_hash )
233+ required_credits = await compute_required_credit_balance (executions )
234+
210235 # Check if the balance held in the wallet is sufficient stream tier resources
211- for sender , chains in pool .get_executions_by_sender (payment_type = PaymentType .superfluid ).items ():
236+ for execution_address , chains in pool .get_executions_by_address (payment_type = PaymentType .superfluid ).items ():
212237 for chain , executions in chains .items ():
213238 try :
214- stream = await get_stream (sender = sender , receiver = settings .PAYMENT_RECEIVER_ADDRESS , chain = chain )
239+ stream = await get_stream (
240+ sender = execution_address , receiver = settings .PAYMENT_RECEIVER_ADDRESS , chain = chain
241+ )
215242
216243 logger .debug (
217- f"Stream flow from { sender } to { settings .PAYMENT_RECEIVER_ADDRESS } = { stream } { chain .value } "
244+ f"Stream flow from { execution_address } to { settings .PAYMENT_RECEIVER_ADDRESS } = { stream } { chain .value } "
218245 )
219246 except ValueError as error :
220- logger .error (f"Error found getting stream for chain { chain } and sender { sender } : { error } " )
247+ logger .error (f"Error found getting stream for chain { chain } and sender { execution_address } : { error } " )
221248 continue
222249 try :
223- community_stream = await get_stream (sender = sender , receiver = community_wallet , chain = chain )
224- logger .debug (f"Stream flow from { sender } to { community_wallet } (community) : { stream } { chain } " )
250+ community_stream = await get_stream (sender = execution_address , receiver = community_wallet , chain = chain )
251+ logger .debug (
252+ f"Stream flow from { execution_address } to { community_wallet } (community) : { stream } { chain } "
253+ )
225254
226255 except ValueError as error :
227- logger .error (f"Error found getting stream for chain { chain } and sender { sender } : { error } " )
256+ logger .error (f"Error found getting stream for chain { chain } and sender { execution_address } : { error } " )
228257 continue
229258
230259 while executions :
@@ -249,7 +278,7 @@ async def check_payment(pool: VmPool):
249278 )
250279 required_community_stream = format_cost (required_stream * COMMUNITY_STREAM_RATIO )
251280 logger .debug (
252- f"Stream for senders { sender } { len (executions )} executions. CRN : { stream } / { required_crn_stream } ."
281+ f"Stream for senders { execution_address } { len (executions )} executions. CRN : { stream } / { required_crn_stream } ."
253282 f"Community: { community_stream } / { required_community_stream } "
254283 )
255284 # Can pay all executions
@@ -259,7 +288,7 @@ async def check_payment(pool: VmPool):
259288 break
260289 # Stop executions until the required stream is reached
261290 last_execution = executions .pop (- 1 )
262- logger .info (f"Stopping { last_execution } of { sender } due to insufficient stream" )
291+ logger .info (f"Stopping { last_execution } of { execution_address } due to insufficient stream" )
263292 await pool .stop_vm (last_execution .vm_hash )
264293
265294
0 commit comments