Space Operation
The following is a example of Space operation:
/*
* @Description: js-sdk for space demo
* @Autor: cess lab
*/
const { Space, InitAPI, Common, testnetConfig, wellKnownAcct } = require("cess-js-sdk");
async function main() {
const { api, keyring } = await InitAPI(testnetConfig);
const { addr, mnemonic } = wellKnownAcct;
const space = new Space(api, keyring);
const common = new Common(api, keyring);
console.log("query userOwnedSpace:");
let result = await space.userOwnedSpace(addr);
const blockHeight = await common.queryBlockHeight();
result = common.formatSpaceInfo(result.data, blockHeight);
console.log(result);
function getDataIfOk(result) {
return result.msg === "ok" ? result.data : result;
}
if (result.totalSpace) {
console.log("expansionSpace:");
result = await space.expansionSpace(mnemonic, 1);
console.log(getDataIfOk(result), "\n");
console.log("renewalSpace:");
result = await space.renewalSpace(mnemonic, 1);
console.log(getDataIfOk(result), "\n");
} else {
console.log("buySpace:");
result = await space.buySpace(mnemonic, 1);
console.log(getDataIfOk(result), "\n");
}
console.log("query userOwnedSpace:");
result = await space.userOwnedSpace(addr);
result = common.formatSpaceInfo(result.data, blockHeight);
console.log(result);
}
main()
.catch(console.error)
.finally(() => process.exit());
Last updated