Skip to content

Commit

Permalink
feat: refac provider to be multichain
Browse files Browse the repository at this point in the history
  • Loading branch information
zugdev committed Jan 31, 2025
1 parent 3719405 commit ab6147c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log"],
"useGitignore": true,
"language": "en",
"words": ["dataurl", "UUSD", "UBQ", "outdir", "servedir", "reown", "appkit", "proxiable", "TYPEHASH", "twap", "chainlink"],
"words": ["dataurl", "UUSD", "UBQ", "outdir", "servedir", "reown", "appkit", "proxiable", "TYPEHASH", "twap", "chainlink", "lusd", "caip"],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
"ignoreRegExpList": ["[0-9a-fA-F]{6}"]
Expand Down
33 changes: 23 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./router";
import { createAppKit } from "@reown/appkit";
import { Ethers5Adapter } from "@reown/appkit-adapter-ethers5";
import { mainnet } from "@reown/appkit/networks";
import { anvil, mainnet } from "@reown/appkit/networks";
import { ethers } from "ethers";
import { setupContracts } from "./contracts";
import { handleRouting } from "./router";
Expand All @@ -25,16 +25,14 @@ const metadata = {
icons: ["https://avatars.githubusercontent.com/u/76412717"],
};

// create provider & signer for Ethereum mainnet
export const provider = new ethers.providers.JsonRpcProvider("https://eth.llamarpc.com");
export let userSigner: ethers.Signer;

// setup contract instances
export const { dollarContract, governanceContract, diamondContract, twapOracleContract, lusdFeedContract } = setupContracts(provider);
const rpcPerChainId: { [key: string]: string } = {
"1": "https://eth.llamarpc.com",
"31337": "http://localhost:8545",
}

export const appState = createAppKit({
adapters: [new Ethers5Adapter()],
networks: [mainnet],
networks: [mainnet, anvil],
defaultNetwork: mainnet,
metadata,
projectId,
Expand All @@ -43,11 +41,26 @@ export const appState = createAppKit({
},
});

export const getNetworkId = () => appState.getCaipNetworkId()?.toString();

// create provider & signer for Ethereum mainnet
export const provider = () => {
const networkId = getNetworkId();
if (!networkId) {
throw new Error("Network ID not found");
}
return new ethers.providers.JsonRpcProvider(rpcPerChainId[networkId]);
};
export let userSigner: ethers.Signer;

// setup contract instances
export const { dollarContract, governanceContract, diamondContract, twapOracleContract, lusdFeedContract } = setupContracts(provider());

async function waitForConnection() {
return new Promise<void>((resolve) => {
const interval = setInterval(() => {
if (appState.getIsConnectedState()) {
userSigner = provider.getSigner(appState.getAddress());
userSigner = provider().getSigner(appState.getAddress());
console.log(`User connected: ${appState.getAddress()}`);
clearInterval(interval);
resolve();
Expand Down Expand Up @@ -80,7 +93,7 @@ async function updatePrices() {

export async function mainModule() {
try {
console.log("Provider:", provider);
console.log("Provider:", provider());

console.log("Waiting for user connection...");
void waitForConnection();
Expand Down
6 changes: 3 additions & 3 deletions src/pages/redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ async function linkRedeemButton(collateralOptions: CollateralOption[]) {

// Wait for 2 blocks before initiating the collection
await new Promise((resolve) => {
const startBlock = provider.blockNumber;
const startBlock = provider().blockNumber;

const checkBlock = () => {
void (async () => {
const currentBlock = await provider.getBlockNumber();
if (currentBlock >= startBlock + 2) {
const currentBlock = await provider().getBlockNumber();
if (currentBlock >= startBlock + 3) {
resolve(null);
} else {
setTimeout(checkBlock, 1000); // Check every sec
Expand Down

0 comments on commit ab6147c

Please sign in to comment.