Skip to content

Commit c7788e5

Browse files
committed
feat: Add JSDoc comments with examples to ValidationError and SdlValidationError classes
- Enhanced the `ValidationError` class with detailed JSDoc comments, including usage examples for the `assert` method. - Updated the `SdlValidationError` class to include comprehensive JSDoc comments and examples, improving code documentation and usability. - Ensured consistency in documentation style across both error classes. Signed-off-by: Greg Osuri <me@gregosuri.com>
1 parent 916cf7c commit c7788e5

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

src/keplr/index.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ import { AminoTypes } from "@cosmjs/stargate";
55
import { Certificate } from "@akashnetwork/akash-api/akash/cert/v1beta2";
66
import { MsgCreateCertificate } from "@akashnetwork/akash-api/deprecated/akash/cert/v1beta1";
77

8+
/**
9+
* Represents a blockchain chain.
10+
* @typedef {Object} Chain
11+
* @property {string} id - The unique identifier of the chain.
12+
*/
813
interface Chain {
914
id: string;
1015
}
1116

17+
/**
18+
* Returns the available blockchain chains.
19+
* @returns {Object} An object containing mainnet and testnet chain configurations.
20+
*/
1221
export function getChains() {
1322
return {
1423
mainnet: {
@@ -20,25 +29,45 @@ export function getChains() {
2029
};
2130
}
2231

32+
/**
33+
* Retrieves the signer for a given blockchain chain.
34+
* @param {Chain} chain - The blockchain chain for which to get the signer.
35+
* @returns {Promise<OfflineSigner | OfflineDirectSigner>} A promise that resolves to the signer.
36+
*/
2337
export function getSigner(chain: Chain) {
2438
return window.getOfflineSignerAuto(chain.id);
2539
}
2640

41+
/**
42+
* Connects to a blockchain endpoint with a given signer.
43+
* @param {Chain} chain - The blockchain chain to connect to.
44+
* @param {OfflineSigner | OfflineDirectSigner} signer - The signer to use for the connection.
45+
* @param {string} endPoint - The endpoint URL to connect to.
46+
* @returns {Promise<SigningStargateClient>} A promise that resolves to the connected client.
47+
*/
2748
export async function get(chain: Chain, signer: OfflineSigner | OfflineDirectSigner, endPoint: string) {
49+
// Define custom Amino types for specific message types
2850
const customAminoTypes = new AminoTypes({
51+
// Specify the message type for creating a certificate
2952
"/akash.cert.v1beta2.MsgCreateCertificate": {
53+
// Define the Amino type string for the message
3054
aminoType: "cert/cert-create-certificate",
55+
// Function to convert the message to Amino format
3156
toAmino: ({ cert, pubkey }: MsgCreateCertificate) => {
57+
// Encode the certificate and public key into a binary format
3258
const buf = Certificate.encode(
3359
Certificate.fromPartial({
3460
cert,
3561
pubkey
3662
})
3763
).finish();
64+
// Convert the binary data to a base64 string
3865
const encoded = Buffer.from(buf);
3966
return encoded.toString("base64");
4067
},
68+
// Function to convert the message from Amino format
4169
fromAmino: ({ cert, pubkey }: MsgCreateCertificate) => {
70+
// Create a partial Certificate object from the Amino data
4271
return Certificate.fromPartial({
4372
cert,
4473
pubkey
@@ -47,10 +76,12 @@ export async function get(chain: Chain, signer: OfflineSigner | OfflineDirectSig
4776
}
4877
});
4978

79+
// Create a registry that includes default types and Akash-specific types
5080
const myRegistry = new Registry([...defaultRegistryTypes, ...getAkashTypeRegistry()]);
5181

82+
// Connect to the blockchain endpoint using the provided signer and custom configurations
5283
return await SigningStargateClient.connectWithSigner(endPoint, signer, {
53-
registry: myRegistry,
54-
aminoTypes: customAminoTypes
84+
registry: myRegistry, // Use the custom registry
85+
aminoTypes: customAminoTypes // Use the custom Amino types
5586
});
5687
}

src/network/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,22 @@ export async function getMetadata(network: NETWORK_TYPE): Promise<INetworkMetada
6464
return fetch(`https://raw.githubusercontent.com/ovrclk/net/master/${network}/meta.json`).then(res => res.json());
6565
}
6666

67+
/**
68+
* Retrieves endpoints for a specific network and type
69+
* @param {NETWORK_TYPE} network - The network to get endpoints for
70+
* @param {ENDPOINT_TYPE} type - The type of endpoint to retrieve
71+
* @returns {Promise<{ address: string }[]>} A promise that resolves to an array of endpoint addresses
72+
*/
6773
export function getEndpoints(network: NETWORK_TYPE, type: ENDPOINT_TYPE) {
6874
return getMetadata(network).then(meta => meta.apis[type]);
6975
}
7076

77+
/**
78+
* Retrieves and sorts endpoints by their health status
79+
* @param {NETWORK_TYPE} network - The network to get endpoints for
80+
* @param {ENDPOINT_TYPE} type - The type of endpoint to retrieve
81+
* @returns {Promise<{ address: string, responseTime: number | null }[]>} A promise that resolves to an array of endpoints sorted by response time
82+
*/
7183
export function getEndpointsSorted(network: NETWORK_TYPE, type: ENDPOINT_TYPE) {
7284
return getEndpoints(network, type)
7385
.then(map(getEndpointHealthStatus(800)))
@@ -76,10 +88,20 @@ export function getEndpointsSorted(network: NETWORK_TYPE, type: ENDPOINT_TYPE) {
7688
.then(sortBy(prop("responseTime")));
7789
}
7890

91+
/**
92+
* Checks if a node is responsive based on its response time
93+
* @param {{ responseTime: number | null }} endpoint - The endpoint to check
94+
* @returns {boolean} True if the node is responsive, false otherwise
95+
*/
7996
function isNodeResponsive(endpoint: { responseTime: number | null }) {
8097
return endpoint.responseTime !== null;
8198
}
8299

100+
/**
101+
* Returns a function that checks the health status of an endpoint
102+
* @param {number} timeout - The timeout for the health check request
103+
* @returns {function({ address: string }): Promise<{ address: string, responseTime: number | null }>} A function that returns a promise resolving to the endpoint's health status
104+
*/
83105
function getEndpointHealthStatus(timeout: number) {
84106
return ({ address }: { address: string }) => {
85107
const startTime = performance.now();

src/pbclient/pbclient.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { Message } from "../stargate";
55
import { AminoMsg } from "@cosmjs/amino";
66

77
describe("createAminoMessage", () => {
8+
/**
9+
* Test to ensure createAminoMessage function creates an Amino message correctly.
10+
*/
811
it("creates an amino message", () => {
912
const message = faker.helpers.arrayElement(Object.values(Message));
1013
const messageBody: AminoMsg = {

src/pbclient/pbclient.ts

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ const FEE = {
1212
gas: "100000"
1313
};
1414

15+
/**
16+
* Creates an Amino message object.
17+
*
18+
* @param {Message} message - The type of the message.
19+
* @param {AminoMsg} messageBody - The body of the message.
20+
* @returns {object} The Amino message object.
21+
*/
1522
export function createAminoMessage(message: Message, messageBody: AminoMsg) {
1623
return {
1724
typeUrl: message,
@@ -27,6 +34,14 @@ type MessageTypes = {
2734
};
2835
};
2936

37+
/**
38+
* Creates a Stargate message object with a fee.
39+
*
40+
* @template T
41+
* @param {T} message - The type of the message.
42+
* @param {MessageTypes[T]} messageBody - The body of the message.
43+
* @returns {object} The Stargate message object with a fee.
44+
*/
3045
export function createStarGateMessage<T extends keyof MessageTypes>(message: T, messageBody: MessageTypes[T]) {
3146
return {
3247
message: {

src/rpc/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@ import { getAkashTypeRegistry } from "../stargate";
33
import { OfflineSigner, Registry } from "@cosmjs/proto-signing";
44
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
55

6+
/**
7+
* Gets an RPC client for interacting with the Akash network
8+
* @param {string} endpoint - The RPC endpoint URL to connect to
9+
* @returns {Promise<ReturnType<typeof createProtobufRpcClient>>} A protobuf RPC client instance
10+
* @throws {Error} If connection to the endpoint fails
11+
*/
612
export async function getRpc(endpoint: string) {
713
return getQueryClient(endpoint);
814
}
915

16+
/**
17+
* Creates a query client for making read-only requests to the Akash network
18+
* @param {string} endpoint - The RPC endpoint URL to connect to
19+
* @returns {Promise<ReturnType<typeof createProtobufRpcClient>>} A protobuf RPC client for queries
20+
* @throws {Error} If connection to the endpoint fails
21+
*/
1022
export async function getQueryClient(endpoint: string) {
1123
const tmClient = await Tendermint34Client.connect(endpoint);
1224
const queryClient = new QueryClient(tmClient);
1325
return createProtobufRpcClient(queryClient);
1426
}
1527

28+
/**
29+
* Creates a message client for sending transactions to the Akash network
30+
* @param {string} endpoint - The RPC endpoint URL to connect to
31+
* @param {OfflineSigner} signer - The signer used to sign transactions
32+
* @returns {Promise<SigningStargateClient>} A client for signing and sending transactions
33+
* @throws {Error} If connection to the endpoint fails or if there are issues with the signer
34+
*/
1635
export async function getMsgClient(endpoint: string, signer: OfflineSigner) {
1736
const msgRegistry = new Registry(getAkashTypeRegistry());
1837
const options: SigningStargateClientOptions = {

0 commit comments

Comments
 (0)