@massalabs/massa-web3
Version:
massa's web3 sdk client
106 lines • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonRpcPublicProvider = void 0;
const __1 = require("..");
const __2 = require("../..");
const formatObjects_1 = require("../../client/formatObjects");
class JsonRpcPublicProvider {
client;
constructor(client) {
this.client = client;
}
static fromRPCUrl(url) {
return new JsonRpcPublicProvider(new __2.PublicAPI(url));
}
static mainnet() {
return JsonRpcPublicProvider.fromRPCUrl(__2.PublicApiUrl.Mainnet);
}
static buildnet() {
return JsonRpcPublicProvider.fromRPCUrl(__2.PublicApiUrl.Buildnet);
}
async balanceOf(addresses, final = true) {
const addressesInfo = await this.client.getMultipleAddressInfo(addresses);
const balances = addressesInfo.map((addressInfo) => ({
address: addressInfo.address,
balance: final
? __2.Mas.fromString(addressInfo.final_balance)
: __2.Mas.fromString(addressInfo.candidate_balance),
}));
return balances;
}
async networkInfos() {
const chainId = await this.client.getChainId();
let name = 'Unknown';
if (chainId === __2.CHAIN_ID.Mainnet) {
name = __2.NetworkName.Mainnet;
}
else if (chainId === __2.CHAIN_ID.Buildnet) {
name = __2.NetworkName.Buildnet;
}
return {
name,
chainId,
url: this.client.url,
minimalFee: await this.client.getMinimalFee(),
};
}
async getOperationStatus(opId) {
return this.client.getOperationStatus(opId);
}
async getEvents(filter) {
return this.client.getEvents(filter);
}
async getNodeStatus() {
const status = await this.client.status();
return (0, formatObjects_1.formatNodeStatusObject)(status);
}
/**
* Reads smart contract function.
* @param params - readSCParams.
* @returns A promise that resolves to a ReadSCData.
*
* @remarks Be a aware that if you don't provide a caller address, it will generate a random one.
*/
async readSC(params) {
const args = params.parameter ?? new Uint8Array();
const caller = params.caller ?? (await __2.Account.generate()).address.toString();
const readOnlyParams = {
...params,
caller,
parameter: args instanceof Uint8Array ? args : args.serialize(),
};
return this.client.executeReadOnlyCall(readOnlyParams);
}
async getStorageKeys(address, filter = new Uint8Array(), final = true) {
const filterBytes = typeof filter === 'string' ? (0, __2.strToBytes)(filter) : filter;
return this.client.getDataStoreKeys(address, filterBytes, final);
}
async readStorage(address, keys, final = true) {
const entries = keys.map((key) => ({ address, key }));
return this.client.getDatastoreEntries(entries, final);
}
/**
* Returns the gas estimation for a given function.
*
* @remarks To avoid running out of gas, the gas estimation is increased by 20%.
*
* @param params - ReadSCParams. caller must be provided
* @throws If the read operation returns an error.
* @returns The gas estimation for the operation execution.
*/
async getGasEstimation(params) {
if (!params.caller) {
throw new Error('Caller must be provided for gas estimation');
}
const result = await this.readSC(params);
if (result.info.error) {
throw new Error(result.info.error);
}
const gasCost = BigInt(result.info.gasCost);
return (0, __2.minBigInt)(
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
gasCost + (gasCost * __1.GAS_ESTIMATION_TOLERANCE) / 100n, __2.MAX_GAS_CALL);
}
}
exports.JsonRpcPublicProvider = JsonRpcPublicProvider;
//# sourceMappingURL=jsonRpcPublicProvider.js.map