|
1 | 1 | import db from "src/db"; |
2 | 2 | import logger from "src/utils/logger-handler"; |
3 | | -import {EventsProcessed, EventsQuery,} from "src/interfaces/block-chain-service"; |
4 | | -import {Network_v2, Web3Connection} from "@taikai/dappkit"; |
5 | | -import {Op} from "sequelize"; |
6 | | -import {getChainsRegistryAndNetworks} from "../utils/block-process"; |
7 | | -import { handleCurators, updateIsCurrentlyCurator } from "src/modules/handle-curators"; |
| 3 | +import { EventsProcessed, EventsQuery } from "src/interfaces/block-chain-service"; |
| 4 | +import { updateNetworkParameters } from "src/modules/update-network-params"; |
| 5 | +import { Web3Connection } from "@taikai/dappkit"; |
8 | 6 |
|
9 | | -export const name = "updateNetworkParameters"; |
| 7 | +export const name = "UpdateNetworkParameters"; |
10 | 8 | export const schedule = "0 0 * * *"; |
11 | 9 | export const description = "update network parameters on database"; |
12 | 10 | export const author = "Vitor Hugo"; |
13 | 11 |
|
14 | | -const {NEXT_WALLET_PRIVATE_KEY: privateKey} = process.env; |
15 | | - |
16 | 12 | export async function action(query?: EventsQuery): Promise<EventsProcessed> { |
17 | 13 | const eventsProcessed: EventsProcessed = {}; |
18 | 14 |
|
19 | 15 | logger.info(`${name} start`); |
20 | 16 |
|
21 | | - const entries = await getChainsRegistryAndNetworks(); |
22 | | - for (const [web3Host, {chainId: chain_id,}] of entries) { |
23 | | - const where = { |
24 | | - chain_id, |
25 | | - ...query?.networkName ? {name: {[Op.iLike]: query.networkName}} : {}, |
26 | | - } |
27 | | - |
28 | | - const networks = await db.networks.findAll({ |
29 | | - where, |
30 | | - include: [{ association: "curators", required: false }], |
31 | | - }); |
32 | | - |
33 | | - try { |
34 | | - |
35 | | - if (!networks || !networks.length) { |
36 | | - logger.warn(`${name} found no networks`); |
37 | | - return eventsProcessed; |
38 | | - } |
39 | | - |
40 | | - const web3Connection = new Web3Connection({web3Host, privateKey}); |
41 | | - await web3Connection.start(); |
42 | | - |
43 | | - for (const network of networks) { |
44 | | - try { |
45 | | - const networkContract = new Network_v2(web3Connection, network.networkAddress); |
46 | | - await networkContract.start(); |
47 | | - const councilAmount = await networkContract.councilAmount(); |
48 | | - const needsToUpdateCurators = councilAmount !== network.councilAmount; |
49 | | - |
50 | | - network.councilAmount = councilAmount; |
51 | | - network.disputableTime = (await networkContract.disputableTime()) / 1000; |
52 | | - network.draftTime = (await networkContract.draftTime()) / 1000; |
53 | | - network.oracleExchangeRate = await networkContract.oracleExchangeRate(); |
54 | | - network.mergeCreatorFeeShare = await networkContract.mergeCreatorFeeShare(); |
55 | | - network.percentageNeededForDispute = await networkContract.percentageNeededForDispute(); |
56 | | - network.cancelableTime = (await networkContract.cancelableTime()) / 1000; |
57 | | - network.proposerFeeShare = await networkContract.proposerFeeShare(); |
| 17 | + if (!query || !query?.chainId || !query?.address) { |
| 18 | + logger.info(`${name} missing parameters`); |
| 19 | + return eventsProcessed; |
| 20 | + } |
58 | 21 |
|
59 | | - await network.save(); |
| 22 | + const { chainId, address } = query; |
60 | 23 |
|
61 | | - if (needsToUpdateCurators) { |
62 | | - await Promise.all(network.curators.map(async (curator) => { |
63 | | - const actorVotesResume = await networkContract.getOraclesResume(curator.address); |
64 | | - await handleCurators(curator.address, actorVotesResume, councilAmount, network.id); |
65 | | - })); |
| 24 | + const chain = await db.chains.findOne({ |
| 25 | + where: { |
| 26 | + chainId: +chainId |
| 27 | + } |
| 28 | + }); |
66 | 29 |
|
67 | | - const currentCurators = await db.curators.findAll({ |
68 | | - where: { |
69 | | - isCurrentlyCurator: true, |
70 | | - networkId: network.id |
71 | | - } |
72 | | - }); |
| 30 | + if (!chain) { |
| 31 | + logger.info(`${name} invalid chain ${chainId}`); |
| 32 | + return eventsProcessed; |
| 33 | + } |
73 | 34 |
|
74 | | - network.councilMembers = currentCurators.map(({ address }) => address); |
75 | | - await network.save(); |
76 | | - } |
| 35 | + try { |
| 36 | + const connection = new Web3Connection({ |
| 37 | + web3Host: chain.privateChainRpc, |
| 38 | + skipWindowAssignment: true, |
| 39 | + }); |
| 40 | + await connection.start(); |
77 | 41 |
|
78 | | - eventsProcessed[network.name!] = ["updated"]; |
| 42 | + await updateNetworkParameters({ |
| 43 | + networkAddress: address, |
| 44 | + chainId: +chainId, |
| 45 | + connection |
| 46 | + }); |
79 | 47 |
|
80 | | - logger.info(`${name} parameters saved ${network.name} ${network.networkAddress}`); |
81 | | - } catch (error: any) { |
82 | | - logger.error(`${name} Failed to save parameters of ${network.name} ${network.networkAddress}`, error?.message || error.toString()); |
83 | | - } |
84 | | - } |
85 | | - } catch (err: any) { |
86 | | - logger.error(`${name} Error`, err?.message || err.toString()); |
87 | | - } |
| 48 | + eventsProcessed[address] = ["updated"]; |
88 | 49 |
|
| 50 | + logger.info(`${name} parameters saved ${address} ${chainId}`); |
| 51 | + } catch (error: any) { |
| 52 | + logger.error(`${name} Failed to save parameters of ${address} ${chainId}`, error?.message || error.toString()); |
89 | 53 | } |
90 | 54 |
|
91 | 55 | return eventsProcessed; |
|
0 commit comments