UNPKG

@api3/contracts

Version:

Contracts through which API3 services are delivered

146 lines 6.11 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable no-console */ const ethers = __importStar(require("ethers")); const yargs_1 = __importDefault(require("yargs")); const helpers_1 = require("yargs/helpers"); const index_1 = require("./index"); const COMMON_COMMAND_ARGUMENTS = { dappAlias: { type: 'string', demandOption: true, describe: 'dApp alias as specified in https://github.com/api3dao/contracts/tree/main/data/dapps', }, chainId: { type: 'string', demandOption: true, describe: 'Chain ID', }, dapiName: { type: 'string', demandOption: true, describe: 'dAPI (data feed) name as it appears on https://market.api3.org/', }, strict: { type: 'boolean', default: true, describe: 'Requires validation steps to pass to print the proxy address', }, }; const { dappAlias, chainId, dapiName, strict } = COMMON_COMMAND_ARGUMENTS; // From https://github.com/api3dao/data-feeds/blob/main/packages/api3-market/src/utils/format.ts const slugify = (text) => text.toLowerCase().replaceAll(/[^\da-z-]+/g, '-'); (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) .command('print-api3readerproxyv1-address', 'Prints the dApp-specific Api3ReaderProxyV1 address', { 'dapp-alias': dappAlias, 'chain-id': chainId, 'dapi-name': dapiName, strict, }, async (args) => { const chain = index_1.CHAINS.find((chain) => chain.id === args['chain-id']); if (!chain) { console.error(`Chain with ID ${args['chain-id']} is not known`); process.exit(1); } console.log(`dApp alias: ${args['dapp-alias']}\nchain: ${chain.name}\ndAPI name: ${args['dapi-name']}`); const dappInfo = index_1.DAPPS.find((dapp) => Object.keys(dapp.aliases).includes(args['dapp-alias'])); if (!dappInfo) { const message = `⚠️ Could not find any record for alias "${args['dapp-alias']}"`; if (args['strict']) { console.error(message); process.exit(1); } console.warn(message); } if (dappInfo) { const [, aliasInfo] = Object.entries(dappInfo.aliases).find(([alias]) => alias === args['dapp-alias']); if (aliasInfo.description) console.log(`\nℹ️ ${aliasInfo.description}\n`); } const provider = new ethers.JsonRpcProvider(chain.providers.find((provider) => provider.alias === 'default').rpcUrl); const api3ServerV1 = new ethers.Contract(index_1.deploymentAddresses['Api3ServerV1'][chain.id], index_1.Api3ServerV1__factory.abi, provider); let timestamp; try { [, timestamp] = await api3ServerV1.readDataFeedWithDapiNameHash(ethers.keccak256(ethers.encodeBytes32String(args['dapi-name']))); } catch (error) { const message = '⚠️ Attempted to read the feed and failed'; if (args['strict']) { console.error(message); console.error(error.message); process.exit(1); } console.warn(message); } if (timestamp && timestamp + BigInt(24 * 60 * 60) < Date.now() / 1000) { const message = `⚠️ Feed timestamp (${new Date(Number(timestamp) * 1000).toISOString()}) appears to be older than a day`; if (args['strict']) { console.error(message); process.exit(1); } console.warn(message); } const proxyAddress = (0, index_1.computeDappSpecificApi3ReaderProxyV1Address)(args['dapp-alias'], args['chain-id'], args['dapi-name']); let code; try { code = await provider.getCode(proxyAddress); } catch (error) { const message = '⚠️ Attempted to check if the proxy has been deployed and failed'; if (args['strict']) { console.error(message); console.error(error.message); process.exit(1); } console.warn(message); } if (code && code === '0x') { const message = '⚠️ Proxy does not appear to have been deployed'; if (args['strict']) { console.error(message); process.exit(1); } console.warn(message); } const marketUrl = `https://market.api3.org/${chain.alias}/${slugify(args['dapi-name'])}`; console.log(`• Please confirm that ${marketUrl} points to an active feed.`); console.log(`• Your proxy is at ${chain.explorer.browserUrl}address/${proxyAddress}\nPlease confirm that there is a contract deployed at this address before using it.`); }) .help().argv; //# sourceMappingURL=cli.js.map