Skip to content

Commit

Permalink
feat: add create client util
Browse files Browse the repository at this point in the history
  • Loading branch information
janndriessen committed Sep 20, 2024
1 parent a0ca679 commit 9eaeaeb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/utils/clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

import { ChainId } from 'constants/chains'

export function createClient(chainId: number) {
if (chainId !== ChainId.Mainnet) return null
return createPublicClient({
chain: mainnet,
transport: http(process.env.MAINNET_ALCHEMY_API),
})
}
20 changes: 12 additions & 8 deletions src/utils/component-swap-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AddressZero } from 'constants/addresses'
import { USDC } from 'constants/tokens'
import { SwapQuote, SwapQuoteProvider } from 'quote'
import { isSameAddress } from 'utils/addresses'
import { createClient } from 'utils/clients'
import { getIssuanceModule } from 'utils/issuanceModules'
import { getRpcProvider } from 'utils/rpc-provider'
import { Exchange, SwapData } from 'utils/swap-data'
Expand Down Expand Up @@ -155,7 +156,9 @@ export async function getIssuanceComponentSwapData(
indexTokenAmount
)
const underlyingERC20sPromises: Promise<WrappedToken>[] =
issuanceComponents.map((component: string) => getUnderlyingErc20(component))
issuanceComponents.map((component: string) =>
getUnderlyingErc20(component, chainId)
)
const wrappedTokens = await Promise.all(underlyingERC20sPromises)
// TODO:
// const buyAmountsPromises = issuanceComponents.map(
Expand Down Expand Up @@ -211,7 +214,9 @@ export async function getRedemptionComponentSwapData(
indexTokenAmount
)
const underlyingERC20sPromises: Promise<WrappedToken>[] =
issuanceComponents.map((component: string) => getUnderlyingErc20(component))
issuanceComponents.map((component: string) =>
getUnderlyingErc20(component, chainId)
)
const wrappedTokens = await Promise.all(underlyingERC20sPromises)
// TODO: check google docs
// const buyAmountsPromises = issuanceComponents.map(
Expand Down Expand Up @@ -280,12 +285,11 @@ function getIssuanceContract(
return new Contract(issuanceModule.address, abi, provider)
}

async function getUnderlyingErc20(token: string): Promise<WrappedToken> {
// FIXME: pass in? or config externally?
const publicClient = createPublicClient({
chain: mainnet,
transport: http(process.env.MAINNET_ALCHEMY_API),
})
async function getUnderlyingErc20(
token: string,
chainId: number
): Promise<WrappedToken> {
const publicClient = createClient(chainId)!

Check warning on line 292 in src/utils/component-swap-data.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion
const decimals: number = await publicClient.readContract({
address: token as Address,
abi: parseAbi(['function decimals() view returns (uint8)']),
Expand Down

0 comments on commit 9eaeaeb

Please sign in to comment.