Skip to content

Commit

Permalink
Wrap Agora api
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Apr 7, 2024
1 parent ad570b6 commit 0a9e9b2
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/ethernaut-retropgf/src/internal/agora/Agora.js
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions packages/ethernaut-retropgf/src/internal/agora/Governance.js
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions packages/ethernaut-retropgf/src/internal/agora/Identity.js
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions packages/ethernaut-retropgf/src/internal/agora/Retro.js
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/ethernaut-retropgf/src/tasks/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0a9e9b2

Please sign in to comment.