|
| 1 | +const debug = require('ethernaut-common/src/ui/debug') |
| 2 | + |
| 3 | +class Delegates { |
| 4 | + constructor(agora) { |
| 5 | + this.agora = agora |
| 6 | + } |
| 7 | + |
| 8 | + // Get a list of delegates with pagination |
| 9 | + async getDelegates({ limit = 10, offset = 0, sort } = {}) { |
| 10 | + try { |
| 11 | + const axiosInstance = this.agora.createAxiosInstance() |
| 12 | + const response = await axiosInstance.get('/delegates', { |
| 13 | + params: { limit, offset, sort }, |
| 14 | + }) |
| 15 | + |
| 16 | + debug.log(`Delegates: ${response.data.data}`, 'ethernaut-optigov') |
| 17 | + return response.data.data.map((delegate) => ({ |
| 18 | + address: delegate.address, |
| 19 | + votingPower: delegate.votingPower?.total, |
| 20 | + twitter: delegate.statement?.payload?.twitter, |
| 21 | + discord: delegate.statement?.payload?.discord, |
| 22 | + delegateStatement: |
| 23 | + delegate.statement?.payload?.delegateStatement?.substring(0, 100), |
| 24 | + })) |
| 25 | + } catch (error) { |
| 26 | + this.agora.handleError(error) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Get a specific delegate by address or ENS name |
| 31 | + async getDelegateById(addressOrEnsName) { |
| 32 | + try { |
| 33 | + const axiosInstance = this.agora.createAxiosInstance() |
| 34 | + const response = await axiosInstance.get(`/delegates/${addressOrEnsName}`) |
| 35 | + |
| 36 | + debug.log(`Delegate: ${response.data}`, 'ethernaut-optigov') |
| 37 | + const data = response.data |
| 38 | + return { |
| 39 | + address: data.address, |
| 40 | + votingPower: { |
| 41 | + advanced: data.votingPower.advanced, |
| 42 | + direct: data.votingPower.direct, |
| 43 | + total: data.votingPower.total, |
| 44 | + }, |
| 45 | + votingPowerRelativeToVotableSupply: |
| 46 | + data.votingPowerRelativeToVotableSupply, |
| 47 | + votingPowerRelativeToQuorum: data.votingPowerRelativeToQuorum, |
| 48 | + proposalsCreated: data.proposalsCreated, |
| 49 | + proposalsVotedOn: data.proposalsVotedOn, |
| 50 | + votedFor: data.votedFor, |
| 51 | + votedAgainst: data.votedAgainst, |
| 52 | + votedAbstain: data.votedAbstain, |
| 53 | + votingParticipation: data.votingParticipation, |
| 54 | + lastTenProps: data.lastTenProps, |
| 55 | + numOfDelegators: data.numOfDelegators, |
| 56 | + } |
| 57 | + } catch (error) { |
| 58 | + this.agora.handleError(error) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Get a paginated list of votes for a specific delegate |
| 63 | + async getDelegateVotes({ |
| 64 | + addressOrEnsName, |
| 65 | + limit = 10, |
| 66 | + offset = 0, |
| 67 | + sort, |
| 68 | + } = {}) { |
| 69 | + try { |
| 70 | + const axiosInstance = this.agora.createAxiosInstance() |
| 71 | + const response = await axiosInstance.get( |
| 72 | + `/delegates/${addressOrEnsName}/votes`, |
| 73 | + { |
| 74 | + params: { limit, offset, sort }, |
| 75 | + }, |
| 76 | + ) |
| 77 | + |
| 78 | + debug.log( |
| 79 | + `Votes for Delegate ${addressOrEnsName}: ${response.data.data}`, |
| 80 | + 'ethernaut-optigov', |
| 81 | + ) |
| 82 | + return response.data.data.map((vote) => ({ |
| 83 | + transactionHash: vote.transactionHash, |
| 84 | + proposalId: vote.proposalId, |
| 85 | + address: vote.address, |
| 86 | + support: vote.support, |
| 87 | + reason: vote.reason, |
| 88 | + weight: vote.weight, |
| 89 | + proposalValue: vote.proposalValue, |
| 90 | + proposalTitle: vote.proposalTitle, |
| 91 | + proposalType: vote.proposalType, |
| 92 | + timestamp: vote.timestamp, |
| 93 | + })) |
| 94 | + } catch (error) { |
| 95 | + this.agora.handleError(error) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Get a paginated list of delegators for a specific delegate |
| 100 | + async getDelegateDelegators({ |
| 101 | + addressOrEnsName, |
| 102 | + limit = 10, |
| 103 | + offset = 0, |
| 104 | + sort, |
| 105 | + } = {}) { |
| 106 | + try { |
| 107 | + const axiosInstance = this.agora.createAxiosInstance() |
| 108 | + const response = await axiosInstance.get( |
| 109 | + `/delegates/${addressOrEnsName}/delegators`, |
| 110 | + { |
| 111 | + params: { limit, offset, sort }, |
| 112 | + }, |
| 113 | + ) |
| 114 | + |
| 115 | + debug.log( |
| 116 | + `Delegators for Delegate ${addressOrEnsName}: ${response.data.data}`, |
| 117 | + 'ethernaut-optigov', |
| 118 | + ) |
| 119 | + return response.data.data.map((delegator) => ({ |
| 120 | + from: delegator.from, |
| 121 | + allowance: delegator.allowance, |
| 122 | + timestamp: delegator.timestamp, |
| 123 | + type: delegator.type, |
| 124 | + amount: delegator.amount, |
| 125 | + })) |
| 126 | + } catch (error) { |
| 127 | + this.agora.handleError(error) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = Delegates |
0 commit comments