-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullFromVera.mjs
120 lines (108 loc) · 3.45 KB
/
pullFromVera.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
import pg from "pg";
import createSubscriber from "pg-listen";
import dotenv from "dotenv";
import fetch from "node-fetch";
import logger from "./logger.mjs";
dotenv.config();
const { Client } = pg;
const veraClient = new Client({
host: process.env.VERA_HOST,
database: process.env.VERA_DB,
user: process.env.VERA_USER,
password: process.env.VERA_PASSWORD,
port: process.env.VERA_PORT,
});
const subscriber = createSubscriber({
host: process.env.VERA_HOST,
port: parseInt(process.env.VERA_PORT) || 5432,
database: process.env.VERA_DB,
user: process.env.VERA_USER,
password: process.env.VERA_PASSWORD,
});
async function main() {
await veraClient.connect();
subscriber.notifications.on("new_verified_contract", async (payload) => {
logger.info("Received notification in 'new_verified_contract'", {
veraVerifiedContractId: payload.id,
});
// Skip verified_contracts pushed by sourcify
if (payload.created_by === "sourcify") {
logger.info("Contract inserted by Sourcify, skipping.");
return;
}
// Get all FK information
const {
rows: [deployment],
} = await veraClient.query(
"SELECT * FROM contract_deployments WHERE id = $1",
[payload.deployment_id]
);
const {
rows: [compilation],
} = await veraClient.query(
"SELECT * FROM compiled_contracts WHERE id = $1",
[payload.compilation_id]
);
// For some reason inside `compilation.compiler_settings` there is a compilationTarget parameter that is not supported by solc
const settings = compilation.compiler_settings;
delete settings.compilationTarget;
const settingsJson = JSON.stringify({
language: "Solidity",
sources: Object.keys(compilation.sources).reduce((obj, current) => {
obj[current] = {
content: compilation.sources[current],
};
return obj;
}, {}),
settings: compilation.compiler_settings,
});
logger.silly(
"Contract's information fetched, calling sourcify-server /verify/solc-json with parameters",
{
veraVerifiedContractId: payload.id,
address: "0x" + deployment.address.toString("hex"),
chainId: deployment.chain_id,
jsonInput: settingsJson,
}
);
try {
const res = await fetch(
`${process.env.SOURCIFY_SERVER_HOST}/verify/solc-json`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
compilerVersion: compilation.version,
contractName: compilation.name,
address: "0x" + deployment.address.toString("hex"),
chainId: deployment.chain_id,
files: {
"settings.json": settingsJson,
},
}),
}
);
if (res.status === 200) {
const response = await res.json();
logger.info("Contract successfully verified", {
veraVerifiedContractId: payload.id,
address: response.result[0].address,
chainId: response.result[0].chainId,
status: response.result[0].status,
});
} else {
throw new Error((await res.json()).error);
}
} catch (error) {
logger.warn("Failed to verify contract with error", {
error: error.message,
});
}
});
subscriber.connect();
logger.info("Started listening for VerA verified_contracts...");
subscriber.listenTo("new_verified_contract");
}
main();