-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
218 lines (196 loc) · 7.22 KB
/
server.mjs
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
218
/* eslint-disable no-unused-vars */
import { createAlchemyWeb3 } from '@alch/alchemy-web3';
import { WebClient } from '@slack/web-api';
import {
webhookOptions,
fetcher,
openseaFetchOptions,
SLACK_API_TOKEN,
channelId,
openseaForceUpdateURL,
networkStrings,
getContractAbi,
blackholeAddress,
newMintString,
BIRTHBLOCK_CONTRACT_ADDRESS,
BIRTHBLOCK_WEBHOOK_URL,
TOKEN_GARDEN_CONTRACT_ADDRESS,
TOKEN_GARDEN_WEBHOOK_URL,
alchemyUpdateWebhookAddressesURL,
notifyOptions,
AddAddressToTokenGardenListener,
HEARTBEAT_CONTRACT_ADDRESS,
HEARTBEAT_WEBHOOK_URL,
LOGBOOK_CONTRACT_ADDRESS,
LOGBOOK_WEBHOOK_URL,
NOMAD_WHITEHAT_CONTRACT_ADDRESS,
NOMAD_WHITEHAT_WEBHOOK_URL,
LLAMA_PFP_CONTRACT_ADDRESS,
LLAMA_PFP_WEBHOOK_URL,
ROBO_NOVA_CONTRACT_ADDRESS,
ROBO_NOVA_WEBHOOK_URL,
CDMX_AXOLOTLS_CONTRACT_ADDRESS,
AVATAR_STUDIO_WEBHOOK_URL,
VANDERBILT_CONTRACT_ADDRESS,
sleep,
NETWORK,
networkNames,
alchemyWebsocketRrcUrl,
} from './utils/index.mjs';
import { logError, logSuccess } from './utils/logging.mjs';
const web3 = createAlchemyWeb3(alchemyWebsocketRrcUrl);
const slackClient = new WebClient(SLACK_API_TOKEN);
async function slack(text) {
return await slackClient.chat.postMessage({
channel: channelId,
text,
});
}
const contracts = [
{
nftName: 'BirthBlock',
contractAddress: BIRTHBLOCK_CONTRACT_ADDRESS,
webhookURL: BIRTHBLOCK_WEBHOOK_URL,
testNetwork: networkNames.rinkeby,
},
{
nftName: 'Token Garden',
contractAddress: TOKEN_GARDEN_CONTRACT_ADDRESS,
webhookURL: TOKEN_GARDEN_WEBHOOK_URL,
testNetwork: networkNames.rinkeby,
},
{
nftName: 'Heartbeat',
contractAddress: HEARTBEAT_CONTRACT_ADDRESS,
webhookURL: HEARTBEAT_WEBHOOK_URL,
testNetwork: networkNames.rinkeby,
},
{
nftName: 'Logbook',
contractAddress: LOGBOOK_CONTRACT_ADDRESS,
webhookURL: LOGBOOK_WEBHOOK_URL,
testNetwork: networkNames.rinkeby,
},
{
nftName: 'Nomad whitehat',
contractAddress: NOMAD_WHITEHAT_CONTRACT_ADDRESS,
webhookURL: NOMAD_WHITEHAT_WEBHOOK_URL,
testNetwork: networkNames.rinkeby,
},
{
nftName: 'llamaPfp',
contractAddress: LLAMA_PFP_CONTRACT_ADDRESS,
webhookURL: LLAMA_PFP_WEBHOOK_URL,
testNetwork: networkNames.sepolia,
},
{
nftName: 'robo-nova',
contractAddress: ROBO_NOVA_CONTRACT_ADDRESS,
webhookURL: ROBO_NOVA_WEBHOOK_URL,
testNetwork: networkNames.sepolia,
},
// {
// nftName: 'cdmx-axolotls',
// contractAddress: CDMX_AXOLOTLS_CONTRACT_ADDRESS,
// webhookURL: AVATAR_STUDIO_WEBHOOK_URL,
// testNetwork: networkNames.sepolia,
// },
{
nftName: 'commodore', // project slug
contractAddress: VANDERBILT_CONTRACT_ADDRESS,
webhookURL: AVATAR_STUDIO_WEBHOOK_URL,
testNetwork: networkNames.sepolia,
},
];
// eslint-disable-next-line no-unused-vars
for (const contractData of contracts) {
const { nftName, contractAddress, webhookURL, testNetwork } = contractData;
console.log(`Starting ${nftName} listener... network: ${NETWORK}, testNetwork: ${testNetwork}`);
// sorry Rinkeby you're over
if (NETWORK === networkNames.ethereum || NETWORK === testNetwork) {
let status;
let contractAbi;
try {
({ status, result: contractAbi } = await getContractAbi(contractAddress));
await sleep(500);
} catch (error) {
console.log(`contract: ${nftName}`, error);
logError(error);
}
if (status !== '1') {
console.error(`getContractAbi Error: ${contractAbi}`);
process.exit(1);
}
const contract = new web3.eth.Contract(JSON.parse(contractAbi), contractAddress);
const logData = {
level: 'info',
function_name: `setUpListenerFor${nftName}`,
contract_address: contractAddress,
};
console.log(
`listening on https://${networkStrings.etherscan}etherscan.io/address/${contractAddress} : ${webhookURL}`,
);
logSuccess(
logData,
`listening on https://${networkStrings.etherscan}etherscan.io/address/${contractAddress} : ${webhookURL}`,
);
contract.events
.Transfer({
filter: { from: blackholeAddress },
})
.on('data', async (event) => {
const body = {
minterAddress: event.returnValues[1],
tokenId: event.returnValues[2],
contractAddress,
nftName,
};
const logData = {
level: 'info',
function_name: `${nftName}Mint`,
nft_name: nftName,
contract_address: contractAddress,
wallet_address: body.minterAddress,
token_id: body.tokenId,
};
const message = `${body.minterAddress} minted tokenId ${body.tokenId} for ${nftName}`;
logSuccess(logData, message);
let status, result; // could also pull out 'message'
/* Send data to service */
try {
({ status, result } = await fetcher(webhookURL, webhookOptions(body)));
if (nftName === 'Token Garden') {
let alchemyData = 'ok so its nothing?';
alchemyData = await fetcher(
alchemyUpdateWebhookAddressesURL,
notifyOptions(AddAddressToTokenGardenListener(body.minterAddress)),
);
}
/* If no error from service, force update Opensea and send Slack message */
if (status == 1) {
const { minterAddress, tokenId, ensName } = result;
const userName = ensName || minterAddress.substr(0, 6);
console.log(
`${minterAddress} with tokenId ${tokenId} has been added or updated for ${nftName}`,
);
await slack(newMintString(userName, tokenId, nftName, contractAddress));
// opensea force update happens in the queued job for Token Garden & Heartbeat
if (nftName !== 'Token Garden' && nftName !== 'Heartbeat') {
const { permalink } = await fetcher(
openseaForceUpdateURL(tokenId, contractAddress),
openseaFetchOptions,
4, // max retries, 16s
);
console.log(permalink);
}
}
} catch (error) {
logError(logData, error);
}
})
.on('error', (error) => {
logError(logData, error);
})
.on('end', () => console.log('connection ended'));
}
}