UNPKG

@crustio/crust-pin

Version:

Decentralized pin on Crust Network

69 lines (68 loc) 2.79 kB
"use strict"; 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.sendTx = void 0; const keyring_1 = require("@polkadot/keyring"); /** * Send tx to Crust Network * @param {SubmittableExtrinsic} tx substrate-style tx * @param {string} seeds tx already been sent */ function sendTx(tx, seeds) { return __awaiter(this, void 0, void 0, function* () { console.log('⛓ Send tx to chain...'); const krp = loadKeyringPair(seeds); return new Promise((resolve, reject) => { tx.signAndSend(krp, ({ events = [], status }) => { console.log(` ↪ 💸 Transaction status: ${status.type}, nonce: ${tx.nonce}`); if (status.isInvalid || status.isDropped || status.isUsurped || status.isRetracted) { reject(new Error('Invalid transaction')); } else { // Pass it } if (status.isInBlock) { events.forEach(({ event: { method, section } }) => { if (section === 'system' && method === 'ExtrinsicFailed') { // Error with no detail, just return error console.error(` ↪ ❌ Send transaction(${tx.type}) failed.`); resolve(false); } else if (method === 'ExtrinsicSuccess') { console.log(` ↪ ✅ Send transaction(${tx.type}) success.`); resolve(true); } }); } else { // Pass it } }).catch(e => { reject(e); }); }); }); } exports.sendTx = sendTx; /** * Load keyring pair with seeds * @param {string} seeds */ function loadKeyringPair(seeds) { const kr = new keyring_1.Keyring({ type: 'sr25519', }); const krp = kr.addFromUri(seeds); return krp; }