Skip to content
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

added devconfig support to sdk #17

Merged
merged 15 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 156 additions & 1 deletion packages/client/src/__tests__/talentLayerClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import { Profile } from '../profile';
import { Proposal } from '../proposals';
import { Review } from '../reviews';
import { Service } from '../services';
import { CustomConfig, NetworkEnum } from '../types';
import { testPlatformId } from '../__mocks__/fixtures';
import TalentLayerID from '../contracts/ABI/TalentLayerID.json';
import TalerLayerService from '../contracts/ABI/TalentLayerService.json';
import TalentLayerReview from '../contracts/ABI/TalentLayerReview.json';
import TalentLayerEscrow from '../contracts/ABI/TalentLayerEscrow.json';
import TalentLayerPlatformID from '../contracts/ABI/TalentLayerPlatformID.json';
import TalentLayerArbitrator from '../contracts/ABI/TalentLayerArbitrator.json';
import { getChainConfig } from '../config';



jest.mock('axios')
Expand Down Expand Up @@ -62,4 +71,150 @@ describe('TalentLayerClient', () => {
expect(client.review).toBeInstanceOf(Review)
})
})
});

describe('getChainConfig', () => {
it('should return the chain config based on the network id passed', () => {
// Arrange
const networkId = NetworkEnum.MUMBAI

// Act
const config = client.getChainConfig(networkId);

// Assert
expect(config).toEqual(getChainConfig(networkId));
})
})
});


describe('TalentLayerClient:dev', () => {
let client: any;

beforeEach(() => {
Object.defineProperty(globalThis, 'window', {
});
const chainId = 137;
const ipfsConfig = {
clientId: 'abcd',
clientSecret: 'abcde',
baseUrl: 'www.example.com'
}
const viemConfig = {};
const platformID = testPlatformId;
const signatureApiUrl = 'www.example.com';
const localNetworkId = NetworkEnum.LOCAL;
const customConfig: CustomConfig = {
chainConfig: {
id: localNetworkId,
name: 'local',
network: 'local',
nativeCurrency: {
name: 'racoon',
symbol: 'RCN',
decimals: 18
},
rpcUrls: {
default: {
http: ['http:localhost:1337']
},
public: {
http: ['http:localhost:1337']
}
}



},
contractConfig: {
networkId: localNetworkId,
subgraphUrl: 'www.example.com',
escrowConfig: {
adminFee: '0',
adminWallet: '0xC01FcDfDE3B2ABA1eab76731493C617FfAED2F10',
timeoutPayment: 3600 * 24 * 7,
},
tokens: {
'0x0000000000000000000000000000000000000000': {
address: '0x0000000000000000000000000000000000000000',
symbol: 'RLC',
name: 'iExec RLC',
decimals: 18,
},
'0xe62C28709E4F19Bae592a716b891A9B76bf897E4': {
address: '0xe62C28709E4F19Bae592a716b891A9B76bf897E4',
symbol: 'SERC20',
name: 'SimpleERC20',
decimals: 18,
},
},
contracts: {
talentLayerId: {
address: '0xC51537E03f56650C63A9Feca4cCb5a039c77c822',
abi: TalentLayerID.abi,
},
talentLayerService: {
address: '0x45E8F869Fd316741A9316f39bF09AD03Df88496f',
abi: TalerLayerService.abi,
},
talentLayerReview: {
address: '0x6A5BF452108DA389B7B38284E871f538671Ad375',
abi: TalentLayerReview.abi,
},
talentLayerEscrow: {
address: '0x7A534501a6e63448EBC691f27B27B76d4F9b7E17',
abi: TalentLayerEscrow.abi,
},
talentLayerPlatformId: {
address: '0x05D8A2E01EB06c284ECBae607A2d0c2BE946Bf49',
abi: TalentLayerPlatformID.abi,
},
talentLayerArbitrator: {
address: '0x24cEd045b50cF811862B1c33dC6B1fbC8358F521',
abi: TalentLayerArbitrator.abi,
},
}
}
}

client = new TalentLayerClient({
chainId: chainId,
ipfsConfig: ipfsConfig,
platformId: testPlatformId,
signatureApiUrl: signatureApiUrl,
customConfig: customConfig
})
})

describe('constructor', () => {
it('should be initialised successfully', async () => {
expect(client).toBeDefined()

})
})

describe('getters', () => {
it('should return all domain specific getters', async () => {
expect(client.platform).toBeInstanceOf(Platform)
expect(client.erc20).toBeInstanceOf(ERC20)
expect(client.proposal).toBeInstanceOf(Proposal)
expect(client.disputes).toBeInstanceOf(Disputes)
expect(client.service).toBeInstanceOf(Service)
expect(client.profile).toBeInstanceOf(Profile)
expect(client.escrow).toBeInstanceOf(Escrow)
expect(client.review).toBeInstanceOf(Review)
})
})

describe('getChainConfig', () => {
it('should return the chain config irrespective of the network id passed', () => {
// Arrange
const networkId = NetworkEnum.MUMBAI

// Act
const config = client.getChainConfig(networkId);

// Assert
expect(config).toEqual(client.customConfig.contractConfig);
})
})
})
13 changes: 8 additions & 5 deletions packages/client/src/blockchain-bindings/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ERC20Contract from '../contracts/ABI/ERC20.json';

