-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into more-strict-rate-limit-for-spammer
- Loading branch information
Showing
23 changed files
with
871 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'dotenv/config'; | ||
import db from '../src/helpers/mysql'; | ||
import networks from '@snapshot-labs/snapshot.js/src/networks.json'; | ||
|
||
async function main() { | ||
if (process.argv.length < 2) { | ||
console.error(`Usage: yarn ts-node scripts/hibernate.ts run|preview`); | ||
return process.exit(1); | ||
} | ||
|
||
const [, , action] = process.argv; | ||
|
||
const commands = { | ||
preview: `SELECT COUNT(id) as count from toHibernate`, | ||
run: `UPDATE spaces SET hibernated = 1 where spaces.id IN (SELECT id FROM toHibernate)` | ||
}; | ||
|
||
if (!commands[action]) { | ||
console.error(`First argument should be either "run" or "preview"`); | ||
return process.exit(1); | ||
} | ||
|
||
const liveNetworks = Object.values(networks) | ||
.filter((network: any) => !network.testnet) | ||
.map((network: any) => network.key); | ||
|
||
const query = ` | ||
WITH toHibernate AS ( | ||
WITH data AS ( | ||
SELECT | ||
id, | ||
(SELECT MAX(created) FROM proposals WHERE space = spaces.id LIMIT 1) AS lastProposalEndDate | ||
FROM spaces | ||
WHERE hibernated = 0 | ||
) | ||
SELECT | ||
id, lastProposalEndDate | ||
FROM data | ||
WHERE | ||
# Filtering out spaces that have not been active in the past year | ||
FROM_UNIXTIME(lastProposalEndDate) < DATE_SUB(CURRENT_DATE, INTERVAL 1 YEAR) | ||
) | ||
${commands[action]}; | ||
`; | ||
|
||
const results = await db.queryAsync(query, liveNetworks); | ||
|
||
if (action === 'preview') { | ||
console.log(`Spaces eligible for hibernation: ${results[0].count}`); | ||
} else { | ||
console.log(`${results.message}`); | ||
} | ||
} | ||
|
||
(async () => { | ||
try { | ||
await main(); | ||
process.exit(0); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import snapshot from '@snapshot-labs/snapshot.js'; | ||
import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
import { addOrUpdateSpace } from './actions'; | ||
|
||
type Space = Record<string, any>; | ||
|
||
const DEFAULT_NETWORK = process.env.DEFAULT_NETWORK ?? '1'; | ||
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org'; | ||
|
||
export default async function poke(id: string): Promise<Space> { | ||
const space = await getSpaceENS(id); | ||
|
||
try { | ||
if (snapshot.utils.validateSchema(snapshot.schemas.space, space) !== true) { | ||
return Promise.reject('invalid space format'); | ||
} | ||
|
||
await addOrUpdateSpace(id, space); | ||
|
||
return space; | ||
} catch (e: any) { | ||
capture(e); | ||
return Promise.reject('unable to save the space'); | ||
} | ||
} | ||
|
||
async function getSpaceENS(id: string): Promise<Space> { | ||
const uri = await snapshot.utils.getSpaceUri(id, DEFAULT_NETWORK, { broviderUrl }); | ||
|
||
if (uri) { | ||
if (!isValidUri(uri)) { | ||
return Promise.reject('TXT record is not a valid uri'); | ||
} | ||
|
||
try { | ||
return await snapshot.utils.getJSON(uri); | ||
} catch (e) { | ||
return Promise.reject(`${uri} is not a valid JSON file`); | ||
} | ||
} | ||
|
||
return Promise.reject(`missing snapshot TXT record on ENS name ${id}`); | ||
} | ||
|
||
function isValidUri(uri: string): boolean { | ||
return /^(ip[fn]s|https?):\/\//.test(uri); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.