Skip to content

Commit

Permalink
Merge branch 'main' into infographics
Browse files Browse the repository at this point in the history
  • Loading branch information
mapachurro committed Oct 4, 2023
2 parents f3953ec + cc723d3 commit 79aff69
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 103 deletions.
131 changes: 70 additions & 61 deletions docs/build-on-linea/gas-fees.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,75 @@ title: Gas fees on Linea
sidebar_position: 6
---

If you're familiar with gas fees on Ethereum, then you know that they heavily fluctuate depending on how busy the network is (for a refresher on gas click [here](https://support.metamask.io/hc/en-us/articles/4404600179227-User-Guide-Gas#:~:text=A%20normal%20transaction%20sending%20ETH,transactions%20also%20cost%2021%2C000%20gas.)). Linea's gas fees depend on Ethereum's fee, and we will explain how the L2 fees are calculated, but the **TLDR is that Linea's gas fees should be around 1/15th of Ethereum's, and we hope to reduce them even further in the future.**
If you're familiar with gas fees on Ethereum, then you know that they heavily fluctuate depending on how busy the network is (for a refresher on gas click [here](https://support.metamask.io/hc/en-us/articles/4404600179227-User-Guide-Gas#:~:text=A%20normal%20transaction%20sending%20ETH,transactions%20also%20cost%2021%2C000%20gas.)). **Linea's gas fees on average should be 15x cheaper than Ethereum's, and we hope to reduce them even further in the future.**


## How do I make sure my transactions go through?

Linea is compatible with EIP-1559; however, there are minor differences.

1. The base fee on Linea is fixed at 7 wei to ensure that blocks aren't over 50% full, so that we don't burn any ETH on Linea.

2. We don't mine a transaction if `gasPrice` or `maxPriorityFeePerGas` is lower than a given value that fluctuates over time.

To ensure that your transaction gets included by the sequencer, we recommend using EIP-1559 with the following settings:

- maxBaseFee = 1.35 * previousBlockMaxBaseFee

- `maxPriorityFeePerGas` = reward value from eth_feeHistory( 5 blocks, latest, 20th percentile)

You can use the `eth_feeHistory` RPC method with the params below to get the recommended values:

```bash
curl https://linea-mainnet.infura.io/v3/your-api-key \
-X POST \
-H "Content-Type: application/json" \
-d '{"id": 1, "jsonrpc": "2.0", "method": "eth_feeHistory", "params": [4, "latest", [20]] }'
```

## Example Code

```typescript

public async get1559Fees(percentile = this.gasEstimationPercentile): Promise<Fees> {
const currentBlockNumber = await this.provider.getBlockNumber();
if (this.cacheIsValidForBlockNumber.lt(currentBlockNumber)) {
const { reward, baseFeePerGas }: FeeHistory = await this.provider.send("eth_feeHistory", [
"0x4",
"latest",
[percentile],
]);

const maxPriorityFeePerGas = reward
.reduce((acc: BigNumber, currentValue: string[]) => acc.add(currentValue[0]), BigNumber.from(0))
.div(reward.length);

if (maxPriorityFeePerGas && maxPriorityFeePerGas.gt(this.maxFeePerGasFromConfig)) {
throw new FeeEstimationError(
`Estimated miner tip of ${maxPriorityFeePerGas} exceeds configured max fee per gas of ${this.maxFeePerGasFromConfig}!`,
);
}

this.cacheIsValidForBlockNumber = BigNumber.from(currentBlockNumber);

const maxFeePerGas = BigNumber.from(baseFeePerGas[baseFeePerGas.length - 1])
.mul(2)
.add(maxPriorityFeePerGas);

if (maxFeePerGas.gt(0) && maxPriorityFeePerGas.gt(0)) {
this.feesCache = {
maxPriorityFeePerGas,
maxFeePerGas: maxFeePerGas.gt(this.maxFeePerGasFromConfig) ? this.maxFeePerGasFromConfig : maxFeePerGas,
};
} else {
this.feesCache = {
maxFeePerGas: this.maxFeePerGasFromConfig,
};
}
}
return this.feesCache;
}
```

