Skip to content

Commit 51e56b8

Browse files
authored
Merge pull request #360 from api3dao/stricter-cli-command
Stricter CLI command
2 parents 62ec5e8 + 4087faf commit 51e56b8

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@api3/contracts': patch
3+
---
4+
5+
Have the print-api3readerproxyv1-address CLI command print better looking errors

.changeset/tame-avocados-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@api3/contracts': patch
3+
---
4+
5+
Have the print-api3readerproxyv1-address CLI command require strict validation by default

src/cli.ts

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import { CHAINS } from '@api3/chains';
23
import * as ethers from 'ethers';
34
import yargs from 'yargs';
@@ -26,9 +27,14 @@ const COMMON_COMMAND_ARGUMENTS = {
2627
demandOption: true,
2728
describe: 'dAPI (data feed) name as it appears on https://market.api3.org/',
2829
},
30+
strict: {
31+
type: 'boolean',
32+
default: true,
33+
describe: 'Requires validation steps to pass to print the proxy address',
34+
},
2935
} as const;
3036

31-
const { dappAlias, chainId, dapiName } = COMMON_COMMAND_ARGUMENTS;
37+
const { dappAlias, chainId, dapiName, strict } = COMMON_COMMAND_ARGUMENTS;
3238

3339
// From https://github.com/api3dao/data-feeds/blob/main/packages/api3-market/src/utils/format.ts
3440
const slugify = (text: string) => text.toLowerCase().replaceAll(/[^\da-z-]+/g, '-');
@@ -41,13 +47,14 @@ yargs(hideBin(process.argv))
4147
'dapp-alias': dappAlias,
4248
'chain-id': chainId,
4349
'dapi-name': dapiName,
50+
strict,
4451
},
4552
async (args) => {
4653
const chain = CHAINS.find((chain) => chain.id === args['chain-id']);
4754
if (!chain) {
48-
throw new Error(`Chain with ID ${args['chain-id']} is not known`);
55+
console.error(`Chain with ID ${args['chain-id']} is not known`);
56+
process.exit(1);
4957
}
50-
// eslint-disable-next-line no-console
5158
console.log(`dApp alias: ${args['dapp-alias']}\nchain: ${chain.name}\ndAPI name: ${args['dapi-name']}`);
5259
const provider = new ethers.JsonRpcProvider(
5360
chain.providers.find((provider) => provider.alias === 'default')!.rpcUrl
@@ -57,39 +64,57 @@ yargs(hideBin(process.argv))
5764
Api3ServerV1__factory.abi,
5865
provider
5966
) as unknown as Api3ServerV1;
67+
let timestamp;
6068
try {
61-
const [, timestamp] = await api3ServerV1.readDataFeedWithDapiNameHash(
69+
[, timestamp] = await api3ServerV1.readDataFeedWithDapiNameHash(
6270
ethers.keccak256(ethers.encodeBytes32String(args['dapi-name']))
6371
);
64-
if (timestamp! + BigInt(24 * 60 * 60) < Date.now() / 1000) {
65-
// eslint-disable-next-line no-console
66-
console.warn('⚠️ Feed timestamp appears to be stale');
72+
} catch (error) {
73+
const message = '⚠️ Attempted to read the feed and failed';
74+
if (args['strict']) {
75+
console.error(message);
76+
console.error((error as Error).message);
77+
process.exit(1);
78+
}
79+
console.warn(message);
80+
}
81+
if (timestamp && timestamp + BigInt(24 * 60 * 60) < Date.now() / 1000) {
82+
const message = `⚠️ Feed timestamp (${new Date(Number(timestamp) * 1000).toISOString()}) appears to be older than a day`;
83+
if (args['strict']) {
84+
console.error(message);
85+
process.exit(1);
6786
}
68-
} catch {
69-
// eslint-disable-next-line no-console
70-
console.warn('⚠️ Attempted to read the feed and failed');
87+
console.warn(message);
7188
}
7289
const proxyAddress = computeDappSpecificApi3ReaderProxyV1Address(
7390
args['dapp-alias'],
7491
args['chain-id'],
7592
args['dapi-name']
7693
);
94+
let code;
7795
try {
78-
const code = await provider.getCode(proxyAddress);
79-
if (code === '0x') {
80-
// eslint-disable-next-line no-console
81-
console.warn('⚠️ Proxy appears to not have been deployed');
96+
code = await provider.getCode(proxyAddress);
97+
} catch (error) {
98+
const message = '⚠️ Attempted to check if the proxy has been deployed and failed';
99+
if (args['strict']) {
100+
console.error(message);
101+
console.error((error as Error).message);
102+
process.exit(1);
103+
}
104+
console.warn(message);
105+
}
106+
if (code && code === '0x') {
107+
const message = '⚠️ Proxy does not appear to have been deployed';
108+
if (args['strict']) {
109+
console.error(message);
110+
process.exit(1);
82111
}
83-
} catch {
84-
// eslint-disable-next-line no-console
85-
console.warn('⚠️ Attempted to check if the proxy has been deployed and failed');
112+
console.warn(message);
86113
}
87114
const marketUrl = `https://market.api3.org/${chain.alias}/${slugify(args['dapi-name'])}`;
88-
// eslint-disable-next-line no-console
89115
console.log(`• Please confirm that ${marketUrl} points to an active feed.`);
90-
// eslint-disable-next-line no-console
91116
console.log(
92-
`• Your proxy address is ${chain.explorer.browserUrl}address/${proxyAddress}\nPlease confirm that there is a contract deployed at this address before using it.`
117+
`• Your proxy is at ${chain.explorer.browserUrl}address/${proxyAddress}\nPlease confirm that there is a contract deployed at this address before using it.`
93118
);
94119
}
95120
)

0 commit comments

Comments
 (0)