-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.ts
95 lines (85 loc) · 2.76 KB
/
handler.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
import axios from 'axios';
import { ethers } from 'ethers';
import { contracts } from '@impactmarket/core';
import * as prismic from '@prismicio/client'
const endpoint = prismic.getEndpoint(process.env.PRISMIC_REPO!);
const accessToken = process.env.PRISMIC_ACCESS_TOKEN;
const client = prismic.createClient(endpoint, { accessToken });
export const verifyProposals = async (event: any, context: any) => {
try {
const provider = new ethers.providers.JsonRpcProvider(
process.env.CHAIN_JSON_RPC_URL
);
const DAOContract = new ethers.Contract(
process.env.PACT_DELEGATOR!,
contracts.PACTDelegateABI,
provider
);
const proposalCount = parseInt(await DAOContract.proposalCount());
const lastHour = new Date(new Date().getTime() - 60*60*1000).getTime();
const newProposals: any = [];
for(let i = proposalCount-1; ; i--) {
const proposal = await DAOContract.proposals(i);
const block = await getBlockByNumber(proposal.startBlock._hex);
const blockTimestamp = parseInt(block.timestamp) * 1000;
if(blockTimestamp >= lastHour) {
if(!proposal.canceled && !proposal.executed) {
newProposals.push(proposal);
}
} else {
break
}
}
if(newProposals.length > 0) {
const data = await client.getAllByType(process.env.PRISMIC_CUSTOM_TYPE!, { lang: process.env.PRISMIC_LANGUAGE });
const title = data[0].data.opened_proposal_title
const description = data[0].data.opened_proposal_description
const promises = newProposals.map((proposal) =>
axios.post(process.env.DISCORD_WEBHOOK!, {
embeds: [{
type: 'rich',
title: title,
description: description.replace('{id}', parseInt(proposal.id)),
url: process.env.GOVERNACE_SYSTEM_URL,
color: 39423,
timestamp: null,
fields: [],
thumbnail: null,
image: null,
video: null,
author: null,
provider: null,
footer: null,
files: []
}]
})
);
await Promise.all(promises);
} else {
console.info('No new proposals');
}
} catch (error) {
console.error(error);
}
}
const getBlockByNumber = async (blockNumber: string) => {
const requestContent = {
id: 0,
jsonrpc: '2.0',
method: 'eth_getBlockByNumber',
params: [blockNumber, false],
};
const requestHeaders = {
headers: {
Accept: 'application/json',
'Accept-encoding': 'gzip, deflate',
'Content-Type': 'application/json',
},
};
const response = await axios.post(
process.env.CHAIN_JSON_RPC_URL!,
requestContent,
requestHeaders
);
return response.data.result;
}