-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6c7425
commit 8ba9a3a
Showing
17 changed files
with
92 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,4 @@ | ||
import { Chain, RpcUrls } from "@starknet-react/chains"; | ||
import { Chain } from "@starknet-react/chains"; | ||
import { ProviderInterface } from "starknet"; | ||
|
||
export type ChainProviderFactory = (chain: Chain) => { | ||
chain: Chain; | ||
rpcUrls: RpcUrls; | ||
} | null; | ||
|
||
/** Returns a new chain with the default RPC URL set. */ | ||
export function setDefaultRpcUrl(chain: Chain, url: string): Chain { | ||
return { | ||
...chain, | ||
rpcUrls: { | ||
...chain.rpcUrls, | ||
default: { http: [url] }, | ||
}, | ||
}; | ||
} | ||
export type ChainProviderFactory<T extends ProviderInterface = ProviderInterface> = (chain: Chain) => T | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { Chain } from "@starknet-react/chains"; | ||
import { RpcProvider, RpcProviderOptions } from "starknet"; | ||
|
||
import { ChainProviderFactory, setDefaultRpcUrl } from "./factory"; | ||
import { starknetChainId } from "~/context"; | ||
import { ChainProviderFactory } from "./factory"; | ||
|
||
/** Arguments for `jsonRpcProvider`. */ | ||
export type JsonRpcProviderArgs = { | ||
rpc: (chain: Chain) => { http: string } | null; | ||
rpc: (chain: Chain) => RpcProviderOptions | null; | ||
}; | ||
|
||
/** Configure the JSON-RPC provider using the provided function. */ | ||
export function jsonRpcProvider({ | ||
rpc, | ||
}: JsonRpcProviderArgs): ChainProviderFactory { | ||
}: JsonRpcProviderArgs): ChainProviderFactory<RpcProvider> { | ||
return function (chain) { | ||
const config = rpc(chain); | ||
if (!config || config.http === "") return null; | ||
return { | ||
chain: setDefaultRpcUrl(chain, config.http), | ||
rpcUrls: { | ||
http: [config.http], | ||
}, | ||
}; | ||
if (!config) return null; | ||
const chainId = starknetChainId(chain.id); | ||
|
||
const provider = new RpcProvider({ ...config, chainId }); | ||
return provider; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { ChainProviderFactory } from "./factory"; | ||
import { jsonRpcProvider } from "./jsonrpc"; | ||
|
||
/** Configure the provider to use the public RPC endpoint. */ | ||
export function publicProvider(): ChainProviderFactory { | ||
return function (chain) { | ||
if (!chain.rpcUrls.public.http[0]) return null; | ||
return { | ||
chain, | ||
rpcUrls: chain.rpcUrls.public, | ||
}; | ||
}; | ||
export function publicProvider() { | ||
return jsonRpcProvider({ | ||
rpc: chain => { | ||
const nodeUrl = chain.rpcUrls.public.http[0]; | ||
if (!nodeUrl) return null; | ||
return { nodeUrl }; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.