Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/async-2.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
shotaronowhere authored Sep 18, 2023
2 parents be47724 + 39c43ac commit dc726d9
Show file tree
Hide file tree
Showing 25 changed files with 473 additions and 320 deletions.
24 changes: 14 additions & 10 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
BOT_TOKEN=
BOT_USERNAME=

# Gnosis
CHAIN_ID = 100
WEB3_PROVIDER_URL=
PRIVATE_KEY=
TRANSACTION_BATCHER_CONTRACT_ADDRESS=
MODERATE_SUBGRAPH=https://api.thegraph.com/subgraphs/name/shotaronowhere/kleros-moderator-bot

REALITITY_ETH_V30=
REALITIO_ARBITRATOR_EN=
REALITIO_ARBITRATOR_ES=
TEMPLATE_ID=
WEB3_PROVIDER_URL=
BOT_USERNAME=

SUSIE_SUPPORT_EN=
SUSIE_SUPPORT_ES=
JUSTICE_LEAGUE_EN=
JUSTICE_LEAGUE_ES=
PRIVATE_KEY=
TRANSACTION_BATCHER_CONTRACT_ADDRESS=

NTBA_FIX_319=1
CHAIN_ID =
MODERATE_SUBGRAPH=https://api.thegraph.com/subgraphs/name/shotaronowhere/kleros-moderate-goerli
TRANSACTION_BATCHER_CONTRACT_ADDRESS=
JUSTICE_LEAGUE=
PRIVATE_KEY=
BOT_USERNAME=

# Optional
BETTERUPTIME_TOKEN=
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ subgraph/build/
!subgraph/build/.gitkeep
subgraph/generated/
!subgraph/generated/.gitkeep
cache/
cache/
# Local Netlify folder
.netlify
4 changes: 2 additions & 2 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
apps : [{
name: 'moderator_telegram_bot',
script: 'yarn',
script: './yarn.sh',
args: 'start-telegram-bot',
interpreter: '/bin/bash',
log_date_format : 'YYYY-MM-DD HH:mm Z',
Expand All @@ -13,7 +13,7 @@ module.exports = {
},
{
name: 'moderator_cron',
script: 'yarn',
script: './yarn.sh',
args: 'cron',
interpreter: '/bin/bash',
log_date_format : 'YYYY-MM-DD HH:mm Z',
Expand Down
114 changes: 114 additions & 0 deletions functions/realitioReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require('dotenv').config();
import {getRealityETHV30Provider, getRealitioArbitratorProvider} from "../lib/ethers"
import {JsonRpcProvider} from "@ethersproject/providers";
import { BigNumber } from "ethers";
import { RulingEvent } from "../lib/typechain/RealitioV21ArbitratorWithAppeals";
import axios from "axios";
import { StatusCodes } from "http-status-codes";
import { Handler, schedule } from "@netlify/functions";

const wait = (ms) => new Promise((r) => setTimeout(r, ms));

const retryOperation = (operation, delay, retries) =>
new Promise((resolve, reject) => {
return operation()
.then(resolve)
.catch((reason) => {
if (retries > 0) {
// log retry
console.log("retrying", retries);
console.log(reason);
return wait(delay)
.then(retryOperation.bind(null, operation, delay, retries - 1))
.then(resolve)
.catch(reject);
}
return reject(reason);
});
});

const handler: Handler = async () => {
try{
const provider = new JsonRpcProvider(process.env.WEB3_PROVIDER_URL);

const reality = getRealityETHV30Provider(process.env.REALITY_ETH_V30, process.env.PRIVATE_KEY, provider)
const arbitrator = getRealitioArbitratorProvider(process.env.REALITIO_ARBITRATOR_EN, process.env.PRIVATE_KEY, provider)

const finalizedBlock = await provider.getBlock("finalized");
const fromBlock = finalizedBlock.number - 10000; // past hour

const ruleEvents = await retryOperation(()=>arbitrator.queryFilter(arbitrator.filters.Ruling(), fromBlock, finalizedBlock.number), 1000, 10) as RulingEvent[]
for (const eventLog of ruleEvents) {
try{
const _disputeID: BigNumber = BigNumber.from(eventLog.args._disputeID)
const questionID: BigNumber = await retryOperation(()=>arbitrator.externalIDtoLocalID(_disputeID.toHexString()), 1000, 10) as BigNumber
const question = (await retryOperation(()=>reality.questions(questionID.toHexString()),1000,10)) as {
content_hash: string;
arbitrator: string;
opening_ts: number;
timeout: number;
finalize_ts: number;
is_pending_arbitration: boolean;
bounty: BigNumber;
best_answer: string;
history_hash: string;
bond: BigNumber;
min_bond: BigNumber;
}

const bestAnswer = question.best_answer

const query = {
query: `{
responses(where: {question_: {questionId: "${questionID.toHexString()}"}}){
historyHash
timestamp
answer
user
}
}`
}

const responses = (await axios.post(`https://api.thegraph.com/subgraphs/name/realityeth/realityeth-gnosis`,
query))?.data?.data?.responses ?? []

responses.sort((a,b) => a.timestamp-b.timestamp)

const answerer = responses[responses.length - 1].user
const historyHash = responses[Math.max(0, responses.length - 2)].historyHash

// DEBUG
console.log('Reporting answer for disputeID ' + _disputeID)
console.log(`questionID: ${questionID.toHexString()}`)
console.log(`historyHash: ${historyHash}`)
console.log(`bestAnswer: ${bestAnswer}`)
console.log(`answerer: ${answerer}`)

const arbitrationRequest = await retryOperation(()=> arbitrator.arbitrationRequests(questionID), 1000, 10) as {
status: number;
requester: string;
disputeID: BigNumber;
ruling: BigNumber;
};
console.log(`arbitrationRequest.status: ${arbitrationRequest.status}`);
if (arbitrationRequest.status == 3){
console.log(`Dispute already resolved, skipping`, _disputeID);
continue;
}
else {
const txHash = await arbitrator.reportAnswer(questionID.toHexString(), historyHash, bestAnswer, answerer, {gasLimit: 100000})
const txnReceipt = await txHash.wait()
console.log(`txHash: ${txnReceipt.transactionHash}`)
}
} catch (e) {
console.log(e)
}
}
return {statusCode: StatusCodes.OK};
} catch (e) {
console.log(e)
return {statusCode: StatusCodes.INTERNAL_SERVER_ERROR};
}
};

module.exports.handler = schedule("*/15 * * * *", handler);
2 changes: 1 addition & 1 deletion lib/bot-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const reportUser = (batchedSend:any, lang: string, hasBanningPermission:

return {
questionId: questionId,
questionUrl: `https://reality.eth.limo/app/#!/network/${process.env.CHAIN_ID}/question/${process.env.REALITY_ETH_V30}-${questionId}`
questionUrl: `https://reality.eth.limo/app/#!/template/${template_id}/network/${process.env.CHAIN_ID}/question/${process.env.REALITY_ETH_V30}-${questionId}`
};

}
Expand Down
Loading

0 comments on commit dc726d9

Please sign in to comment.