forked from crustio/ipfs-crust-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
72 lines (64 loc) · 1.78 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { Keyring } = require("@polkadot/keyring");
/* PUBLIC METHODS */
/**
* Check seeds(12 words) legality
* @param {string} seeds
* @returns boolean
*/
function checkSeeds(seeds) {
return seeds.split(" ").length === 12;
}
/**
* Send tx to Crust Network
* @param {import('@polkadot/api/types').SubmittableExtrinsic} tx
* @param {string} seeds 12 secret words
* @returns Promise<boolean> send tx success or failed
*/
async function sendTx(tx, seeds) {
// 1. Load keyring
console.log("⛓ Sending tx to chain...");
const krp = loadKeyringPair(seeds);
// 2. Send tx to chain
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 failed");
resolve(false);
} else if (method === "ExtrinsicSuccess") {
console.log(" ↪ ✅ Send transaction success.");
resolve(true);
}
});
} else {
// Pass it
}
}).catch((e) => {
reject(e);
});
});
}
/* PRIVATE METHODS */
/**
* Load keyring pair with seeds
* @param {string} seeds
*/
function loadKeyringPair(seeds) {
const kr = new Keyring({
type: "sr25519",
});
const krp = kr.addFromUri(seeds);
return krp;
}
module.exports = {
checkSeeds,
sendTx,
};