## How do I check the gas price on Linea?

Expand All @@ -29,63 +97,4 @@ curl https://mainnet.infura.io/v3/your-infura-api-key \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_gasPrice","params": [],"id":1}'
```

## The Formula

**Total L2 Fee Equation**

$$
\text{total\_L2\_fee} = \text{L2\_base\_fee} + \text{L2\_miner\_tip}
$$

The L2 fee equation is comprised of two parts, the base fee and the miner tip. The base fee is 7 wei, which is the minimum base fee a standard protocol allows. The miner tip equation is a bit complex and will be explained in more detail below.


**L2 Miner Tip Equation**

$$
l2\_miner\_tip = \left( \sum_{i} (baseFee[i] \times 0.066 + reward[i] \times 0.066) \times ratio[i] \right) \div \left( \sum_{i} ratio[i] \right)
$$

Looking at this equation might seem overwhelming, but we are just calculating the weighted average to get the L2 miner tip.

In this equation, the following variables are constants that could change in the future as we fine-tune our gas fee calculation: ```base_fee_coefficient = 0.066```, ```priority_fee_coefficient = 0.066```, ```batchSubmissionPercentile = 15```, and ```numBlocks = 200```.

We use the [eth_feeHistory JSON-RPC method](https://docs.infura.io/networks/ethereum/json-rpc-methods/eth_feehistory) with the constant parameters given above to figure out what the 'baseFeePerGas', 'gasUsedRatio', and 'reward'is on Ethereum and then find the weighted average by plugging them into the equation.

The request would look like the code below.

```bash
curl https://mainnet.infura.io/v3/YOUR-API-KEY \
-X POST \
-H "Content-Type: application/json" \
-d '{"id": 1, "jsonrpc": "2.0", "method": "eth_feeHistory", "params": ["200", "latest", [15]] }'

```

## Example Calculation

For practical purposes let's assume we ran the `eth_feeHistory` and used `numBlocks=3`, and got the following response:

```
{"jsonrpc":"2.0","id":1,"result":{"baseFeePerGas":["1001","1002","1003","1004"],"gasUsedRatio":[0.4,0.5,0.6],"oldestBlock":"0x113159b","reward":[["901"],["902"],["903"]]}}
```

This maps to the following inputs for the equation:

```
baseFee = ["1001","1002","1003"]
reward= ["901", "902", "903"]
ratio = [0.4, 0.5, 0.6]
```

Using the formula, we would iterate through all the values in the list, where i ranges from 1 to 3, to obtain:

$$
l2\_miner\_tip = \frac{{(1001 \times 0.66 + 901 \times 0.66) \times 0.4 + (1002 \times 0.66 + 902 \times 0.66) \times 0.5 + (1003 \times 0.66 + 903 \times 0.66) \times 0.6}}{{0.4 + 0.5 + 0.6}}
$$

After meticulously going through all this math, you should arrive at your gas price (if you do this for 200 blocks instead of 3). This should be close to the gas price the `eth_gasPrice` method returns from above.
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Here's a video walkthrough:

## Prerequisites

Before you begin, Ensure you've:
Before you begin, ensure you've:

1. [Set up your wallet](../../../use-mainnet/set-up-your-wallet.mdx)
1. [Funded your wallet with Linea ETH](../../../use-mainnet/fund.md#get-test-eth-on-linea)
Expand Down Expand Up @@ -53,7 +53,7 @@ These instructions use API keys and private keys inline. We highly recommend hid
<Tabs className="my-tabs">
<TabItem value="Infura" label="Infura" default>

To use Infura, you'll need to [get an API key](https://support.infura.io/hc/en-us/articles/15116941373979-Connecting-to-the-Linea-network)
To use Infura, you'll need to [get an API key](https://support.infura.io/hc/en-us/articles/15116941373979-Connecting-to-the-Linea-network).

On testnet:

Expand Down Expand Up @@ -113,7 +113,7 @@ Then, run:
source .env
```

