Skip to content

Commit

Permalink
Get Job Launcher fee from KVStore
Browse files Browse the repository at this point in the history
  • Loading branch information
flopez7 committed Jan 23, 2024
1 parent 72cf9ac commit 872e42b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,6 @@ describe('JobService', () => {
createPaymentMock.mockResolvedValue(true);
});

beforeAll(() => {
(KVStoreClient.build as any).mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));
});

afterEach(() => {
jest.restoreAllMocks();
});
Expand All @@ -335,6 +329,14 @@ describe('JobService', () => {
save: jest.fn().mockResolvedValue(true),
};

(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));

jobRepository.create = jest.fn().mockResolvedValue(mockJobEntity);

await jobService.createJob(userId, JobRequestType.FORTUNE, fortuneJobDto);
Expand Down Expand Up @@ -373,6 +375,14 @@ describe('JobService', () => {
.spyOn(routingProtocolService, 'selectNetwork')
.mockReturnValue(ChainId.MOONBEAM);

(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));

await jobService.createJob(userId, JobRequestType.FORTUNE, {
...fortuneJobDto,
chainId: undefined,
Expand Down Expand Up @@ -407,6 +417,10 @@ describe('JobService', () => {
.spyOn(paymentService, 'getUserBalance')
.mockResolvedValue(userBalance);

(KVStoreClient.build as any).mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}));

getUserBalanceMock.mockResolvedValue(userBalance);

await expect(
Expand All @@ -419,6 +433,14 @@ describe('JobService', () => {

getUserBalanceMock.mockResolvedValue(userBalance);

(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));

jest.spyOn(jobRepository, 'create').mockResolvedValue(undefined!);

await expect(
Expand Down Expand Up @@ -908,6 +930,14 @@ describe('JobService', () => {

jobRepository.create = jest.fn().mockResolvedValue(mockJobEntity);

(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));

await jobService.createJob(
userId,
JobRequestType.IMAGE_POINTS,
Expand Down Expand Up @@ -960,6 +990,10 @@ describe('JobService', () => {
type: JobRequestType.IMAGE_POINTS,
};

(KVStoreClient.build as any).mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}));

await expect(
jobService.createJob(
userId,
Expand Down Expand Up @@ -994,6 +1028,10 @@ describe('JobService', () => {
type: JobRequestType.IMAGE_POINTS,
};

(KVStoreClient.build as any).mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}));

await expect(
jobService.createJob(
userId,
Expand Down Expand Up @@ -1027,6 +1065,10 @@ describe('JobService', () => {
type: JobRequestType.IMAGE_POINTS,
};

(KVStoreClient.build as any).mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}));

await expect(
jobService.createJob(
userId,
Expand Down Expand Up @@ -1060,6 +1102,10 @@ describe('JobService', () => {
type: JobRequestType.IMAGE_POINTS,
};

(KVStoreClient.build as any).mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}));

await expect(
jobService.createJob(
userId,
Expand All @@ -1082,6 +1128,14 @@ describe('JobService', () => {
.spyOn(routingProtocolService, 'selectNetwork')
.mockReturnValue(ChainId.MOONBEAM);

(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));

await jobService.createJob(userId, JobRequestType.IMAGE_POINTS, {
...imageLabelBinaryJobDto,
chainId: undefined,
Expand Down Expand Up @@ -1123,6 +1177,10 @@ describe('JobService', () => {

getUserBalanceMock.mockResolvedValue(userBalance);

(KVStoreClient.build as any).mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}));

await expect(
jobService.createJob(
userId,
Expand All @@ -1139,6 +1197,14 @@ describe('JobService', () => {

jest.spyOn(jobRepository, 'create').mockResolvedValue(undefined!);

(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));

await expect(
jobService.createJob(
userId,
Expand Down Expand Up @@ -1178,6 +1244,13 @@ describe('JobService', () => {
beforeEach(() => {
getUserBalanceMock = jest.spyOn(paymentService, 'getUserBalance');
createPaymentMock.mockResolvedValue(true);
(KVStoreClient.build as any)
.mockImplementationOnce(() => ({
get: jest.fn().mockResolvedValue(MOCK_ORACLE_FEE),
}))
.mockImplementation(() => ({
get: jest.fn().mockResolvedValue(MOCK_PGP_PUBLIC_KEY),
}));
});

afterEach(() => {
Expand Down
17 changes: 9 additions & 8 deletions packages/apps/job-launcher/server/src/modules/job/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,7 @@ export class JobService {
fundAmount = dto.fundAmount;
}
const userBalance = await this.paymentService.getUserBalance(userId);
const feePercentage = this.configService.get<number>(
ConfigNames.JOB_LAUNCHER_FEE,
)!;
const feePercentage = Number(await this.getOracleFee(chainId));
const fee = mul(div(feePercentage, 100), fundAmount);
const usdTotalAmount = add(fundAmount, fee);

Expand Down Expand Up @@ -593,16 +591,16 @@ export class JobService {
reputationOracle: oracleAddresses.recordingOracle,
exchangeOracle: oracleAddresses.exchangeOracle,
recordingOracleFee: await this.getOracleFee(
oracleAddresses.recordingOracle,
jobEntity.chainId,
oracleAddresses.recordingOracle,
),
reputationOracleFee: await this.getOracleFee(
oracleAddresses.reputationOracle,
jobEntity.chainId,
oracleAddresses.reputationOracle,
),
exchangeOracleFee: await this.getOracleFee(
oracleAddresses.exchangeOracle,
jobEntity.chainId,
oracleAddresses.exchangeOracle,
),
manifestUrl: jobEntity.manifestUrl,
manifestHash: jobEntity.manifestHash,
Expand Down Expand Up @@ -1460,14 +1458,17 @@ export class JobService {
}

private async getOracleFee(
oracleAddress: string,
chainId: ChainId,
oracleAddress?: string,
): Promise<bigint> {
const signer = this.web3Service.getSigner(chainId);

const kvStoreClient = await KVStoreClient.build(signer);

const feeValue = await kvStoreClient.get(oracleAddress, KVStoreKeys.fee);
const feeValue = await kvStoreClient.get(
oracleAddress || signer.address,
KVStoreKeys.fee,
);

return BigInt(feeValue ? feeValue : 1);
}
Expand Down

0 comments on commit 872e42b

Please sign in to comment.