@yubing744/rooch-sdk
Version:
185 lines (184 loc) • 5.24 kB
JavaScript
import { HTTPTransport, RequestManager } from "@open-rpc/client-js";
import { JsonRpcClient } from "../generated/client";
import { DevNetwork } from "../constants";
import {
bcsTypes
} from "../types";
import {
addressToSeqNumber,
encodeArg,
functionIdToStirng,
toHexString,
typeTagToString
} from "../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 = DevNetwork, options = DEFAULT_OPTIONS) {
this.options = options;
this.network = network;
const opts = { ...DEFAULT_OPTIONS, ...options };
this.options = opts;
this.client = new JsonRpcClient(
new RequestManager([
new HTTPTransport(network.url, {
headers: {
"Content-Type": "application/json"
},
fetcher: opts.fetcher
})
])
);
}
switchChain(network) {
this.client.close();
this.network = network;
this.client = new JsonRpcClient(
new RequestManager([
new 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) => typeTagToString(v));
const bcsArgs = params.args?.map((arg) => toHexString(encodeArg(arg)));
return this.client.rooch_executeViewFunction({
function_id: 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 bcsTypes.MultiChainAddress(
BigInt(params.multiChainID),
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");
}
}
export {
ROOCH_CLIENT_BRAND,
RoochClient,
isRoochClient
};
//# sourceMappingURL=rooch-client.js.map