Finally, we can modify the `foundry.toml` file to conveniently store the various rpc endpoints we might be working with. Add this section:
Finally, we can modify the `foundry.toml` file to conveniently store the various RPC endpoints we might be working with. Add this section:

```bash
[rpc_endpoints]
Expand Down
12 changes: 6 additions & 6 deletions docs/build-on-linea/quickstart/deploy-smart-contract/hardhat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ In the menu that appears, select `Create an empty hardhat.config.js` and press *

Hardhat recommends using their plugin, `@nomicfoundation/hardhat-toolbox`, which can be downloaded by entering `Y` during the project creation process.

You should now have a sample project from Hardhat the deploys and test a `Lock` contract that locks funds for a set amount of time.
You should now have a sample project from Hardhat, the deploys, and test a `Lock` contract that locks funds for a set amount of time.

:::note

Expand All @@ -56,29 +56,29 @@ The default script in `scripts/deploy.js` locks 0.001 ETH in the contract upon d

The sample project already includes the deployment script. To deploy on Linea, you'll just need to make a few modifications.

You can use [Truffle Dashboard](#truffle-dashboard) to deploy directly with your MetaMask wallet or deploy with the Hardhat CLI by [adding Linea to your `hardhat.config.js`](#hardhatconfigjs).
You can use [Truffle Dashboard](#truffle-dashboard) to deploy directly with your MetaMask wallet or deploy with the Hardhat CLI by [adding Linea to your `hardhat.config.js`](#use-hardhatconfigjs).

### Use Truffle Dashboard

[Truffle Dashboard](https://trufflesuite.com/docs/truffle/how-to/use-the-truffle-dashboard/) allows you to forgo saving your private keys locally, instead connecting to your MetaMask wallet for deployments. To deploy with Truffle Dashboard using the [Hardhat plugin](https://github.com/trufflesuite/truffle/tree/develop/packages/dashboard-hardhat-plugin#truffledashboard-hardhat-plugin):

1. [Install Truffle using the recommended installation procedure](https://trufflesuite.com/docs/truffle/how-to/install/)
1. Run
1. Run:

```
npm install --save-dev @truffle/dashboard-hardhat-plugin
```
1. Import the plugin into `hardhat.config.js`
1. Import the plugin into `hardhat.config.js`, with:

```javascript
import "@truffle/dashboard-hardhat-plugin";
```
or
or:
```javascript
require("@truffle/dashboard-hardhat-plugin");
```
1. Run `truffle dashboard` in your terminal, which will open a window on port `24012`.
1. Navigate to `localhost:24012` in your browser. Please ensure that Dashboard is connected to the Linea testnet or mainnet by connecting your MetaMask wallet to Linea. For reference, the Linea testnet network ID is `59140`.
1. Navigate to `localhost:24012` in your browser. Please ensure that your Truffle Dashboard is connected to the Linea testnet or mainnet by connecting your MetaMask wallet to Linea. For reference, the Linea testnet network ID is `59140`.
<img
src={
require("@site/static/img/docs/build-on-linea/quickstart/deploy-smart-contract/dashboard_network.png")
Expand Down
6 changes: 3 additions & 3 deletions docs/build-on-linea/quickstart/deploy-smart-contract/remix.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The default project includes a code sample with a configured smart contract.

## Compile a Remix contract

To compile this sample contract, navigate to the "Solidity compiler" icon, and click on "Compile contract"!
To compile this sample contract, navigate to the "Solidity compiler" icon, and click on "Compile contract".

<img
src={
Expand All @@ -47,7 +47,7 @@ If you deploy using the injected provider, Remix can auto-detect the network you

:::caution

The public endpoints are rate limited and not meant for production systems. To use Infura, you'll need to [get an API key](https://support.infura.io/hc/en-us/articles/15116941373979-Connecting-to-the-Linea-network). Then, you can [manually add a network to your MetaMask wallet](https://support.metamask.io/hc/en-us/articles/360043227612-How-to-add-a-custom-network-RPC#h_01G63GGJ83DGDRCS2ZWXM37CV5) with the mainnet information found [here](../../../use-mainnet/info-contracts.md#network-information)
The public endpoints are rate limited and not meant for production systems. To use Infura, you'll need to [get an API key](https://support.infura.io/hc/en-us/articles/15116941373979-Connecting-to-the-Linea-network). You can then [manually add a network to your MetaMask wallet](https://support.metamask.io/hc/en-us/articles/360043227612-How-to-add-a-custom-network-RPC#h_01G63GGJ83DGDRCS2ZWXM37CV5) with the mainnet information found [here](../../../use-mainnet/info-contracts.md#network-information).

:::

Expand All @@ -64,7 +64,7 @@ Switch to the Linea Mainnet network in your MetaMask wallet, and select "Injecte

:::caution

The public endpoints are rate limited and not meant for production systems. To use Infura, you'll need to [get an API key](https://support.infura.io/hc/en-us/articles/15116941373979-Connecting-to-the-Linea-network). Then, you can [manually add a network to your MetaMask wallet](https://support.metamask.io/hc/en-us/articles/360043227612-How-to-add-a-custom-network-RPC#h_01G63GGJ83DGDRCS2ZWXM37CV5) with the testnet information found [here](../../../use-mainnet/info-contracts.md#network-information)
The public endpoints are rate limited and not meant for production systems. To use Infura, you'll need to [get an API key](https://support.infura.io/hc/en-us/articles/15116941373979-Connecting-to-the-Linea-network). You can then [manually add a network to your MetaMask wallet](https://support.metamask.io/hc/en-us/articles/360043227612-How-to-add-a-custom-network-RPC#h_01G63GGJ83DGDRCS2ZWXM37CV5) with the testnet information found [here](../../../use-mainnet/info-contracts.md#network-information).

:::

Expand Down
24 changes: 12 additions & 12 deletions docs/build-on-linea/quickstart/deploy-smart-contract/thirdweb.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: thirdweb
---

thirdweb is a complete web3 development framework that provides everything you need to connect your apps and games to decentralized networks
thirdweb is a complete web3 development framework that provides everything you need to connect your apps and games to decentralized networks.

## Create a smart contract

Expand All @@ -23,7 +23,7 @@ To create a new smart contract using thirdweb [CLI](https://portal.thirdweb.com/
3. Once created, navigate to your project’s directory and open in your preferred code editor.
4. If you open the `contracts` folder, you will find your smart contract; this is your smart contract written in Solidity.

The following is code for an ERC721Base contract without specified extensions. It implements all of the logic inside the [ERC721Base.sol](https://github.com/thirdweb-dev/contracts/blob/main/contracts/base/ERC721Base.sol) contract; which implements the [ERC721A](https://github.com/thirdweb-dev/contracts/blob/main/contracts/eip/ERC721A.sol) standard.
The following is code for an `ERC721Base` contract without specified extensions. It implements all of the logic inside the [ERC721Base.sol](https://github.com/thirdweb-dev/contracts/blob/main/contracts/base/ERC721Base.sol) contract; which implements the [ERC721A](https://github.com/thirdweb-dev/contracts/blob/main/contracts/eip/ERC721A.sol) standard.

```bash
// SPDX-License-Identifier: MIT
Expand All @@ -41,13 +41,13 @@ To create a new smart contract using thirdweb [CLI](https://portal.thirdweb.com/
}
```

This contract inherits the functionality of ERC721Base through the following steps:
This contract inherits the functionality of `ERC721Base` through the following steps:

- Importing the ERC721Base contract
- Inheriting the contract by declaring that our contract is an ERC721Base contract
- Importing the `ERC721Base` contract
- Inheriting the contract by declaring that our contract is an `ERC721Base` contract
- Implementing any required methods, such as the constructor.

5. After modifying your contract with your desired custom logic, you may deploy it to [LineaTestnet](https://thirdweb.com/linea-testnet) using [Deploy](https://portal.thirdweb.com/deploy).
5. After modifying your contract with your desired custom logic, you may deploy it to the [Linea testnet](https://thirdweb.com/linea-testnet) using [`deploy`](https://portal.thirdweb.com/deploy).

---

Expand All @@ -64,9 +64,9 @@ Alternatively, you can deploy a prebuilt contract for NFTs, tokens, or marketpla
## Deploy a smart contract

Deploy allows you to deploy a smart contract to any EVM compatible network without configuring RPC URLs, exposing your private keys, writing scripts, and other additional setup such as verifying your contract.
Deploy allows you to deploy a smart contract to any EVM-compatible network without configuring RPC URLs, exposing your private keys, writing scripts, and other additional setup such as verifying your contract.

1. To deploy your smart contract using deploy, navigate to the root directory of your project and execute the following command:
1. To deploy your smart contract using `deploy`, navigate to the root directory of your project and execute the following command:

```bash
npx thirdweb deploy
Expand All @@ -78,15 +78,15 @@ Deploy allows you to deploy a smart contract to any EVM compatible network witho
- Providing the option to select which contract(s) you wish to deploy.
- Uploading your contract source code (ABI) to IPFS.

