Skip to content

Commit

Permalink
feat: add curve sdk, add ethers adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Sep 1, 2023
1 parent e830256 commit 6f6b437
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 2 deletions.
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';
43 changes: 43 additions & 0 deletions libs/shared/providers/src/curve/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useEffect, useState } from 'react';

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

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

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

useEffect(() => {
const initPublic = async () => {
await curve.init('JsonRpc', {}, {});
setState(curve);
};

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

await curve.init(
'Web3',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ externalProvider: ethersSigner.provider as any },
{},
);
setState(curve);
};

if (isConnected) {
initWallet();
} else {
initPublic();
}
}, [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 @@ -10,6 +10,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 @@ -19,6 +20,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 6f6b437

Please sign in to comment.