import { ViemClient } from '../viem';
import { getChainConfig } from '../config';
import { NetworkEnum } from '../types';
import { Config, CustomConfig, NetworkEnum } from '../types';

export interface IERC20 {
balanceOf(tokenAddress: `0x${string}`): Promise<any>;
Expand All @@ -16,19 +16,22 @@ export class ERC20 {
viemClient: ViemClient;
platformID: number;
chainId: NetworkEnum;
customConfig?: CustomConfig

constructor(
ipfsClient: IPFSClient,
viemClient: ViemClient,
platformId: number,
chainId: NetworkEnum,
customConfig?: CustomConfig
) {
console.log('SDK: erc20 initialising: ');

this.platformID = platformId;
this.ipfsClient = ipfsClient;
this.viemClient = viemClient;
this.chainId = chainId;
this.customConfig = customConfig
}

public async balanceOf(tokenAddress: `0x${string}`): Promise<any> {
Expand All @@ -47,13 +50,13 @@ export class ERC20 {
}

public async checkAllowance(tokenAddress: `0x${string}`): Promise<any> {
// @ts-ignore

const [address] = await this.viemClient.client.getAddresses();

const chainConfig = getChainConfig(this.chainId);
const chainConfig: Config = this.customConfig ? this.customConfig.contractConfig : getChainConfig(this.chainId);

const contract = chainConfig.contracts['talentLayerEscrow'];

// @ts-ignore
const allowance: any = await this.viemClient.publicClient.readContract({
address: tokenAddress,
abi: ERC20Contract.abi,
Expand All @@ -68,7 +71,7 @@ export class ERC20 {
// @ts-ignore
const [address] = await this.viemClient.client.getAddresses();

const chainConfig = getChainConfig(this.chainId);
const chainConfig: Config = this.customConfig ? this.customConfig.contractConfig : getChainConfig(this.chainId);
const contract = chainConfig.contracts['talentLayerEscrow'];

// @ts-ignore
Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ const chains: { [networkId in NetworkEnum]: Config } = {
[NetworkEnum.MUMBAI]: mumbai,
[NetworkEnum.IEXEC]: iexec,
[NetworkEnum.POLYGON]: polygon,
// This value is a place holder. The local value is provided through
// dev config when using the sdk in dev mode
[NetworkEnum.LOCAL]: mumbai,
};

export const getChainConfig = (networkId: NetworkEnum) => chains[networkId];
Expand Down
7 changes: 5 additions & 2 deletions packages/client/src/disputes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hash, parseEther, toHex, zeroAddress } from 'viem';
import { getChainConfig } from '../config';
import GraphQLClient from '../graphql';
import { getPlatformById } from '../platform/graphql/queries';
import { NetworkEnum } from '../types';
import { Config, CustomConfig, NetworkEnum, TransactionHash } from '../types';
import { ViemClient } from '../viem';

/**
Expand All @@ -15,16 +15,19 @@ export class Disputes {
platformID: number;
subgraph: GraphQLClient;
chainId: NetworkEnum;
customConfig?: CustomConfig
constructor(
walletClient: ViemClient,
platformId: number,
graphQlClient: GraphQLClient,
chainId: number,
customConfig?: CustomConfig
) {
this.wallet = walletClient;
this.platformID = platformId;
this.subgraph = graphQlClient;
this.chainId = chainId;
this.customConfig = customConfig;
}

/**
Expand All @@ -45,7 +48,7 @@ export class Disputes {
return 0;
}

const chainConfig = getChainConfig(this.chainId);
const chainConfig: Config = this.customConfig ? this.customConfig.contractConfig : getChainConfig(this.chainId);
const contract = chainConfig.contracts['talentLayerArbitrator'];

console.log('SDK: reading contract');
Expand Down
20 changes: 15 additions & 5 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getChainConfig, getGraphQLConfig } from './config';
import GraphQLClient from './graphql';
import { Config, NetworkEnum, TalentLayerClientConfig } from './types';
import { Config, CustomConfig, NetworkEnum, TalentLayerClientConfig } from './types';
import IPFSClient from './ipfs';
import { ViemClient } from './viem';
import { IERC20, ERC20 } from './blockchain-bindings/erc20';
Expand Down Expand Up @@ -30,10 +30,9 @@ export class TalentLayerClient {
viemClient: ViemClient;
/** @hidden */
platformID: number;
/** @hidden */
chainId: NetworkEnum;
/** @hidden */
chainId: NetworkEnum | number;
signatureApiUrl?: string;
customConfig?: CustomConfig;

/** @hidden */
constructor(config: TalentLayerClientConfig) {
Expand All @@ -48,12 +47,23 @@ export class TalentLayerClient {
this.viemClient = new ViemClient(config.chainId, config.walletConfig || {});
this.chainId = config.chainId;
this.signatureApiUrl = config?.signatureApiUrl;

// DEV mode overrides
if (config.customConfig) {
this.customConfig = config?.customConfig;
this.chainId = config.customConfig.chainConfig.id
this.graphQlClient = new GraphQLClient(getGraphQLConfig(config.customConfig.chainConfig.id));
this.viemClient = new ViemClient(config.customConfig.chainConfig.id, config.walletConfig || {}, this.customConfig);
}
}

/**
* Returns chain config based on netowrk ID
*/
public getChainConfig(networkId: NetworkEnum): Config {
if (this.customConfig) {
return this.customConfig.contractConfig;
}
return getChainConfig(networkId);
}

Expand All @@ -63,7 +73,7 @@ export class TalentLayerClient {
*/
// @ts-ignore
get erc20(): IERC20 {
return new ERC20(this.ipfsClient, this.viemClient, this.platformID, this.chainId);
return new ERC20(this.ipfsClient, this.viemClient, this.platformID, this.chainId, this.customConfig);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions packages/client/src/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getChainConfig } from '../config';
import { FEE_RATE_DIVIDER } from '../constants';
import GraphQLClient from '../graphql';
import IPFSClient from '../ipfs';
import { ClientTransactionResponse, NetworkEnum } from '../types';
import { ClientTransactionResponse, Config, CustomConfig, NetworkEnum, TransactionHash } from '../types';
import { ViemClient } from '../viem';
import { getPlatformById, getPlatformsByOwner } from './graphql/queries';
import { Arbitrator, PlatformDetails } from './types';
Expand All @@ -17,6 +17,7 @@ export class Platform {
platformID: number;
/** @hidden */
ipfs: IPFSClient;
customConfig?: CustomConfig;

/** @hidden */
static readonly UPDATE_ERROR = 'unable to update platform details';
Expand All @@ -27,11 +28,13 @@ export class Platform {
walletClient: ViemClient,
platformId: number,
ipfsClient: IPFSClient,
customConfig?: CustomConfig
) {
this.subgraph = graphQlClient;
this.wallet = walletClient;
this.platformID = platformId;
this.ipfs = ipfsClient;
this.customConfig = customConfig;
}

/**
Expand Down Expand Up @@ -122,7 +125,7 @@ export class Platform {
* @returns {Arbitrator[]} An array of arbitrator objects available for the specified chain.
*/
public getArbitrators(chainId: NetworkEnum): Arbitrator[] {
const chainConfig = getChainConfig(chainId);
const chainConfig: Config = this.customConfig ? this.customConfig.contractConfig : getChainConfig(chainId);
const contract = chainConfig.contracts['talentLayerArbitrator'];
const allowedArbitrators = [
{ address: zeroAddress, name: 'None' },
Expand Down
Loading