forked from solana-labs/governance-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgovernance-notifier.ts
217 lines (191 loc) Β· 6.74 KB
/
governance-notifier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { Connection, PublicKey } from '@solana/web3.js'
import axios from 'axios'
import { getConnectionContext } from 'utils/connection'
import {
getGovernanceAccounts,
Governance,
Proposal,
ProposalState,
pubkeyFilter,
} from '@solana/spl-governance'
import { getCertifiedRealmInfo } from '@models/registry/api'
import { accountsToPubkeyMap } from '@tools/sdk/accounts'
import { fmtTokenAmount } from '@utils/formatting'
import { formatNumber } from '@utils/formatNumber'
const fiveMinutesSeconds = 5 * 60
const toleranceSeconds = 30
if (!process.env.MAINNET_RPC) {
console.error('Please set MAINNET_RPC to a rpc node of choice!')
process.exit(1)
}
export function errorWrapper() {
runNotifier().catch((error) => {
console.error(error)
})
}
export async function runNotifier() {
const REALM = 'Jito'
console.log('Starting governance notifier')
const connectionContext = getConnectionContext('mainnet')
const realmInfo = await getCertifiedRealmInfo(REALM, connectionContext)
const connection = new Connection(process.env.MAINNET_RPC!)
console.log(`- getting all governance accounts for ${REALM}`)
const governances = await getGovernanceAccounts(
connection,
realmInfo!.programId,
Governance,
[pubkeyFilter(1, realmInfo!.realmId)!]
)
const governancesMap = accountsToPubkeyMap(governances)
let webhookTriggered = false
console.log(`- getting all proposals for all governances`)
const proposalsByGovernance = await Promise.all(
Object.keys(governancesMap).map((governancePk) => {
return getGovernanceAccounts(connection, realmInfo!.programId, Proposal, [
pubkeyFilter(1, new PublicKey(governancePk))!,
])
})
)
console.log(`- scanning all '${REALM}' proposals`)
let countJustOpenedForVoting = 0
let countOpenForVotingSinceSomeTime = 0
let countVotingNotStartedYet = 0
let countClosed = 0
let countCancelled = 0
const nowInSeconds = new Date().getTime() / 1000
for (const proposals_ of proposalsByGovernance) {
for (const proposal of proposals_) {
//// debugging
// console.log(
// `-- proposal ${proposal.account.governance.toBase58()} - ${
// proposal.account.name
// }`
// )
if (
// proposal is cancelled
proposal.account.state === ProposalState.Cancelled
) {
countCancelled++
continue
}
if (
// voting is closed
proposal.account.votingCompletedAt
) {
if (
nowInSeconds - proposal.account.votingCompletedAt.toNumber() <=
fiveMinutesSeconds + toleranceSeconds
) {
const votingTokenDecimals = 6
const yesVotes = fmtTokenAmount(
proposal.account.getYesVoteCount(),
votingTokenDecimals
)
const noVotes = fmtTokenAmount(
proposal.account.getNoVoteCount(),
votingTokenDecimals
)
const minVotesNeeded = 100000000
const quorumReached = yesVotes >= minVotesNeeded
const isSuccess = yesVotes > noVotes && quorumReached
const msg = `
Proposal Ended: ${proposal.account.name}
Status: ${
isSuccess
? 'Success'
: !quorumReached
? 'Defeated - Quorum Not Reached'
: 'Defeated'
}
π³οΈ Voting Breakdown:
- Yes Votes: ${formatNumber(yesVotes, undefined, {
minimumFractionDigits: 0,
})}
- No Votes: ${formatNumber(noVotes, undefined, {
minimumFractionDigits: 0,
})}
π https://realms.today/dao/${escape(
REALM
)}/proposal/${proposal.pubkey.toBase58()}`
console.log(msg)
if (process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: msg })
webhookTriggered = true
}
}
countClosed++
continue
}
if (
// voting has not started yet
!proposal.account.votingAt
) {
countVotingNotStartedYet++
continue
}
if (
// proposal opened in last 5 mins
nowInSeconds - proposal.account.votingAt.toNumber() <=
fiveMinutesSeconds + toleranceSeconds
// proposal opened in last 24 hrs - useful to notify when bot recently stopped working
// and missed the 5 min window
// (nowInSeconds - proposal.info.votingAt.toNumber())/(60 * 60) <=
// 24
) {
countJustOpenedForVoting++
const msg = `β${
proposal.account.name
}β proposal just opened for voting π³ https://realms.today/dao/${escape(
REALM
)}/proposal/${proposal.pubkey.toBase58()}`
console.log(msg)
if (process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: msg })
webhookTriggered = true
}
}
// note that these could also include those in finalizing state, but this is just for logging
else if (proposal.account.state === ProposalState.Voting) {
countOpenForVotingSinceSomeTime++
//// in case bot has an issue, uncomment, and run from local with webhook url set as env var
// const msg = `β${
// proposal.account.name
// }β proposal just opened for voting π³ https://realms.today/dao/${escape(
// REALM
// )}/proposal/${proposal.pubkey.toBase58()}`
//
// console.log(msg)
// if (process.env.WEBHOOK_URL) {
// axios.post(process.env.WEBHOOK_URL, { content: msg })
// }
}
const remainingInSeconds =
governancesMap[proposal.account.governance.toBase58()].account.config
.baseVotingTime +
proposal.account.votingAt.toNumber() -
nowInSeconds
if (
remainingInSeconds > 86400 &&
remainingInSeconds < 86400 + fiveMinutesSeconds + toleranceSeconds
) {
const msg = `β${
proposal.account.name
}β proposal will close for voting π³ https://realms.today/dao/${escape(
REALM
)}/proposal/${proposal.pubkey.toBase58()} in 24 hrs`
console.log(msg)
if (process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: msg })
webhookTriggered = true
}
}
}
}
if (!webhookTriggered && process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: 'Nothing to Report' })
}
console.log(
`-- countOpenForVotingSinceSomeTime: ${countOpenForVotingSinceSomeTime}, countJustOpenedForVoting: ${countJustOpenedForVoting}, countVotingNotStartedYet: ${countVotingNotStartedYet}, countClosed: ${countClosed}, countCancelled: ${countCancelled}`
)
}
errorWrapper()