Skip to content

Commit

Permalink
Add in generation
Browse files Browse the repository at this point in the history
  • Loading branch information
smitch88 committed Sep 1, 2023
2 parents d83d00f + 94bf478 commit 437e007
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VITE_WALLET_CONNECT_PROJECT_ID=
VITE_INFURA_ID=
VITE_ALCHEMY_ID=
35 changes: 23 additions & 12 deletions apps/oeth/src/clients/wagmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import {
} from '@rainbow-me/rainbowkit/wallets';
import { configureChains, createConfig } from 'wagmi';
import { goerli, localhost, mainnet } from 'wagmi/chains';
import { infuraProvider } from 'wagmi/providers/infura';
import { alchemyProvider } from 'wagmi/providers/alchemy';
import { publicProvider } from 'wagmi/providers/public';

const VITE_WALLET_CONNECT_PROJECT_ID = import.meta.env
.VITE_WALLET_CONNECT_PROJECT_ID;
const VITE_INFURA_ID = import.meta.env.VITE_INFURA_ID;

export const { chains, publicClient, webSocketPublicClient } = configureChains(
[mainnet, goerli, localhost],
[infuraProvider({ apiKey: VITE_INFURA_ID }), publicProvider()],
[
alchemyProvider({ apiKey: import.meta.env.VITE_ALCHEMY_ID }),
publicProvider(),
],
);

const connectors = connectorsForWallets([
Expand All @@ -32,12 +31,15 @@ const connectors = connectorsForWallets([
metaMaskWallet({
chains,
shimDisconnect: true,
projectId: VITE_WALLET_CONNECT_PROJECT_ID,
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
}),
ledgerWallet({
chains,
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
}),
ledgerWallet({ chains, projectId: VITE_WALLET_CONNECT_PROJECT_ID }),
walletConnectWallet({
chains,
projectId: VITE_WALLET_CONNECT_PROJECT_ID,
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
}),
coinbaseWallet({ appName: 'mStable', chains }),
],
Expand All @@ -47,10 +49,19 @@ const connectors = connectorsForWallets([
wallets: [
injectedWallet({ chains, shimDisconnect: true }),
safeWallet({ chains }),
rainbowWallet({ chains, projectId: VITE_WALLET_CONNECT_PROJECT_ID }),
rainbowWallet({
chains,
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
}),
braveWallet({ chains, shimDisconnect: true }),
argentWallet({ chains, projectId: VITE_WALLET_CONNECT_PROJECT_ID }),
imTokenWallet({ chains, projectId: VITE_WALLET_CONNECT_PROJECT_ID }),
argentWallet({
chains,
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
}),
imTokenWallet({
chains,
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
}),
],
},
]);
Expand Down
2 changes: 1 addition & 1 deletion apps/oeth/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

interface ImportMetaEnv {
readonly VITE_WALLET_CONNECT_PROJECT_ID: string;
readonly VITE_INFURA_ID: string;
readonly VITE_ALCHEMY_ID: string;
}

interface ImportMeta {
Expand Down
2 changes: 2 additions & 0 deletions apps/oeth/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CssBaseline,
Experimental_CssVarsProvider as CssVarsProvider,
} from '@mui/material';
import { CurveProvider } from '@origin/shared/providers';
import { theme } from '@origin/shared/theme';
import { composeContexts } from '@origin/shared/utils';
import { darkTheme, RainbowKitProvider } from '@rainbow-me/rainbowkit';
Expand All @@ -33,6 +34,7 @@ root.render(
[CssVarsProvider, { theme: theme, defaultMode: 'dark' }],
[WagmiConfig, { config: wagmiConfig }],
[RainbowKitProvider, { chains: chains, theme: darkTheme() }],
[CurveProvider, { alchemyApiKey: import.meta.env.VITE_ALCHEMY_ID }],
],
<>
<CssBaseline />
Expand Down
8 changes: 4 additions & 4 deletions libs/oeth/swap/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import redeemMix from './redeemMix';
import type { SwapAction, SwapApi } from '../types';

export const swapActions: Record<SwapAction, SwapApi> = {
'swap-curve': { ...defaultApi, ...mintVault },
'swap-zapper': { ...defaultApi, ...redeemMix },
'mint-vault': { ...defaultApi },
'redeem-mix': { ...defaultApi },
'swap-curve': { ...defaultApi },
'swap-zapper': { ...defaultApi },
'mint-vault': { ...defaultApi, ...mintVault },
'redeem-mix': { ...defaultApi, ...redeemMix },
'wrap-oeth': { ...defaultApi },
'unwrap-woeth': { ...defaultApi },
};
14 changes: 11 additions & 3 deletions libs/oeth/swap/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { isNilOrEmpty } from '@origin/shared/utils';
import { produce } from 'immer';

import { useSwapState } from './state';
import { getAllAvailableTokens, getAvailableTokensForSource } from './utils';
import {
getAllAvailableTokens,
getAvailableRoutes,
getAvailableTokensForSource,
} from './utils';

import type { Token } from '@origin/shared/contracts';

Expand Down Expand Up @@ -79,6 +83,10 @@ export const useHandleTokenChange = () => {
state.tokenIn = availableTokensIn[0];
}
}
state.swapRoute = getAvailableRoutes(
state.tokenIn,
state.tokenOut,
)[0];
}),
);
},
Expand All @@ -103,11 +111,11 @@ export const useHandleTokenFlip = () => {
};

