|
1 | | -import '../../src/helper/supressLogs'; |
2 | 1 | import { |
3 | 2 | createAuthManager, |
4 | 3 | storagePlugins, |
5 | 4 | ViemAccountAuthenticator, |
6 | 5 | } from '@lit-protocol/auth'; |
7 | | -import * as StateManager from './StateManager'; |
8 | 6 | import { createLitClient } from '@lit-protocol/lit-client'; |
9 | | -import { getOrCreatePkp } from '../../../e2e/src/helper/pkp-utils'; |
10 | 7 | import * as NetworkManager from '../../../e2e/src/helper/NetworkManager'; |
| 8 | +import { getOrCreatePkp } from '../../../e2e/src/helper/pkp-utils'; |
| 9 | +import '../../src/helper/supressLogs'; |
11 | 10 | import * as AccountManager from '../src/AccountManager'; |
| 11 | +import * as StateManager from './StateManager'; |
12 | 12 |
|
13 | 13 | const _network = process.env['NETWORK']; |
14 | 14 |
|
| 15 | +const AUTO_TOP_UP_FLAG = '--auto-topup'; |
| 16 | +const args = process.argv.slice(2); |
| 17 | + |
15 | 18 | // CONFIGURATIONS |
16 | 19 | const REJECT_BALANCE_THRESHOLD = 0; |
17 | 20 | const 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; |
| 24 | + |
| 25 | +if (Number.isNaN(LEDGER_MINIMUM_BALANCE) || LEDGER_MINIMUM_BALANCE < 0) { |
| 26 | + throw new Error('❌ LEDGER_MINIMUM_BALANCE must be a non-negative number'); |
| 27 | +} |
| 28 | + |
| 29 | +const ensureLedgerThreshold = async ({ |
| 30 | + paymentManager, |
| 31 | + accountAddress, |
| 32 | + minimumBalance, |
| 33 | +}: { |
| 34 | + paymentManager: Awaited< |
| 35 | + ReturnType<typeof AccountManager.getAccountDetails> |
| 36 | + >['paymentManager']; |
| 37 | + accountAddress: `0x${string}`; |
| 38 | + minimumBalance: number; |
| 39 | +}) => { |
| 40 | + const { availableBalance } = await paymentManager.getBalance({ |
| 41 | + userAddress: accountAddress, |
| 42 | + }); |
| 43 | + |
| 44 | + const currentAvailable = Number(availableBalance); |
| 45 | + |
| 46 | + if (currentAvailable >= minimumBalance) { |
| 47 | + return currentAvailable; |
| 48 | + } |
| 49 | + |
| 50 | + const diff = minimumBalance - currentAvailable; |
| 51 | + |
| 52 | + console.log( |
| 53 | + `🚨 Live Master Account Ledger Balance (${currentAvailable}) is below threshold (${minimumBalance}). Depositing ${difference} ETH.` |
| 54 | + ); |
| 55 | + |
| 56 | + await paymentManager.deposit({ |
| 57 | + amountInEth: diff.toString(), |
| 58 | + }); |
| 59 | + |
| 60 | + const { availableBalance: postTopUpBalance } = |
| 61 | + await paymentManager.getBalance({ |
| 62 | + userAddress: accountAddress, |
| 63 | + }); |
| 64 | + |
| 65 | + console.log('✅ New Live Master Account Payment Balance:', postTopUpBalance); |
| 66 | + |
| 67 | + return Number(postTopUpBalance); |
| 68 | +}; |
18 | 69 |
|
19 | 70 | (async () => { |
20 | 71 | // -- Start |
@@ -43,32 +94,11 @@ const LEDGER_MINIMUM_BALANCE = 10000; |
43 | 94 | ); |
44 | 95 | } |
45 | 96 |
|
46 | | - if (LEDGER_MINIMUM_BALANCE > Number(masterAccountDetails.ledgerBalance)) { |
47 | | - // find the difference between the minimum balance and the current balance |
48 | | - const difference = |
49 | | - LEDGER_MINIMUM_BALANCE - Number(masterAccountDetails.ledgerBalance); |
50 | | - |
51 | | - console.log( |
52 | | - `🚨 Live Master Account Ledger Balance is less than LEDGER_MINIMUM_BALANCE: ${LEDGER_MINIMUM_BALANCE} ETH. Attempting to top up the difference of ${difference} ETH to the master account.` |
53 | | - ); |
54 | | - |
55 | | - // deposit the difference |
56 | | - console.log( |
57 | | - '\x1b[90m✅ Depositing the difference to Live Master Account Payment Manager...\x1b[0m' |
58 | | - ); |
59 | | - await masterAccountDetails.paymentManager.deposit({ |
60 | | - amountInEth: difference.toString(), |
61 | | - }); |
62 | | - |
63 | | - // print the new balance |
64 | | - const newBalance = await masterAccountDetails.paymentManager.getBalance({ |
65 | | - userAddress: masterAccount.address, |
66 | | - }); |
67 | | - console.log( |
68 | | - '✅ New Live Master Account Payment Balance:', |
69 | | - newBalance.availableBalance |
70 | | - ); |
71 | | - } |
| 97 | + await ensureLedgerThreshold({ |
| 98 | + paymentManager: masterAccountDetails.paymentManager, |
| 99 | + accountAddress: masterAccount.address, |
| 100 | + minimumBalance: LEDGER_MINIMUM_BALANCE, |
| 101 | + }); |
72 | 102 |
|
73 | 103 | // 3. Authenticate the master account and store the auth data |
74 | 104 | const masterAccountAuthData = await StateManager.getOrUpdate( |
@@ -145,5 +175,38 @@ const LEDGER_MINIMUM_BALANCE = 10000; |
145 | 175 |
|
146 | 176 | // console.log('✅ PKP Sign Test Result:', res); |
147 | 177 |
|
148 | | - process.exit(); |
| 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 | + } |
149 | 212 | })(); |
0 commit comments