@@ -6,63 +6,54 @@ import {
66import { createLitClient } from '@lit-protocol/lit-client' ;
77import * as NetworkManager from '../../../e2e/src/helper/NetworkManager' ;
88import { getOrCreatePkp } from '../../../e2e/src/helper/pkp-utils' ;
9+ import { printAligned } from '../../../e2e/src/helper/utils' ;
910import '../../src/helper/supressLogs' ;
1011import * as AccountManager from '../src/AccountManager' ;
1112import * as StateManager from './StateManager' ;
1213
1314const _network = process . env [ 'NETWORK' ] ;
1415
15- const AUTO_TOP_UP_FLAG = '--auto-topup' ;
16- const args = process . argv . slice ( 2 ) ;
17-
1816// CONFIGURATIONS
1917const REJECT_BALANCE_THRESHOLD = 0 ;
2018const LEDGER_MINIMUM_BALANCE = 10000 ;
21- const AUTO_TOP_UP_ENABLED = args . includes ( AUTO_TOP_UP_FLAG ) ;
22- const AUTO_TOP_UP_INTERVAL = 10_000 ;
23- const AUTO_TOP_UP_THRESHOLD = LEDGER_MINIMUM_BALANCE ;
2419
2520if ( Number . isNaN ( LEDGER_MINIMUM_BALANCE ) || LEDGER_MINIMUM_BALANCE < 0 ) {
2621 throw new Error ( '❌ LEDGER_MINIMUM_BALANCE must be a non-negative number' ) ;
2722}
2823
29- const ensureLedgerThreshold = async ( {
30- paymentManager ,
31- accountAddress ,
24+ const ensureLedgerBalance = async ( {
25+ label ,
26+ balanceFetcher ,
3227 minimumBalance,
28+ topUp,
3329} : {
34- paymentManager : Awaited <
35- ReturnType < typeof AccountManager . getAccountDetails >
36- > [ 'paymentManager' ] ;
37- accountAddress : `0x${string } `;
30+ label : string ;
31+ balanceFetcher : ( ) => Promise < { availableBalance : string } > ;
3832 minimumBalance : number ;
33+ topUp : ( difference : number ) => Promise < void > ;
3934} ) => {
40- const { availableBalance } = await paymentManager . getBalance ( {
41- userAddress : accountAddress ,
42- } ) ;
35+ const { availableBalance } = await balanceFetcher ( ) ;
4336
4437 const currentAvailable = Number ( availableBalance ) ;
4538
4639 if ( currentAvailable >= minimumBalance ) {
40+ console . log (
41+ `✅ ${ label } ledger balance healthy (${ currentAvailable } ETH, threshold ${ minimumBalance } ETH)`
42+ ) ;
4743 return currentAvailable ;
4844 }
4945
50- const diff = minimumBalance - currentAvailable ;
46+ const difference = minimumBalance - currentAvailable ;
5147
5248 console . log (
53- `🚨 Live Master Account Ledger Balance (${ currentAvailable } ) is below threshold (${ minimumBalance } ). Depositing ${ difference } ETH.`
49+ `🚨 ${ label } ledger balance (${ currentAvailable } ETH ) is below threshold (${ minimumBalance } ETH ). Depositing ${ difference } ETH.`
5450 ) ;
5551
56- await paymentManager . deposit ( {
57- amountInEth : diff . toString ( ) ,
58- } ) ;
52+ await topUp ( difference ) ;
5953
60- const { availableBalance : postTopUpBalance } =
61- await paymentManager . getBalance ( {
62- userAddress : accountAddress ,
63- } ) ;
54+ const { availableBalance : postTopUpBalance } = await balanceFetcher ( ) ;
6455
65- console . log ( '✅ New Live Master Account Payment Balance:' , postTopUpBalance ) ;
56+ console . log ( `✅ ${ label } ledger balance after top-up: ${ postTopUpBalance } ETH` ) ;
6657
6758 return Number ( postTopUpBalance ) ;
6859} ;
@@ -94,10 +85,18 @@ const ensureLedgerThreshold = async ({
9485 ) ;
9586 }
9687
97- await ensureLedgerThreshold ( {
98- paymentManager : masterAccountDetails . paymentManager ,
99- accountAddress : masterAccount . address ,
88+ await ensureLedgerBalance ( {
89+ label : 'Master Account' ,
90+ balanceFetcher : ( ) =>
91+ masterAccountDetails . paymentManager . getBalance ( {
92+ userAddress : masterAccount . address ,
93+ } ) ,
10094 minimumBalance : LEDGER_MINIMUM_BALANCE ,
95+ topUp : async ( difference ) => {
96+ await masterAccountDetails . paymentManager . deposit ( {
97+ amountInEth : difference . toString ( ) ,
98+ } ) ;
99+ } ,
101100 } ) ;
102101
103102 // 3. Authenticate the master account and store the auth data
@@ -130,6 +129,58 @@ const ensureLedgerThreshold = async ({
130129
131130 console . log ( '✅ Master Account PKP:' , masterAccountPkp ) ;
132131
132+ const pkpEthAddress = masterAccountPkp ?. ethAddress ;
133+
134+ if ( ! pkpEthAddress ) {
135+ throw new Error ( '❌ Master Account PKP is missing an ethAddress' ) ;
136+ }
137+
138+ const pkpLedgerBalance = await masterAccountDetails . paymentManager . getBalance (
139+ {
140+ userAddress : pkpEthAddress ,
141+ }
142+ ) ;
143+
144+ console . log ( '\n========== Master Account PKP Details ==========' ) ;
145+
146+ const pkpStatus =
147+ Number ( pkpLedgerBalance . availableBalance ) < 0
148+ ? {
149+ label : '🚨 Status:' ,
150+ value : `Negative balance (debt): ${ pkpLedgerBalance . availableBalance } ` ,
151+ }
152+ : { label : '' , value : '' } ;
153+
154+ printAligned (
155+ [
156+ { label : '🔑 PKP ETH Address:' , value : pkpEthAddress } ,
157+ {
158+ label : '💳 Ledger Total Balance:' ,
159+ value : pkpLedgerBalance . totalBalance ,
160+ } ,
161+ {
162+ label : '💳 Ledger Available Balance:' ,
163+ value : pkpLedgerBalance . availableBalance ,
164+ } ,
165+ pkpStatus ,
166+ ] . filter ( ( item ) => item . label )
167+ ) ;
168+
169+ await ensureLedgerBalance ( {
170+ label : 'Master Account PKP' ,
171+ balanceFetcher : ( ) =>
172+ masterAccountDetails . paymentManager . getBalance ( {
173+ userAddress : pkpEthAddress ,
174+ } ) ,
175+ minimumBalance : LEDGER_MINIMUM_BALANCE ,
176+ topUp : async ( difference ) => {
177+ await masterAccountDetails . paymentManager . depositForUser ( {
178+ userAddress : pkpEthAddress ,
179+ amountInEth : difference . toString ( ) ,
180+ } ) ;
181+ } ,
182+ } ) ;
183+
133184 // create pkp auth context
134185 // const masterAccountPkpAuthContext = await authManager.createPkpAuthContext({
135186 // authData: masterAccountAuthData,
@@ -175,38 +226,5 @@ const ensureLedgerThreshold = async ({
175226
176227 // console.log('✅ PKP Sign Test Result:', res);
177228
178- if ( AUTO_TOP_UP_ENABLED ) {
179- console . log (
180- `\n✅ Auto top-up enabled. Monitoring every ${ AUTO_TOP_UP_INTERVAL } ms with threshold ${ AUTO_TOP_UP_THRESHOLD } ETH. Press Ctrl+C to exit.`
181- ) ;
182-
183- let isTopUpInProgress = false ;
184-
185- const poll = async ( ) => {
186- if ( isTopUpInProgress ) {
187- return ;
188- }
189-
190- isTopUpInProgress = true ;
191-
192- try {
193- await ensureLedgerThreshold ( {
194- paymentManager : masterAccountDetails . paymentManager ,
195- accountAddress : masterAccount . address ,
196- minimumBalance : AUTO_TOP_UP_THRESHOLD ,
197- } ) ;
198- } catch ( error ) {
199- console . error ( '❌ Auto top-up check failed:' , error ) ;
200- } finally {
201- isTopUpInProgress = false ;
202- }
203- } ;
204-
205- await poll ( ) ;
206- setInterval ( ( ) => {
207- void poll ( ) ;
208- } , AUTO_TOP_UP_INTERVAL ) ;
209- } else {
210- process . exit ( ) ;
211- }
229+ process . exit ( ) ;
212230} ) ( ) ;
0 commit comments