-
Notifications
You must be signed in to change notification settings - Fork 44
Update cvat payout calculator for the new manifest #3981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,6 @@ import { faker } from '@faker-js/faker'; | |||||||||||||||||||||||||||||||
| import { createMock } from '@golevelup/ts-jest'; | ||||||||||||||||||||||||||||||||
| import { EscrowClient } from '@human-protocol/sdk'; | ||||||||||||||||||||||||||||||||
| import { Test } from '@nestjs/testing'; | ||||||||||||||||||||||||||||||||
| import { ethers } from 'ethers'; | ||||||||||||||||||||||||||||||||
| import _ from 'lodash'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import { CvatAnnotationMeta } from '@/common/types'; | ||||||||||||||||||||||||||||||||
|
|
@@ -48,14 +47,24 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| const mockedGetIntermediateResultsUrl = jest | ||||||||||||||||||||||||||||||||
| .fn() | ||||||||||||||||||||||||||||||||
| .mockImplementation(async () => faker.internet.url()); | ||||||||||||||||||||||||||||||||
| const mockedGetTokenAddress = jest.fn().mockImplementation(async () => { | ||||||||||||||||||||||||||||||||
| return faker.finance.ethereumAddress(); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| const mockedGetReservedFunds = jest | ||||||||||||||||||||||||||||||||
| .fn() | ||||||||||||||||||||||||||||||||
| .mockImplementation(async () => | ||||||||||||||||||||||||||||||||
| BigInt(faker.number.int({ min: 1000, max: 100000 })), | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| beforeAll(() => { | ||||||||||||||||||||||||||||||||
| mockedEscrowClient.build.mockResolvedValue({ | ||||||||||||||||||||||||||||||||
| getIntermediateResultsUrl: mockedGetIntermediateResultsUrl, | ||||||||||||||||||||||||||||||||
| getTokenAddress: mockedGetTokenAddress, | ||||||||||||||||||||||||||||||||
| getReservedFunds: mockedGetReservedFunds, | ||||||||||||||||||||||||||||||||
| } as unknown as EscrowClient); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||||||||||
| jest.resetAllMocks(); | ||||||||||||||||||||||||||||||||
| mockedEscrowClient.build.mockResolvedValue({ | ||||||||||||||||||||||||||||||||
| getIntermediateResultsUrl: mockedGetIntermediateResultsUrl, | ||||||||||||||||||||||||||||||||
| getReservedFunds: mockedGetReservedFunds, | ||||||||||||||||||||||||||||||||
| } as unknown as EscrowClient); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+63
to
69
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need in this block
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -84,23 +93,22 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should properly calculate workers bounties trimming the decimals', async () => { | ||||||||||||||||||||||||||||||||
| it('should properly calculate workers bounties', async () => { | ||||||||||||||||||||||||||||||||
| const annotators = [ | ||||||||||||||||||||||||||||||||
| faker.finance.ethereumAddress(), | ||||||||||||||||||||||||||||||||
| faker.finance.ethereumAddress(), | ||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const jobsPerAnnotator = faker.number.int({ min: 1, max: 3 }); | ||||||||||||||||||||||||||||||||
| const tokenDecimals = BigInt(6); | ||||||||||||||||||||||||||||||||
| const jobCount = jobsPerAnnotator * annotators.length; | ||||||||||||||||||||||||||||||||
| const payoutPerJob = BigInt(faker.number.int({ min: 1000, max: 100000 })); | ||||||||||||||||||||||||||||||||
| const reservedFunds = payoutPerJob * BigInt(jobCount); | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const annotationsMeta: CvatAnnotationMeta = { | ||||||||||||||||||||||||||||||||
| jobs: Array.from( | ||||||||||||||||||||||||||||||||
| { length: jobsPerAnnotator * annotators.length }, | ||||||||||||||||||||||||||||||||
| (_v, index: number) => ({ | ||||||||||||||||||||||||||||||||
| job_id: index, | ||||||||||||||||||||||||||||||||
| final_result_id: faker.number.int(), | ||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||
| jobs: Array.from({ length: jobCount }, (_v, index: number) => ({ | ||||||||||||||||||||||||||||||||
| job_id: index, | ||||||||||||||||||||||||||||||||
| final_result_id: faker.number.int(), | ||||||||||||||||||||||||||||||||
| })), | ||||||||||||||||||||||||||||||||
| results: [], | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
| for (const job of annotationsMeta.jobs) { | ||||||||||||||||||||||||||||||||
|
|
@@ -130,26 +138,19 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| mockedStorageService.downloadJsonLikeData.mockResolvedValueOnce( | ||||||||||||||||||||||||||||||||
| annotationsMeta, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| mockedWeb3Service.getTokenDecimals.mockResolvedValueOnce(tokenDecimals); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const mockedJobBounty = '0.123456789'; // more decimals than token has | ||||||||||||||||||||||||||||||||
| const manifest = { | ||||||||||||||||||||||||||||||||
| ...generateCvatManifest(), | ||||||||||||||||||||||||||||||||
| job_bounty: mockedJobBounty, | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| mockedGetReservedFunds.mockResolvedValueOnce(reservedFunds); | ||||||||||||||||||||||||||||||||
| const manifest = generateCvatManifest(); | ||||||||||||||||||||||||||||||||
| const payouts = await calculator.calculate({ | ||||||||||||||||||||||||||||||||
| chainId, | ||||||||||||||||||||||||||||||||
| escrowAddress, | ||||||||||||||||||||||||||||||||
| manifest, | ||||||||||||||||||||||||||||||||
| manifest: manifest, | ||||||||||||||||||||||||||||||||
| finalResultsUrl: faker.internet.url(), | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+142
to
149
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const trimmedBounty = '0.123456'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const expectedJobBounty = payoutPerJob; | ||||||||||||||||||||||||||||||||
| const expectedAmountPerAnnotator = | ||||||||||||||||||||||||||||||||
| BigInt(jobsPerAnnotator) * | ||||||||||||||||||||||||||||||||
| ethers.parseUnits(trimmedBounty, tokenDecimals); | ||||||||||||||||||||||||||||||||
| BigInt(jobsPerAnnotator) * expectedJobBounty; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const expectedPayouts = annotators.map((address) => ({ | ||||||||||||||||||||||||||||||||
| address, | ||||||||||||||||||||||||||||||||
|
|
@@ -168,7 +169,9 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const jobsPerAnnotator = faker.number.int({ min: 1, max: 3 }); | ||||||||||||||||||||||||||||||||
| const tokenDecimals = BigInt(faker.number.int({ min: 6, max: 18 })); | ||||||||||||||||||||||||||||||||
| const reservedFunds = BigInt( | ||||||||||||||||||||||||||||||||
| faker.number.int({ min: 1000, max: 100000 }).toString(), | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const annotationsMeta: CvatAnnotationMeta = { | ||||||||||||||||||||||||||||||||
| jobs: Array.from( | ||||||||||||||||||||||||||||||||
|
|
@@ -207,7 +210,7 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| mockedStorageService.downloadJsonLikeData.mockResolvedValueOnce( | ||||||||||||||||||||||||||||||||
| annotationsMeta, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| mockedWeb3Service.getTokenDecimals.mockResolvedValueOnce(tokenDecimals); | ||||||||||||||||||||||||||||||||
| mockedGetReservedFunds.mockResolvedValueOnce(reservedFunds); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const manifest = generateCvatManifest(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -218,9 +221,14 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| finalResultsUrl: faker.internet.url(), | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const matchedJobCount = annotationsMeta.jobs.filter((job) => | ||||||||||||||||||||||||||||||||
| annotationsMeta.results.some( | ||||||||||||||||||||||||||||||||
| (result) => result.id === job.final_result_id, | ||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||
| ).length; | ||||||||||||||||||||||||||||||||
| const expectedJobBounty = reservedFunds / BigInt(matchedJobCount); | ||||||||||||||||||||||||||||||||
| const expectedAmountPerAnnotator = | ||||||||||||||||||||||||||||||||
| BigInt(jobsPerAnnotator) * | ||||||||||||||||||||||||||||||||
| ethers.parseUnits(manifest.job_bounty, tokenDecimals); | ||||||||||||||||||||||||||||||||
| BigInt(jobsPerAnnotator) * expectedJobBounty; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const expectedPayouts = annotators.map((address) => ({ | ||||||||||||||||||||||||||||||||
| address, | ||||||||||||||||||||||||||||||||
|
|
@@ -231,5 +239,33 @@ describe('CvatPayoutsCalculator', () => { | |||||||||||||||||||||||||||||||
| _.sortBy(expectedPayouts, 'address'), | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('throws when there are no jobs with matching final results', async () => { | ||||||||||||||||||||||||||||||||
| mockedStorageService.downloadJsonLikeData.mockResolvedValueOnce({ | ||||||||||||||||||||||||||||||||
| jobs: [ | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| job_id: faker.number.int(), | ||||||||||||||||||||||||||||||||
| final_result_id: faker.number.int(), | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||
| results: [ | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| id: faker.number.int(), | ||||||||||||||||||||||||||||||||
| job_id: faker.number.int(), | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though we use faker there, there is a very little chance that |
||||||||||||||||||||||||||||||||
| annotator_wallet_address: faker.finance.ethereumAddress(), | ||||||||||||||||||||||||||||||||
| annotation_quality: faker.number.float(), | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||
| } as CvatAnnotationMeta); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await expect( | ||||||||||||||||||||||||||||||||
| calculator.calculate({ | ||||||||||||||||||||||||||||||||
| chainId, | ||||||||||||||||||||||||||||||||
| escrowAddress, | ||||||||||||||||||||||||||||||||
| manifest: generateCvatManifest(), | ||||||||||||||||||||||||||||||||
| finalResultsUrl: faker.internet.url(), | ||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||
| ).rejects.toThrow('Invalid annotation meta'); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import { EscrowClient } from '@human-protocol/sdk'; | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { ethers } from 'ethers'; | ||
| import type { OverrideProperties } from 'type-fest'; | ||
|
|
||
| import { CVAT_VALIDATION_META_FILENAME } from '@/common/constants'; | ||
|
|
@@ -27,7 +26,6 @@ export class CvatPayoutsCalculator implements EscrowPayoutsCalculator { | |
| ) {} | ||
|
|
||
| async calculate({ | ||
| manifest, | ||
| chainId, | ||
| escrowAddress, | ||
| }: CalculateCvatPayoutsInput): Promise<CalculatedPayout[]> { | ||
|
|
@@ -41,26 +39,28 @@ export class CvatPayoutsCalculator implements EscrowPayoutsCalculator { | |
| await this.storageService.downloadJsonLikeData<CvatAnnotationMeta>( | ||
| `${intermediateResultsUrl}/${CVAT_VALIDATION_META_FILENAME}`, | ||
| ); | ||
|
|
||
| if (!annotations.jobs.length || !annotations.results.length) { | ||
| throw new Error('Invalid annotation meta'); | ||
| } | ||
|
|
||
| const tokenAddress = await escrowClient.getTokenAddress(escrowAddress); | ||
| const tokenDecimals = await this.web3Service.getTokenDecimals( | ||
| chainId, | ||
| tokenAddress, | ||
| ); | ||
| const jobBountyValue = this.parseJobBounty( | ||
| manifest.job_bounty, | ||
| Number(tokenDecimals), | ||
| ); | ||
| const workersBounties = new Map<string, bigint>(); | ||
| const reservedFunds = await escrowClient.getReservedFunds(escrowAddress); | ||
|
|
||
| for (const job of annotations.jobs) { | ||
| const jobFinalResult = annotations.results.find( | ||
| (result) => result.id === job.final_result_id, | ||
| const matchedJobResults = annotations.jobs | ||
| .map((job) => | ||
| annotations.results.find((result) => result.id === job.final_result_id), | ||
| ) | ||
| .filter((result): result is CvatAnnotationMeta['results'][number] => | ||
| Boolean(result), | ||
| ); | ||
|
|
||
| if (!matchedJobResults.length) { | ||
| throw new Error('Invalid annotation meta'); | ||
| } | ||
|
|
||
| const jobBountyValue = reservedFunds / BigInt(matchedJobResults.length); | ||
| const workersBounties = new Map<string, bigint>(); | ||
|
|
||
| for (const jobFinalResult of matchedJobResults) { | ||
| // TODO: enable annotation quality validation when ready | ||
| if ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already filtered above, so probably we can remove this check on |
||
| jobFinalResult | ||
|
|
@@ -84,21 +84,4 @@ export class CvatPayoutsCalculator implements EscrowPayoutsCalculator { | |
| }), | ||
| ); | ||
| } | ||
|
|
||
| private parseJobBounty(jobBounty: string, tokenDecimals: number): bigint { | ||
| const parts = jobBounty.split('.'); | ||
| if (parts.length > 1) { | ||
| const decimalsInBounty = parts[1].length; | ||
| if (decimalsInBounty > tokenDecimals) { | ||
| if (tokenDecimals === 0) { | ||
| return ethers.parseUnits(parts[0], tokenDecimals); | ||
| } | ||
| return ethers.parseUnits( | ||
| `${parts[0]}.${parts[1].slice(0, tokenDecimals)}`, | ||
| tokenDecimals, | ||
| ); | ||
| } | ||
| } | ||
| return ethers.parseUnits(jobBounty, tokenDecimals); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not mock default with some random number, because it can confuse in case of incorrect/failing test suite, it's better to mimic real behavior (i.e. funds not reserved) which returns 0n