-
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.
feat: implement request-resolved handler
- Loading branch information
1 parent
70b5a99
commit 4ca2aa6
Showing
6 changed files
with
114 additions
and
42 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
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,4 +1,31 @@ | ||
export default () => async () => { | ||
// TODO: Remove item from watch list. | ||
// TODO: Withdraw pending crowdfunding rewards. | ||
} | ||
import withdrawRewards from "../utils/withdraw-rewards" | ||
import { BigNumber } from "ethers/utils" | ||
import { ethers } from "ethers" | ||
|
||
/** | ||
* Builds a handler for request resolved events (or rather, ItemStatusChange events with resolved value set to true.) | ||
*/ | ||
export default ( | ||
tcr: ethers.Contract, | ||
batchWithdraw: ethers.Contract, | ||
intervals: BlockInterval[], | ||
provider: ethers.providers.Provider | ||
) => async ( | ||
_itemID: string, | ||
_requestIndex: BigNumber, | ||
_roundIndex: BigNumber, | ||
_disputed: boolean, | ||
_resolved: boolean | ||
) => { | ||
if (!_resolved) return | ||
await withdrawRewards( | ||
_itemID, | ||
_requestIndex, | ||
tcr, | ||
batchWithdraw, | ||
intervals, | ||
provider | ||
) | ||
|
||
// TODO: Remove item from watch list. | ||
} |
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,4 @@ | ||
interface BlockInterval { | ||
fromBlock: number | ||
toBlock: number | ||
} |
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,43 @@ | ||
import { ethers } from "ethers" | ||
import { BigNumber } from "ethers/utils" | ||
|
||
export default async function withdrawRewards( | ||
itemID: string, | ||
requestID: BigNumber, | ||
tcr: ethers.Contract, | ||
batchWithdraw: ethers.Contract, | ||
blockIntervals: BlockInterval[], | ||
provider: ethers.providers.Provider | ||
) { | ||
const { disputed } = await tcr.getRequestInfo(itemID, requestID) | ||
if (!disputed) return // No rewards to withdraw if there was never a dispute. | ||
|
||
const contributionEvents = (await Promise.all( | ||
blockIntervals.map(async interval => provider.getLogs({ | ||
...tcr.filters.AppealContribution(itemID, null, requestID), | ||
})) | ||
)) | ||
.reduce((acc, curr) => [...acc, ...curr]) | ||
.map(rawEvent => tcr.interface.parseLog(rawEvent)) | ||
.filter(({ values: { _round } }) => _round.toNumber() !== 0) // Ignore first round | ||
|
||
// A new AppealContribution event is emmited every time | ||
// someone makes a contribution. | ||
// Since batchRoundWithdraw() withdraws all contributions from | ||
// every round by a contributor, we avoid withdrawing | ||
// for the same contributor more than once by using a set. | ||
const done = new Set() | ||
contributionEvents.forEach(async ({ values: { _contributor, itemID, _request } }) => { | ||
if (done.has(_contributor)) | ||
await batchWithdraw.batchRoundWithdraw( | ||
tcr.address, | ||
_contributor, | ||
itemID, | ||
_request, | ||
0, | ||
0 | ||
) | ||
|
||
done.add(_contributor) | ||
}) | ||
} |