Skip to content

Commit

Permalink
Merge branch 'fb-wss' into origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Metroxe committed Dec 13, 2023
2 parents 656b3b2 + a0c4038 commit ecc0516
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
55 changes: 18 additions & 37 deletions packages/core/src/operator/listenForChallenges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,32 @@ import { Challenge, getChallenge } from "../index.js";
* @param callback - The callback function to be triggered when ChallengeSubmitted event is emitted.
* @returns A function that can be called to stop listening for the event.
*/
export function listenForChallenges(callback: (challengeNumber: bigint, challenge: Challenge, event: any) => void, log: (log: string) => void = () => {}): () => void {
let intervalId: NodeJS.Timeout;
let refereeContract: ethers.Contract;
let provider: ethers.JsonRpcProvider | ethers.WebSocketProvider | ethers.AlchemyProvider;
export function listenForChallenges(callback: (challengeNumber: bigint, challenge: Challenge, event: any) => void): () => void {
// get a provider for the arb one network
const provider = getProvider();

const challengeNumberMap: { [challengeNumber: string]: boolean } = {};

const startListening = () => {
log(`[${new Date().toISOString()}] Starting to listen for ChallengeSubmitted events`);

if (intervalId) {
log(`[${new Date().toISOString()}] Clearing existing interval and listeners`);
clearInterval(intervalId);
if (refereeContract) {
refereeContract.removeAllListeners("ChallengeSubmitted");
}
}

log(`[${new Date().toISOString()}] Creating provider and contract instance`);
provider = getProvider("https://arb-mainnet.g.alchemy.com/v2/p_LSgTIj_JtEt3JPM7IZIZFL1a70yvQJ", true);
// create an instance of the Referee contract
const refereeContract = new ethers.Contract(config.refereeAddress, RefereeAbi, provider);

refereeContract = new ethers.Contract(config.refereeAddress, RefereeAbi, provider);

log(`[${new Date().toISOString()}] Setting up listener for ChallengeSubmitted events`);
refereeContract.on("ChallengeSubmitted", async (challengeNumber, event) => {
if (!challengeNumberMap[challengeNumber.toString()]) {
log(`[${new Date().toISOString()}] ChallengeSubmitted event received for new challengeNumber: ${challengeNumber}`);
challengeNumberMap[challengeNumber.toString()] = true;
// create a map to keep track of challengeNumbers that have called the callback
const challengeNumberMap: { [challengeNumber: string]: boolean } = {};

const challenge = await getChallenge(challengeNumber);
// listen for the ChallengeSubmitted event
refereeContract.on("ChallengeSubmitted", async (challengeNumber, event) => {

void callback(challengeNumber, challenge, event);
}
});
// if the challengeNumber has not been seen before, call the callback and add it to the map
if (!challengeNumberMap[challengeNumber.toString()]) {
challengeNumberMap[challengeNumber.toString()] = true;

log(`[${new Date().toISOString()}] Setting up interval to recreate listener every 4 minutes`);
intervalId = setInterval(startListening, 4 * 60 * 1000);
};
// lookup the challenge
const challenge = await getChallenge(challengeNumber);

log(`[${new Date().toISOString()}] Starting initial listening`);
startListening();
void callback(challengeNumber, challenge, event);
}
});

// return a function that can be used to stop listening for the event
return () => {
log(`[${new Date().toISOString()}] Stopping listening for ChallengeSubmitted events`);
clearInterval(intervalId);
refereeContract.removeAllListeners("ChallengeSubmitted");
};
}
2 changes: 1 addition & 1 deletion packages/core/src/operator/operatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export async function operatorRuntime(

// start a listener for new challenges
const challengeNumberMap: { [challengeNumber: string]: boolean } = {};
async function listenForChallengesCallback(challengeNumber: bigint, challenge: Challenge) {
async function listenForChallengesCallback(challengeNumber: bigint, challenge: Challenge, event?: any) {

if (challenge.openForSubmissions) {
logFunction(`Received new challenge with number: ${challengeNumber}.`);
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/utils/getProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const providers: { [key: string]: ethers.JsonRpcProvider | ethers.WebSocketProvi
* @returns An ethers provider.
*/
export function getProvider(
rpcUrl: string | undefined = undefined,
rpcUrl: string | undefined = "wss://arb-mainnet.g.alchemy.com/v2/p_LSgTIj_JtEt3JPM7IZIZFL1a70yvQJ",
ignoreMemo: boolean = false,
alchemyNetwork: Networkish = {name: "arbitrum", chainId: 42161}
): ethers.JsonRpcProvider | ethers.WebSocketProvider | ethers.AlchemyProvider {
Expand All @@ -24,10 +24,13 @@ export function getProvider(

let provider: ethers.JsonRpcProvider | ethers.WebSocketProvider | ethers.AlchemyProvider;
if (memoKey.startsWith('http') || memoKey.startsWith('https')) {
console.log("Provisioning http provider.");
provider = new ethers.JsonRpcProvider(memoKey);
} else if (memoKey.startsWith('wss')) {
console.log("Provisioning web socket provider.");
provider = new ethers.WebSocketProvider(memoKey);
} else {
console.log("Provisioning alchemy provider.");
const apiKey = 'p_LSgTIj_JtEt3JPM7IZIZFL1a70yvQJ';
provider = new ethers.AlchemyProvider(alchemyNetwork, apiKey);
}
Expand Down

0 comments on commit ecc0516

Please sign in to comment.