Skip to content

Commit 77a7ad7

Browse files
refactore: improve ensureBalances with warnOnlyRlc option for RLC balance
- Add warnOnlyRlc parameter to ensureBalances function - Always check native assets (required for transaction fees) - Allow RLC balance warnings instead of blocking deployment - Maintain strict RLC checking for app execution - Improve error messages with better separation of concerns - Use warnOnlyRlc: true in deploy.ts for flexible deployment
1 parent e5b86b7 commit 77a7ad7

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

cli/src/cli-helpers/ensureBalances.ts

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export async function ensureBalances({
88
iexec,
99
nRlcMin,
1010
weiMin,
11+
warnOnlyRlc = false,
1112
}: {
1213
spinner: Spinner;
1314
iexec: IExec;
1415
nRlcMin?: BN;
1516
weiMin?: BN;
17+
warnOnlyRlc?: boolean;
1618
}): Promise<{
1719
wei: BN;
1820
nRLC: BN;
@@ -34,45 +36,72 @@ export async function ensureBalances({
3436
(chainId !== 134 && totalRlc.isZero()) ||
3537
(!!nRlcMin && totalRlc.lt(nRlcMin));
3638

37-
if (!missingNative && !missingRlc) {
38-
return {
39-
wei,
40-
nRLC,
41-
stake,
42-
};
43-
}
44-
45-
const helpers = [];
39+
// Always check native assets - they're required for transaction fees
4640
if (missingNative) {
4741
const msg = wei.isZero()
4842
? ' - Native balance is empty'
4943
: ' - Native balance is insufficient';
5044
const requirement = weiMin
5145
? ` (requires ${utils.formatEth(weiMin as BN)} ether)`
5246
: '';
53-
helpers.push(`${msg}${requirement}`);
47+
48+
spinner.log(
49+
warnBox(`Current chain requires native asset to pay transaction fees!
50+
51+
Your wallet balance is insufficient:
52+
${msg}${requirement}
53+
54+
You can either:
55+
- Fund your wallet ${emphasis(address)}
56+
- Import another wallet (run ${command('iapp wallet import')})
57+
- Select an imported wallet (run ${command('iapp wallet select')})
58+
- Use another chain (use option ${command('--chain <name>')})`)
59+
);
60+
throw Error(`Native balance is insufficient`);
5461
}
62+
63+
// For RLC, either warn only or block based on warnOnlyRlc option
5564
if (missingRlc) {
56-
const msg = totalRlc.isZero()
57-
? ' - RLC balance is empty'
58-
: ' - RLC balance is insufficient';
59-
const requirement = nRlcMin
60-
? ` (requires ${utils.formatRLC(nRlcMin as BN)} RLC)`
61-
: '';
62-
helpers.push(`${msg}${requirement}`);
63-
}
65+
if (warnOnlyRlc) {
66+
// Just warn for RLC, don't block
67+
const msg = totalRlc.isZero()
68+
? ' - RLC balance is empty'
69+
: ' - RLC balance is insufficient';
70+
const requirement = nRlcMin
71+
? ` (requires ${utils.formatRLC(nRlcMin as BN)} RLC)`
72+
: '';
73+
74+
spinner.warn(
75+
`⚠️ Warning: Your wallet has insufficient RLC balance:${msg}${requirement}. You'll need RLC to run your iApp later. Consider funding your wallet ${emphasis(address)} or importing another wallet.`
76+
);
77+
} else {
78+
// Block for RLC (original behavior)
79+
const msg = totalRlc.isZero()
80+
? ' - RLC balance is empty'
81+
: ' - RLC balance is insufficient';
82+
const requirement = nRlcMin
83+
? ` (requires ${utils.formatRLC(nRlcMin as BN)} RLC)`
84+
: '';
6485

65-
spinner.log(
66-
warnBox(`Current chain requires ${chainId !== 134 ? 'native asset to pay transaction fees and ' : ''}RLC to pay iApp runs!
86+
spinner.log(
87+
warnBox(`Current chain requires RLC to pay iApp runs!
6788
6889
Your wallet balance is insufficient:
69-
${helpers.join('\n')}
90+
${msg}${requirement}
7091
7192
You can either:
7293
- Fund your wallet ${emphasis(address)}
7394
- Import another wallet (run ${command('iapp wallet import')})
7495
- Select an imported wallet (run ${command('iapp wallet select')})
7596
- Use another chain (use option ${command('--chain <name>')})`)
76-
);
77-
throw Error(`Balance is insufficient`);
97+
);
98+
throw Error(`RLC balance is insufficient`);
99+
}
100+
}
101+
102+
return {
103+
wei,
104+
nRLC,
105+
stake,
106+
};
78107
}

cli/src/cmd/deploy.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { hintBox } from '../cli-helpers/box.js';
2222
import { addDeploymentData } from '../utils/cacheExecutions.js';
2323
import { deployTdxApp, getIExecTdx } from '../utils/tdx-poc.js';
2424
import { useTdx } from '../utils/featureFlags.js';
25+
import { ensureBalances } from '../cli-helpers/ensureBalances.js';
2526
import { warnBeforeTxFees } from '../cli-helpers/warnBeforeTxFees.js';
2627

2728
export async function deploy({ chain }: { chain?: string }) {
@@ -45,21 +46,8 @@ export async function deploy({ chain }: { chain?: string }) {
4546
iexec = getIExec({ ...chainConfig, signer });
4647
}
4748

48-
// Check balance and show warning if no RLC
49-
const chainId = await iexec.config.resolveChainId();
50-
const address = await iexec.wallet.getAddress();
51-
const [{ nRLC }, { stake }] = await Promise.all([
52-
iexec.wallet.checkBalances(address),
53-
iexec.account.checkBalance(address),
54-
]);
55-
56-
const totalRlc = stake.add(nRLC);
57-
58-
if (totalRlc.isZero() && chainId !== 134) {
59-
spinner.warn(
60-
`⚠️ Warning: Your wallet has no RLC balance. You'll need RLC to run your iApp later. Consider funding your wallet ${address} or importing another wallet.`
61-
);
62-
}
49+
// Check balances: always verify native assets for transaction fees, but only warn for RLC
50+
await ensureBalances({ spinner, iexec, warnOnlyRlc: true });
6351

6452
const dockerhubUsername = await askForDockerhubUsername({ spinner });
6553
const dockerhubAccessToken = await askForDockerhubAccessToken({ spinner });

0 commit comments

Comments
 (0)