This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate-beacons.ts
253 lines (221 loc) · 9.2 KB
/
update-beacons.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import * as node from '@api3/airnode-node';
import { DapiServer, DapiServer__factory as DapiServerFactory } from '@api3/airnode-protocol-v1';
import { go, GoAsyncOptions } from '@api3/promise-utils';
import { ethers } from 'ethers';
import { isEmpty } from 'lodash';
import { getCurrentBlockNumber } from './block-number';
import { checkOnchainDataFreshness, checkSignedDataFreshness, checkUpdateCondition } from './check-condition';
import { INT224_MAX, INT224_MIN, NO_BEACONS_EXIT_CODE, PROTOCOL_ID } from './constants';
import { getOracleGasPrice } from './gas-oracle';
import { logger, LogOptionsOverride } from './logging';
import { getState, Provider } from './state';
import { getTransactionCount } from './transaction-count';
import { shortenAddress, sleep, prepareGoOptions } from './utils';
import { BeaconUpdate } from './validation';
type ProviderSponsorBeacons = {
provider: Provider;
sponsorAddress: string;
updateInterval: number;
beacons: BeaconUpdate[];
};
export const groupBeaconsByProviderSponsor = () => {
const { config, providers: stateProviders } = getState();
return Object.entries(config.triggers.beaconUpdates).reduce(
(acc: ProviderSponsorBeacons[], [chainId, beaconUpdatesPerSponsor]) => {
const providers = stateProviders[chainId];
const providerSponsorGroups = Object.entries(beaconUpdatesPerSponsor).reduce(
(acc: ProviderSponsorBeacons[], [sponsorAddress, beaconUpdate]) => {
return [...acc, ...providers.map((provider) => ({ provider, sponsorAddress, ...beaconUpdate }))];
},
[]
);
return [...acc, ...providerSponsorGroups];
},
[]
);
};
export const initiateBeaconUpdates = () => {
logger.debug('Initiating beacon updates');
const providerSponsorBeaconsGroups = groupBeaconsByProviderSponsor();
if (isEmpty(providerSponsorBeaconsGroups)) {
logger.error('No beacons for processing found. Stopping.');
process.exit(NO_BEACONS_EXIT_CODE);
}
providerSponsorBeaconsGroups.forEach(updateBeaconsInLoop);
};
export const updateBeaconsInLoop = async (providerSponsorBeacons: ProviderSponsorBeacons) => {
while (!getState().stopSignalReceived) {
const startTimestamp = Date.now();
const { updateInterval } = providerSponsorBeacons;
await updateBeacons(providerSponsorBeacons);
const duration = Date.now() - startTimestamp;
const waitTime = Math.max(0, updateInterval * 1_000 - duration);
await sleep(waitTime);
}
};
// We pass return value from `prepareGoOptions` (with calculated timeout) to every `go` call in the function to enforce the update cycle.
// This solution is not precise but since chain operations are the only ones that actually take some time this should be a good enough solution.
export const updateBeacons = async (providerSponsorBeacons: ProviderSponsorBeacons) => {
const { config, beaconValues } = getState();
const { provider, sponsorAddress, beacons } = providerSponsorBeacons;
const { rpcProvider, chainId, providerName } = provider;
const logOptionsSponsor = {
meta: { chainId, providerName },
additional: { Sponsor: shortenAddress(sponsorAddress) },
};
logger.debug(`Processing beacon updates`, logOptionsSponsor);
const startTime = Date.now();
// All the beacon updates for given provider & sponsor have up to <updateInterval> seconds to finish
const totalTimeout = providerSponsorBeacons.updateInterval * 1_000;
// Prepare contract for beacon updates
const contractAddress = config.chains[chainId].contracts['DapiServer'];
const contract = DapiServerFactory.connect(contractAddress, rpcProvider);
// Get current block number
const blockNumber = await getCurrentBlockNumber(provider, prepareGoOptions(startTime, totalTimeout));
if (blockNumber === null) {
logger.warn(`Unable to obtain block number`, logOptionsSponsor);
return;
}
// Derive sponsor wallet address
const sponsorWallet = node.evm
.deriveSponsorWalletFromMnemonic(config.airseekerWalletMnemonic, sponsorAddress, PROTOCOL_ID)
.connect(rpcProvider);
// Get transaction count
const transactionCount = await getTransactionCount(
provider,
sponsorWallet.address,
blockNumber,
prepareGoOptions(startTime, totalTimeout)
);
if (transactionCount === null) {
logger.warn(`Unable to fetch transaction count`, logOptionsSponsor);
return;
}
// Process beacon updates
let nonce = transactionCount;
const voidSigner = new ethers.VoidSigner(ethers.constants.AddressZero, rpcProvider);
for (const beacon of beacons) {
const logOptionsBeaconId = {
...logOptionsSponsor,
additional: {
...logOptionsSponsor.additional,
'Sponsor-Wallet': shortenAddress(sponsorWallet.address),
'Beacon-ID': beacon.beaconId,
},
};
const beaconUpdateData = { ...beacon, ...config.beacons[beacon.beaconId] };
logger.debug(`Updating beacon`, logOptionsBeaconId);
// Check whether we have a value for given beacon
const newBeaconResponse = beaconValues[beaconUpdateData.beaconId];
if (!newBeaconResponse) {
logger.warn(`No data available for beacon. Skipping.`, logOptionsBeaconId);
continue;
}
// Based on https://github.com/api3dao/airnode-protocol-v1/blob/main/contracts/dapis/DapiServer.sol#L878
const newBeaconValue = ethers.BigNumber.from(
ethers.utils.defaultAbiCoder.decode(['int256'], newBeaconResponse.data.value)[0]
);
if (newBeaconValue.gt(INT224_MAX) || newBeaconValue.lt(INT224_MIN)) {
logger.warn(`New beacon value is out of type range. Skipping.`, logOptionsBeaconId);
continue;
}
const onChainData = await readOnChainBeaconData(
voidSigner,
contract,
beaconUpdateData.beaconId,
prepareGoOptions(startTime, totalTimeout),
logOptionsBeaconId
);
if (!onChainData) {
continue;
}
// Check that signed data is newer than on chain value
const isSignedDataFresh = checkSignedDataFreshness(onChainData.timestamp, newBeaconResponse.data.timestamp);
if (!isSignedDataFresh) {
logger.warn(`Signed data older than on chain record. Skipping.`, logOptionsBeaconId);
continue;
}
// Check that on chain data is newer than hearbeat interval
const isOnchainDataFresh = checkOnchainDataFreshness(onChainData.timestamp, beaconUpdateData.heartbeatInterval);
if (!isOnchainDataFresh) {
logger.info(
`On chain data timestamp older than heartbeat. Updating without condition check.`,
logOptionsBeaconId
);
} else {
// Check beacon condition
const shouldUpdate = checkUpdateCondition(onChainData.value, beaconUpdateData.deviationThreshold, newBeaconValue);
if (shouldUpdate === null) {
logger.warn(`Unable to fetch current beacon value`, logOptionsBeaconId);
// This can happen only if we reach the total timeout so it makes no sense to continue with the rest of the beacons
return;
}
if (!shouldUpdate) {
logger.info(`Deviation threshold not reached. Skipping.`, logOptionsBeaconId);
continue;
}
logger.info(`Deviation threshold reached. Updating.`, logOptionsBeaconId);
}
// Get gas price from oracle
const gasPrice = await getOracleGasPrice(provider, config.chains[chainId].options.gasOracle);
// Update beacon
const tx = await go(
() =>
contract
.connect(sponsorWallet)
.updateBeaconWithSignedData(
beaconUpdateData.airnode,
beaconUpdateData.templateId,
newBeaconResponse.data.timestamp,
newBeaconResponse.data.value,
newBeaconResponse.signature,
{
gasLimit: ethers.BigNumber.from(config.chains[chainId].options.fulfillmentGasLimit),
type: config.chains[chainId].options.txType === 'eip1559' ? 2 : 0,
gasPrice,
nonce,
}
),
{
...prepareGoOptions(startTime, totalTimeout),
onAttemptError: (goError) =>
logger.warn(`Failed attempt to update beacon. Error ${goError.error}`, logOptionsBeaconId),
}
);
if (!tx.success) {
logger.warn(`Unable to update beacon with nonce ${nonce}. Error: ${tx.error}`, logOptionsBeaconId);
return;
}
logger.info(
`Beacon successfully updated with value ${newBeaconValue}. Tx hash ${tx.data.hash}.`,
logOptionsBeaconId
);
nonce++;
}
};
export interface OnChainBeaconData {
value: ethers.BigNumber;
timestamp: number;
}
export const readOnChainBeaconData = async (
voidSigner: ethers.VoidSigner,
dapiServer: DapiServer,
beaconId: string,
goOptions: GoAsyncOptions,
logOptions: LogOptionsOverride
): Promise<OnChainBeaconData | null> => {
const logOptionsDapiServerAddress = {
...logOptions,
additional: { ...logOptions.additional, 'Dapi-Server': dapiServer.address },
};
const goDataFeed = await go(() => dapiServer.connect(voidSigner).readDataFeedWithId(beaconId), {
...goOptions,
onAttemptError: (goError) =>
logger.warn(`Failed attempt to read data feed. Error: ${goError.error}`, logOptionsDapiServerAddress),
});
if (!goDataFeed.success) {
logger.warn(`Unable to read data feed. Error: ${goDataFeed.error}`, logOptionsDapiServerAddress);
return null;
}
return goDataFeed.data;
};