forked from kleros/gtcr-twitter-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (148 loc) · 4.29 KB
/
index.js
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
const ethers = require('ethers')
const level = require('level')
const Twitter = require('twitter-lite')
const fetch = require('node-fetch')
const _GTCRFactory = require('./abis/GTCRFactory.json')
const _GeneralizedTCRView = require('./abis/GeneralizedTCRView.json')
const _LightGTCRFactory = require('./abis/LightGTCRFactory.json')
const _LightGeneralizedTCRView = require('./abis/LightGeneralizedTCRView.json')
const gtcrBot = require('./gtcr')
const lightGtcrBot = require('./light-gtcr')
const db = level('./db')
let twitterClient
if (
!!process.env.CONSUMER_KEY &&
!!process.env.CONSUMER_SECRET &&
!!process.env.ACCESS_TOKEN &&
!!process.env.ACCESS_TOKEN_SECRET
)
twitterClient = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
})
const NETWORKS = Object.freeze({
ethereum: 1,
xDai: 100,
kovan: 42
})
const providerUrls = JSON.parse(process.env.PROVIDER_URLS)
const providerMainnet = new ethers.providers.JsonRpcProvider(
providerUrls[NETWORKS.ethereum]
)
const providerXDai = new ethers.providers.JsonRpcProvider(
providerUrls[NETWORKS.xDai]
)
const providerKovan = new ethers.providers.JsonRpcProvider(
providerUrls[NETWORKS.kovan]
)
providerMainnet.pollingInterval = 60 * 1000 // Poll every minute.
providerXDai.pollingInterval = 60 * 1000 // Poll every minute.
providerKovan.pollingInterval = 60 * 1000 // Poll every minute
const factoryAddresses = JSON.parse(process.env.FACTORY_ADDRESSES)
const gtrcViewAddresses = JSON.parse(process.env.GENERALIZED_TCR_VIEW_ADDRESSES)
const lFactoryAddresses = JSON.parse(process.env.LFACTORY_ADDRESSES)
const lGtrcViewAddresses = JSON.parse(
process.env.LGENERALIZED_TCR_VIEW_ADDRESSES
)
const gtcrFactoryMainnet = new ethers.Contract(
factoryAddresses[NETWORKS.ethereum],
_GTCRFactory,
providerMainnet
)
const gtcrFactoryXDai = new ethers.Contract(
factoryAddresses[NETWORKS.xDai],
_GTCRFactory,
providerXDai
)
const gtcrViewMainnet = new ethers.Contract(
gtrcViewAddresses[NETWORKS.ethereum],
_GeneralizedTCRView,
providerMainnet
)
const gtcrViewXDai = new ethers.Contract(
gtrcViewAddresses[NETWORKS.xDai],
_GeneralizedTCRView,
providerXDai
)
const lightGtcrFactoryMainnet = new ethers.Contract(
lFactoryAddresses[NETWORKS.ethereum],
_LightGTCRFactory,
providerMainnet
)
const lightGtcrFactoryXDai = new ethers.Contract(
lFactoryAddresses[NETWORKS.xDai],
_LightGTCRFactory,
providerXDai
)
const lightGtcrViewMainnet = new ethers.Contract(
lGtrcViewAddresses[NETWORKS.ethereum],
_LightGeneralizedTCRView,
providerMainnet
)
const lightGtcrViewXDai = new ethers.Contract(
lGtrcViewAddresses[NETWORKS.xDai],
_LightGeneralizedTCRView,
providerXDai
)
;(async () => {
console.info('Instantiating bitly client:', process.env.BITLY_TOKEN)
const groupIDResponse = await fetch('https://api-ssl.bitly.com/v4/groups', {
method: 'get',
headers: {
Authorization: `Bearer ${process.env.BITLY_TOKEN}`
}
})
const groupID = (await groupIDResponse.json()).groups[0].guid
console.info(`Got bitly groupID ${groupID}`)
const bitly = {
shorten: async url =>
`https://${
(
await (
await fetch('https://api-ssl.bitly.com/v4/shorten', {
method: 'post',
headers: {
Authorization: `Bearer ${process.env.BITLY_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
long_url: url,
group_guid: groupID
})
})
).json()
).id
}`
}
// // initialize gtrcBot for Mainnet
gtcrBot(
providerMainnet,
gtcrFactoryMainnet,
twitterClient,
gtcrViewMainnet,
db,
bitly
)
// // initialize gtrcBot for Gnosis chain
gtcrBot(providerXDai, gtcrFactoryXDai, twitterClient, gtcrViewXDai, db, bitly)
// initialize lGtrcBot for Mainnet
lightGtcrBot(
providerMainnet,
lightGtcrFactoryMainnet,
twitterClient,
lightGtcrViewMainnet,
db,
bitly
)
// initialize lGtrcBot for Gnosis chain
lightGtcrBot(
providerXDai,
lightGtcrFactoryXDai,
twitterClient,
lightGtcrViewXDai,
db,
bitly
)
})()