@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
88 lines • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryAccountResponse = void 0;
var utils_1 = require("../../../utils");
/**
*
*
* @class QueryAccountResponse
*/
var QueryAccountResponse = /** @class */ (function () {
/**
* Query Account Response.
* @constructor
* @param {object} account - Current account object.
*/
function QueryAccountResponse(address, balance, publicKey) {
this.address = address;
this.balance = balance;
this.publicKey = publicKey;
if (!this.isValid()) {
throw new TypeError("Invalid QueryAccountResponse properties.");
}
}
/**
*
* Creates a QueryAccountResponse object using a JSON string
* @param {String} json - JSON string.
* @returns {QueryAccountResponse} - QueryAccountResponse object.
* @memberof QueryAccountResponse
*/
QueryAccountResponse.fromJSON = function (json) {
try {
var rawObjValue = JSON.parse(json);
if (rawObjValue === null) {
throw new Error("Account doesn't exist in the world state.");
}
var balance = "0";
var coins = rawObjValue.coins;
if (coins && utils_1.typeGuard(coins, Array) && coins.length === 1) {
var coinObj = coins[0];
balance = coinObj.amount || "0";
}
return new QueryAccountResponse(rawObjValue.address, balance, rawObjValue.public_key);
}
catch (error) {
throw error;
}
};
/**
*
* Creates a JSON object with the QueryAccountResponse properties
* @returns {JSON} - JSON Object.
* @memberof QueryAccountResponse
*/
QueryAccountResponse.prototype.toJSON = function () {
return {
"address": this.address,
"coins": [
{
"amount": this.balance,
"denom": "upokt"
}
],
"public_key": this.publicKey
};
};
/**
*
* Check if the QueryAccountResponse object is valid
* @returns {boolean} - True or false.
* @memberof QueryAccountResponse
*/
QueryAccountResponse.prototype.isValid = function () {
var validAddress = true;
var validPubKey = true;
if (this.address) {
validAddress = utils_1.validateAddressHex(this.address) === undefined ? true : false;
}
if (this.publicKey) {
validPubKey = utils_1.validatePublicKey(this.publicKey);
}
return validAddress === true &&
validPubKey === true;
};
return QueryAccountResponse;
}());
exports.QueryAccountResponse = QueryAccountResponse;
//# sourceMappingURL=query-account-response.js.map