Skip to content

Commit 5079f78

Browse files
committed
feat(artillery, auto-topup): implement auto top-up functionality for ledger balance
test command: ``` NETWORK=naga-staging bun run artillery:init --auto-topu ```
1 parent 8d44c6e commit 5079f78

File tree

1 file changed

+93
-30
lines changed

1 file changed

+93
-30
lines changed

e2e/artillery/src/init.ts

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,71 @@
1-
import '../../src/helper/supressLogs';
21
import {
32
createAuthManager,
43
storagePlugins,
54
ViemAccountAuthenticator,
65
} from '@lit-protocol/auth';
7-
import * as StateManager from './StateManager';
86
import { createLitClient } from '@lit-protocol/lit-client';
9-
import { getOrCreatePkp } from '../../../e2e/src/helper/pkp-utils';
107
import * as NetworkManager from '../../../e2e/src/helper/NetworkManager';
8+
import { getOrCreatePkp } from '../../../e2e/src/helper/pkp-utils';
9+
import '../../src/helper/supressLogs';
1110
import * as AccountManager from '../src/AccountManager';
11+
import * as StateManager from './StateManager';
1212

1313
const _network = process.env['NETWORK'];
1414

15+
const AUTO_TOP_UP_FLAG = '--auto-topup';
16+
const args = process.argv.slice(2);
17+
1518
// CONFIGURATIONS
1619
const REJECT_BALANCE_THRESHOLD = 0;
1720
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+
};
1869

1970
(async () => {
2071
// -- Start
@@ -43,32 +94,11 @@ const LEDGER_MINIMUM_BALANCE = 10000;
4394
);
4495
}
4596

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+
});
72102

73103
// 3. Authenticate the master account and store the auth data
74104
const masterAccountAuthData = await StateManager.getOrUpdate(
@@ -145,5 +175,38 @@ const LEDGER_MINIMUM_BALANCE = 10000;
145175

146176
// console.log('✅ PKP Sign Test Result:', res);
147177

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+
}
149212
})();

0 commit comments

Comments
 (0)