@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
201 lines • 6.95 kB
JavaScript
import { ProposalTypes } from "../common/constants.js";
import { log } from "../logger.js";
import { formatDateToYYYYMMDD } from "../utils/index.js";
import axios from "axios";
// json-rpc id
const JSON_RPC_ID = 1;
// json-rpc version
const JSON_RPC_VERSION = "2.0";
// http provider
class HttpProvider {
// construct a http provider with base url
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
// send a post request to node and return json-rpc response
async post(jsonRpcBody, headers) {
try {
const response = await this.rawPost(jsonRpcBody, headers);
return response.data;
}
catch (error) {
if (error instanceof Error) {
throw error;
}
throw new Error("Unknown error");
}
}
// send a post-request to node and return axios response
async rawPost(jsonRpcBody, headers) {
log.debug("发起HTTP请求,请求地址:%s,请求体:%o", this.baseUrl, jsonRpcBody);
try {
const response = await axios.post(this.baseUrl, jsonRpcBody, {
headers: headers || {}
});
log.debug("HTTP请求成功,响应状态码:%d,响应体:%o", response.status, response.data);
return response;
}
catch (error) {
if (error instanceof Error) {
throw error;
}
log.error("HTTP请求失败,错误信息:%o", error);
throw new Error("Unknown error");
}
}
}
// http client implementation
class HttpClientImpl {
// construct an http client with http provider
constructor(provider) {
this.provider = provider;
this.httpProvider = provider;
}
/**
* Handle json-rpc response. when the response is null, throw error
* when the response internal error is not null, throw error
*
* @param response - The json-rpc response
* @returns The result
*/
handleJsonRpcResponse(response) {
if (!response) {
throw new Error("response is null");
}
if (response.error) {
throw new Error(`${response.error.code}: ${response.error.message}`);
}
//return JSON.parse(JSON.stringify(response.result)) as T;
return response.result;
}
async getLatestBlock(chainId, accountAddress) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "latc_getCurrentTBDB",
params: [accountAddress]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
/**
* Send signed transaction to blockchain.
*
* @param transaction signed transaction
* @return transaction hash
*/
async sendTransaction(chainId, transaction) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "wallet_sendRawTBlock",
params: [transaction]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
// batch send transactions to chain
async batchSendTransactions(chainId, transactions) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "wallet_sendRawBatchTBlock",
params: [transactions]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
async getReceipt(chainId, hash) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "latc_getReceipt",
params: [hash]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
async getLatestDBlock(chainId) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "latc_getCurrentDBlock",
params: []
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
async preCallContract(chainId, transaction) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "wallet_preExecuteContract",
params: [transaction]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
async getContractLifecycleProposals(chainId, contractAddress, state, startDate, endDate) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "wallet_getProposal",
params: [
{
proposalAddress: contractAddress,
proposalState: state,
proposalType: ProposalTypes.ContractLifecycle,
dateStart: formatDateToYYYYMMDD(startDate),
dateEnd: formatDateToYYYYMMDD(endDate)
}
]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
async getModifyChainConfigurationProposals(chainId, state, startDate, endDate) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "wallet_getProposal",
params: [
{
proposalState: state,
proposalType: ProposalTypes.ModifyChainConfiguration,
dateStart: formatDateToYYYYMMDD(startDate),
dateEnd: formatDateToYYYYMMDD(endDate)
}
]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
async getSubchainProposals(chainId, state, startDate, endDate) {
const response = await this.httpProvider.post({
id: JSON_RPC_ID,
jsonrpc: JSON_RPC_VERSION,
method: "wallet_getProposal",
params: [
{
proposalState: state,
proposalType: ProposalTypes.ChainByChain,
dateStart: formatDateToYYYYMMDD(startDate),
dateEnd: formatDateToYYYYMMDD(endDate)
}
]
}, {
ChainId: chainId.toString()
});
return this.handleJsonRpcResponse(response);
}
}
export { HttpProvider, HttpClientImpl };
//# sourceMappingURL=http_provider.js.map