Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

BEC-270 | Implement import-receipt #23

Merged
merged 4 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"create-boilerplate": "ts-node src/create-boilerplate.ts",
"create-config": "ts-node src/create-config.ts",
"import-receipt": "ts-node src/import-receipt.ts",
"deploy-all": "yarn deploy-airnode; yarn deploy-airkeeper",
"deploy-airkeeper": "yarn ts-node src/deploy-airkeeper",
"deploy-airnode": "yarn ts-node src/deploy-airnode",
Expand Down
61 changes: 61 additions & 0 deletions src/import-receipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { existsSync, mkdirSync, rmSync } from 'fs';
import { join } from 'path';
import { PromptObject } from 'prompts';
import { OperationsRepository } from './types';
import { runAndHandleErrors } from './utils/cli';
import { promptQuestions } from './utils/prompts';
import { readJsonFile, readOperationsRepository } from './utils/read-operations';
import { writeOperationsRepository } from './utils/write-operations';

const questions = (operationsRepository: OperationsRepository): PromptObject[] => {
return [
{
type: 'autocomplete',
name: 'name',
message: 'Which API Integration is the receipt for?',
choices: Object.keys(operationsRepository.apis).map((api) => ({ title: api, value: api })),
},
{
type: 'confirm',
name: 'receipt',
message: 'Have you placed the "receipt.json" in the "/import" folder?',
initial: true,
},
];
};

const main = async () => {
const importPath = join(__dirname, '..', 'import');
if (!existsSync(importPath)) mkdirSync(importPath, { recursive: true });

const operationsRepository = readOperationsRepository();
const response = await promptQuestions(questions(operationsRepository));
if (!response.receipt)
return console.log('Please place the "receipt.json" in the "/import" folder and re-run the command.');

const receipt = readJsonFile(join(importPath, 'receipt.json'));

const updatedOpsData = {
...operationsRepository,
apis: {
...operationsRepository.apis,
[response.name]: {
...operationsRepository.apis[response.name],
apiMetadata: {
...operationsRepository.apis[response.name].apiMetadata,
airnode: receipt.airnodeWallet.airnodeAddress,
xpub: receipt.airnodeWallet.airnodeXpub,
},
},
},
};

console.log(`🔑 HTTP Gateway API Url: ${receipt.api.httpGatewayUrl} (Please keep a note)`);
console.log(`🔑 HTTP Signed Data Gateway API Url: ${receipt.api.httpSignedDataGatewayUrl} (Please keep a note)`);
console.log(`Succesfully imported receipt for ${response.name}`);

writeOperationsRepository(updatedOpsData);
rmSync(importPath, { recursive: true });
};

runAndHandleErrors(main);