@energyweb/node-red-contrib-green-proof-worker
Version:
## Peer dependencies
49 lines (48 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.voteOnPolkadot = void 0;
const tslib_1 = require("tslib");
const polkadotApi = tslib_1.__importStar(require("@polkadot/api"));
const polkadotUtilCrypto = tslib_1.__importStar(require("@polkadot/util-crypto"));
const voteOnPolkadot = async (params) => {
const workerSeed = params.connection.workerSeed;
const votingUrl = params.connection.votingUrl;
const namespace = params.connection.solutionNamespace;
const resultHash = polkadotUtilCrypto.blake2AsHex(params.vote);
const wsProvider = new polkadotApi.WsProvider(votingUrl, false);
const keyring = new polkadotApi.Keyring({ type: 'sr25519' }).addFromUri(workerSeed);
const publicKey = keyring.publicKey;
const signature = keyring.sign(resultHash);
await wsProvider.connect();
const api = await polkadotApi.ApiPromise.create({ provider: wsProvider, throwOnConnect: true, throwOnUnknown: true });
const utx = api.tx.workerNodePallet.submitSolutionResult(namespace, params.votingId, resultHash, signature, publicKey);
const { txHash } = await new Promise((resolve, reject) => {
let unsub;
utx.send((result) => {
if (result.status.isFinalized) {
unsub();
const failedEvents = result.events.filter(({ event }) => api.events.system.ExtrinsicFailed.is(event));
if (failedEvents.length === 0) {
resolve({ txHash: result.toHuman().status.Finalized });
}
else {
const { event: { data: [originalError] } } = failedEvents[0];
const error = originalError;
if (error.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(error.asModule);
const { docs, method, section } = decoded;
reject(new Error(`Transaction failed: ${section}.${method}: ${docs.join(' ')}`));
}
else {
// Other, CannotLookup, BadOrigin, no extra info
reject(new Error(`Transaction failed ${error.toString()}`));
}
}
}
}).then(r => unsub = r).catch(reject);
});
await api.disconnect();
return { votingTxHash: txHash };
};
exports.voteOnPolkadot = voteOnPolkadot;