pegasys-orchestrate
Version:
The PegaSys Orchestrate library provides convenient access to the Codefi Orchestrate API from applications written in server-side JavaScript
89 lines (88 loc) • 3.63 kB
JavaScript
;
// tslint:disable: no-console
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerContractHandler = exports.getTagsHandler = exports.getContractHandler = exports.getCatalogHandler = void 0;
const fs_1 = require("fs");
const client_1 = require("../../client");
function getCatalogHandler(options) {
return __awaiter(this, void 0, void 0, function* () {
const registry = new client_1.OrchestrateClient(options.endpoint);
try {
const catalog = yield registry.getContractsCatalog();
console.log(catalog);
}
catch (error) {
console.log(`Failed to get catalog: ${error}`);
}
});
}
exports.getCatalogHandler = getCatalogHandler;
function getContractHandler(options) {
return __awaiter(this, void 0, void 0, function* () {
const registry = new client_1.OrchestrateClient(options.endpoint);
try {
const contract = yield registry.getContract(options.name, options.tag);
console.log(contract);
}
catch (error) {
console.log(`Failed to get contract: ${error}`);
}
});
}
exports.getContractHandler = getContractHandler;
function getTagsHandler(options) {
return __awaiter(this, void 0, void 0, function* () {
const registry = new client_1.OrchestrateClient(options.endpoint);
try {
const tags = yield registry.getContractTags(options.name);
console.log(tags);
}
catch (error) {
console.log(`Failed to get tags: ${error}`);
}
});
}
exports.getTagsHandler = getTagsHandler;
function registerContractHandler(options) {
return __awaiter(this, void 0, void 0, function* () {
const registry = new client_1.OrchestrateClient(options.endpoint);
let artifact;
try {
artifact = JSON.parse(fs_1.readFileSync(options.filepath).toString());
checkArtifact(artifact);
yield registry.registerContract({
name: options.name,
tag: options.tag,
abi: artifact.abi,
bytecode: artifact.bytecode,
deployedBytecode: artifact.deployedBytecode
});
console.log('Contract successfully registered');
}
catch (error) {
console.log(`Failed to register contract: ${error}`);
}
});
}
exports.registerContractHandler = registerContractHandler;
const checkArtifact = (artifact) => {
if (!artifact.abi || typeof artifact.abi !== 'object') {
throw new Error('No abi in artifact');
}
if (!artifact.deployedBytecode || typeof artifact.deployedBytecode !== 'string') {
throw new Error('No deployedBytecode in artifact');
}
if (!artifact.bytecode || typeof artifact.bytecode !== 'string') {
throw new Error('No bytecode in artifact');
}
return true;
};