2. When it is completed, it will open a dashboard interface to finish filling out the parameters.
2. When this process is completed, a dashboard interface will open, where you will need to finish filling out the parameters:
- `_name`: contract name
- `_symbol`: symbol or "ticker"
- `_royaltyRecipient`: wallet address to receive royalties from secondary sales
- `_royaltyBps`: basis points (bps) that will be given to the royalty recipient for each secondary sale, e.g. 500 = 5%
3. Select [LineaTestnet](https://thirdweb.com/linea-testnet) as the network
4. Manage additional settings on your contract’s dashboard as needed such as uploading NFTs, configuring permissions, and more.
3. Select [Linea testnet](https://thirdweb.com/linea-testnet) as the network
4. Manage additional settings on your contract’s dashboard as needed, such as uploading NFTs, configuring permissions, and more.

> For additional information on Deploy, please reference [thirdweb’s documentation](https://portal.thirdweb.com/deploy).
> For additional information on `deploy`, please reference [thirdweb’s documentation](https://portal.thirdweb.com/deploy).
If you have any further questions or encounter any issues during the process, please [reach out to thirdweb support](https://support.thirdweb.com).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ You can check if it compiles by running `truffle compile` from the root folder.

## Write the migration script

To tell Truffle how, and in what order we want to deploy our smart contracts, we need to write a migration script.
To tell Truffle how we want to deploy our smart contracts, and in what order, we need to write a migration script.

Create `1_deploy_token.js` in the `migrations` directory, and add the following code:

Expand Down Expand Up @@ -117,7 +117,7 @@ Truffle allows you to deploy through the [Truffle Dashboard](#truffle-dashboard)

### truffle-config.js

You can deploy with Truffle using the command line, by specifying the Linea in `truffle-config.js`. To do so, you need to:
You can deploy with Truffle using the command line, by specifying Linea testnet in `truffle-config.js`. To do so, you need to:

1. Create a `.env` file in the root folder with your wallet's mnemonic. This is your [secret recovery phrase](https://support.metamask.io/hc/en-us/articles/360015290032-How-to-reveal-your-Secret-Recovery-Phrase).

Expand Down
Loading

0 comments on commit 79aff69

Please sign in to comment.