pchain-dapp-sdk-js
Version:
ParallelChain Mainnet DApp JavaScript SDK
297 lines (296 loc) • 14.6 kB
JavaScript
"use strict";
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PChainReader = void 0;
var bn_js_1 = __importDefault(require("bn.js"));
var pchain_client_js_1 = require("pchain-client-js");
var pchain_types_js_1 = require("pchain-types-js");
var encoding_1 = require("./utils/encoding");
var PChainReader = /** @class */ (function () {
function PChainReader(endpoint) {
this.pchain_client = new pchain_client_js_1.Client(endpoint);
}
//
// Address-related queries
//
PChainReader.prototype.getAccountBalance = function (address) {
return __awaiter(this, void 0, void 0, function () {
var account;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccount(address, false)];
case 1:
account = _a.sent();
if (account === null) {
// an account not associated with any transfer conceptually has zero balance
return [2 /*return*/, new bn_js_1.default(0)];
}
return [2 /*return*/, account.balance];
}
});
});
};
PChainReader.prototype.getAccountNonce = function (address) {
return __awaiter(this, void 0, void 0, function () {
var account;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccount(address, false)];
case 1:
account = _a.sent();
if (account === null) {
// an account which is not associated with any user action conceptually has a zero nonce
return [2 /*return*/, new bn_js_1.default(0)];
}
return [2 /*return*/, account.nonce];
}
});
});
};
PChainReader.prototype.getContractCode = function (contractAddress) {
return __awaiter(this, void 0, void 0, function () {
var contractAccount, contract;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccount(contractAddress, true)];
case 1:
contractAccount = _a.sent();
contract = contractAccount.contract;
if (contract.value === null || !contract) {
return [2 /*return*/, null];
}
return [2 /*return*/, contract.value];
}
});
});
};
//
// Block-related queries
//
PChainReader.prototype.getBlock = function (id) {
return __awaiter(this, void 0, void 0, function () {
var blockHash, blockHashResponse;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(id instanceof bn_js_1.default)) return [3 /*break*/, 2];
return [4 /*yield*/, this.pchain_client.block_hash_by_height(new pchain_types_js_1.BlockHashByHeightRequest({
block_height: id,
}))];
case 1:
blockHashResponse = (_a.sent()).block_hash;
if (blockHashResponse.value === null) {
throw new Error("Unknown block number");
}
blockHash = blockHashResponse.value;
return [3 /*break*/, 3];
case 2:
if (typeof id === "string") {
blockHash = new pchain_types_js_1.Sha256Hash((0, encoding_1.base64UrlToBase64)(id));
}
else if (id instanceof pchain_types_js_1.Sha256Hash) {
blockHash = id;
}
else {
throw new Error("Invalid block identifier");
}
_a.label = 3;
case 3: return [2 /*return*/, this.getBlockByHash(blockHash)];
}
});
});
};
PChainReader.prototype.getLatestCommittedBlock = function () {
return __awaiter(this, void 0, void 0, function () {
var blockHashResponse, blockHash, block;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.pchain_client.highest_committed_block()];
case 1:
blockHashResponse = (_a.sent()).block_hash;
if (blockHashResponse.value === null) {
throw new Error("Error fetching highest committed block");
}
blockHash = blockHashResponse.value;
return [4 /*yield*/, this.getBlockByHash(blockHash)];
case 2:
block = _a.sent();
if (block === null) {
throw new Error("Error fetching highest committed block");
}
return [2 /*return*/, block];
}
});
});
};
//
// Transaction-related queries
//
PChainReader.prototype.getTransaction = function (hash) {
return __awaiter(this, void 0, void 0, function () {
var transactionResponse;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (typeof hash === "string") {
hash = new pchain_types_js_1.Sha256Hash(hash);
}
return [4 /*yield*/, this.pchain_client.transaction(new pchain_types_js_1.TransactionRequest({ transaction_hash: hash, include_receipt: true }))];
case 1:
transactionResponse = _a.sent();
// only consider a Transaction valid if all relevant fields are present
if (transactionResponse.transaction.value == null ||
transactionResponse.receipt.value == null ||
transactionResponse.block_hash.value == null ||
transactionResponse.position.value == null) {
return [2 /*return*/, null];
}
return [2 /*return*/, {
transaction: transactionResponse.transaction.value,
receipt: transactionResponse.receipt.value,
block_hash: transactionResponse.block_hash.value,
position: transactionResponse.position.value,
}];
}
});
});
};
//
// private methods
//
PChainReader.prototype.getAccount = function (account_address, includeContract) {
return __awaiter(this, void 0, void 0, function () {
var response, target_account, _a, _b, _c, addr, acct, accountWithContract, accountWithoutContract, account;
var e_1, _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
if (typeof account_address == "string") {
account_address = new pchain_types_js_1.PublicAddress((0, encoding_1.base64UrlToBase64)(account_address));
}
if (account_address.toBytes().length != 32) {
throw Error("Account input is not a valid 32-byte PublicAddress or equivalent string");
}
return [4 /*yield*/, this.pchain_client.state(new pchain_types_js_1.StateRequest({
accounts: new Set([account_address]),
include_contract: includeContract,
storage_keys: new Map(),
}))];
case 1:
response = _e.sent();
if (!response.accounts) {
throw new Error("Error fetching account");
}
target_account = null;
try {
for (_a = __values(response.accounts), _b = _a.next(); !_b.done; _b = _a.next()) {
_c = __read(_b.value, 2), addr = _c[0], acct = _c[1];
if (addr.value.compare(account_address.value) === 0) {
target_account = acct;
break;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
if (target_account === null) {
return [2 /*return*/, null];
}
accountWithContract = target_account.accountWithContract, accountWithoutContract = target_account.accountWithoutContract;
account = accountWithContract || accountWithoutContract;
if (!account) {
throw new Error("Error fetching account"); // malformed response
}
return [2 /*return*/, account];
}
});
});
};
PChainReader.prototype.getBlockByHash = function (blockHash) {
return __awaiter(this, void 0, void 0, function () {
var block_response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.pchain_client.block(new pchain_types_js_1.BlockRequest({
block_hash: blockHash,
}))];
case 1:
block_response = _a.sent();
return [2 /*return*/, block_response.block.value];
}
});
});
};
return PChainReader;
}());
exports.PChainReader = PChainReader;