Skip to content

Commit 1fa2d62

Browse files
committed
minor refactor
1 parent 2e45bb3 commit 1fa2d62

File tree

10 files changed

+193
-154
lines changed

10 files changed

+193
-154
lines changed

prisma/prod.db

-76 KB
Binary file not shown.

src/commands/index.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { getCache, setCache } from '@/cache'
22
import { env } from '@/config'
33
import { logger } from '@/logger'
44
import { getDAOsForOwners } from '@/services/builder/get-daos-for-owners'
5-
import { getProposalData } from '@/services/builder/get-proposal-for-propdate'
5+
import { getProposalData } from '@/services/builder/get-proposal-from-id'
66
import { Chain, Proposal } from '@/services/builder/types'
77
import { getFollowers } from '@/services/warpcast/get-followers'
88
import { getMe } from '@/services/warpcast/get-me'
99
import { getVerifications } from '@/services/warpcast/get-verifications'
10-
import { shortenAddress } from '@/utils'
1110
import { Hex } from 'viem'
1211

1312
export const CACHE_MAX_AGE_MS = 86400 * 1000 // 1 day in milliseconds
@@ -116,18 +115,13 @@ export async function getUserFid() {
116115
}
117116

118117
/**
119-
* Retrieves proposal data associated with a propdate
118+
* Retrieves proposal data for a given proposal ID
120119
* @param chain - The blockchain network to query
121-
* @param propdateId - attestation Id for caching
122120
* @param proposalId - proposal Id
123121
* @returns A promise that resolves to the DAO object
124122
*/
125-
export async function getProposalFromPropdate(
126-
chain: Chain,
127-
propdateId: Hex,
128-
proposalId: Hex,
129-
) {
130-
const cacheKey = `propdate_${shortenAddress(proposalId)}_${shortenAddress(propdateId)}`
123+
export async function getProposalFromId(chain: Chain, proposalId: Hex) {
124+
const cacheKey = `propdate_${proposalId.toLowerCase()}`
131125
let proposal = await getCache<Proposal | null>(cacheKey, CACHE_MAX_AGE_MS)
132126

133127
if (proposal) {

src/commands/process/propdates.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import {
33
getFollowerAddresses,
44
getFollowerDAOs,
55
getFollowerFids,
6-
getProposalFromPropdate,
6+
getProposalFromId,
77
getUserFid,
88
} from '@/commands'
99
import { logger } from '@/logger'
1010
import { addToQueue } from '@/queue'
11-
import { getAttestations } from '@/services/builder/get-propdate-attestations'
12-
import { shortenAddress } from '@/utils'
11+
import { getPropdateAttestations } from '@/services/eas/get-propdate-attestations'
1312
import { filter, pipe } from 'remeda'
1413
import { JsonValue } from 'type-fest'
1514

@@ -20,9 +19,9 @@ import { JsonValue } from 'type-fest'
2019
*/
2120
async function handleProposalUpdates() {
2221
try {
23-
logger.info('Fetching new updates...')
24-
const { propdates } = await getAttestations()
25-
logger.info({ propdates }, 'New updates retrieved.')
22+
logger.info('Fetching new propdates...')
23+
const { propdates } = await getPropdateAttestations()
24+
logger.info({ propdates }, 'New propdates retrieved.')
2625

2726
const userFid = await getUserFid()
2827
logger.debug({ userFid }, 'User FID retrieved.')
@@ -89,27 +88,26 @@ async function handleProposalUpdates() {
8988
for (const propdate of propdates) {
9089
logger.debug(
9190
{
92-
proposalId: shortenAddress(propdate.proposalId),
93-
propdateId: shortenAddress(propdate.id),
91+
propdateId: propdate.id,
92+
proposalId: propdate.proposalId,
9493
},
9594
'Processing proposal update for follower.',
9695
)
9796

98-
// get dao and proposal data from dao treasury address
99-
const proposal = await getProposalFromPropdate(
97+
// get proposal data from proposal id
98+
const proposal = await getProposalFromId(
10099
propdate.chain,
101-
propdate.id,
102100
propdate.proposalId,
103101
)
104102

105-
if (proposal === null || propdate.proposalId === '0x') {
103+
if (!proposal) {
106104
// skip if proposal doesn't exist or Json parsing error
107105
logger.debug(
108106
{
109-
proposalId: shortenAddress(propdate.proposalId),
110-
propdateId: shortenAddress(propdate.id),
107+
propdateId: propdate.id,
108+
proposalId: propdate.proposalId,
111109
},
112-
'proposal update for skipped.',
110+
'Proposal not found, skipping.',
113111
)
114112
continue
115113
}
@@ -118,8 +116,8 @@ async function handleProposalUpdates() {
118116
if (!daos.includes(proposal.dao.id)) {
119117
logger.debug(
120118
{
121-
proposalId: shortenAddress(propdate.proposalId),
122-
propdateId: shortenAddress(propdate.id),
119+
propdateId: propdate.id,
120+
proposalId: propdate.proposalId,
123121
follower,
124122
},
125123
'Proposal DAO ID not associated with follower, skipping.',
@@ -131,8 +129,8 @@ async function handleProposalUpdates() {
131129
if (notifiedUpdatesSet.has(propdate.id)) {
132130
logger.debug(
133131
{
134-
proposalId: shortenAddress(propdate.proposalId),
135-
propdateId: shortenAddress(propdate.id),
132+
propdateId: propdate.id,
133+
proposalId: propdate.proposalId,
136134
follower,
137135
},
138136
'Proposal update already notified, skipping.',
@@ -143,8 +141,8 @@ async function handleProposalUpdates() {
143141
// Add the proposal to the queue for notifications
144142
logger.info(
145143
{
146-
proposalId: shortenAddress(propdate.proposalId),
147-
propdateId: shortenAddress(propdate.id),
144+
propdateId: propdate.id,
145+
proposalId: propdate.proposalId,
148146
follower,
149147
},
150148
'Adding proposal update to notification queue.',
@@ -160,8 +158,8 @@ async function handleProposalUpdates() {
160158
notifiedUpdatesSet.add(propdate.id)
161159
logger.debug(
162160
{
163-
proposalId: shortenAddress(propdate.proposalId),
164-
propdateId: shortenAddress(propdate.id),
161+
propdateId: propdate.id,
162+
proposalId: propdate.proposalId,
165163
follower,
166164
},
167165
'Proposal Update marked as notified.',

src/commands/queues/consume.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type TaskData = {
1515

1616
interface NotificationData {
1717
recipient: number
18-
proposal?: Proposal
18+
proposal: Proposal
1919
propdate?: Propdate
2020
}
2121

@@ -79,15 +79,16 @@ function formatPropdateMessage(propdate: Propdate, proposal: Proposal): string {
7979

8080
const milestoneText =
8181
parsedMessage.milestoneId !== undefined
82-
? ` for milestone ${parsedMessage.milestoneId.toString()}`
82+
? // Add 1 to the milestone ID to account for the fact that the milestone ID starts at 0
83+
` for milestone ${(parsedMessage.milestoneId + 1).toString()}`
8384
: ''
8485

8586
return (
8687
`📢 A new update to proposal (#${proposalNumber.toString()}: "${proposalTitle}")${milestoneText} has been created on ${daoName} ` +
8788
`around ${toRelativeTime(Number(createdAt))}. ` +
8889
`\n\n${truncatedUpdate} ` +
8990
`\n\n🚀 Check it out for more details and participate in the voting process!` +
90-
`\n\nhttps://nouns.build/dao/${chainName}/${daoId}/vote/${proposalNumber.toString()}`
91+
`\n\nhttps://nouns.build/dao/${chainName.toLowerCase()}/${daoId}/vote/${proposalNumber.toString()}`
9192
)
9293
}
9394

@@ -101,14 +102,13 @@ async function handleNotification(taskId: string, data: NotificationData) {
101102
try {
102103
const { recipient, proposal, propdate } = data
103104
let message: string | undefined
104-
if (proposal && !propdate) {
105-
message = formatProposalMessage(proposal)
106-
} else if (proposal && propdate) {
105+
106+
if (propdate) {
107107
message = formatPropdateMessage(propdate, proposal)
108+
} else {
109+
message = formatProposalMessage(proposal)
108110
}
109-
if (!message) {
110-
throw new Error('No valid message format found')
111-
}
111+
112112
const idempotencyKey = sha256(message).toString()
113113

114114
const result = await sendDirectCast(env, recipient, message, idempotencyKey)

src/services/builder/get-proposal-for-propdate.ts renamed to src/services/builder/get-proposal-from-id.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { gql, GraphQLClient } from 'graphql-request'
44
import { JsonObject } from 'type-fest'
55

66
type Data = {
7-
proposal: Proposal
7+
proposal?: Proposal
88
} & JsonObject
99

1010
interface Result {
@@ -16,16 +16,19 @@ export const getProposalData = async (
1616
proposalId: string,
1717
): Promise<Result> => {
1818
const query = gql`
19-
{
20-
proposal(
21-
id: "${proposalId}"
22-
) {
19+
query GetProposal($id: ID!) {
20+
proposal(id: $id) {
21+
id
2322
proposalNumber
24-
title
25-
dao{
23+
dao {
2624
id
2725
name
2826
}
27+
title
28+
proposer
29+
timeCreated
30+
voteStart
31+
voteEnd
2932
}
3033
}
3134
`
@@ -38,8 +41,11 @@ export const getProposalData = async (
3841
throw new Error(`Endpoint not found for chain ID: ${chain.id.toString()}`)
3942
}
4043
const client = new GraphQLClient(endpoint.endpoint)
41-
const response = await client.request<Data>(query)
42-
if (!response.proposal.title) {
44+
const variables = {
45+
id: proposalId.toLowerCase(),
46+
}
47+
const response = await client.request<Data>(query, variables)
48+
if (!response.proposal) {
4349
throw new Error(`Proposal does not exist: ${proposalId}`)
4450
}
4551
return { proposal: response.proposal }

src/services/builder/index.ts

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ export const endpoints: Record<number, string> = {
2727
7777777: env.BUILDER_SUBGRAPH_ZORA_URL,
2828
}
2929

30-
const attestationEndpoints: Record<number, string> = {
31-
1: 'https://easscan.org/graphql',
32-
10: 'https://optimism.easscan.org/graphql',
33-
8453: 'https://base.easscan.org/graphql',
34-
}
35-
36-
const PROPDATES_SCHEMA_ID =
37-
'0x8bd0d42901ce3cd9898dbea6ae2fbf1e796ef0923e7cbb0a1cecac2e42d47cb3'
38-
3930
export const chainEndpoints = chains.map((chain) => {
4031
const endpoint: string | undefined = endpoints[chain.id]
4132
if (!endpoint) {
@@ -46,76 +37,3 @@ export const chainEndpoints = chains.map((chain) => {
4637
endpoint,
4738
}
4839
})
49-
50-
export const attestationChainEndpoints = chains
51-
.filter((chain) => chain.id != 7777777)
52-
.map((chain) => {
53-
const endpoint = attestationEndpoints[chain.id]
54-
const schemaId = PROPDATES_SCHEMA_ID
55-
if (!endpoint) {
56-
throw new Error(`Endpoint not found for chain ID: ${chain.id.toString()}`)
57-
}
58-
return {
59-
chain,
60-
endpoint,
61-
schemaId,
62-
}
63-
})
64-
65-
const IPFS_ENDPOINTS = [
66-
`https://ipfs.io/ipfs/`,
67-
`https://cloudflare-ipfs.com/ipfs/`,
68-
`https://dweb.link/ipfs/`,
69-
`https://w3s.link/ipfs/`,
70-
`https://flk-ipfs.xyz/ipfs/`,
71-
]
72-
73-
/**
74-
* Strips protocol prefixes from a CID
75-
* @param cid - The CID that might contain protocol prefixes
76-
* @returns The CID without protocol prefixes
77-
*/
78-
function stripProtocolFromCid(cid: string): string {
79-
return cid.replace(/^https?:\/\/|^ipfs:\/\//, '')
80-
}
81-
82-
/**
83-
* Fetches content from IPFS using multiple gateways
84-
* @param cid - The IPFS CID to fetch, with or without protocol prefix
85-
* @returns The content fetched from IPFS as a string
86-
*/
87-
export const fetchFromIPFS = async (
88-
cid: string | undefined,
89-
): Promise<string> => {
90-
if (!cid) {
91-
throw new Error('CID is required')
92-
}
93-
94-
const cleanCid = stripProtocolFromCid(cid)
95-
const controllers = IPFS_ENDPOINTS.map(() => new AbortController())
96-
97-
try {
98-
const response = await Promise.any<string>(
99-
IPFS_ENDPOINTS.map(async (endpoint, index) => {
100-
const controller = controllers[index]
101-
const { signal } = controller
102-
103-
const res = await fetch(`${endpoint}${cleanCid}`, { signal })
104-
if (res.ok) {
105-
// Abort other requests once a successful one is found
106-
controllers.forEach((ctrl, i) => {
107-
if (i !== index) ctrl.abort()
108-
})
109-
const data = await res.text()
110-
return data
111-
}
112-
throw new Error(`Failed to fetch from ${endpoint}`)
113-
}),
114-
)
115-
116-
return response
117-
} catch (error) {
118-
console.error(`Failed to fetch from IPFS for CID: ${cleanCid}: `, error)
119-
throw new Error(`Failed to fetch from IPFS for CID: ${cleanCid}`)
120-
}
121-
}

0 commit comments

Comments
 (0)