js-databox
Version:
databox & metabox
201 lines (200 loc) • 6.85 kB
JavaScript
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());
});
};
import { idlFactory } from "./did/metabox";
import { Actor } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { getToAccountIdentifier, getUint8ArrayFromHex } from "../utils";
export const mb_cid = "zbzr7-xyaaa-aaaan-qadeq-cai";
export class MetaBox {
constructor(agent) {
this.metaBoxCai = mb_cid;
this.agent = agent;
this.MetaBoxActor = Actor.createActor(idlFactory, { agent, canisterId: this.metaBoxCai });
}
isNotFirstDataBox() {
return __awaiter(this, void 0, void 0, function* () {
try {
const Actor = this.MetaBoxActor;
return yield Actor.isNotFirstDataBox();
}
catch (e) {
throw e;
}
});
}
get_accountID() {
return __awaiter(this, void 0, void 0, function* () {
const principal = yield this.agent.getPrincipal();
return getToAccountIdentifier(Principal.from(this.metaBoxCai), principal);
});
}
createBoxFree(arg) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
const Actor = this.MetaBoxActor;
const Arg = {
'metadata': arg
};
const res = yield Actor.createDataBoxFree(Arg);
if (Object.keys(res)[0] === "ok")
return resolve(res.ok);
else
reject(`${Object.keys(res.err)[0]}`);
}
catch (e) {
reject(e);
}
}));
});
}
createBoxFee(arg, is_need_refresh) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
const Actor = this.MetaBoxActor;
const Arg = {
'metadata': arg
};
const res = yield Actor.createDataBoxFee(Arg, is_need_refresh);
if (Object.keys(res)[0] === "ok")
return resolve(res.ok);
else
reject(`${Object.keys(res.err)[0]}`);
}
catch (e) {
reject(e);
}
}));
});
}
getICP() {
return __awaiter(this, void 0, void 0, function* () {
try {
const Actor = this.MetaBoxActor;
const res = yield Actor.getIcp();
return Number(res);
}
catch (e) {
throw e;
}
});
}
getBoxes(principal) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.getBoxes(principal);
}
catch (e) {
throw e;
}
});
}
deleteBox(delBoxArgs) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.deleteBox(delBoxArgs);
}
catch (e) {
throw e;
}
});
}
transferDataboxOwner(canister_id, to) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.transferDataboxOwner(canister_id, to);
}
catch (e) {
throw e;
}
});
}
startBox(boxInfo) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.MetaBoxActor.startBox(boxInfo);
}
catch (e) {
throw e;
}
});
}
topUpBox(TopUpArgs) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.topUpBox(TopUpArgs);
}
catch (e) {
throw e;
}
});
}
upgradeBox(UpgradeBoxArgs) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.upgradeBox(UpgradeBoxArgs);
}
catch (e) {
throw e;
}
});
}
getDataBoxVersion() {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.getDataBoxVersion();
}
catch (e) {
throw e;
}
});
}
updateBoxInfo(BoxInfo__1) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.MetaBoxActor.updateBoxInfo(BoxInfo__1);
}
catch (e) {
throw e;
}
});
}
createDataBoxControl(arg, is_need_refresh, controller) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
const Actor = this.MetaBoxActor;
const arg_0 = {
metadata: arg
};
const res = yield Actor.createDataBoxControl(arg_0, is_need_refresh, controller ? [controller] : []);
if (Object.keys(res)[0] === "ok")
return resolve(res.ok);
else
reject(`${Object.keys(res.err)[0]}`);
}
catch (e) {
throw e;
}
}));
}
withDrawICP(to, amount) {
return __awaiter(this, void 0, void 0, function* () {
try {
const Actor = this.MetaBoxActor;
const tmp1 = getUint8ArrayFromHex(to);
return yield Actor.transferOutICP(tmp1, BigInt(amount * 1e8));
}
catch (e) {
throw e;
}
});
}
}