hyjs-test
Version:
first hymatrix-js sdk
91 lines (90 loc) • 3.69 kB
JavaScript
import { getHyMatrixHost } from './config';
import { BaseType, HyMatrixBase } from './types';
import { getInfo, getResult, getResults, getMessage, getMessageByNonce, getAssignByNonce, getAssignByMessage, getBalanceOfByAccid, getStakeOfByAccid, getNodes, send, getNode, getNodesByProcess, getProcesses } from './api/api';
import { createAndSignItem } from './lib';
import { getDefaultBase, mergeTags, baseToTags } from './utils';
export * from './types';
class HyMatrix extends HyMatrixBase {
constructor(config) {
var _a, _b;
super();
this._config = {
...config,
debug: (_a = config === null || config === void 0 ? void 0 : config.debug) !== null && _a !== void 0 ? _a : false,
accid: (_b = config === null || config === void 0 ? void 0 : config.accid) !== null && _b !== void 0 ? _b : ''
};
this._apiHost = ((config === null || config === void 0 ? void 0 : config.url) != null && config.url !== undefined && config.url !== '') ? config.url : getHyMatrixHost(this._config.debug);
}
async info() {
const result = await getInfo(this._apiHost);
return result;
}
async getResult(msgId) {
const result = await getResult(this._apiHost, msgId);
return result;
}
async getResults(processId, limit) {
const result = await getResults(this._apiHost, processId, limit);
return result;
}
async getMessage(msgId) {
const result = await getMessage(this._apiHost, msgId);
return result;
}
async getMessageByNonce(processId, nonce) {
const result = await getMessageByNonce(this._apiHost, processId, nonce);
return result;
}
async getAssignByNonce(processId, nonce) {
const result = await getAssignByNonce(this._apiHost, processId, nonce);
return result;
}
async getAssignByMessage(msgId) {
const result = await getAssignByMessage(this._apiHost, msgId);
return result;
}
async getNodes() {
const result = await getNodes(this._apiHost);
return result;
}
async getNode(accid) {
const accTemp = accid !== null && accid !== void 0 ? accid : this._config.accid;
const result = await getNode(this._apiHost, accTemp);
return result;
}
async getNodesByProcess(processId) {
const result = await getNodesByProcess(this._apiHost, processId);
return result;
}
async getProcesses(accid) {
const accTemp = accid !== null && accid !== void 0 ? accid : this._config.accid;
const result = await getProcesses(this._apiHost, accTemp);
return result;
}
async balanceOf(accid) {
const accTemp = accid !== null && accid !== void 0 ? accid : this._config.accid;
const result = await getBalanceOfByAccid(this._apiHost, accTemp);
return result;
}
async stakeOf(accid) {
const accTemp = accid !== null && accid !== void 0 ? accid : this._config.accid;
const result = await getStakeOfByAccid(this._apiHost, accTemp);
return result;
}
async sendMessage(params) {
const info = await this.info();
const defaultBaseMessage = getDefaultBase(BaseType.TypeMessage, info);
const msg = {
Base: defaultBaseMessage
};
const baseTags = baseToTags(msg.Base);
const msgTags = mergeTags(baseTags, params.tags);
const binary = await createAndSignItem(this._config, {
...params,
tags: msgTags
});
const result = await send(this._apiHost, binary);
return { id: result.id };
}
}
export default HyMatrix;