From 0a9e9b206a28ae02c671888b490672017e6fd28c Mon Sep 17 00:00:00 2001 From: Alejandro Date: Sun, 7 Apr 2024 11:49:02 -0300 Subject: [PATCH] Wrap Agora api --- .../src/internal/agora/Agora.js | 73 +++++++++++++++++ .../src/internal/agora/Governance.js | 82 +++++++++++++++++++ .../src/internal/agora/Identity.js | 21 +++++ .../src/internal/agora/Retro.js | 54 ++++++++++++ .../ethernaut-retropgf/src/tasks/lists.js | 1 + 5 files changed, 231 insertions(+) create mode 100644 packages/ethernaut-retropgf/src/internal/agora/Agora.js create mode 100644 packages/ethernaut-retropgf/src/internal/agora/Governance.js create mode 100644 packages/ethernaut-retropgf/src/internal/agora/Identity.js create mode 100644 packages/ethernaut-retropgf/src/internal/agora/Retro.js diff --git a/packages/ethernaut-retropgf/src/internal/agora/Agora.js b/packages/ethernaut-retropgf/src/internal/agora/Agora.js new file mode 100644 index 0000000..cc60898 --- /dev/null +++ b/packages/ethernaut-retropgf/src/internal/agora/Agora.js @@ -0,0 +1,73 @@ +const axios = require('axios') +const debug = require('ethernaut-common/src/ui/debug') +const EthernautCliError = require('ethernaut-common/src/error/error') +const Identity = require('./Identity') +const Governance = require('./Governance') +const Retro = require('./Retro') + +const BASE_URL = 'https://api.agora.space/ethernaut/retropgf' + +class Agora { + constructor(apiKey) { + this.apiKey = apiKey + + this._identity = new Identity(this) + this._governance = new Governance(this) + this._retro = new Retro(this) + } + + get identity() { + return this._identity + } + + get governance() { + return this._governance + } + + get retro() { + return this._retro + } + + async getSpec() { + return this.createRequest('/api/v1/spec', {}) + } + + async createRequest(endpoint, params = {}) { + let populatedEndpoint = endpoint + for (const key in params) { + populatedEndpoint = populatedEndpoint.replace(`:${key}`, params[key]) + } + + const config = { + method: 'POST', + url: `${BASE_URL}${populatedEndpoint}`, + headers: { + Authorization: `Bearer ${this.apiKey}`, + }, + responseType: 'json', + } + + const response = await axios(config) + + // Http error + if (response.status !== 200) { + throw new EthernautCliError( + 'ethernaut-retropgf', + `Http status error: ${response.status}`, + ) + } + + // Api error + if (response.data.status !== '1') { + debug.log(response.data, 'interact') + throw new EthernautCliError( + 'ethernaut-retropgf', + `Agora api error: ${response.data.result}`, + ) + } + + return response.data.result + } +} + +module.exports = Agora diff --git a/packages/ethernaut-retropgf/src/internal/agora/Governance.js b/packages/ethernaut-retropgf/src/internal/agora/Governance.js new file mode 100644 index 0000000..2007c52 --- /dev/null +++ b/packages/ethernaut-retropgf/src/internal/agora/Governance.js @@ -0,0 +1,82 @@ +class Governance { + constructor(agora) { + this.agora = agora + } + + async getMetrics() { + return this.agora.createRequest('/api/v1/admin/metrics', {}) + } + + async getVotingTokenContractMetadata() { + return this.agora.createRequest('api/v1/contracts/votingToken', {}) + } + + async getAlligatorContractMetadata() { + return this.agora.createRequest('api/v1/contracts/alligator', {}) + } + + async getGovernorContractMetadata() { + return this.agora.createRequest('api/v1/contracts/governor', {}) + } + + async getContractMetadata() { + return this.agora.createRequest('/api/v1/contracts', {}) + } + + async getVote(transactionId) { + return this.agora.createRequest('/api/v1/votes/:transactionId', { + transactionId, + }) + } + + async getVotes() { + return this.agora.createRequest('/api/v1/votes', {}) + } + + async getProposalTypes() { + return this.agora.createRequest('/api/v1/proposals/types', {}) + } + + async getProposalVotes(proposalId) { + return this.agora.createRequest('/api/v1/proposals/:proposalID/votes', { + proposalId, + }) + } + + async getProposal(proposalId) { + return this.agora.createRequest('/api/v1/proposals/:proposalId', { + proposalId, + }) + } + + async getProposals() { + return this.agora.createRequest('/api/v1/proposals', {}) + } + + async getDelegateVotes(addressOrENSName) { + return this.agora.createRequest( + '/api/v1/delegates/:addressOrENSName/votes', + { + addressOrENSName, + }, + ) + } + + async getDelegatees(addressOrENSName) { + return this.agora.createRequest('/api/v1/delegatees/:addressOrENSName', { + addressOrENSName, + }) + } + + async getDelegate(addressOrENSName) { + return this.agora.createRequest('/api/v1/delegates/:addressOrENSName', { + addressOrENSName, + }) + } + + async getDelegates() { + return this.agora.createRequest('/api/v1/delegates', {}) + } +} + +module.exports = Governance diff --git a/packages/ethernaut-retropgf/src/internal/agora/Identity.js b/packages/ethernaut-retropgf/src/internal/agora/Identity.js new file mode 100644 index 0000000..79f3fb5 --- /dev/null +++ b/packages/ethernaut-retropgf/src/internal/agora/Identity.js @@ -0,0 +1,21 @@ +class Identity { + constructor(agora) { + this.agora = agora + } + + async isCitizen(addressOrENSName) { + return this.agora.createRequest('/api/v1/citizens/:addressOrENSName', { + addressOrENSName, + }) + } + + async getCitizens() { + return this.agora.createRequest('/api/v1/citizens', {}) + } + + async getProjects() { + return this.agora.createRequest('/api/v1/projects', {}) + } +} + +module.exports = Identity diff --git a/packages/ethernaut-retropgf/src/internal/agora/Retro.js b/packages/ethernaut-retropgf/src/internal/agora/Retro.js new file mode 100644 index 0000000..c266cd0 --- /dev/null +++ b/packages/ethernaut-retropgf/src/internal/agora/Retro.js @@ -0,0 +1,54 @@ +class Retro { + constructor(agora) { + this.agora = agora + } + + async submitBallot(roundId, addressOrENSName) { + return this.agora.createRequest( + '/api/v1/retropgf/round/:roundID/ballot/:addressOrENSName/submit', + { + roundId, + addressOrENSName, + }, + ) + } + + async addProjectToBallot(projectId, roundId, addressOrENSName) { + return this.agora.createRequest( + '/api/v1/retropgf/round/:roundID/ballot/:addressOrENSName/addProject?projectID= ', + { + projectId, + roundId, + addressOrENSName, + }, + ) + } + + async removeProjectFromBallot(projectId, roundId, addressOrENSName) { + return this.agora.createRequest( + '/api/v1/retropgf/round/:roundID/ballot/:addressOrENSName/removeProject?projectID= ', + { + projectId, + roundId, + addressOrENSName, + }, + ) + } + + async getRoundApplicants(roundId) { + return this.agora.createRequest( + '/api/v1/retropgf/round/:roundID/projects', + { + roundId, + }, + ) + } + + async getRoundInfo(roundId) { + return this.agora.createRequest('/api/v1/retropgf/round/:roundID ', { + roundId, + }) + } +} + +module.exports = Retro diff --git a/packages/ethernaut-retropgf/src/tasks/lists.js b/packages/ethernaut-retropgf/src/tasks/lists.js index c8d4a28..af448ac 100644 --- a/packages/ethernaut-retropgf/src/tasks/lists.js +++ b/packages/ethernaut-retropgf/src/tasks/lists.js @@ -4,6 +4,7 @@ require('../scopes/retro') .task('lists', 'Prints out all the lists of the current round') .setAction(async () => { try { + // TODO return output.resultBox('RetroPGF') } catch (err) { return output.errorBox(err)