Skip to content

Commit

Permalink
docs(fee-estimation): Update docs for fee-estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Cory authored and Will Cory committed Jul 24, 2023
1 parent d653ea6 commit 489eadd
Showing 1 changed file with 32 additions and 98 deletions.
130 changes: 32 additions & 98 deletions packages/fee-estimation/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @eth-optimism/fee-estimation

Tools for estimating gas on OP chains

- **Tip** the [specs file](./src/estimateFees.spec.ts) has usage examples of every method in this library.

## Overview

This package is designed to provide an easy way to estimate gas on OP chains.
Expand Down Expand Up @@ -30,26 +34,29 @@ npm install @eth-optimism/fee-estimation
yarn add @eth-optimism/fee-estimation
```

## Usage

### Import the package
### Basic Usage

```ts
import { estimateFees } from '@eth-optimism/fee-estimation'
```
import {
estimateFees,
} from '@eth-optimism/fee-estimation'
import {optimistABI} from '@eth-optimism/contracts-ts'

### Basic Usage
const optimistOwnerAddress =
'0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const
const tokenId = BigInt(optimistOwnerAddress)

```ts
const fees = await estimateFees({
client: {
chainId: 10,
rpcUrl: 'https://mainnet.optimism.io',
},
blockNumber: BigInt(106889079),
account: '0xe371815c5f8a4f9acd1576879de288acd81269f1',
to: '0xe35f24470730f5a488da9721548c1ab0b65b53d5',
data: '0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1',
functionName: 'burn',
abi: optimistABI,
args: [tokenid],
account: optimistOwnerAddress,
to: '0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5',
chainId: 10,
})
```

Expand All @@ -58,51 +65,38 @@ const fees = await estimateFees({
### `estimateFees` function

```ts
estimateFees(params: EstimateFeeParams): Promise<bigint>
estimateFees(options: OracleTransactionParameters<TAbi, TFunctionName> & GasPriceOracleOptions & Omit<EstimateGasParameters, 'data'>): Promise<bigint>
```

#### Parameters

`params`: An object with the following fields:

- `client`: An object with `rpcUrl` and `chainId` fields, or an instance of a Viem `PublicClient`.
`options`: An object with the following fields:

- `blockNumber`: A BigInt representing the block number at which you want to estimate the fees.
- `abi`: A variable of type `Abi` or an array representing the ABI for the transaction call data.

- `blockTag`: A string representing the block tag to query from.
- `accessList`(optional): An array representing the access list for the transaction.

- `account`: A string representing the account making the transaction.

- `to`: A string representing the recipient of the transaction.

- `data`: A string representing the data being sent with the transaction. This should be a 0x-prefixed hex string.

#### Returns
- `args`: An array representing the arguments for the transaction call data.

A Promise that resolves to a BigInt representing the estimated fee in wei.
- `blockNumber`(optional): A BigInt representing the block number at which you want to estimate the fees.

## FAQ
- `blockTag`(optional): A string representing the block tag to query from.

### How to encode function data?
- `client`: An object with `rpcUrl` and `chainId` fields, or an instance of a Viem `PublicClient`.

You can use our package to encode the function data. Here is an example:
- `data`: A string representing the data being sent with the transaction. This should be a 0x-prefixed hex string.

```ts
import { encodeFunctionData } from '@eth-optimism/fee-estimation'
import { optimistABI } from '@eth-optimism/contracts-ts'
- `functionName`: A string representing the function name for the transaction call data.

const data = encodeFunctionData({
functionName: 'burn',
abi: optimistABI,
args: [BigInt('0x77194aa25a06f932c10c0f25090f3046af2c85a6')],
})
```
- `to`: A string representing the recipient of the transaction.

This will return a 0x-prefixed hex string that represents the encoded function data.
- `value`(optional): A BigInt representing the value in wei sent along with the transaction.

## Testing
#### Returns

The package provides a set of tests that you can run to verify its operation. The tests are a great resource for examples. You can find them at `./src/estimateFees.spec.ts`.
A Promise that resolves to a BigInt representing the estimated fee in wei.

## Other methods

Expand Down Expand Up @@ -361,63 +355,3 @@ const libraryVersion = await version(paramsWithClient)
```

---

### encodeFunctionData()

Encodes function data based on a given function name and arguments.

```ts
encodeFunctionData({ functionName, abi, args }: EncodeFunctionDataParams): string;
```

#### Parameters

- `{ functionName, abi, args }: EncodeFunctionDataParams` - An object containing the function name, ABI (Application Binary Interface), and arguments.

#### Returns

- `string` - Returns the encoded function data as a string.

#### Example

```ts
const encodedData = encodeFunctionData({
functionName: 'burn',
abi: optimistABI,
args: [BigInt(optimistOwnerAddress)],
})
```

---

### estimateFees()

Estimates the fee for a transaction given the input data and the address of the sender and recipient.

```ts
estimateFees({ client, data, account, to, blockNumber }: EstimateFeesParams): Promise<bigint>;
```

#### Parameters

- `{ client, data, account, to, blockNumber }: EstimateFeesParams` - An object containing the client, transaction data, sender's address, recipient's address, and block number.

#### Returns

- `Promise<bigint>` - Returns a promise that resolves to the estimated fee for the given transaction.

#### Example

```ts
const estimateFeesParams = {
data: '0xd1e16f0a603acf1f8150e020434b096e408bafa429a7134fbdad2ae82a9b2b882bfcf5fe174162cf4b3d5f2ab46ff6433792fc99885d55ce0972d982583cc1e11b64b1d8d50121c0497642000000000000000000000000000000000000060a2c8052ed420000000000000000000000000000000000004234002c8052edba12222222228d8ba445958a75a0704d566bf2c84200000000000000000000000000000000000006420000000000000000000000000000000000004239965c9dab5448482cf7e002f583c812ceb53046000100000000000000000003',
account: '0xe371815c5f8a4f9acd1576879de288acd81269f1',
to: '0xe35f24470730f5a488da9721548c1ab0b65b53d5',
}
const estimatedFees = await estimateFees({
...paramsWithClient,
...estimateFeesParams,
})
```

I hope this information is helpful!

0 comments on commit 489eadd

Please sign in to comment.