From 7524af8f17bebf94cbff694359d04adf7a0fa52f Mon Sep 17 00:00:00 2001 From: Alejandro Date: Thu, 25 Apr 2024 08:47:49 -0300 Subject: [PATCH] Connecting to Agora API --- packages/ethernaut-cli/hardhat.config.js | 1 + .../src/internal/agora/Agora.js | 26 ++++++++----------- .../src/internal/agora/Retro.js | 8 ++++++ .../ethernaut-retropgf/src/tasks/lists.js | 6 +++++ 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/ethernaut-cli/hardhat.config.js b/packages/ethernaut-cli/hardhat.config.js index 98c2979..96168eb 100644 --- a/packages/ethernaut-cli/hardhat.config.js +++ b/packages/ethernaut-cli/hardhat.config.js @@ -26,6 +26,7 @@ require('ethernaut-wallet-ui') require('ethernaut-challenges') require('ethernaut-ui') require('ethernaut-oso') +require('ethernaut-retropgf') require('ethernaut-ai-ui') async function main() { diff --git a/packages/ethernaut-retropgf/src/internal/agora/Agora.js b/packages/ethernaut-retropgf/src/internal/agora/Agora.js index cc60898..c623800 100644 --- a/packages/ethernaut-retropgf/src/internal/agora/Agora.js +++ b/packages/ethernaut-retropgf/src/internal/agora/Agora.js @@ -5,11 +5,14 @@ const Identity = require('./Identity') const Governance = require('./Governance') const Retro = require('./Retro') -const BASE_URL = 'https://api.agora.space/ethernaut/retropgf' +const BASE_URL = 'https://vote.optimism.io/api/v1' + +// See API spec at https://vote.optimism.io/api_v1 +// API categories split into Identity, Governance, and Retro objects class Agora { - constructor(apiKey) { - this.apiKey = apiKey + constructor() { + this.bearerToken = process.env.AGORA_BEARER_TOKEN this._identity = new Identity(this) this._governance = new Governance(this) @@ -32,17 +35,17 @@ class Agora { return this.createRequest('/api/v1/spec', {}) } - async createRequest(endpoint, params = {}) { + async createRequest(endpoint, params = {}, method = 'GET') { let populatedEndpoint = endpoint for (const key in params) { populatedEndpoint = populatedEndpoint.replace(`:${key}`, params[key]) } const config = { - method: 'POST', + method, url: `${BASE_URL}${populatedEndpoint}`, headers: { - Authorization: `Bearer ${this.apiKey}`, + Authorization: `Bearer ${this.bearerToken}`, }, responseType: 'json', } @@ -57,16 +60,9 @@ class Agora { ) } - // Api error - if (response.data.status !== '1') { - debug.log(response.data, 'interact') - throw new EthernautCliError( - 'ethernaut-retropgf', - `Agora api error: ${response.data.result}`, - ) - } + debug.log(response.data, 'retropgf') - return response.data.result + return response.data } } diff --git a/packages/ethernaut-retropgf/src/internal/agora/Retro.js b/packages/ethernaut-retropgf/src/internal/agora/Retro.js index c266cd0..a6166a5 100644 --- a/packages/ethernaut-retropgf/src/internal/agora/Retro.js +++ b/packages/ethernaut-retropgf/src/internal/agora/Retro.js @@ -49,6 +49,14 @@ class Retro { roundId, }) } + + async getProjects(limit = 10, offset = 0) { + const data = await this.agora.createRequest( + `/projects?limit=${limit}&?offset=${offset}`, + ) + + return data.projects + } } module.exports = Retro diff --git a/packages/ethernaut-retropgf/src/tasks/lists.js b/packages/ethernaut-retropgf/src/tasks/lists.js index af448ac..15cebb3 100644 --- a/packages/ethernaut-retropgf/src/tasks/lists.js +++ b/packages/ethernaut-retropgf/src/tasks/lists.js @@ -1,9 +1,15 @@ const output = require('ethernaut-common/src/ui/output') +const Agora = require('../internal/agora/Agora') require('../scopes/retro') .task('lists', 'Prints out all the lists of the current round') .setAction(async () => { try { + const agora = new Agora() + + const info = await agora.retro.getProjects() + console.log('@@@@', info) + // TODO return output.resultBox('RetroPGF') } catch (err) {