Skip to content

Commit

Permalink
add blocks counter and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
defi-dev committed Nov 28, 2023
1 parent e11a186 commit d17922f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ let app: App;
resolve_min_success_count: process.env.NETWORK_MIN_SUCCESS_RESOLVE
? parseInt(process.env.NETWORK_MIN_SUCCESS_RESOLVE)
: undefined,
block_logs_mode: process.env.NETWORK_BLOCK_LOG_MODE === 'true' || process.env.NETWORK_BLOCK_LOG_MODE === '1',
block_logs_mode: process.env.NETWORK_BLOCK_LOGS_MODE === 'true' || process.env.NETWORK_BLOCK_LOGS_MODE === '1',
agents: {
[agentAddress]: agentConfig,
},
Expand Down
9 changes: 2 additions & 7 deletions app/ConfigGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,12 @@ export function getDefaultExecutorConfig() {
gas_price_priority_add_gwei: 2,
};
}
//TODO: use AVERAGE_BLOCK_TIME_SECONDS
const maxNewBlockDelayByNetwork = {
gnosis: 10,
sepolia: 20,
mainnet: 20,
};

export function getDefaultNetworkConfig(name) {
const avgBlockTime = AVERAGE_BLOCK_TIME_SECONDS[name];
return {
max_block_delay: 60,
max_new_block_delay: maxNewBlockDelayByNetwork[name] || 10,
max_new_block_delay: avgBlockTime ? avgBlockTime * 3 : 10,
resolve_min_success_count: 3,
block_logs_mode: false,
};
Expand Down
3 changes: 3 additions & 0 deletions app/agents/AbstractAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export abstract class AbstractAgent implements IAgent {
const keeperConfig = await this.queryKeeperDetails(this.keeperId);
this.myStake = keeperConfig.currentStake;
this.myKeeperIsActive = keeperConfig.isActive;
if (!this.myKeeperIsActive) {
this.clog('warn', `Your keeper(${this.keeperId}) is not active in agent contract(${this.contract.address})`);
}

if (toChecksummedAddress(this.workerSigner.address) !== toChecksummedAddress(keeperConfig.worker)) {
throw this.err(
Expand Down
27 changes: 25 additions & 2 deletions app/services/ContractEventsEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default class ContractEventsEmitter {
contractByAddress = {};
eventByContractTopic = {};

contractEmitterCount = {};
emitByBlockCount = {};

constructor(_blockLogsMode) {
this.blockLogsMode = _blockLogsMode;
}
Expand All @@ -25,18 +28,31 @@ export default class ContractEventsEmitter {
if (!this.blockLogsMode && !forceEmit) {
return;
}
console.log('emitByBlockLogs logs.length', logs.length);
let address, blockNumber;
logs.forEach(l => {
const address = l.address.toLowerCase();
address = l.address.toLowerCase();
if (!this.contractEmitterByAddress[address]) {
return;
}
if (!this.emitByBlockCount[address]) {
this.emitByBlockCount[address] = {};
}
const eventName = this.eventByContractTopic[address][l.topics[0]];
if (!eventName) {
return;
}
blockNumber = l.blockNumber;
this.emitByBlockCount[address][blockNumber] = (this.emitByBlockCount[address][blockNumber] || 0) + 1;
this.emitByContractAddress(address, eventName, this.contractByAddress[address].parseLog(l));
});
if (blockNumber && address) {
console.log(
blockNumber + ' block logs count(query:',
this.emitByBlockCount[address][blockNumber],
'websocket:',
this.contractEmitterCount[address][blockNumber] + ')',
);
}
}

on(contract: ContractWrapper, eventName, callback) {
Expand All @@ -51,6 +67,9 @@ export default class ContractEventsEmitter {
if (!this.eventByContractTopic[address][eventTopic]) {
this.eventByContractTopic[address][eventTopic] = eventName;
contract.on(eventName, value => {
const { blockNumber } = value;
this.contractEmitterCount[address][blockNumber] = (this.contractEmitterCount[address][blockNumber] || 0) + 1;
delete this.contractEmitterCount[address][blockNumber - 1];
if (this.blockLogsMode) {
return;
}
Expand All @@ -61,6 +80,10 @@ export default class ContractEventsEmitter {
}

contractEmitter(contract): EventEmitter {
const address = contract.address.toLowerCase();
if (!this.contractEmitterCount[address]) {
this.contractEmitterCount[address] = {};
}
return {
on: (eventName, callback) => {
this.on(contract, eventName, callback);
Expand Down

0 comments on commit d17922f

Please sign in to comment.