@yubing744/rooch-sdk
Version:
197 lines (196 loc) • 6.34 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rooch_client_exports = {};
__export(rooch_client_exports, {
ROOCH_CLIENT_BRAND: () => ROOCH_CLIENT_BRAND,
RoochClient: () => RoochClient,
isRoochClient: () => isRoochClient
});
module.exports = __toCommonJS(rooch_client_exports);
var import_client_js = require("@open-rpc/client-js");
var import_client = require("../generated/client");
var import_constants = require("../constants");
var import_types = require("../types");
var import_utils = require("../utils");
const ROOCH_CLIENT_BRAND = Symbol.for("@roochnetwork/rooch-sdk");
function isRoochClient(client) {
return typeof client === "object" && client !== null && client[ROOCH_CLIENT_BRAND] === true;
}
const DEFAULT_OPTIONS = {
versionCacheTimeoutInSeconds: 600
};
class RoochClient {
constructor(network = import_constants.DevNetwork, options = DEFAULT_OPTIONS) {
this.options = options;
this.network = network;
const opts = { ...DEFAULT_OPTIONS, ...options };
this.options = opts;
this.client = new import_client.JsonRpcClient(
new import_client_js.RequestManager([
new import_client_js.HTTPTransport(network.url, {
headers: {
"Content-Type": "application/json"
},
fetcher: opts.fetcher
})
])
);
}
switchChain(network) {
this.client.close();
this.network = network;
this.client = new import_client.JsonRpcClient(
new import_client_js.RequestManager([
new import_client_js.HTTPTransport(network.url, {
headers: {
"Content-Type": "application/json"
},
fetcher: this.options.fetcher
})
])
);
}
ChainInfo() {
return this.network.info;
}
getChainId() {
return this.network.id;
}
async getRpcApiVersion() {
if (this.rpcApiVersion && this.cacheExpiry && this.cacheExpiry <= Date.now()) {
return this.rpcApiVersion;
}
try {
this.rpcApiVersion = await this.client.getRpcApiVersion();
this.cacheExpiry = // Date.now() is in milliseconds, but the timeout is in seconds
Date.now() + (this.options.versionCacheTimeoutInSeconds ?? 0) * 1e3;
return this.rpcApiVersion;
} catch (err) {
return void 0;
}
}
// Execute a read-only function call The function do not change the state of Application
async executeViewFunction(params) {
const tyStrArgs = params.tyArgs?.map((v) => (0, import_utils.typeTagToString)(v));
const bcsArgs = params.args?.map((arg) => (0, import_utils.toHexString)((0, import_utils.encodeArg)(arg)));
return this.client.rooch_executeViewFunction({
function_id: (0, import_utils.functionIdToStirng)(params.funcId),
ty_args: tyStrArgs ?? [],
args: bcsArgs ?? []
});
}
// Send the signed transaction in bcs hex format
// This method does not block waiting for the transaction to be executed.
async sendRawTransaction(playload) {
return this.client.rooch_sendRawTransaction(playload);
}
async getTransactionsByHashes(tx_hashes) {
return await this.client.rooch_getTransactionsByHash(tx_hashes);
}
async getTransactions(params) {
return this.client.rooch_getTransactionsByOrder(
params.cursor.toString(),
params.limit.toString()
);
}
// Get the events by event handle id
async getEvents(params) {
return await this.client.rooch_getEventsByEventHandle(
params.eventHandleType,
params.cursor.toString(),
params.limit.toString(),
{ decode: true }
);
}
// Get the states by access_path
async getStates(access_path) {
return await this.client.rooch_getStates(access_path, { decode: true });
}
// TODO: bug? next_cursor The true type is string
async listStates(params) {
return await this.client.rooch_listStates(
params.accessPath,
params.cursor,
params.limit.toString(),
{
decode: true
}
);
}
async queryGlobalStates(params) {
return await this.client.rooch_queryGlobalStates(
params.filter,
params.cursor,
params.limit.toString(),
params.descending_order
);
}
async queryTableStates(params) {
return await this.client.rooch_queryTableStates(
params.filter,
params.cursor,
params.limit.toString(),
params.descending_order
);
}
async queryInscriptions(params) {
return await this.client.btc_queryInscriptions(
params.filter,
params.cursor,
params.limit.toString(),
params.descending_order
);
}
async queryUTXOs(params) {
return await this.client.btc_queryUTXOs(
params.filter,
params.cursor,
params.limit.toString(),
params.descending_order
);
}
// Resolve the rooch address
async resoleRoochAddress(params) {
const ma = new import_types.bcsTypes.MultiChainAddress(
BigInt(params.multiChainID),
(0, import_utils.addressToSeqNumber)(params.address)
);
const result = await this.executeViewFunction({
funcId: "0x3::address_mapping::resolve_or_generate",
tyArgs: [],
args: [
{
type: {
Struct: {
address: "0x3",
module: "address_mapping",
name: "MultiChainAddress"
}
},
value: ma
}
]
});
if (result && result.vm_status === "Executed" && result.return_values) {
return result.return_values[0].decoded_value;
}
throw new Error("resolve rooch address fail");
}
}
//# sourceMappingURL=rooch-client.js.map