Skip to content

Commit

Permalink
Merge pull request #191 from api3dao/validate-dapi-name
Browse files Browse the repository at this point in the history
Validate provided dAPI names to the scripts
  • Loading branch information
bbenligiray authored Dec 30, 2024
2 parents 3286a19 + a3f47b8 commit 795b2d4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scripts/deploy-communal-api3readerproxyv1.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const hre = require('hardhat');
const api3Contracts = require('@api3/contracts');
const { validateDapiName } = require('./utils');

async function main() {
const dapiName = process.env.DAPI_NAME;
if (!dapiName) {
throw new Error('Environment variable DAPI_NAME is not defined');
}
validateDapiName(dapiName);
const chainId = hre.network.config.chainId;
const api3ReaderProxyV1Address = api3Contracts.computeCommunalApi3ReaderProxyV1Address(chainId, dapiName);
if ((await hre.ethers.provider.getCode(api3ReaderProxyV1Address)) === '0x') {
Expand Down
2 changes: 2 additions & 0 deletions scripts/deploy-dapp-specific-api3readerproxyv1-unsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// proxies with arbirary dApp aliases to be deployed.
const hre = require('hardhat');
const api3Contracts = require('@api3/contracts');
const { validateDapiName } = require('./utils');

// Unlike the `@api3/contracts` version, the below does not throw due to
// `dappAlias` not being recognized
Expand All @@ -20,6 +21,7 @@ async function main() {
if (!dapiName) {
throw new Error('Environment variable DAPI_NAME is not defined');
}
validateDapiName(dapiName);
const dappAlias = process.env.DAPP_ALIAS;
if (!dappAlias) {
throw new Error('Environment variable DAPP_ALIAS is not defined');
Expand Down
2 changes: 2 additions & 0 deletions scripts/deploy-dapp-specific-api3readerproxyv1.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const hre = require('hardhat');
const api3Contracts = require('@api3/contracts');
const { validateDapiName } = require('./utils');

async function main() {
const dapiName = process.env.DAPI_NAME;
if (!dapiName) {
throw new Error('Environment variable DAPI_NAME is not defined');
}
validateDapiName(dapiName);
const dappAlias = process.env.DAPP_ALIAS;
if (!dappAlias) {
throw new Error('Environment variable DAPP_ALIAS is not defined');
Expand Down
2 changes: 2 additions & 0 deletions scripts/print-communal-api3readerproxyv1-address.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const hre = require('hardhat');
const api3Contracts = require('@api3/contracts');
const { validateDapiName } = require('./utils');

async function main() {
const dapiName = process.env.DAPI_NAME;
if (!dapiName) {
throw new Error('Environment variable DAPI_NAME is not defined');
}
validateDapiName(dapiName);
const chainId = hre.network.config.chainId;
const api3ReaderProxyV1Address = api3Contracts.computeCommunalApi3ReaderProxyV1Address(chainId, dapiName);
console.log(`The address of the communal Api3ReaderProxyV1 for ${dapiName} is ${api3ReaderProxyV1Address}`);
Expand Down
2 changes: 2 additions & 0 deletions scripts/print-dapp-specific-api3readerproxyv1-address.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const hre = require('hardhat');
const api3Contracts = require('@api3/contracts');
const { validateDapiName } = require('./utils');

async function main() {
const dapiName = process.env.DAPI_NAME;
if (!dapiName) {
throw new Error('Environment variable DAPI_NAME is not defined');
}
validateDapiName(dapiName);
const dappAlias = process.env.DAPP_ALIAS;
if (!dappAlias) {
throw new Error('Environment variable DAPP_ALIAS is not defined');
Expand Down
15 changes: 15 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const api3DapiManagement = require('@api3/dapi-management');

module.exports = {
validateDapiName: (dapiName) => {
const dapi = api3DapiManagement.dapis.find((dapi) => dapi.name === dapiName);
if (!dapi) {
throw new Error(`dAPI with name ${dapiName} does not exist`);
}
if (dapi.stage === 'deprecated') {
console.warn(`dAPI with name ${dapiName} is deprecated`);
} else if (dapi.stage !== 'active') {
throw new Error(`dAPI with name ${dapiName} is not active, its current state is ${dapi.stage}`);
}
},
};

0 comments on commit 795b2d4

Please sign in to comment.