UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

222 lines 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchProposals = fetchProposals; exports.fetchAllProposals = fetchAllProposals; exports.fetchProposal = fetchProposal; exports.fetchProposalVotes = fetchProposalVotes; exports.fetchAllProposalVotes = fetchAllProposalVotes; exports.fetchProposalStateChanges = fetchProposalStateChanges; exports.fetchAllProposalStateChanges = fetchAllProposalStateChanges; exports.fetchVoters = fetchVoters; exports.fetchAllVoters = fetchAllVoters; exports.fetchVoter = fetchVoter; exports.fetchVoterProposals = fetchVoterProposals; exports.fetchAllVoterProposals = fetchAllVoterProposals; exports.fetchVoterVotes = fetchVoterVotes; exports.fetchAllVoterVotes = fetchAllVoterVotes; exports.fetchUserVoteReceipt = fetchUserVoteReceipt; const axiosWithRetry_js_1 = require("../axiosWithRetry.js"); const getGovernorApiUrl = (environment) => { return environment.governanceIndexerUrl; }; 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 (0, axiosWithRetry_js_1.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; } 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; } async function fetchProposal(environment, proposalId) { const baseUrl = getGovernorApiUrl(environment); const response = await (0, axiosWithRetry_js_1.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; } 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 (0, axiosWithRetry_js_1.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; } 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; } 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 (0, axiosWithRetry_js_1.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; } 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; } 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 (0, axiosWithRetry_js_1.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}`); } } 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; } async function fetchVoter(environment, address) { const baseUrl = getGovernorApiUrl(environment); const response = await (0, axiosWithRetry_js_1.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; } 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 (0, axiosWithRetry_js_1.getWithRetry)(endpoint); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch voter proposals: ${response.statusText}`); } return response.data; } 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; } 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 (0, axiosWithRetry_js_1.getWithRetry)(endpoint); if (response.status !== 200 || !response.data) { throw new Error(`Failed to fetch voter votes: ${response.statusText}`); } return response.data; } 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; } async function fetchUserVoteReceipt(environment, proposalId, voterAddress) { const baseUrl = getGovernorApiUrl(environment); const response = await (0, axiosWithRetry_js_1.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