Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create client with url #101

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/utils/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ export function createClient(chainId: number) {
transport: http(process.env.MAINNET_ALCHEMY_API),
})
}

export function createClientWithUrl(chainId: number, rpcUrl: string) {
if (chainId === ChainId.Base) {
return createPublicClient({
chain: base,
transport: http(rpcUrl),
})
}
if (chainId !== ChainId.Mainnet) return null
return createPublicClient({
chain: mainnet,
transport: http(rpcUrl),
})
}
26 changes: 17 additions & 9 deletions src/utils/component-swap-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Address, parseAbi } from 'viem'
import { AddressZero } from 'constants/addresses'
import { SwapQuote, SwapQuoteProvider } from 'quote'
import { isSameAddress } from 'utils/addresses'
import { createClient } from 'utils/clients'
import { createClientWithUrl } from 'utils/clients'
import { getIssuanceModule } from 'utils/issuanceModules'
import { getRpcProvider } from 'utils/rpc-provider'
import { Exchange, SwapDataV3 } from 'utils/swap-data'
Expand Down Expand Up @@ -73,12 +73,12 @@ export async function getIssuanceComponentSwapData(
)
const underlyingERC20sPromises: Promise<WrappedToken>[] =
issuanceComponents.map((component: string) =>
getUnderlyingErc20(component, chainId)
getUnderlyingErc20(component, chainId, rpcUrl)
)
const units = issuanceUnits.map((unit: BigNumber) => unit.toString())
const amountPromises = issuanceComponents.map(
(component: Address, index: number) =>
getAmount(true, component, BigInt(units[index]), chainId)
getAmount(true, component, BigInt(units[index]), chainId, rpcUrl)
)
const wrappedTokens = await Promise.all(underlyingERC20sPromises)
const amounts: bigint[] = await Promise.all(amountPromises)
Expand Down Expand Up @@ -129,11 +129,17 @@ export async function getRedemptionComponentSwapData(
)
const underlyingERC20sPromises: Promise<WrappedToken>[] =
issuanceComponents.map((component: string) =>
getUnderlyingErc20(component, chainId)
getUnderlyingErc20(component, chainId, rpcUrl)
)
const amountPromises = issuanceComponents.map(
(component: Address, index: number) =>
getAmount(false, component, issuanceUnits[index].toBigInt(), chainId)
getAmount(
false,
component,
issuanceUnits[index].toBigInt(),
chainId,
rpcUrl
)
)
const wrappedTokens = await Promise.all(underlyingERC20sPromises)
const amounts = await Promise.all(amountPromises)
Expand Down Expand Up @@ -191,11 +197,12 @@ async function getAmount(
isMinting: boolean,
component: Address,
issuanceUnits: bigint,
chainId: number
chainId: number,
rpcUrl: string
): Promise<bigint> {
try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const publicClient = createClient(chainId)!
const publicClient = createClientWithUrl(chainId, rpcUrl)!
const erc4626Abi = [
'function convertToAssets(uint256 shares) view returns (uint256 assets)',
'function previewMint(uint256 shares) view returns (uint256 assets)',
Expand Down Expand Up @@ -238,10 +245,11 @@ function getIssuanceContract(

async function getUnderlyingErc20(
token: string,
chainId: number
chainId: number,
rpcUrl: string
): Promise<WrappedToken> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const publicClient = createClient(chainId)!
const publicClient = createClientWithUrl(chainId, rpcUrl)!
Copy link
Collaborator Author

@janndriessen janndriessen Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was the createClient would try to use a internal env var which is not available when used from clients like the API. So now we're passing the RPC URL.

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