Skip to content

Commit

Permalink
Merge pull request #10 from lidofinance/feat/add_define_web3_provider
Browse files Browse the repository at this point in the history
feat: add define web3 provider, update readme
  • Loading branch information
DiRaiks authored Aug 24, 2023
2 parents 61905e3 + 960fdcd commit e88e2ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const lidoSDK = new LidoSDK({
rpcUrls: ["https://eth-goerli.alchemyapi.io/v2/{ALCHEMY_API_KEY}"],
});

// Define default web3 provider in sdk (window.ethereum) if web3Provider is not defined in constructor
lidoSDK.core.defineWeb3Provider();

// Views
const balanceETH = await lidoSDK.core.balanceETH(address);
const balanceStETH = await lidoSDK.staking.balanceStETH(address);
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ const lidoSDK = new LidoSDK({
chainId: 5,
});

// Create default web3 provider (window.ethereum)
lidoSDK.createWeb3Provider(lidoSDK.chain);
// Define default web3 provider in sdk (window.ethereum) if web3Provider is not defined in constructor
lidoSDK.core.defineWeb3Provider();

const callback: StakeStageCallback = ({ stage, payload }) => {
switch (stage) {
Expand Down
55 changes: 31 additions & 24 deletions packages/sdk/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import { LidoSDKCoreProps } from "./types.js";
export default class LidoSDKCore {
readonly chainId: (typeof SUPPORTED_CHAINS)[number];
readonly rpcUrls: string[] | undefined;
readonly rpcProvider: PublicClient;
readonly web3Provider: WalletClient | undefined;
readonly chain: Chain;
readonly rpcProvider: PublicClient;
public web3Provider: WalletClient | undefined;

constructor(props: LidoSDKCoreProps, version?: string) {
const { chain, rpcProvider, web3Provider } = this.init(props, version);
Expand Down Expand Up @@ -81,35 +81,42 @@ export default class LidoSDKCore {

@Logger("Provider:")
public createRpcProvider(chain: Chain, rpcUrls: string[]): PublicClient {
if (!this.rpcProvider) {
const rpcs = rpcUrls.map((url) => http(url));

return createPublicClient({
batch: {
multicall: true,
},
chain,
transport: fallback(rpcs),
});
}
return this.rpcProvider;
const rpcs = rpcUrls.map((url) => http(url));

return createPublicClient({
batch: {
multicall: true,
},
chain,
transport: fallback(rpcs),
});
}

@Logger("Provider:")
public createWeb3Provider(chain: Chain): WalletClient {
if (!this.web3Provider) {
return createWalletClient({
chain,
// TODO: fix type
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
transport: custom(window.ethereum),
});
}
return createWalletClient({
chain,
// TODO: fix type
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
transport: custom(window.ethereum),
});
}

@Logger("Provider:")
public defineWeb3Provider(): WalletClient {
invariant(!this.web3Provider, "Web3 provider is already defined");

this.web3Provider = this.createWeb3Provider(this.chain);

return this.web3Provider;
}

@Logger("Provider:")
public setWeb3Provider(web3Provider: WalletClient): void {
invariant(web3Provider.chain === this.chain, "Wrong chain");

this.web3Provider = web3Provider;
}
// Balances

@Logger("Balances:")
Expand Down

0 comments on commit e88e2ec

Please sign in to comment.