Skip to content

Commit

Permalink
refactor(api): fixes any types usages (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Apr 22, 2024
1 parent 7cefd8f commit 9dd6a0e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/certificates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { create as create509, pems } from "./generate509";
import { SigningStargateClient } from "@cosmjs/stargate";
import { messages as stargateMessages } from "../stargate";
import { Message as stargateMessages } from "../stargate";
import { createStarGateMessage } from "../pbclient/pbclient";

import { QueryCertificatesRequest, QueryCertificatesResponse, CertificateFilter } from "@akashnetwork/akash-api/akash/cert/v1beta3";
Expand Down Expand Up @@ -34,7 +34,7 @@ export async function createCertificate(bech32Address: string) {
export async function revokeCertificate(owner: string, serial: string, client: SigningStargateClient) {
const message = createStarGateMessage(stargateMessages.MsgRevokeCertificate, {
id: {
owner: owner,
owner,
serial
}
});
Expand Down
11 changes: 8 additions & 3 deletions src/pbclient/pbclient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { faker } from "@faker-js/faker";

import { createAminoMessage } from "./pbclient";
import { messages } from "../stargate";
import { Message } from "../stargate";
import { AminoMsg } from "cosmwasm";

describe("createAminoMessage", () => {
it("creates an amino message", () => {
const message = faker.helpers.arrayElement(Object.values(messages));
const messageBody = "messageBody";
const message = faker.helpers.arrayElement(Object.values(Message));
const messageBody: AminoMsg = {
type: faker.string.alpha(10),
value: faker.string.alpha(10)
};
const result = createAminoMessage(message, messageBody);

expect(result).toEqual({
typeUrl: message,
value: messageBody
Expand Down
19 changes: 13 additions & 6 deletions src/pbclient/pbclient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { messages } from "../stargate";
import { Message } from "../stargate";
import { AminoMsg } from "cosmwasm";
import { MsgCreateCertificate, MsgRevokeCertificate } from "../protobuf/akash/cert/v1beta3/cert";

// dynamically determine max gas
const fee = {
const FEE = {
amount: [
{
denom: "uakt",
Expand All @@ -11,19 +12,25 @@ const fee = {
gas: "100000"
};

export function createAminoMessage(message: messages, messageBody: any) {
export function createAminoMessage(message: Message, messageBody: AminoMsg) {
return {
typeUrl: message,
value: messageBody
};
}

export function createStarGateMessage(message: messages, messageBody: any) {
type WithoutType<T> = Omit<T, "$type">;
type MessageTypes = {
[Message.MsgCreateCertificate]: WithoutType<MsgCreateCertificate>;
[Message.MsgRevokeCertificate]: Omit<WithoutType<MsgRevokeCertificate>, "id"> & { id: WithoutType<MsgRevokeCertificate["id"]> };
};

export function createStarGateMessage<T extends keyof MessageTypes>(message: T, messageBody: MessageTypes[T]) {
return {
message: {
typeUrl: message,
value: messageBody
},
fee: fee
fee: FEE
};
}
2 changes: 1 addition & 1 deletion src/stargate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getAkashTypeRegistry: () => [string, MessageType<UnknownMessage>][]
export const getTypeUrl: (type: MessageType) => string = type => `/${type.$type}`;

/* TODO: this should be generated from the proto files */
export enum messages {
export enum Message {
MsgCreateCertificate = "/akash.cert.v1beta3.MsgCreateCertificate",
MsgRevokeCertificate = "/akash.cert.v1beta3.MsgRevokeCertificate",
MsgCreateDeployment = "/akash.deployment.v1beta3.MsgCreateDeployment",
Expand Down

0 comments on commit 9dd6a0e

Please sign in to comment.