# Web3 API

## Signing a Transaction

Here’s an [example of signing a transaction](https://github.com/Fantom-foundation/web3-methods/blob/main/common.js) to update the blockchain state by creating a transaction. [This example](https://github.com/Fantom-foundation/web3-methods/blob/4ff1819a51467e18db8e6646de524721c7f03882/scripts/web3.eth.contract.js#L26) demonstrates updating a state variable in a deployed contract:

1. We call the function `setGreeting` and store the transaction in a variable `tx`
2. We then pass the `tx` along with contract details&#x20;
3. The function `signTransaction` creates a block of data containing all the necessary information like gas price, contract information, network information, etc., and calls the web3 method to sign the transaction.

## Gas Price

We have added [two methods](https://github.com/Fantom-foundation/web3-methods/blob/main/scripts/gasPrice.js) to fetch the gas price:&#x20;

1. `web3.eth.getGasPrice()` gets the latest gas price
2. `web3.eth.getFeeHistory()` gets the gas price using custom parameters, such as:
   1. No. of blocks (the number of previous blocks it searches)
   2. String/BN value to request the newest block in the requested range
   3. Percentile options (1-99th percentile)
   4. CallBack&#x20;

`getFeeHistory()` will calculate an average of the blocks (mentioned in the first parameter) and figure out the best gas prices based on the percentile for slow/average/fast options. This API also returns the `baseFeePerGas` (introduced after EIP-1559) which is added to the gas prices outputted by the above call.&#x20;

Rest of the Web3 API calls and examples on Opera:

* <https://github.com/Fantom-foundation/web3-methods/wiki>

## Transfer

We have added two ways to sign a transaction with a [simple example](https://github.com/Fantom-foundation/web3-methods/blob/main/scripts/transfer.js) of transferring FTM between accounts.&#x20;

### **transfer**

The [first method](https://github.com/Fantom-foundation/web3-methods/blob/4aff3f020b699ce177d5749f6594270599ae8494/scripts/transfer.js#L8) takes a simple approach and uses the predefined web3 method to determine the gas price required to send FTM between two accounts.&#x20;

### **transferWithParams**

[This method](https://github.com/Fantom-foundation/web3-methods/blob/4aff3f020b699ce177d5749f6594270599ae8494/scripts/transfer.js#L48) transfers with additional parameters in its transaction block.&#x20;

```
// Change maxFeePerGas and maxPriorityFeePerGas as per the current gas prices
var transaction = {
    to: TO_ADDRESS,
    from: address,
    nonce: nonceVal,
    value: testWeb3.utils.toHex(testWeb3.utils.toWei('0.1', 'ether')),
    gas: testWeb3.utils.toHex(21000),
    maxFeePerGas: '0x174876E800', // 100 Gwei 
    maxPriorityFeePerGas: '0xBA43B7400', // 50 Gwei
    chainId
};
```

The code snippet mentioned above employs the variables **`maxFeePerGas`** and **`maxPriorityFeePerGas`**. \
\
Following the London fork, each block now includes a **`baseFeePerGas`**. This base fee represents the minimum cost required for sending a transaction on the network. Unlike miners, the network itself determines the base fee. The base fee varies from block to block depending on the previous block's capacity.

When submitting a transaction, you must also provide a "tip" in the `maxPriorityFeePerGas` field. To ensure that the miner has an incentive to process your transaction, the minimum tip amount you should offer is 1 wei. The likelihood of your transaction being included in a block increases as you offer a higher tip.\
\
To initiate a transaction on the network, users have the option to specify the maximum amount they are willing to pay for their transaction to be executed. This parameter is called `maxFeePerGas`. However, for a transaction to be successfully executed, the max fee specified must be greater than or equal to the sum of the base fee and the maxPriorityFeePerGas.
