From 3d965dc0affb08bed06e9212319b58169305ad38 Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 2 May 2024 13:28:21 +0100 Subject: [PATCH] feat: flexible way for user to use their own blockchain client --- .../src/lib/contracts-sdk.spec.ts | 13 +++++++++ .../contracts-sdk/src/lib/contracts-sdk.ts | 29 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/packages/contracts-sdk/src/lib/contracts-sdk.spec.ts b/packages/contracts-sdk/src/lib/contracts-sdk.spec.ts index 4e17643a5b..96c8fc33da 100644 --- a/packages/contracts-sdk/src/lib/contracts-sdk.spec.ts +++ b/packages/contracts-sdk/src/lib/contracts-sdk.spec.ts @@ -79,4 +79,17 @@ describe('contractsSdk', () => { expect(litContracts.rateLimitNftContract.write.address).toBeDefined(); expect(litContracts.stakingContract.read.address).toBeDefined(); }); + + it('should get all contracts', async () => { + // polyfill + const crossFetch = require('cross-fetch'); + globalThis.fetch = crossFetch.fetch; + + // test + const contracts = await LitContracts.getContracts(); + expect(Object.entries(contracts).length).toBe(3); + expect(contracts.cayenne).toBeDefined(); + expect(contracts.manzano).toBeDefined(); + expect(contracts.habanero).toBeDefined(); + }); }); diff --git a/packages/contracts-sdk/src/lib/contracts-sdk.ts b/packages/contracts-sdk/src/lib/contracts-sdk.ts index 3c0ae8fdaf..5de268602f 100644 --- a/packages/contracts-sdk/src/lib/contracts-sdk.ts +++ b/packages/contracts-sdk/src/lib/contracts-sdk.ts @@ -52,6 +52,7 @@ import { convertRequestsPerDayToPerSecond, requestsToKilosecond, } from './utils'; +import { LitNetwork } from '@lit-protocol/constants'; const DEFAULT_RPC = 'https://chain-rpc.litprotocol.com/http'; const BLOCK_EXPLORER = 'https://chain.litprotocol.com/'; @@ -568,6 +569,34 @@ export class LitContracts { this.connected = true; }; + /** + * Here are the Lit contracts for different networks, you can use whatever blockchain clients you want to interact with them. + * eg. ethers.js, web3.js, viem, etc. We don't care. + * + * @returns {Promise<{ + * manzano: LitContractContext, + * habanero: LitContractContext, + * cayenne: LitContractContext + * }>} Returns an object with the contracts for each network. + */ + public static getContracts = async (): Promise<{ + manzano: LitContractContext; + habanero: LitContractContext; + cayenne: LitContractContext; + }> => { + const [manzano, habanero, cayenne] = await Promise.all([ + await LitContracts._resolveContractContext(LitNetwork.Manzano), + await LitContracts._resolveContractContext(LitNetwork.Habanero), + await LitContracts._resolveContractContext(LitNetwork.Cayenne), + ]); + + return { + manzano, + habanero, + cayenne, + }; + }; + public static async getStakingContract( network: 'cayenne' | 'manzano' | 'habanero' | 'custom' | 'localhost', context?: LitContractContext | LitContractResolverContext