Skip to content

Commit

Permalink
Fixing error
Browse files Browse the repository at this point in the history
  • Loading branch information
babkenmes committed Jan 15, 2025
1 parent c957fd5 commit bc35e93
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 61 deletions.
81 changes: 22 additions & 59 deletions lib/gases/providers/evmGasProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import { GasProps } from "../../../Models/Balance"
import { NetworkType, Network, Token } from "../../../Models/Network"
import { Provider } from "./types"
import { PublicClient, TransactionSerializedEIP1559, encodeFunctionData, serializeTransaction } from "viem";
import { PublicClient, TransactionSerializedEIP1559, createPublicClient, encodeFunctionData, http, parseEther, serializeTransaction } from "viem";
import { erc20Abi } from "viem";
import { datadogRum } from "@datadog/browser-rum";
import formatAmount from "../../formatAmount";
import { publicActionsL2 } from 'viem/op-stack'
import resolveChain from "../../resolveChain";

export class EVMGasProvider implements Provider {
supportsNetwork(network: Network): boolean {
Expand Down Expand Up @@ -215,6 +217,14 @@ class getEthereumGas extends getEVMGas {


export default class getOptimismGas extends getEVMGas {

chain = resolveChain(this.from)

client = createPublicClient({
chain: this.chain,
transport: http(),
}).extend(publicActionsL2())

resolveGas = async () => {
const feeData = await this.resolveFeeData()

Expand All @@ -227,13 +237,13 @@ export default class getOptimismGas extends getEVMGas {
if (!multiplier)
return undefined

let totalGas = (multiplier * estimatedGasLimit) + await this.GetOpL1Fee()
let totalGas = (multiplier * estimatedGasLimit) + await this.GetOpL1Fee(feeData.gasPrice!)

const formattedGas = formatAmount(totalGas, this.nativeToken?.decimals)
return formattedGas
}

private GetOpL1Fee = async (): Promise<bigint> => {
private GetOpL1Fee = async (gasPrice: bigint): Promise<bigint> => {
const amount = BigInt(1000)
let serializedTransaction: TransactionSerializedEIP1559

Expand All @@ -249,7 +259,7 @@ export default class getOptimismGas extends getEVMGas {
}

serializedTransaction = serializeTransaction({
client: this.publicClient,
client: this.client,
abi: erc20Abi,
functionName: "transfer",
chainId: this.chainId,
Expand All @@ -261,70 +271,23 @@ export default class getOptimismGas extends getEVMGas {
}
else {
serializedTransaction = serializeTransaction({
client: this.publicClient,
client: this.client,
chainId: this.chainId,
to: this.destination,
data: this.constructSweeplessTxData(),
type: 'eip1559',
}) as TransactionSerializedEIP1559
}

const fee = await getL1Fee({
const fee = await this.client.estimateL1Fee({
data: serializedTransaction,
client: this.publicClient,
oracleContract: this.from.metadata.evm_oracle_contract
})
to: this.destination,
account: this.account,
gasPriceOracleAddress: this.from.metadata.evm_oracle_contract as `0x${string}`,
gasPrice: gasPrice as any
})

return fee;
}

}

//from https://github.com/ethereum-optimism/optimism/blob/develop/packages/fee-estimation/src/estimateFees.ts
import {
gasPriceOracleABI,
gasPriceOracleAddress,
} from '@eth-optimism/contracts-ts'
import {
getContract,
BlockTag,
} from 'viem'

/**
* Options to query a specific block
*/
type BlockOptions = {
/**
* Block number to query from
*/
blockNumber?: bigint
/**
* Block tag to query from
*/
blockTag?: BlockTag
}

/**
* Get gas price Oracle contract
*/
const getGasPriceOracleContract = (client: PublicClient, oracleContract?: `0x${string}` | null) => {
return getContract({
address: oracleContract || gasPriceOracleAddress['420'],
abi: gasPriceOracleABI,
client
})
}

/**
* Computes the L1 portion of the fee based on the size of the rlp encoded input
* transaction, the current L1 base fee, and the various dynamic parameters.
* @example
* const L1FeeValue = await getL1Fee(data, params);
*/
const getL1Fee = async (options: { data: `0x02${string}`, client: PublicClient, oracleContract: `0x${string}` | null | undefined } & BlockOptions) => {
const contract = getGasPriceOracleContract(options.client, options.oracleContract)
return contract.read.getL1Fee([options.data], {
blockNumber: options.blockNumber,
blockTag: options.blockTag,
})
}
}
10 changes: 8 additions & 2 deletions lib/wallets/paradex/useParadex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { walletClientToSigner } from "../../ethersToViem/ethers"
import AuhorizeEthereum from "./Authorize/Ethereum"
import { getWalletClient } from '@wagmi/core'
import { useConfig } from "wagmi"
import { mainnet } from "viem/chains"
import { usePersistedState } from "../../../hooks/usePersistedState"
import { LOCAL_STORAGE_KEY } from "./lib/constants"
import { useSettingsState } from "../../../context/settings"

type Props = {
network: Network | undefined,
Expand All @@ -25,6 +25,7 @@ type Props = {
export default function useParadex({ network }: Props): WalletProvider {
const name = 'paradex'
const id = 'prdx'
const { networks } = useSettingsState()
const selectedProvider = useWalletStore((state) => state.selectedProveder)
const selectProvider = useWalletStore((state) => state.selectProvider)
const [paradexAddresses, updateParadexAddresses] = usePersistedState<{ [key: string]: string }>({}, LOCAL_STORAGE_KEY);
Expand Down Expand Up @@ -59,8 +60,13 @@ export default function useParadex({ network }: Props): WalletProvider {
if (!connectionResult) return
selectProvider(evmProvider.name)
if (!paradexAddresses[connectionResult.address.toLowerCase()]) {
const l1Network = networks.find(n => n.name === KnownInternalNames.Networks.EthereumMainnet || n.name === KnownInternalNames.Networks.EthereumSepolia);
const l1ChainId = Number(l1Network?.chain_id)
if (!Number(l1ChainId)) {
throw Error("Could not find ethereum network")
}
const client = await getWalletClient(config, {
chainId: mainnet.id,
chainId: l1ChainId,
})
const ethersSigner = walletClientToSigner(client)
if (!ethersSigner) {
Expand Down

0 comments on commit bc35e93

Please sign in to comment.