-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8361b77
commit 330c82d
Showing
9 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Web3--- | ||
FACTORY_ADDRESS=0x9fDd978101EF4F7A3AE5AEC08898258b5CDBF52B | ||
PROVIDER_URL=https://kovan.infura.io/v3/<secret-key> | ||
# The block time is used calculate how many blocks to scan when fetching | ||
# logs and avoid rate limiting by web3 providers. | ||
BLOCK_TIME_MILLISECONDS=4000 | ||
|
||
# The bot learns the addresses of TCRs to watch by fetching logs, | ||
# emitted by the factory. We can fetch logs more quickly by | ||
# sweeping the blockchain from the deployment block of the factory contract. | ||
# This field is optional, if not set, 0 will be used. | ||
FACTORY_BLOCK_NUM=16384123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ethers } from 'ethers' | ||
|
||
import requestSubmittedHandler from './request-submitted' | ||
import requestResolvedHandler from './request-resolved' | ||
|
||
export default async function addTCRListeners(tcr: ethers.Contract) { | ||
// Submissions and removal requests. | ||
tcr.on( | ||
tcr.filters.RequestSubmitted(), | ||
requestSubmittedHandler() | ||
) | ||
|
||
// Request resolved. | ||
tcr.on( | ||
tcr.filters.ItemStatusChange(null, null, null, null, true), | ||
requestResolvedHandler() | ||
) | ||
|
||
console.info(`Done setting up listeners for ${tcr.address}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default () => async () => { | ||
// TODO: Stop monitoring item. | ||
// TODO: Withdraw pending crowdfunding rewards. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default () => async () => { | ||
// TODO: Add to DB and start monitoring. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,82 @@ | ||
// TODO | ||
import { ethers } from 'ethers' | ||
|
||
import _GTCRFactory from '@kleros/tcr/build/contracts/GTCRFactory.json' | ||
import _GeneralizedTCR from '@kleros/tcr/build/contracts/GeneralizedTCR.json' | ||
|
||
// Run env variable checks. | ||
import './utils/env-check' | ||
import addTCRListeners from './handlers' | ||
|
||
const provider = new ethers.providers.JsonRpcProvider(process.env.PROVIDER_URL) | ||
provider.pollingInterval = 60 * 1000 // Poll every minute. | ||
|
||
const gtcrFactory = new ethers.Contract( | ||
process.env.FACTORY_ADDRESS as string, | ||
_GTCRFactory.abi, | ||
provider | ||
) | ||
|
||
// Run bot. | ||
;(async () => { | ||
// Initial setup. | ||
console.info('Booting...') | ||
console.info() | ||
const [currBlock, network] = await Promise.all([ | ||
provider.getBlockNumber(), | ||
provider.getNetwork() | ||
]) | ||
|
||
console.info(`Connected to ${network.name} of chain of ID ${network.chainId}`) | ||
console.info(`GTCR Factory deployed at ${process.env.FACTORY_ADDRESS}`) | ||
|
||
// Fetch all TCR addresses from factory logs, instantiate and add | ||
// event listeners. | ||
const deploymentBlock = Number(process.env.FACTORY_BLOCK_NUM) || 0 | ||
|
||
// Fetch logs by scanning the blockchain in batches of 4 months | ||
// to avoid rate-limiting. | ||
const blocksPerMinute = Math.floor( | ||
60 / (Number(process.env.BLOCK_TIME_MILLISECONDS as string) / 1000) | ||
) | ||
const blocksPerRequest = blocksPerMinute * 60 * 24 * 30 * 4 | ||
|
||
// Fetch the addresses of TCRs deployed with this factory. | ||
const logPromises = [] | ||
for (let fromBlock = deploymentBlock; ; ) { | ||
logPromises.push( | ||
provider.getLogs({ | ||
...gtcrFactory.filters.NewGTCR(), | ||
fromBlock: fromBlock, | ||
toBlock: fromBlock + blocksPerRequest | ||
}) | ||
) | ||
|
||
if (fromBlock + blocksPerRequest >= currBlock) break | ||
fromBlock += blocksPerRequest | ||
} | ||
|
||
// Concat results and instantiate TCRs. | ||
const tcrs = (await Promise.all(logPromises)) | ||
.reduce((acc, curr) => acc.concat(curr), []) | ||
.map(log => gtcrFactory.interface.parseLog(log).values._address) | ||
.map(address => new ethers.Contract(address, _GeneralizedTCR.abi, provider)) | ||
|
||
// Add listeners for events emitted by the TCRs. | ||
await Promise.all( | ||
tcrs.map(tcr => | ||
addTCRListeners( | ||
tcr | ||
) | ||
) | ||
) | ||
|
||
// Watch for new TCRs and add listeners. | ||
gtcrFactory.on(gtcrFactory.filters.NewGTCR(), _address => | ||
addTCRListeners( | ||
new ethers.Contract(_address, _GeneralizedTCR.abi, provider), | ||
) | ||
) | ||
|
||
console.info() | ||
console.info('Done. Watching for blockchain events.') | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Web3 | ||
if (!process.env.PROVIDER_URL) { | ||
console.error( | ||
'No web3 provider set. Please set the PROVIDER_URL environment variable' | ||
) | ||
process.exit(1) | ||
} | ||
|
||
if (!process.env.FACTORY_ADDRESS) { | ||
console.error( | ||
'No factory address set. Please set the FACTORY_ADDRESS environment variable' | ||
) | ||
process.exit(1) | ||
} | ||
|
||
if (!process.env.BLOCK_TIME_MILLISECONDS) { | ||
console.error( | ||
'Network block time not set. Please set the BLOCK_TIME_MILLISECONDS environment variable' | ||
) | ||
process.exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters