LogoLogo
WebsiteTwitterGitHub
  • Introduction
    • Bridge
  • Wallets
    • fWallet
    • Rabby
    • MetaMask
    • Coinbase Wallet
    • Ledger
  • Staking
    • Overview
    • Stake FTM
    • Liquid Staking
    • Governance
  • Build on Opera
    • Overview
    • Tutorials
      • Deploy Contract
      • Verify Contract
      • Unit Test Contract
      • Create Fixed-Cap Asset
      • Create Variable-Cap Asset
      • Proxy Pattern
      • Diamond Proxy Pattern
    • Oracles
      • Chainlink
      • Band Protocol
      • API3
    • API
      • Public Endpoints
      • Transaction Tracing
      • Web3 API
      • GraphQL
        • Getting Started
        • Installation
        • Schema Basics
        • Schema Structure
        • Implementation Details
    • Providers
      • Contracts
        • Chainstack
        • thirdweb
        • GetBlock
      • API
        • The Graph
        • Covalent
        • Moralis
  • Funding
    • Gas Monetization
  • Run a Node
    • Overview
    • Node Providers
    • Mainnet
      • Run Validator Node
      • Run API Node
    • Testnet
      • Run Validator Node
      • Run Read-Only Node
    • Troubleshooting
    • FAQ
  • Technology
    • Overview
    • Consensus
    • Database Storage
    • Proof of Stake
    • Transaction Fees
    • Stablecoin
    • FAQ
  • Security
    • Contract Library
    • Fantom Safe
Powered by GitBook

© 2024 Fantom Foundation

On this page
  • Signing a Transaction
  • Gas Price
  • Transfer
  • transfer
  • transferWithParams
Export as PDF
  1. Build on Opera
  2. API

Web3 API

PreviousTransaction TracingNextGraphQL

Signing a Transaction

Here’s an to update the blockchain state by creating a transaction. 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

  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 to fetch the gas price:

  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

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.

Rest of the Web3 API calls and examples on Opera:

Transfer

transfer

transferWithParams

// 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.

We have added two ways to sign a transaction with a of transferring FTM between accounts.

The takes a simple approach and uses the predefined web3 method to determine the gas price required to send FTM between two accounts.

transfers with additional parameters in its transaction block.

example of signing a transaction
This example
two methods
https://github.com/Fantom-foundation/web3-methods/wiki
simple example
first method
This method