-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageOrder.js
48 lines (43 loc) · 1.45 KB
/
storageOrder.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
const { Keyring } = require("@polkadot/api");
const { ApiPromise, WsProvider } = require("@polkadot/api");
const { typesBundleForPolkadot } = require("@crustio/type-definitions");
const crustChainEndpoint = "wss://rpc.crust.network";
const api = new ApiPromise({
provider: new WsProvider(crustChainEndpoint),
typesBundle: typesBundleForPolkadot,
});
async function placeStorageOrder(cid, size) {
// 1. Construct place-storage-order tx
const fileCid = cid;
const fileSize = size;
const tips = 0;
const memo = "";
console.log({ fileCid, fileSize });
const tx = api.tx.market.placeStorageOrder(fileCid, fileSize, tips, memo);
// 2. Load seeds(account)
const seeds = process.env.CRUST_WALLET_SEED_PHRASE;
const kr = new Keyring({ type: "sr25519" });
console.log(seeds);
// const krp = kr.addFromUri(seeds);
const krp = kr.addFromUri(seeds);
// 3. Send transaction
await api.isReadyOrError;
return new Promise((resolve, reject) => {
tx.signAndSend(krp, ({ events = [], status }) => {
console.log(`💸 Tx status: ${status.type}, nonce: ${tx.nonce}`);
if (status.isInBlock) {
events.forEach(({ event: { method, section } }) => {
if (method === "ExtrinsicSuccess") {
console.log(`✅ Place storage order success!`);
resolve(true);
}
});
} else {
// Pass it
}
}).catch((e) => {
reject(e);
});
});
}
module.exports = placeStorageOrder;