UNPKG

avail-js-sdk

Version:

Avail library of functions to interact with blockchain and manipulate transactions

73 lines (72 loc) 3.78 kB
"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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransactionPaymentApi_queryInfo = TransactionPaymentApi_queryInfo; exports.TransactionPaymentApi_queryFeeDetails = TransactionPaymentApi_queryFeeDetails; exports.TransactionPaymentCallApi_queryCallInfo = TransactionPaymentCallApi_queryCallInfo; exports.TransactionPaymentCallApi_queryCallFeeDetails = TransactionPaymentCallApi_queryCallFeeDetails; const metadata_1 = require("./metadata"); const decoder_1 = require("./decoder"); const util_1 = require("@polkadot/util"); /// Parameter "tx" is hex encoded transaction. function TransactionPaymentApi_queryInfo(client, tx) { return __awaiter(this, void 0, void 0, function* () { const len = (0, util_1.hexToU8a)(tx).length; const encodedLen = (0, util_1.u8aToHex)(encodeU32(len)); tx += encodedLen.slice(2); const value = yield client.api.rpc.state.call("TransactionPaymentApi_query_info", tx); const decoder = new decoder_1.Decoder(value, 0); return new metadata_1.RuntimeDispatchInfo(decoder); }); } /// Parameter "tx" is hex encoded transaction. function TransactionPaymentApi_queryFeeDetails(client, tx) { return __awaiter(this, void 0, void 0, function* () { const len = (0, util_1.hexToU8a)(tx).length; const encodedLen = (0, util_1.u8aToHex)(encodeU32(len)); tx += encodedLen.slice(2); const value = yield client.api.rpc.state.call("TransactionPaymentApi_query_fee_details", tx); const decoder = new decoder_1.Decoder(value, 0); return new metadata_1.FeeDetails(decoder); }); } function TransactionPaymentCallApi_queryCallInfo(client, call) { return __awaiter(this, void 0, void 0, function* () { const len = (0, util_1.hexToU8a)(call).length; const encodedLen = (0, util_1.u8aToHex)(encodeU32(len)); call += encodedLen.slice(2); const value = yield client.api.rpc.state.call("TransactionPaymentCallApi_query_call_info", call); const decoder = new decoder_1.Decoder(value, 0); return new metadata_1.RuntimeDispatchInfo(decoder); }); } function TransactionPaymentCallApi_queryCallFeeDetails(client, call) { return __awaiter(this, void 0, void 0, function* () { const len = (0, util_1.hexToU8a)(call).length; const encodedLen = (0, util_1.u8aToHex)(encodeU32(len)); call += encodedLen.slice(2); const value = yield client.api.rpc.state.call("TransactionPaymentCallApi_query_call_fee_details", call); const decoder = new decoder_1.Decoder(value, 0); return new metadata_1.FeeDetails(decoder); }); } function encodeU32(value) { if (value < 0 || value > 0xffffffff) { throw new Error("Value out of range for u32"); } // Convert number to 4-byte little-endian Uint8Array const buffer = new Uint8Array(4); buffer[0] = value & 0xff; // Least significant byte buffer[1] = (value >> 8) & 0xff; buffer[2] = (value >> 16) & 0xff; buffer[3] = (value >> 24) & 0xff; // Most significant byte return buffer; }