import {z} from 'zod' export type GetApiGovernanceProposalsArgs = {proposalId?: number | undefined, state?: string | undefined, order?: string | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiGovernanceProposalsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiGovernanceProposalsResponse = z.object({offset: z.number().optional(), limit: z.number().optional(), total: z.number().optional(), result: z.array(z.object({proposalId: z.number().int(), description: z.string(), createdBlock: z.number().int(), createdTxHash: z.string(), createdTimestamp: z.number().int(), startBlock: z.number().int(), startTimestamp: z.number().int(), cancelBlock: z.number().int().nullable(), cancelTxHash: z.string().nullable(), cancelTimestamp: z.number().int().nullable(), endBlock: z.number().int(), endTimestamp: z.number().int(), queuedBlock: z.number().int().nullable(), queuedTxHash: z.string().nullable(), queuedTimestamp: z.number().int().nullable(), executedBlock: z.number().int().nullable(), executedTxHash: z.string().nullable(), executedTimestamp: z.number().int().nullable(), proposer: z.string(), eta: z.number().int(), forVotes: z.string().optional(), againstVotes: z.string().optional(), canceled: z.boolean().optional(), executed: z.boolean().optional(), state: z.number().int(), voterCount: z.number().int().optional(), abstainedVotes: z.string().optional(), governorName: z.string(), proposalType: z.number().int().optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Returns paginated array of proposals. * * GET /governance/proposals */ export const getApiGovernanceProposals = async (args: GetApiGovernanceProposalsArgs, options: GetApiGovernanceProposalsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.proposalId != null) query.append("proposalId", String(args.proposalId)) if (args.state != null) query.append("state", String(args.state)) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) const url = `${baseUrl.replace(/\/+$/, '')}/governance/proposals${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiGovernanceProposalsResponse, await res.json()) };