UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

255 lines 9.29 kB
import { getWithRetry } from "../axiosWithRetry.js"; /** * Base configuration for Governor API requests * The Governor API is the new governance indexer, accessed via governanceIndexerUrl */ const getGovernorApiUrl = (environment) => { return environment.governanceIndexerUrl; }; /** * Fetch proposals from Governor API */ export async function fetchProposals(environment, options) { const baseUrl = getGovernorApiUrl(environment); const params = new URLSearchParams(); if (options?.limit) params.append("limit", options.limit.toString()); if (options?.cursor) params.append("cursor", options.cursor); if (options?.chainId) params.append("chainId", options.chainId.toString()); const response = await getWithRetry(`${baseUrl}/api/v1/governor/proposals?${params.toString()}`); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch proposals: ${response.statusText}`); } return response.data; } /** * Fetch all proposals (handles pagination internally) */ export async function fetchAllProposals(environment, options) { const allProposals = []; let cursor = undefined; do { const response = await fetchProposals(environment, { limit: 1000, ...(cursor && { cursor }), ...(options?.chainId && { chainId: options.chainId }), }); allProposals.push(...response.results); cursor = response.nextCursor; } while (cursor); return allProposals; } /** * Fetch a single proposal from Governor API */ export async function fetchProposal(environment, proposalId) { const baseUrl = getGovernorApiUrl(environment); const response = await getWithRetry(`${baseUrl}/api/v1/governor/proposals/${proposalId}`); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch proposal: ${response.statusText}`); } return response.data; } /** * Fetch votes for a proposal */ export async function fetchProposalVotes(environment, proposalId, options) { const baseUrl = getGovernorApiUrl(environment); const params = new URLSearchParams(); if (options?.limit) params.append("limit", options.limit.toString()); if (options?.cursor) params.append("cursor", options.cursor); const response = await getWithRetry(`${baseUrl}/api/v1/governor/proposals/${proposalId}/votes?${params.toString()}`); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch proposal votes: ${response.statusText}`); } return response.data; } /** * Fetch all votes for a proposal (handles pagination internally) */ export async function fetchAllProposalVotes(environment, proposalId) { const allVotes = []; let cursor = undefined; do { const response = await fetchProposalVotes(environment, proposalId, { limit: 1000, ...(cursor && { cursor }), }); allVotes.push(...response.results); cursor = response.nextCursor; } while (cursor); return allVotes; } /** * Fetch state changes for a proposal */ export async function fetchProposalStateChanges(environment, proposalId, options) { const baseUrl = getGovernorApiUrl(environment); const params = new URLSearchParams(); if (options?.limit) params.append("limit", options.limit.toString()); if (options?.cursor) params.append("cursor", options.cursor); const response = await getWithRetry(`${baseUrl}/api/v1/governor/proposals/${proposalId}/state-changes?${params.toString()}`); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch proposal state changes: ${response.statusText}`); } return response.data; } /** * Fetch all state changes for a proposal (handles pagination internally) */ export async function fetchAllProposalStateChanges(environment, proposalId) { const allStateChanges = []; let cursor = undefined; do { const response = await fetchProposalStateChanges(environment, proposalId, { limit: 1000, ...(cursor && { cursor }), }); allStateChanges.push(...response.results); cursor = response.nextCursor; } while (cursor); return allStateChanges; } /** * Fetch all voters (delegates) */ export async function fetchVoters(environment, options) { const baseUrl = getGovernorApiUrl(environment); const params = new URLSearchParams(); if (options?.limit) params.append("limit", options.limit.toString()); if (options?.cursor) params.append("cursor", options.cursor); const endpoint = `${baseUrl}/api/v1/governor/voters?${params.toString()}`; try { const response = await getWithRetry(endpoint); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch voters: ${response.statusText}`); } return response.data; } catch (error) { console.error("[Governor API] CORS/Network Error:", { message: error.message, code: error.code, endpoint, error: error.response || error, }); throw new Error(`CORS or Network error when fetching voters. The API at ${baseUrl} may need CORS headers configured. Original error: ${error.message}`); } } /** * Fetch all voters (handles pagination internally) */ export async function fetchAllVoters(environment) { const allVoters = []; let cursor = undefined; do { const response = await fetchVoters(environment, { limit: 100, ...(cursor && { cursor }), }); allVoters.push(...response.results); cursor = response.nextCursor; } while (cursor); return allVoters; } /** * Fetch a single voter */ export async function fetchVoter(environment, address) { const baseUrl = getGovernorApiUrl(environment); const response = await getWithRetry(`${baseUrl}/api/v1/governor/voters/${address}`); if (response.status === 404) { throw new Error("Voter not found"); } if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch voter: ${response.statusText}`); } return response.data; } /** * Fetch proposals created by a specific address */ export async function fetchVoterProposals(environment, address, options) { const baseUrl = getGovernorApiUrl(environment); const params = new URLSearchParams(); if (options?.limit) params.append("limit", options.limit.toString()); if (options?.cursor) params.append("cursor", options.cursor); const endpoint = `${baseUrl}/api/v1/governor/voters/${address}/proposals?${params.toString()}`; const response = await getWithRetry(endpoint); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch voter proposals: ${response.statusText}`); } return response.data; } /** * Fetch all proposals created by a specific address (handles pagination internally) */ export async function fetchAllVoterProposals(environment, address) { const allProposals = []; let cursor = undefined; do { const response = await fetchVoterProposals(environment, address, { limit: 1000, ...(cursor && { cursor }), }); allProposals.push(...response.results); cursor = response.nextCursor; } while (cursor); return allProposals; } /** * Fetch votes cast by a specific address */ export async function fetchVoterVotes(environment, address, options) { const baseUrl = getGovernorApiUrl(environment); const params = new URLSearchParams(); if (options?.limit) params.append("limit", options.limit.toString()); if (options?.cursor) params.append("cursor", options.cursor); const endpoint = `${baseUrl}/api/v1/governor/voters/${address}/votes?${params.toString()}`; const response = await getWithRetry(endpoint); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch voter votes: ${response.statusText}`); } return response.data; } /** * Fetch all votes cast by a specific address (handles pagination internally) */ export async function fetchAllVoterVotes(environment, address) { const allVotes = []; let cursor = undefined; do { const response = await fetchVoterVotes(environment, address, { limit: 1000, ...(cursor && { cursor }), }); allVotes.push(...response.results); cursor = response.nextCursor; } while (cursor); return allVotes; } /** * Fetch vote receipts for a specific user on a proposal * Returns all votes across all chains for multichain proposals */ export async function fetchUserVoteReceipt(environment, proposalId, voterAddress) { const baseUrl = getGovernorApiUrl(environment); const response = await getWithRetry(`${baseUrl}/api/v1/governor/proposals/${proposalId}/vote/${voterAddress}`); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch user vote receipt: ${response.statusText}`); } return response.data; } //# sourceMappingURL=governor-api-client.js.map