Skip to content
46 changes: 35 additions & 11 deletions packages/client/src/escrow/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,39 @@ export const getProtocolAndPlatformsFees = (
originServicePlatformId: string,
originValidatedProposalPlatformId: string,
): string => `
{
protocols {
protocolEscrowFeeRate
}
servicePlatform: platform(id:${originServicePlatformId}){
originServiceFeeRate
}
proposalPlatform: platform(id:${originValidatedProposalPlatformId}){
originValidatedProposalFeeRate
}
{
protocols {
protocolEscrowFeeRate
}
servicePlatform: platform(id:${originServicePlatformId}){
originServiceFeeRate
}
proposalPlatform: platform(id:${originValidatedProposalPlatformId}){
originValidatedProposalFeeRate
}
}
`;

export const getPaymentsByService = (serviceId: string, paymentType?: string) => {
let condition = `where: {service: "${serviceId}"`;
paymentType ? (condition += `, paymentType: "${paymentType}"`) : '';
condition += '}, orderBy: id, orderDirection: asc';
const query = `
{
payments(${condition}) {
id
amount
rateToken {
address
decimals
name
symbol
}
`;
paymentType
transactionHash
createdAt
}
}
`;
return query;
};
36 changes: 28 additions & 8 deletions packages/client/src/escrow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Service } from '../services';
import { ClientTransactionResponse, NetworkEnum, RateToken } from '../types';
import { calculateApprovalAmount } from '../utils/fees';
import { ViemClient } from '../viem';
import { getProtocolAndPlatformsFees } from './graphql/queries';
import { getPaymentsByService, getProtocolAndPlatformsFees } from './graphql/queries';

export class Escrow {
graphQlClient: GraphQLClient;
Expand Down Expand Up @@ -59,21 +59,21 @@ export class Escrow {
let tx,
cid = proposal.cid;

const protocolAndPlatformsFeesResponse = await this.graphQlClient.get(
getProtocolAndPlatformsFees(proposal.service.platform.id, proposal.platform.id),
const protocolAndPlatformsFees = await this.getProtocolAndPlatformsFees(
proposal.service.platform.id, proposal.platform.id
);

console.log('SDK: fees', protocolAndPlatformsFeesResponse);
console.log('SDK: fees', protocolAndPlatformsFees);

if (!protocolAndPlatformsFeesResponse.data) {
if (!protocolAndPlatformsFees.data) {
throw Error('Unable to fetch fees');
}

const approvalAmount = calculateApprovalAmount(
proposal.rateAmount,
protocolAndPlatformsFeesResponse.data.servicePlatform.originServiceFeeRate,
protocolAndPlatformsFeesResponse.data.proposalPlatform.originValidatedProposalFeeRate,
protocolAndPlatformsFeesResponse.data.protocols[0].protocolEscrowFeeRate,
protocolAndPlatformsFees.servicePlatform.originServiceFeeRate,
protocolAndPlatformsFees.proposalPlatform.originValidatedProposalFeeRate,
protocolAndPlatformsFees.protocols[0].protocolEscrowFeeRate,
);

console.log('SDK: escrow seeking approval for amount: ', approvalAmount);
Expand Down Expand Up @@ -187,4 +187,24 @@ export class Escrow {

return tx;
}

public async getProtocolAndPlatformsFees(
originServicePlatformId: string,
originValidatedProposalPlatformId: string,
): Promise<any> {
const query = getProtocolAndPlatformsFees(originServicePlatformId, originValidatedProposalPlatformId);

const response = await this.graphQlClient.get(query);

return response?.data || null;
}

public async getByService(serviceId: string, paymentType?: string): Promise<any> {
const query = getPaymentsByService(serviceId, paymentType);

const response = await this.graphQlClient.get(query);

return response?.data?.payments || null
}

}
5 changes: 5 additions & 0 deletions packages/client/src/escrow/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ export interface IEscrow {
): Promise<ClientTransactionResponse>;
release(serviceId: string, amount: bigint, userId: number): Promise<any>;
reimburse(serviceId: string, amount: bigint, userId: number): Promise<any>;
getProtocolAndPlatformsFees(
originServicePlatformId: string,
originValidatedProposalPlatformId: string,
): Promise<any>;
getByService(serviceId: string, paymentType?: string): Promise<any>;
}
6 changes: 3 additions & 3 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { IProfile } from './profile/types';
import { Escrow } from './escrow';
import { IEscrow } from './escrow/types';
import { IService, Service } from './services';
import { IReview } from './review/types';
import { Review } from './review';
import { IReview } from './reviews/types';
import { Review } from './reviews';

/**
* Main client for interacting with the TalentLayer protocol.
Expand Down Expand Up @@ -93,7 +93,7 @@ export class TalentLayerClient {

/**
* Provides access to service functionalities.
* @type {IDispute}
* @type {IService}
*/

// @ts-ignore
Expand Down
172 changes: 145 additions & 27 deletions packages/client/src/profile/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,149 @@ import { Hash } from 'viem';

export const getProfileByAddress = (address: Hash) =>
`
{
users(where: {address: "${address.toLocaleLowerCase()}"}, first: 1) {
id
address
handle
rating
delegates
userStats {
numReceivedReviews
}
updatedAt
createdAt
description {
about
role
name
country
headline
id
image_url
video_url
title
timezone
skills_raw
}
{
users(where: {address: "${address.toLocaleLowerCase()}"}, first: 1) {
id
address
handle
rating
delegates
userStats {
numReceivedReviews
}
updatedAt
createdAt
description {
about
role
name
country
headline
id
image_url
video_url
title
timezone
skills_raw
}
}
}
`;

export const getProfiles = (numberPerPage?: number, offset?: number, searchQuery?: string) => {
const pagination = numberPerPage ? 'first: ' + numberPerPage + ', skip: ' + offset : '';
let condition = ', where: {';
condition += searchQuery ? `, handle_contains_nocase: "${searchQuery}"` : '';
condition += '}';

return `
{
users(orderBy: rating, orderDirection: desc ${pagination} ${condition}) {
id
address
handle
userStats {
numReceivedReviews
}
rating
}
}
`;
};

export const getProfileById = (id: string) => `
{
user(id: "${id}") {
id
address
handle
rating
delegates
userStats {
numReceivedReviews
}
updatedAt
createdAt
description {
about
role
name
country
headline
id
image_url
video_url
title
timezone
skills_raw
web3mailPreferences{
activeOnNewService
activeOnNewProposal
activeOnProposalValidated
activeOnFundRelease
activeOnReview
activeOnPlatformMarketing
activeOnProtocolMarketing
}
}
}
}
`;

export const getUserTotalGains = (id: string) => `
{
user(id: "${id}") {
totalGains{
id
totalGain
token {
id
name
symbol
decimals
}
}
`;
}
}
}
`;

export const getPaymentsForUser = (
userId: string,
numberPerPage?: number,
offset?: number,
startDate?: string,
endDate?: string,
) => {
const pagination = numberPerPage ? 'first: ' + numberPerPage + ', skip: ' + offset : '';

const startDataCondition = startDate ? `, createdAt_gte: "${startDate}"` : '';
const endDateCondition = endDate ? `, createdAt_lte: "${endDate}"` : '';

const query = `
{
payments(where: {
service_: {seller: "${userId}"}
${startDataCondition}
${endDateCondition}
},
orderBy: createdAt orderDirection: desc ${pagination} ) {
id,
rateToken {
address
decimals
name
symbol
}
amount
transactionHash
paymentType
createdAt
service {
id,
cid
}
}
}
`;
return query;
};
57 changes: 56 additions & 1 deletion packages/client/src/profile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import IPFSClient from '../ipfs';
import { getProtocolById } from '../platform/graphql/queries';
import { ClientTransactionResponse } from '../types';
import { ViemClient } from '../viem';
import { getProfileByAddress } from './graphql';
import { getPaymentsForUser, getProfileByAddress, getProfileById, getProfiles, getUserTotalGains } from './graphql';
import { TalentLayerProfile } from './types';

export class Profile {
Expand Down Expand Up @@ -37,6 +37,14 @@ export class Profile {

return null;
}

public async getById(userId: string): Promise<any> {
const query = getProfileById(userId);

const response = await this.graphQlClient.get(query);

return response?.data?.user || null;
}

public async upload(profileData: TalentLayerProfile): Promise<string> {
return this.ipfsClient.post(JSON.stringify(profileData));
Expand Down Expand Up @@ -84,4 +92,51 @@ export class Profile {
BigInt(fee),
);
}

public async getBy(params: {
numberPerPage?: number;
offset?: number;
searchQuery?: string;
}): Promise<any> {
const query = getProfiles(params.numberPerPage, params.offset, params.searchQuery);

return this.graphQlClient.get(query);
}

public async getTotalGains(userId: string): Promise<any> {
const query = getUserTotalGains(userId);

const response = await this.graphQlClient.get(query);

return response?.data?.user?.totalGains || null;
}

public async getPayments(
userId: string,
numberPerPage?: number,
offset?: number,
startDate?: string,
endDate?: string,
): Promise<any> {
const query = getPaymentsForUser(userId, numberPerPage, offset, startDate, endDate)

const response = await this.graphQlClient.get(query);

return response?.data?.payments || null
}

public async getMintFees(): Promise<any> {
const query = `
{
protocols {
userMintFee,
shortHandlesMaxPrice
}
}
`;

const response = await this.graphQlClient.get(query);

return response?.data || null;
}
}
Loading