2024-05-07 10:36:59 +00:00
|
|
|
import yargs from 'yargs';
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import { hideBin } from 'yargs/helpers';
|
2024-05-08 05:43:07 +00:00
|
|
|
|
2024-05-07 10:36:59 +00:00
|
|
|
import { Registry } from '@cerc-io/registry-sdk';
|
|
|
|
|
|
|
|
import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util';
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
const argv = getArgs();
|
|
|
|
const { records: recordsDir, config } = argv;
|
|
|
|
|
|
|
|
const { services: { registry: registryConfig } } = getConfig(config as string);
|
2024-05-08 05:43:07 +00:00
|
|
|
|
|
|
|
if (registryConfig.userKey == null) {
|
|
|
|
throw new Error('userKey not set in config');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (registryConfig.bondId == null) {
|
|
|
|
throw new Error('bondId not set in config');
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:36:59 +00:00
|
|
|
const { rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig);
|
|
|
|
|
|
|
|
const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
|
|
|
|
const fee = getGasAndFees(argv, registryConfig);
|
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
// List record files
|
|
|
|
const files = fs.readdirSync(recordsDir);
|
|
|
|
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
// Read record from each JSON file
|
|
|
|
for (let i = 0; i < jsonFiles.length; i++) {
|
|
|
|
const file = jsonFiles[i];
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
const filePath = path.resolve(recordsDir, file);
|
|
|
|
const record = readRecord(filePath);
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
// Publish record
|
|
|
|
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
console.log(`Published record from ${file}`);
|
|
|
|
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
|
|
|
|
|
|
|
|
// Find the deployment record using filename
|
|
|
|
const deploymentRecordsDir = path.resolve(recordsDir, 'deployments');
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
if (fs.statSync(deploymentRecordsDir).isDirectory()) {
|
|
|
|
const recordName = path.basename(file, path.extname(file));
|
|
|
|
const deploymentRecordFile = `${recordName}Deployment.json`;
|
|
|
|
const deploymentRecordFilePath = path.resolve(deploymentRecordsDir, deploymentRecordFile);
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
// Check if deployment record exists
|
|
|
|
if (fs.existsSync(deploymentRecordFilePath)) {
|
|
|
|
console.log(`Deployment record ${deploymentRecordFile} found`);
|
|
|
|
const deploymentRecord = readRecord(deploymentRecordFilePath);
|
2024-05-07 10:36:59 +00:00
|
|
|
|
2024-05-08 05:43:07 +00:00
|
|
|
// Set watcher field to watcher record id
|
|
|
|
deploymentRecord.watcher = result.id;
|
|
|
|
|
|
|
|
// Publish deployment record
|
|
|
|
const deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee);
|
|
|
|
|
|
|
|
console.log(`Published deployment record from ${deploymentRecordFile}`);
|
|
|
|
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
|
|
|
|
}
|
2024-05-07 10:36:59 +00:00
|
|
|
}
|
2024-05-08 05:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readRecord (filePath: string): any {
|
|
|
|
let record;
|
|
|
|
try {
|
|
|
|
const data = fs.readFileSync(filePath, 'utf8');
|
|
|
|
record = JSON.parse(data);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error reading file ${filePath}:`, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return record;
|
2024-05-07 10:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getArgs (): any {
|
|
|
|
return yargs(hideBin(process.argv)).parserConfiguration({
|
|
|
|
'parse-numbers': false
|
|
|
|
}).usage('Usage: $0 [options]')
|
|
|
|
.option('config', {
|
|
|
|
alias: 'c',
|
|
|
|
describe: 'Config',
|
|
|
|
type: 'string',
|
|
|
|
demandOption: true
|
|
|
|
})
|
|
|
|
.option('records', {
|
|
|
|
alias: 'r',
|
|
|
|
describe: 'Endpoint records',
|
|
|
|
type: 'string',
|
|
|
|
demandOption: true
|
|
|
|
})
|
|
|
|
.help().argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
})
|
2024-05-07 15:34:54 +00:00
|
|
|
.finally(() => {
|
2024-05-07 10:36:59 +00:00
|
|
|
console.log('Done');
|
|
|
|
});
|