From b6d6d13001449c22037ce9a05120ff37ff7cc820 Mon Sep 17 00:00:00 2001 From: pranav-dblocked <164607056+pranav-dblocked@users.noreply.github.com> Date: Fri, 19 Apr 2024 19:06:13 +0530 Subject: [PATCH] Updated types for functions to support platformID (#26) * Added functionality to specify PlatformId as a parameter * Updated Comments (PlatformID parameter) * Updates Types with PlatformId Parameter * Added functions for 'mintForAddress' and 'updateServiceData' * added a function to cancel a service --------- Co-authored-by: pranavsinghal --- package-lock.json | 2 +- packages/client/src/disputes/types.ts | 2 +- packages/client/src/escrow/types/index.ts | 5 +- .../src/platform/__tests__/platform.spec.ts | 22 +++++++ packages/client/src/platform/index.ts | 18 ++++++ packages/client/src/platform/types/index.ts | 15 ++--- .../src/profile/__tests__/profile.spec.ts | 12 ++++ packages/client/src/profile/index.ts | 19 ++++++ packages/client/src/profile/types/index.ts | 3 +- packages/client/src/proposals/types/index.ts | 3 +- .../src/services/__tests__/services.spec.ts | 43 ++++++++++++++ packages/client/src/services/index.ts | 58 +++++++++++++++++++ 12 files changed, 189 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index bdc73db..5ae6580 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15182,7 +15182,7 @@ }, "packages/client": { "name": "@talentlayer/client", - "version": "0.1.14", + "version": "0.1.16", "license": "MIT", "dependencies": { "axios": "^1.5.0", diff --git a/packages/client/src/disputes/types.ts b/packages/client/src/disputes/types.ts index 4d3ac31..f98aaa7 100644 --- a/packages/client/src/disputes/types.ts +++ b/packages/client/src/disputes/types.ts @@ -1,6 +1,6 @@ import { TransactionHash } from '../types'; export interface IDispute { - getArbitrationCost(): Promise; + getArbitrationCost(platformId?: number): Promise; setPrice(value: number | string): Promise; } diff --git a/packages/client/src/escrow/types/index.ts b/packages/client/src/escrow/types/index.ts index 9e74b15..1db3a5d 100644 --- a/packages/client/src/escrow/types/index.ts +++ b/packages/client/src/escrow/types/index.ts @@ -5,9 +5,10 @@ export interface IEscrow { serviceId: string, proposalId: string, metaEvidenceCid: string, + platformId?: number ): Promise; - release(serviceId: string, amount: bigint, userId: number): Promise; - reimburse(serviceId: string, amount: bigint, userId: number): Promise; + release(serviceId: string, amount: bigint, userId: number, platformId?: number): Promise; + reimburse(serviceId: string, amount: bigint, userId: number, platformId?: number): Promise; getProtocolAndPlatformsFees( originServicePlatformId: string, originValidatedProposalPlatformId: string, diff --git a/packages/client/src/platform/__tests__/platform.spec.ts b/packages/client/src/platform/__tests__/platform.spec.ts index 38d1a2e..0c0ea02 100644 --- a/packages/client/src/platform/__tests__/platform.spec.ts +++ b/packages/client/src/platform/__tests__/platform.spec.ts @@ -185,4 +185,26 @@ describe('Platform', () => { }) }) + + describe('mintForAddress', () => { + it('should mint a platform Id for a address', async () => { + // Arrange + const platformName = 'racoon'; + const address = testAddress; + const mintFee = BigInt("1000000000000000000"); + + + // Act + const result = await platform.mintForAddress(platformName, address, mintFee); + + // Assert + expect(mockViemClient.writeContract).toHaveBeenCalledWith('talentLayerPlatformId', + 'mintForAddress', + [platformName, address], + mintFee); + expect(result).toEqual(testAddress); + }) + }) + + }); diff --git a/packages/client/src/platform/index.ts b/packages/client/src/platform/index.ts index 366e6bf..01c0064 100644 --- a/packages/client/src/platform/index.ts +++ b/packages/client/src/platform/index.ts @@ -127,6 +127,24 @@ export class Platform { return tx; } + /** + * Mints a platform Id for a address. + * + * @param {string} platformName - The name of the platform. + * @param {string} address - The address of the user. + * @param {string} mintFee - The Fee for the minting of platform Id. + * @returns {Promise} A promise that resolves to the transaction hash of the create operation. + */ + public async mintForAddress(platformName: string, address: string, mintFee: any): Promise { + const tx = await this.wallet.writeContract( + 'talentLayerPlatformId', + 'mintForAddress', + [platformName, address], + mintFee as bigint + ); + return tx; + } + /** * Sets the fee timeout for the platform. * diff --git a/packages/client/src/platform/types/index.ts b/packages/client/src/platform/types/index.ts index ec29634..7d0c37a 100644 --- a/packages/client/src/platform/types/index.ts +++ b/packages/client/src/platform/types/index.ts @@ -16,14 +16,15 @@ export type Arbitrator = { export interface IPlatform { getOne(id: string): Promise; - update(data: PlatformDetails): Promise; - updateOriginServiceFeeRate(value: number): Promise; - updateOriginValidatedProposalFeeRate(value: number): Promise; - updateServicePostingFee(value: number): Promise; - updateProposalPostingFee(value: number): Promise; + update(data: PlatformDetails, platformId?: number): Promise; + updateOriginServiceFeeRate(value: number, platformId?: number): Promise; + updateOriginValidatedProposalFeeRate(value: number, platformId?: number): Promise; + updateServicePostingFee(value: number, platformId?: number): Promise; + updateProposalPostingFee(value: number, PlatformId?: number): Promise; getByOwner(address: `0x${string}`): Promise; mint(platformName: string): Promise; - setFeeTimeout(timeout: number): Promise; + mintForAddress(platformName: string, address: string, mintFee: any): Promise; + setFeeTimeout(timeout: number, platformId?: number): Promise; getArbitrators(chainId: NetworkEnum): Arbitrator[]; - updateArbitrator(address: `0x${string}`): Promise; + updateArbitrator(address: `0x${string}`, platformId?: number): Promise; } diff --git a/packages/client/src/profile/__tests__/profile.spec.ts b/packages/client/src/profile/__tests__/profile.spec.ts index 86e40d0..3bc5ba6 100644 --- a/packages/client/src/profile/__tests__/profile.spec.ts +++ b/packages/client/src/profile/__tests__/profile.spec.ts @@ -61,6 +61,18 @@ describe('Profile', () => { }) }) + + describe('createForAddress', () => { + it('should create a new profile for a address and return a response', async () => { + const handle = testName; + const userAddress = testAddress; + const platformId = testPlatformId; + const handlePrice = "1000000000000000000"; + const result = await profile.createForAddress(handle, userAddress, platformId, handlePrice); + expect(mockViemClient.writeContract).toHaveBeenCalledWith('talentLayerId', 'mintForAddress', [userAddress, platformId,handle], BigInt(handlePrice)) + }) + }) + describe('getByAddress', () => { it('getByAddress returns data for a given address', async () => { diff --git a/packages/client/src/profile/index.ts b/packages/client/src/profile/index.ts index 5c66412..ddfcf4b 100644 --- a/packages/client/src/profile/index.ts +++ b/packages/client/src/profile/index.ts @@ -131,6 +131,25 @@ export class Profile { ); } + + /** + * create - Creates a new user profile by minting it on the TalentLayerId contract for a particular address. This function is typically called when a new user registers and a profile needs to be created in the system for that user. + * @param {string} handle - The user handle or username for the new profile. + * @param {Hash} userAddress - The address of the user whose profile is created. + * @param {number} platformId - The platform ID where the profile is created. + * @param {string} handlePrice - The fee for minting the new profile. + * @returns {Promise} - A promise that resolves to the transaction hash once the profile creation transaction is completed. + */ + public async createForAddress(handle: string, userAddress: Hash, platformId: number, handlePrice: string): Promise { + const tx = await this.viemClient.writeContract( + 'talentLayerId', + 'mintForAddress', + [userAddress, platformId, handle], + BigInt(handlePrice), + ); + return tx; + } + /** * getBy - Retrieves profiles based on specified search criteria. This function is useful for fetching multiple profiles, for instance, when implementing search functionality. * @param {{ numberPerPage?: number; offset?: number; searchQuery?: string; }} params - The search parameters including pagination and search query. diff --git a/packages/client/src/profile/types/index.ts b/packages/client/src/profile/types/index.ts index d6383fa..e6de769 100644 --- a/packages/client/src/profile/types/index.ts +++ b/packages/client/src/profile/types/index.ts @@ -15,7 +15,8 @@ export interface IProfile { upload(profileData: TalentLayerProfile): Promise; getByAddress(address: `0x${string}`): Promise; getById(userId: string): Promise; - create(handle: string): Promise; + create(handle: string, platformId?: number): Promise; + createForAddress(handle: string, userAddress: `0x${string}`, platformId: number, handlePrice: string): Promise; update(profileData: TalentLayerProfile, userId: string): Promise; getBy(params: { numberPerPage?: number; diff --git a/packages/client/src/proposals/types/index.ts b/packages/client/src/proposals/types/index.ts index d0f8519..26f098b 100644 --- a/packages/client/src/proposals/types/index.ts +++ b/packages/client/src/proposals/types/index.ts @@ -27,7 +27,8 @@ export interface IProposal { serviceId: string, rateToken: string, rateAmount: string, - expirationDate: string + expirationDate: string, + platformId?: number ): Promise; update( diff --git a/packages/client/src/services/__tests__/services.spec.ts b/packages/client/src/services/__tests__/services.spec.ts index 506ad43..791697a 100644 --- a/packages/client/src/services/__tests__/services.spec.ts +++ b/packages/client/src/services/__tests__/services.spec.ts @@ -61,6 +61,49 @@ describe('Service', () => { }) }) + + describe('update', () => { + it('should update an existing service', async () => { + // Arrange + const serviceDetails = testServiceDetails; + const userId = testUserId; + const existingServiceId = testServiceId; + + // Act + const response = await service.update(serviceDetails, userId, parseInt(existingServiceId)); + + // Assert + expect(response.cid).toEqual(testIpfsHash); + expect(response.tx).toEqual(testAddress); + expect(mockViemClient.writeContract).toHaveBeenCalledWith( + 'talentLayerService', + 'updateServiceData', + [userId, parseInt(existingServiceId), testIpfsHash] + ); + }) + }) + + describe('cancel', () => { + it('should cancel an existing service', async () => { + // Arrange + const userId = testUserId; + const serviceId = parseInt(testServiceId); + + // Act + const response = await service.cancel(userId, serviceId); + + // Assert + expect(response).toEqual(testAddress); + expect(mockViemClient.writeContract).toHaveBeenCalledWith( + 'talentLayerService', + 'cancelService', + [userId, serviceId] + ); + + + }) + }) + describe('search', () => { it('should reaturn services based on criteria', async () => { // Arranage diff --git a/packages/client/src/services/index.ts b/packages/client/src/services/index.ts index 4247e65..1144c10 100644 --- a/packages/client/src/services/index.ts +++ b/packages/client/src/services/index.ts @@ -1,3 +1,4 @@ +import { Hash } from 'viem'; import GraphQLClient from '../graphql'; import IPFSClient from '../ipfs'; import { Logger } from '../logger'; @@ -19,9 +20,15 @@ export interface IService { userId: string, platformId: number, ): Promise; + update( + serviceDetails: ServiceDetails, + userId: string, + existingServiceId: number, + ): Promise; updloadServiceDataToIpfs(serviceData: ServiceDetails): Promise; getServices(params: IProps): Promise; search(params: IProps): Promise; + cancel(userId: string, serviceId: number): Promise; } /** @@ -151,4 +158,55 @@ export class Service { throw new Error('Unable to create service'); } + + /** + * Asynchronously updates an existing service. + * @param {ServiceDetails} serviceDetails - The details of the service to update. + * @param {string} userId - The user ID updating the service. + * @param {number} existingServiceId - The existing Service ID which is getting updated. + * @returns {Promise} - A promise that resolves to the transaction response of the service update. + */ + public async update( + serviceDetails: ServiceDetails, + userId: string, + existingServiceId: number, + ): Promise { + + const cid = await this.updloadServiceDataToIpfs(serviceDetails); + + const tx = await this.viemClient.writeContract( + 'talentLayerService', + 'updateServiceData', + [userId, existingServiceId, cid], + ); + + if (cid && tx) { + return { cid, tx }; + } + + throw new Error('Unable to update service'); + } + + /** + * Cancel an existing service. + * @param {string} userId - Id of the user cancelling the service + * @param {number} serviceId - Id of the service being updated + * @returns {Promise} - A promise that resolves to the transaction hash for the cancelService function call + */ + public async cancel( + userId: string, + serviceId: number + ): Promise { + const tx = await this.viemClient.writeContract( + 'talentLayerService', + 'cancelService', + [userId, serviceId] + ) + + if (tx) { + return tx; + } + + throw new Error('Unable to cancel service'); + } }