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

Proposal/return all contracts #451

Closed
wants to merge 2 commits into from
Closed
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
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not import 'cross-fetch/polyfill'; at top of file?

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();
});
});
28 changes: 28 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,33 @@ export class LitContracts {
this.connected = true;
};

/**
* Here are the Lit contracts for different networks; you can use whichever blockchain clients you want to interact with them, e.g., 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),
Comment on lines +587 to +589
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the equivalent of:

    const val = await LitContracts._resolveContractContext(LitNetwork.Manzano);
    const val2 = await LitContracts._resolveContractContext(LitNetwork.Habanero);
    const val3 = await LitContracts._resolveContractContext(LitNetwork.Cayenne);
    
    const [manzano, habanero, cayenne] = await Promise.all([val, val2, val3]);

    return {
      manzano,
      habanero,
      cayenne
    };

🔪 awaiting each array element here -- promise.all() should receive an array of promises that are running in parallel, but this is awaiting each array element, putting the the un-wrapped values into an array, then passing the array of no-longer-promises into promise.all() instead of passing the promises themselves into `promise.all()...

Copy link
Collaborator Author

@Ansonhkg Ansonhkg May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! yea was copy/pasting the original code didn't pay attention to that, but we can discuss if we actually want this. It was a 3am idea that i quickly drafted, so it's not a hard requirement nor tracked in linear.

]);

return {
manzano,
habanero,
cayenne,
};
};

public static async getStakingContract(
network: 'cayenne' | 'manzano' | 'habanero' | 'custom' | 'localhost',
context?: LitContractContext | LitContractResolverContext
Expand Down
Loading