export const useHandleSwap = () => {
const [{ tokenIn, tokenOut, amountIn, swapRoute }] = useSwapState();
const [{ swapRoute }] = useSwapState();

return useCallback(async () => {
if (isNilOrEmpty(swapRoute)) {
return;
}
}, [amountIn, swapRoute, tokenIn, tokenOut]);
}, [swapRoute]);
};
1 change: 1 addition & 0 deletions libs/shared/providers/src/curve/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './state';
61 changes: 61 additions & 0 deletions libs/shared/providers/src/curve/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useEffect, useState } from 'react';

import curve from '@curvefi/api';
import { createContainer } from 'react-tracked';
import { mainnet, useAccount, useNetwork } from 'wagmi';

import { getEthersProvider, getEthersSigner } from '../wagmi';

export type CurveProviderProps = { alchemyApiKey: string };

export const { Provider: CurveProvider, useTrackedState: useCurve } =
createContainer(({ alchemyApiKey }: CurveProviderProps) => {
const [state, setState] = useState(null);
const { isConnected } = useAccount();
const { chain } = useNetwork();

useEffect(() => {
const initPublic = async () => {
const ethersProvider = getEthersProvider({
chainId: chain?.id ?? mainnet.id,
});
await curve.init(
'Alchemy',
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
externalProvider: ethersProvider as any,
apiKey: alchemyApiKey,
},
{ chainId: chain?.id ?? mainnet.id },
);
setState(curve);
};

const initWallet = async () => {
const ethersSigner = await getEthersSigner({
chainId: chain.id,
});

await curve.init(
'Alchemy',
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
externalProvider: ethersSigner as any,
apiKey: alchemyApiKey,
},
{
chainId: chain.id,
},
);
setState(curve);
};

if (isConnected) {
initWallet();
} else {
initPublic();
}
}, [alchemyApiKey, chain?.id, isConnected]);

return [state, setState];
});
1 change: 1 addition & 0 deletions libs/shared/providers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './curve';
export * from './prices';
export * from './wagmi';
1 change: 1 addition & 0 deletions libs/shared/providers/src/wagmi/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './components/AddressLabel';
export * from './components/ConnectedButton';
export * from './components/OpenAccountModalButton';
export * from './utils';
60 changes: 60 additions & 0 deletions libs/shared/providers/src/wagmi/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Adapted from https://wagmi.sh/core/ethers-adapters
import { getPublicClient, getWalletClient } from '@wagmi/core';
import {
BrowserProvider,
FallbackProvider,
JsonRpcProvider,
JsonRpcSigner,
} from 'ethers';

import type { PublicClient } from '@wagmi/core';
import type { HttpTransport } from 'viem';
import type { WalletClient } from 'wagmi';

export function publicClientToProvider(publicClient: PublicClient) {
const { chain, transport } = publicClient;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
if (transport.type === 'fallback') {
const providers = (transport.transports as ReturnType<HttpTransport>[]).map(
({ value }) => new JsonRpcProvider(value?.url, network),
);
if (providers.length === 1) return providers[0];

return new FallbackProvider(providers);
}

return new JsonRpcProvider(transport.url, network);
}

/** Action to convert a viem Public Client to an ethers.js Provider. */
export function getEthersProvider({ chainId }: { chainId?: number } = {}) {
const publicClient = getPublicClient({ chainId });

return publicClientToProvider(publicClient);
}

export function walletClientToSigner(walletClient: WalletClient) {
const { account, chain, transport } = walletClient;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const provider = new BrowserProvider(transport as any, network);
const signer = new JsonRpcSigner(provider, account.address);

return signer;
}

/** Action to convert a viem Wallet Client to an ethers.js Signer. */
export async function getEthersSigner({ chainId }: { chainId?: number } = {}) {
const walletClient = await getWalletClient({ chainId });
if (!walletClient) return undefined;

return walletClientToSigner(walletClient);
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"private": true,
"dependencies": {
"@curvefi/api": "^2.46.1",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/material": "^5.14.3",
Expand All @@ -20,6 +21,7 @@
"@tanstack/react-table": "^8.9.3",
"@wagmi/core": "^1.3.9",
"axios": "^1.4.0",
"ethers": "^6.7.1",
"immer": "^10.0.2",
"ramda": "^0.29.0",
"react": "18.2.0",
Expand Down
Loading

0 comments on commit 437e007

Please sign in to comment.