@fanyinghao/crust-cli
Version:
The Crust command-line interface (Crust CLI) is a set of commands used to access Crust Network resources
65 lines (59 loc) • 2.3 kB
JavaScript
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { typesBundleForPolkadot } = require('@crustio/type-definitions');
const IpfsHttpClient = require('ipfs-http-client');
const { CID } = IpfsHttpClient;
const fs = require('fs');
const { chainAddr, seedsPath, ipfsTimeout } = require('./consts');
const { sendTx, parseObj } = require('./util');
module.exports = {
default: async (cid) => {
try {
// 1. Check cid locally
const cidObj = new CID(cid);
let existed = false;
let host = process.env.HOST || 'localhost';
let port = process.env.PORT || 5001;
let protocol = process.env.PROTOCOL || 'http';
let timeout = process.env.TTL || ipfsTimeout;
const ipfs = IpfsHttpClient({
host,
port,
protocol,
timeout
});
for await (const pin of ipfs.pin.ls({
paths: cidObj,
types: 'recursive'
})) {
if (cidObj.equals(pin.cid)) existed = true;
}
if (!existed) {
console.error(`Cid ${cid} don't existed, please pin it first`);
return;
}
// 2. Get file size
const objInfo = parseObj(await ipfs.object.stat(cid));
const fileSize = objInfo.CumulativeSize;
// 3. Try connect to Crust Network
const chain = new ApiPromise({
provider: new WsProvider(chainAddr),
typesBundle: typesBundleForPolkadot
});
await chain.isReadyOrError;
// 4. Load seeds info
const seeds = fs.readFileSync(seedsPath, 'utf8');
// 5. Send place storage order tx
const tx = chain.tx.market.placeStorageOrder(cid, fileSize, 0, '');
const res = await sendTx(tx, seeds);
if (res) {
console.log(`Publish ${cid} success`)
} else {
console.error('Publish failed with \'Send transaction failed\'')
}
// 6. Disconnect with chain
chain.disconnect();
} catch (e) {
console.error(`Publish failed with: ${e}`);
}
}
}