-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add curve sdk, add ethers adapters
- Loading branch information
Showing
7 changed files
with
313 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './state'; |
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 |
---|---|---|
@@ -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]; | ||
}); |
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,2 +1,3 @@ | ||
export * from './curve'; | ||
export * from './prices'; | ||
export * from './wagmi'; |
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,3 +1,4 @@ | ||
export * from './components/AddressLabel'; | ||
export * from './components/ConnectedButton'; | ||
export * from './components/OpenAccountModalButton'; | ||
export * from './utils'; |
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 |
---|---|---|
@@ -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); | ||
} |
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.