72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import axios from 'axios';
|
|
|
|
class VotingService {
|
|
createVotingSession(title: any, options: any, startTime: any, endTime: any) {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
getVotingSessions() {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
vote(votingSessionId: any, optionId: any, voter: any) {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
getVotingResult(votingSessionId: string) {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
voteOnProposal(proposalId: any, voterId: any, weight: any) {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
tallyVotes(proposalId: string) {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
private apiUrl: string;
|
|
|
|
constructor(apiUrl: string) {
|
|
this.apiUrl = apiUrl;
|
|
}
|
|
|
|
// Method to fetch all proposals
|
|
public async getAllProposals(): Promise<any> {
|
|
try {
|
|
const response = await axios.get(`${this.apiUrl}/proposals`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw new Error(`Failed to fetch proposals: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// Method to fetch a specific proposal by ID
|
|
public async getProposalById(proposalId: string): Promise<any> {
|
|
try {
|
|
const response = await axios.get(`${this.apiUrl}/proposals/${proposalId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw new Error(`Failed to fetch proposal ${proposalId}: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// Method to cast a vote for a proposal
|
|
public async voteForProposal(proposalId: string, voterAddress: string): Promise<void> {
|
|
try {
|
|
const response = await axios.post(`${this.apiUrl}/proposals/${proposalId}/vote`, { voter: voterAddress });
|
|
if (response.status !== 200) {
|
|
throw new Error(`Failed to cast vote for proposal ${proposalId}`);
|
|
}
|
|
} catch (error) {
|
|
throw new Error(`Failed to vote for proposal ${proposalId}: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// Method to retrieve voting results for all proposals
|
|
public async getVotingResults(): Promise<any> {
|
|
try {
|
|
const response = await axios.get(`${this.apiUrl}/voting-results`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw new Error(`Failed to fetch voting results: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new VotingService('https://api.example.com');
|