Skip to content

Commit

Permalink
feat: flexible way for user to use their own blockchain client
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed May 2, 2024
1 parent 1fe907d commit 3d965dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/contracts-sdk/src/lib/contracts-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
29 changes: 29 additions & 0 deletions packages/contracts-sdk/src/lib/contracts-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3d965dc

Please sign in to comment.