avail-js-sdk
Version:
Avail library of functions to interact with blockchain and manipulate transactions
515 lines (509 loc) • 15.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiAddress = exports.TimepointBlocknumber = exports.ProxyType = exports.SessionKeys = exports.PerDispatchClassU32 = exports.DispatchFeeModifier = exports.DispatchInfo = exports.Pays = exports.FeeDetails = exports.InclusionFee = exports.RuntimeDispatchInfo = exports.DispatchClass = exports.Weight = exports.DispatchResult = exports.TransactionalError = exports.ArithmeticError = exports.TokenError = exports.ModuleError = exports.DispatchError = exports.H256 = exports.AccountId = exports.AccountData = void 0;
const util_crypto_1 = require("@polkadot/util-crypto");
const util_1 = require("@polkadot/util");
class AccountData {
constructor(decoder) {
this.free = decoder.decodeU128();
this.reserved = decoder.decodeU128();
this.frozen = decoder.decodeU128();
this.flags = decoder.decodeU128();
}
}
exports.AccountData = AccountData;
class AccountId {
constructor(value) {
if (value.length != 32) {
throw new Error(`Failed to create AccountId. Input needs to have 32 bytes. Input has ${value.length} bytes`);
}
this.value = value;
}
static decode(decoder) {
const data = decoder.bytes(32);
return new AccountId(data);
}
static fromSS58(value) {
return new AccountId((0, util_crypto_1.decodeAddress)(value));
}
toSS58() {
return (0, util_crypto_1.encodeAddress)(this.value);
}
toHex() {
return (0, util_1.u8aToHex)(this.value);
}
toHuman() {
return this.toSS58();
}
toString() {
return this.toSS58();
}
}
exports.AccountId = AccountId;
class H256 {
constructor(value) {
if (value.length != 32) {
throw new Error(`Failed to create H256. Input needs to have 32 bytes. Input has ${value.length} bytes`);
}
this.value = value;
}
static decode(decoder) {
const data = decoder.bytes(32);
return new H256(data);
}
static fromString(value) {
if (value.startsWith("0x")) {
value = value.slice(2);
}
if (value.length != 64) {
throw new Error("Failed to create H256. Input needs to have 64 bytes");
}
return new H256((0, util_1.hexToU8a)(value));
}
toHuman() {
return this.toHex();
}
toString() {
return this.toHex();
}
toHex() {
return (0, util_1.u8aToHex)(this.value);
}
}
exports.H256 = H256;
class DispatchError {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
switch (this.variantIndex) {
case 0:
case 1:
case 2:
break;
case 3:
this.module = new ModuleError(decoder);
break;
case 4:
case 5:
case 6:
break;
case 7:
this.token = new TokenError(decoder);
break;
case 8:
this.arithmetic = new ArithmeticError(decoder);
break;
case 9:
this.transactional = new TransactionalError(decoder);
break;
case 10:
case 11:
case 12:
case 13:
break;
default:
throw new Error("Unknown DispatchError");
}
}
toString() {
var _a, _b, _c, _d;
switch (this.variantIndex) {
case 0:
return "Other";
case 1:
return "CannotLookup";
case 2:
return "BadOrigin";
case 3:
return `Module. Index: ${(_a = this.module) === null || _a === void 0 ? void 0 : _a.index}`;
case 4:
return "ConsumerRemaining";
case 5:
return "NoProviders";
case 6:
return "TooManyConsumers";
case 7:
return `Token. ${(_b = this.token) === null || _b === void 0 ? void 0 : _b.toString()}`;
case 8:
return `Arithmetic. ${(_c = this.arithmetic) === null || _c === void 0 ? void 0 : _c.toString()}`;
case 9:
return `Transactional. ${(_d = this.transactional) === null || _d === void 0 ? void 0 : _d.toString()}`;
case 10:
return "Exhausted";
case 11:
return "Corruption";
case 12:
return "Unavailable";
case 13:
return "RootNotAllowed";
default:
throw new Error("Unknown DispatchError");
}
}
}
exports.DispatchError = DispatchError;
class ModuleError {
constructor(decoder) {
this.index = decoder.decodeU8();
this.error = decoder.bytes(4);
}
}
exports.ModuleError = ModuleError;
class TokenError {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
}
toString() {
switch (this.variantIndex) {
case 0:
return "Underflow";
case 1:
return "Overflow";
case 2:
return "BelowMinimum";
case 3:
return "CannotCreate";
case 4:
return "UnknownAsset";
case 5:
return "Frozen";
case 6:
return "Unsupported";
case 7:
return "CannotCreateHold";
case 8:
return "NotExpendable";
case 9:
return "Blocked";
default:
throw new Error("Unknown TokenError");
}
}
}
exports.TokenError = TokenError;
class ArithmeticError {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
}
toString() {
switch (this.variantIndex) {
case 0:
return "Underflow";
case 1:
return "Overflow";
case 2:
return "DivisionByZero";
default:
throw new Error("Unknown ArithmeticError");
}
}
}
exports.ArithmeticError = ArithmeticError;
class TransactionalError {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
}
toString() {
switch (this.variantIndex) {
case 0:
return "LimitReached";
case 1:
return "NoLayer";
default:
throw new Error("Unknown TransactionalError");
}
}
}
exports.TransactionalError = TransactionalError;
class DispatchResult {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
switch (this.variantIndex) {
case 0:
break;
case 1:
this.err = new DispatchError(decoder);
break;
default:
throw new Error("Unknown DispatchResult");
}
}
toString() {
var _a;
switch (this.variantIndex) {
case 0:
return "Ok";
case 1:
return `Err: ${(_a = this.err) === null || _a === void 0 ? void 0 : _a.toString()}`;
default:
throw new Error("Unknown DispatchResult");
}
}
}
exports.DispatchResult = DispatchResult;
class Weight {
constructor(decoder) {
this.refTime = decoder.decodeU64(true);
this.proofSize = decoder.decodeU64(true);
}
}
exports.Weight = Weight;
class DispatchClass {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
switch (this.variantIndex) {
case 0:
case 1:
case 2:
break;
default:
throw new Error("Unknown DispatchClass");
}
}
toString() {
switch (this.variantIndex) {
case 0:
return "Normal";
case 1:
return "Operational";
case 2:
return "Mandatory";
default:
throw new Error("Unknown DispatchClass");
}
}
}
exports.DispatchClass = DispatchClass;
class RuntimeDispatchInfo {
constructor(decoder) {
this.weight = new Weight(decoder);
this.class = new DispatchClass(decoder);
this.partialFee = decoder.decodeU128();
}
}
exports.RuntimeDispatchInfo = RuntimeDispatchInfo;
class InclusionFee {
constructor(decoder) {
this.baseFee = decoder.decodeU128();
this.lenFee = decoder.decodeU128();
this.adjustedWeightFee = decoder.decodeU128();
}
}
exports.InclusionFee = InclusionFee;
class FeeDetails {
constructor(decoder) {
this.inclusionFee = null;
const isValueThere = decoder.decodeU8();
if (isValueThere == 1) {
this.inclusionFee = new InclusionFee(decoder);
}
}
}
exports.FeeDetails = FeeDetails;
class Pays {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
switch (this.variantIndex) {
case 0:
case 1:
break;
default:
throw new Error("Unknown Pays");
}
}
toString() {
switch (this.variantIndex) {
case 0:
return "Yes";
case 1:
return "No";
default:
throw new Error("Unknown Pays");
}
}
}
exports.Pays = Pays;
class DispatchInfo {
constructor(decoder) {
this.weight = new Weight(decoder);
this.class = new DispatchClass(decoder);
this.pays = new Pays(decoder);
this.feeModifier = new DispatchFeeModifier(decoder);
}
}
exports.DispatchInfo = DispatchInfo;
class DispatchFeeModifier {
constructor(decoder) {
this.weightMaximumFee = null;
this.weightFeeDivider = null;
this.weightFeeMultiplier = null;
const isPresent1 = decoder.decodeU8();
if (isPresent1 == 1) {
this.weightMaximumFee = decoder.decodeU128();
}
const isPresent2 = decoder.decodeU8();
if (isPresent2 == 1) {
this.weightFeeDivider = decoder.decodeU32();
}
const isPresent3 = decoder.decodeU8();
if (isPresent3 == 1) {
this.weightFeeMultiplier = decoder.decodeU32();
}
}
}
exports.DispatchFeeModifier = DispatchFeeModifier;
class PerDispatchClassU32 {
constructor(decoder) {
this.normal = decoder.decodeU32();
this.operational = decoder.decodeU32();
this.mandatory = decoder.decodeU32();
}
}
exports.PerDispatchClassU32 = PerDispatchClassU32;
class SessionKeys {
constructor(babe, grandpa, imOnline, authorityDiscovery) {
this.babe = babe;
this.grandpa = grandpa;
this.imOnline = imOnline;
this.authorityDiscovery = authorityDiscovery;
}
toHex() {
let value = "0x";
value += this.babe.toHex().slice(2);
value += this.grandpa.toHex().slice(2);
value += this.imOnline.toHex().slice(2);
value += this.authorityDiscovery.toHex().slice(2);
return value;
}
static fromHex(keys) {
if (keys.startsWith("0x")) {
keys = keys.slice(2, undefined);
}
const babe = H256.fromString(keys.slice(0, 64));
const grandpa = H256.fromString(keys.slice(64, 128));
const imOnline = H256.fromString(keys.slice(128, 192));
const authorityDiscovery = H256.fromString(keys.slice(192, 256));
return new SessionKeys(babe, grandpa, imOnline, authorityDiscovery);
}
}
exports.SessionKeys = SessionKeys;
class ProxyType {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
switch (this.variantIndex) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
break;
default:
throw new Error("Unknown ProxyType");
}
}
toString() {
switch (this.variantIndex) {
case 0:
return "Any";
case 1:
return "NonTransfer";
case 2:
return "Governance";
case 3:
return "Staking";
case 4:
return "IdentityJudgement";
case 5:
return "NominationPools";
default:
throw new Error("Unknown ProxyType");
}
}
}
exports.ProxyType = ProxyType;
class TimepointBlocknumber {
constructor(height, index) {
this.height = height;
this.index = index;
}
static decode(decoder) {
return new TimepointBlocknumber(decoder.decodeU32(), decoder.decodeU32());
}
}
exports.TimepointBlocknumber = TimepointBlocknumber;
class MultiAddress {
constructor(decoder) {
this.variantIndex = decoder.decodeU8();
this.id = null;
this.index = null;
this.raw = null;
this.address32 = null;
this.address20 = null;
switch (this.variantIndex) {
case 0:
this.id = AccountId.decode(decoder);
return;
case 1:
this.index = decoder.decodeU32();
return;
case 2:
this.raw = decoder.bytesWLen();
return;
case 3:
this.address32 = decoder.bytes(32);
return;
case 4:
this.address20 = decoder.bytes(20);
return;
default:
throw new Error("Unknown MultiAddress");
}
}
toString() {
var _a;
switch (this.variantIndex) {
case 0:
return `Id: ${(_a = this.id) === null || _a === void 0 ? void 0 : _a.toSS58()}`;
case 1:
return `Index: ${this.index}`;
case 2:
return `Raw: ${this.raw}`;
case 3:
return `Address32: ${this.address32}`;
case 4:
return `Address20: ${this.address20}`;
default:
throw new Error("Unknown MultiAddress");
}
}
}
exports.MultiAddress = MultiAddress;
/* export class PoolBondExtra {
public variantIndex: number = 0 // u8
public freeBalance: BN | null = null // BN
constructor() { }
static decode(decoder: Decoder): PoolBondExtra {
let t = new PoolBondExtra()
t.variantIndex = decoder.decodeU8()
switch (t.variantIndex) {
case 0:
t.freeBalance = decoder.decodeU128()
break;
case 1:
break;
default:
throw new Error("Unknown PoolBondExtra")
}
return t
}
toString(): string {
switch (this.variantIndex) {
case 0:
return `Free Balance: ${this.freeBalance?.toString()}`
case 1:
return `Rewards`
default:
throw new Error("Unknown PoolBondExtra")
}
}
} */