@truenetworkio/sdk
Version:
True Network SDK is the abstracted interface for interacting with True Network nodes.
71 lines (70 loc) • 3.38 kB
JavaScript
;
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.runAlgo = exports.saveAlgo = void 0;
const state_1 = require("./state");
const network_1 = require("../../network");
const saveAlgo = (api, account, schemaHashes, code) => __awaiter(void 0, void 0, void 0, function* () {
// compile code to wasm.
// convert wasm to bytes.
// upload bytes to the system.
return yield new Promise((resolve, reject) => {
api.tx[state_1.ALGORITHM_PALLET_NAME]
.saveAlgo(schemaHashes, code, null)
.signAndSend(account, (result) => {
let algorithmId = -1;
result.events.forEach(({ event: { method, data } }) => {
if (method == 'AlgorithmAdded') {
const jsonData = data.toJSON();
if (jsonData) {
algorithmId = jsonData[0];
}
}
if (method == 'ExtrinsicFailed') {
reject('\nTransaction failed, error registering the algorithm.');
}
});
if (result.status.isFinalized) {
console.log(`\nTransaction finalized at blockHash ${result.status.asFinalized}`);
console.log('\nView on prism:', `${network_1.prismUrl}/query/${result.status.asFinalized}`);
if (algorithmId == -1)
throw Error(`Error registering the schema, tx: ${result.status.asFinalized}`);
resolve(algorithmId);
}
});
});
});
exports.saveAlgo = saveAlgo;
const runAlgo = (api, issuerHash, account, userId, algorithmId) => __awaiter(void 0, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
api.tx[state_1.ALGORITHM_PALLET_NAME]
.runAlgoFor(issuerHash, userId, algorithmId)
.signAndSend(account, (result) => {
let reputationScore = -1;
result.events.forEach(({ event: { method, data } }) => {
if (method == 'AlgoResult') {
const jsonData = data.toJSON();
if (jsonData) {
reputationScore = jsonData[0];
}
}
if (method == 'ExtrinsicFailed') {
reject(`\nTransaction failed, error attesting on-chain for the user. \ntx: ${result.status.hash}`);
}
});
if (result.status.isInBlock) {
console.log(`\nTransaction finalized at blockHash ${result.status.asInBlock}`);
resolve(reputationScore);
}
});
});
});
exports.runAlgo = runAlgo;