bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 52쪽 중 11쪽
s this page helpful?](https://airtable.com/apppq4inOe4gmSSlk/pagoZHC2i1iqgphgl/form?prefill_Page+URL=https://docs.filecoin.io/build/architecture/filecoin-evm-runtime/address-types)
# FILForwarder
The FilForwarder is a smart contract that lets users transfer FIL from an Ethereum-based f4 address to a Filecoin address of a different type.
## The problem
Filecoin has multiple [address spaces](/core-concepts/filecoin-virtual-machine/addresses): `f0`, `f1`, `f2`, `f3`, and `f4`. Each address space fits a particular need for the Filecoin network. The `f410` address spaces allow Ethereum addresses to be integrated into the Filecoin network.
Users interacting with the Filecoin EVM runtime need to use `f4` addresses, masked to the Ethereum-style `0x` address. These addresses can be created from wallets like MetaMask, Coinbase wallet, or any other EVM-based wallet that allows for custom networks. There are use cases where a user with FIL in an `0x`-style address would want to send FIL to an `f1`, `f2`, or `f3` address. For example, taking FIL out of a smart contract and sending it to a multi-sig account or an exchange.
This is where the problem lies. Ethereum-based wallets do not recognize the `f1`, `f2`, or `f3` address formats, making it impossible to send FIL from an Ethereum-style address.
## The solution
The FilForwarder exposes a smart contract method called `forward` that takes a byte-level definition of a protocol address in an *f-style* and a message value. It then uses the internal Filecoin APIs exposed using the Filecoin EVM runtime to properly send FIL funds reliably and as cheaply as possible. This also has the side effect of creating the actor ID should the address receiving address be considered new. In this way, using FilForwarder from an Ethereum wallet to any other Filecoin address space is safe and reliable.
## Use FILForwarder
You can use the FilForwarder contract in two ways:
* Using the Glif.io browser wallet
* Manually invoking the contract
### Glif.io
Before we start, make sure you know the address you’d like to forward your FIL to. You’ll need to ensure that the `f410` Ethereum-style address has enough FIL to cover the transaction costs.
1. Go to [Glif.io](https://glif.io/en).
2. Select the network you want to use from the dropdown and click **Connect Wallet**.

In this example, we’re using the (now deprecated) Hyperspace testnet.
3. Confirm that you want to connect your wallet to Glif.io. You will only be prompted to do this once.

4. Click **Close** on the connection confirmation screen.

5. Select your wallet address from the dropdown and click **Forward FIL**.

6. Enter the destination address for your FIL, along with the amount of FIL you want to send:

7. Double-check that your destination address is correct and click **Send**.
8. You can check the transaction by clicking the transaction ID.

9. Your funds should be available at the destination after around two minutes. You can check that your funds have arrived by searching for the destination address in a block explorer.

10. If you can’t see your funds, make sure you’re viewing the correct network.

It generally takes around two minutes for a transaction to complete and for the funds to be available at the destination.
### Manually
The FilForwarder contract can be interacted with using standard Ethereum tooling like Hardhat or Remix. In this guide, we’re going to use Hardhat, but these steps can be easily replicated using the [web-based IDE Remix](/build-on-filecoin/development-frameworks/remix).
#### **Prerequisites**
This guide assumes you have the following installed:
* [Yarn](https://yarnpkg.com/)
* A Filecoin address stored in [MetaMask](/networks-and-tools/assets/metamask-setup)
#### **Environment setup**
First, we need to grab the FilForwarder kit and install the dependencies:
1. Clone the FilForwarder repository and install the dependencies:
```
git clone [https://github.com/FilOzone/FilForwarder](https://github.com/FilOzone/FilForwarder)
cd FilForwarder
```
2. Use Yarn to install the project's dependencies:
```
yarn install
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
...
✨ Done in 16.34s.
```
3. Create an environment variable for your private key.
```shell
export PRIVATE_KEY='<YOUR PRIVATE KEY>'
# For example
# export PRIVATE_KEY='d52cd65a5746ae71cf3d07a8cf392ca29d7acb96deba7d94b19a9cf3c9f63022'l
```
Always be careful when dealing with your private key. Double-check that you’re not hardcoding it anywhere or committing it to source control like GitHub. Anyone with access to your private key has complete control over your funds.
#### **Invoke the contract**
The contract is deterministically deployed on all Filecoin networks at `0x2b3ef6906429b580b7b2080de5ca893bc282c225`. Any contract claiming to be a FilForwarder that does not reside at this address should not be trusted. Any dApp can connect to the wallet and use the ABI in this repository to call this method using any frontend. See the [Glif section](/core-concepts/filecoin-evm-runtime/filforwarder) above for steps on using a GUI.
Inside this repository is a Hardhat task called `forward`. This task will use the private key to send funds using the contract. This task uses the `fil-forwarder-{CHAIN_ID}.json` file to determine the deployed contract address for a given network. These addresses should always be the same, but these files prevent you from having to specify it each time.
The `forward` command uses the following syntax:
```shell
yarn hardhat forward \
--network <NETWORK> \
--destination <DESTINATION_ADDRESS> \
--amount <AMOUNT>
```
* `NETWORK`: The network you want to use. The options are `mainnet` and `calibration`.
* `DESTINATION_ADDRESS`: The address you want to send FIL to. This is a string, like `t01024` or `t3tejq3lb3szsq7spvttqohsfpsju2jof2dbive2qujgz2idqaj2etuolzgbmro3owsmpuebmoghwxgt6ricvq`.
* `AMOUNT`: The amount of FIL you want to send. The value `3.141` would be 3.141 FIL.
#### **Examples**
1. To send 9 FIL to a `t3` address on the Calibration testnet, run:
```sh
yarn hardhat forward \
--network calibration \
--destination t3tejq3lb3szsq7spvttqohsfpsju2jof2dbive2qujgz2idqaj2etuolzgbmro3owsmpuebmoghwxgt6ricvq \
--amount 9.0
```
2. To send 42.5 FIL to a `t1` address on the Calibration testnet, run:
```shell
yarn hardhat forward \
--network calibration \
--destination t010135 \
--amount 42.5
```
[Was this page helpful?](https://airtable.com/apppq4inOe4gmSSlk/pagoZHC2i1iqgphgl/form?prefill_Page+URL=https://docs.filecoin.io/build/architecture/filecoin-evm-runtime/filforwarder)
# Difference with Ethereum
While Filecoin EVM runtime aims to be compatible with the Ethereum ecosystem, it has some marked differences.
## Gas costs
Filecoin charges Filecoin gas only. This includes the Filecoin EVM runtime. Instead of the Filecoin EVM runtime charging gas according to the EVM spec for each EVM opcode executed, the Filecoin virtual machine (FVM) charges Filecoin gas for executing the EVM interpreter itself. The [How gas works](/core-concepts/filecoin-evm-runtime/how-gas-works) page goes into this in more detail. Importantly, this means that Filecoin EVM runtime gas costs and EVM gas costs will be very different:
1. EVM and Filecoin gas are different units of measurement and are not 1:1. Purely based on chain throughput (gas/second), the ratio of Ethereum gas to Filecoin gas is about 1:444. Expect Filecoin gas numbers to look *much* larger than those in Ethereum.
2. Because Filecoin charges Filecoin gas for executing the Filecoin EVM runtime interpreter:
1. Some instructions may be more expensive and/or cheaper in Filecoin EVM runtime than they are in the EVM.
2. EVM instruction costs can depend on the exact Filecoin EVM runtime code-paths taken, and caching.
{% hint style="danger" %}
Filecoin gas costs are not set in stone and should never be hard-coded. Future network upgrades will break any smart contracts that depend on gas costs not changing.
{% endhint %}
## Gas stipend
Solidity calls `address.transfer` and `address.send` to grant a fixed gas stipend of 2300 Ethereum gas to the called contract. The Filecoin EVM runtime automatically detects such calls, and sets the gas limit to 10 million Filecoin gas. This is a relatively more generous limit than Ethereum’s, but it’s future-proof. You should expect the address called to be able to carry out more work than in Ethereum.
## Self destruct
Filecoin EVM runtime emulates EVM self-destruct behavior but isn’t able to entirely duplicate it:
1. There is no gas refund for self-destruct.
2. On self-destruct, the contract is marked as self-destructed, but is not actually deleted from the Filecoin state-tree. Instead, it simply behaves as if it does not exist. It acts like an empty contract.
3. Unlike in the EVM, in Filecoin EVM runtime, self-destruct can *fail* causing the executing contract to revert. Specifically, this can happen if the specified beneficiary address is an embedded [ID address](/core-concepts/filecoin-virtual-machine/addresses) and no actor exists with the specified ID.
4. If funds are sent to a self-destructed contract after it self-destructs but before the end of the transaction, those funds remain with the self-destructed contract. In Ethereum, these funds would vanish after the transaction finishes executing.
## CALLCODE
The `CALLCODE` opcode has not been implemented. Use the newer `DELEGATECALL` opcode.
## BLOCKHASH
Ethereum has one block at every height while Filecoin can have none, one, or many (usually around 4-5). This means that the `BLOCKHASH` instruction behaves a bit differently in the Filecoin EVM:
* Because there can be multiple blocks at any given height, `BLOCKHASH` returns the hash of all the concatenation of the CIDs of all the blocks at the requested height.
* Because there can be no blocks at any given height, if `BLOCKHASH` is called on a height with *no blocks*, it returns the `BLOCKHASH` of the first preceding height with blocks.
## Bare-value sends
In Ethereum, `SELFDESTRUCT` is the only way to send funds to a smart contract without giving the target smart contract a chance to execute code.
In Filecoin, any actor can use `method 0`, also called a bare-value send, to transfer funds to any other actor without invoking the target actor’s code. You can think of this behavior as having the suggested [`PAY` opcode](https://eips.ethereum.org/EIPS/eip-5920) already implemented in Filecoin. However by default, Solidity smart contracts do not accept bare value transfers, unless the author implements the [receive() or fallback() function](https://docs.soliditylang.org/en/v0.8.17/contracts.html#receive-ether-function). For more information see [FIP Discussion #592](https://github.com/filecoin-project/FIPs/discussions/592#discussioncomment-4819619).
Therefore in case the recipient is a smart contract, **it is recommended to always use the `InvokeEVM`** **`method 3844450837` for sends to prevent loss of funds** when sending to an `f410f`/`0x` address recipient.
## Precompiles
The Filecoin EVM runtime, unlike Ethereum, does not usually enforce gas limits when calling precompiles. This means that it isn’t possible to prevent a precompile from consuming all remaining gas. The `call actor` and `call actor id` precompiles are the exception. However, they apply the passed gas limit to the actor call, not the entire precompile operation (i.e., the full precompile execution end-to-end can use more gas than specified, it’s only the final `send` to the target actor that will be limited).
## Multiple Addresses
In Filecoin, contracts generally have multiple addresses. Two of these address types, `f0` and `f410f`, can be converted to 0x-style (Ethereum) addresses which can be used in the `CALL` opcode. See [Converting to a 0x-style address](/core-concepts/filecoin-evm-runtime/address-types#converting-to-a-0x-style-address) for details on how these addresses are derived.
Importantly, this means that any contract can be called by either its “normal” EVM address (corresponding to the contract’s `f410f` address) or its “masked ID address” (corresponding from the contract’s `f0` address).
However, the addresses returned by the CALLER, ORIGIN, and ADDRESS instructions will always be the same for the same contract.
* The ADDRESS will always be derived from the executing contract’s `f410f` address, even if the contract was called via a masked ID address.
* The CALLER/ORIGIN will be derived from the caller/origin’s `f410f` address, if the caller/origin is an Ethereum-style account or an EVM smart contract. Otherwise, the caller/origin’s “masked ID address” (derived from their `f0` address) will be used.
## Deferred execution model
When calling an Ethereum method that allows the user to ask for the `latest` block, Filecoin will return the `chain head` - `1` block. This behavior was implemented for compatibility with the deferred execution mode that Filecoin uses. In this mode, messages submitted at a given `height` are only processed at `height` + `1`. This means that receipts for a block produced at `height` are only available at `height` + `1`.
[Was this page helpful?](https://airtable.com/apppq4inOe4gmSSlk/pagoZHC2i1iqgphgl/form?prefill_Page+URL=https://docs.filecoin.io/core-concepts/filecoin-evm-runtime/difference-with-ethereum)
# How gas works
Instead of assigning a fixed gas cost in each instruction, the Filecoin EVM runtime charges FIL gas based on the WASM code execution of the Filecoin EVM runtime interpreter.
When executing a message that invokes an EVM contract, the Filecoin virtual machine charges for the message chain inclusion (when the message originates off-chain) and then invokes the actor that hosts the contract. The actor is an instance of the EVM actor, which uses the Filecoin EVM runtime interpreter to execute the contract.
The FEVM interpreter must first load its state, including the contract state, which costs additional gas. The interpreter then begins the execution of the contract bytecode. Each opcode interpreted may perform computation, syscalls, state i/o, and send new messages, all of which are charged with FIL gas. Finally, if the contract state is modified, the interpreter must flush it to the blockstore, which costs additional gas.
Generally, it is not possible to compute gas costs for a contract invocation without using gas estimation through speculative execution.
## Calculation example
The total gas fee of a message is calculated as the following:
```plaintext
(Gas usage × Base fee)
+ (GasLimit × GasPremium)
+ (OverEstimationBurn × BaseFee)
```
Take a look at the [Gas usage section of the How Filecoin works page](/core-concepts/filecoin-evm-runtime/how-gas-works) for more information on the various gas-related parameters attached to each message.
Let’s take a transaction as an example. Our gas parameters are:
* `GasUsage` = `1000` attoFIL
* `BaseFee` = `20` attoFIL
* `Gas limit` = `2000` attoFIL
* `Gas premium` = `5` attoFIL
The total fee is `(GasUsage × BaseFee) + (Gaslimit x GasPremium)`:
```plaintext
1000
x 20
= 20000
2000
x 5
= 10000
20000
+ 10000
= 30000 attoFIL
```
Additionally, the message sender can also set the `GasFeeCap` parameter they are willing to pay. If the sender sets the `GasLimit` too high, the network will compute the amount of gas to be refunded and the amount of gas to be burned as `OverEstimationBurn`.
## Estimate gas
Filecoin nodes, such as Lotus, have several JSON-API API endpoints designed to help developers estimate gas usage. The available JSON-RPC APIs are:
* `GasEstimateMessageGas`: estimate gas values for a message without any gas fields set, including GasLimit, GasPremium, and GasFeeCap. Returns a message object with those gas fields set.
* `GasEstimateGasLimit` takes the input message and estimates the `GasLimit` based on the execution cost as well as a transaction multiplier.
* `GasEstimateGasPremium`: estimates what `GasPremium` price you should set to ensure a message will be included in `N` epochs. The smaller `N` is the larger `GasPremium` is likely to be.
* `GasEstimateFeeCap`: estimate the `GasFeeCap` according to `BaseFee` in the parent blocks.
If you want to learn more about how to use those JSON-RPC APIs for the Filecoin gas model, please check the [JSON RPC API docs for Gas](/reference/json-rpc).
Gas estimation varies from network to network. For example, the `BaseFee` on mainnet is different from the `BaseFee` on the Calibration testnet.
If you’d rather not calculate and estimate gas for every message, you can just leave the optional fields unset. The gas fields will be estimated and set when the message is pushed to the mempool.
## Ethereum compatibility
Since Filecoin is fully EVM-compatible, Filecoin nodes also provide Ethereum-compatible APIs to support gas estimation:
* [EthEstimateGas](/reference/json-rpc/eth#ethestimategas): generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
* [EthMaxPriorityFeePerGas](/reference/json-rpc/eth#ethmaxpriorityfeepergas): returns a fee per gas that is an estimate of how much you can pay as a priority fee, or “tip”, to get a transaction included in the current block.
To request the current max priority fee in the network, you can send a request to a public Filecoin endpoint:
```shell
curl --location --request POST 'https://api.calibration.node.glif.io/rpc/v1' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"eth_maxPriorityFeePerGas",
"params": null,
"id":1
}' | jq
```
This will out프로젝트로 돌아가기