Skip to content

Commit

Permalink
feat: implement request-submitted handler
Browse files Browse the repository at this point in the history
  • Loading branch information
eccentricexit committed Apr 15, 2020
1 parent 4ca2aa6 commit 9171572
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 19 deletions.
Empty file added db/.gitkeep
Empty file.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"production": "yarn build && node dist/index.js",
"lint:secrets": "secretlint \"**/*\"",
"lint": "yarn lint:secrets",
"clean": "rimraf ./db/* && nodetouch ./db/.gitkeep",
"release": "standard-version",
"init": "tsc --init"
"cleanDB": "rimraf ./db/* && nodetouch ./db/.gitkeep",
"release": "standard-version"
},
"author": "Kleros",
"repository": "git@github.com:kleros/gtcr-action-bot.git",
"license": "MIT",
"private": true,
"keywords": [
Expand All @@ -24,7 +24,8 @@
"dependencies": {
"@kleros/tcr": "^1.7.1",
"dotenv": "^8.2.0",
"ethers": "^4.0.46"
"ethers": "^4.0.46",
"level": "^6.0.1"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand Down Expand Up @@ -63,6 +64,5 @@
"pre-commit": "yarn lint",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"repository": "git@github.com:kleros/gtcr-action-bot.git"
}
}
5 changes: 3 additions & 2 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export default async function addTCRListeners(
tcr: ethers.Contract,
batchWithdraw: ethers.Contract,
intervals: BlockInterval[],
provider: ethers.providers.Provider
provider: ethers.providers.Provider,
db: Level
) {
// Submissions and removal requests.
tcr.on(
tcr.filters.RequestSubmitted(),
requestSubmittedHandler()
requestSubmittedHandler(db ,tcr)
)

// Request resolved.
Expand Down
17 changes: 15 additions & 2 deletions src/handlers/request-submitted.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export default () => async () => {
// TODO: Add to request o watchlist.
import { ethers } from "ethers"
import { BigNumber } from "ethers/utils"

export default (db: Level, tcr: ethers.Contract) => async (
_itemID: string,
_requestIndex: BigNumber,
_requestType: number
) => {
const challengePeriodDuration = await tcr.challengePeriodDuration()
const { submissionTime } = await tcr.getRequestInfo(_itemID, _requestIndex)

await db.put(tcr.address, {
...(await db.get(tcr.address)),
[_itemID]: submissionTime.add(challengePeriodDuration).toString()
})
}
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { ethers } from 'ethers'
import { bigNumberify } from 'ethers/utils'
import dotenv from "dotenv"
import level from 'level'

import _GTCRFactory from '@kleros/tcr/build/contracts/GTCRFactory.json'
import _GeneralizedTCR from '@kleros/tcr/build/contracts/GeneralizedTCR.json'
import _BatchWidthdraw from '@kleros/tcr/build/contracts/BatchWithdraw.json'

import addTCRListeners from './handlers'
import getSweepIntervals from './utils/get-intervals'
import dotenv from "dotenv"
import withdrawRewards from './utils/withdraw-rewards'
import wrapLevel from './utils/wrap-level'

dotenv.config({ path: ".env" })

// Run env variable checks.
import './utils/env-check'
import { bigNumberify } from 'ethers/utils'
import withdrawRewards from './utils/withdraw-rewards'

const db = wrapLevel(level('./db'))
const provider = new ethers.providers.JsonRpcProvider(process.env.PROVIDER_URL)
provider.pollingInterval = 60 * 1000 // Poll every minute.
const signer = new ethers.Wallet(process.env.WALLET_KEY, provider)
Expand Down Expand Up @@ -81,15 +84,17 @@ const deploymentBlock = Number(process.env.FACTORY_BLOCK_NUM) || 0
tcr,
batchWithdraw,
intervals,
provider
provider,
db
)))

gtcrFactory.on(gtcrFactory.filters.NewGTCR(), _address =>
addTCRListeners(
new ethers.Contract(_address, _GeneralizedTCR.abi, signer),
batchWithdraw,
intervals,
provider
provider,
db
)
)

Expand Down Expand Up @@ -157,7 +162,10 @@ const deploymentBlock = Number(process.env.FACTORY_BLOCK_NUM) || 0
// Challenge period passed with no challenges, execute it.
tcr.executeRequest(_itemID)
} else {
// TODO: The challenge period did not pass yet. Add it to the watchlist.
await db.put(tcr.address, {
...(await db.get(tcr.address)),
[_itemID]: submissionTime.add(challengePeriodDuration).toString()
})
}
})

Expand Down
7 changes: 7 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
declare module 'level'

interface BlockInterval {
fromBlock: number
toBlock: number
}

interface Level {
get: Function
put: Function
}
7 changes: 7 additions & 0 deletions src/utils/wrap-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Wrap level.get and level.put to parse and stringify JSON values.
export default function wrapLevel(db: Level) {
return ({
get: async (key: string) => JSON.parse(await db.get(key)),
put: async (key: string, value: object) => db.put(key, JSON.stringify(value))
})
}
Loading

0 comments on commit 9171572

Please sign in to comment.