Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
How do I charge a transaction fee when a function in my contract is executed?

For example, let's say that every time a user successfully executes a contract function that calculates the sum of two numbers, I’d like to charge a 1% ETH fee that gets sent to an account separate from the contract. My current solution "works", but it is not a good solution as you can imagine. As it stands, the user has to sign two separate transactions, one after the other. First, to sign the fee amount, and second, to sign the contract function. How would I go about creating one transaction which includes both the fee and the contract function together? I'm using Ethers.js for the convenience library, but I'm open to other solutions.

Solidity Contract

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Calculate {
  uint private c = 123;

  constructor() {
    getC();
  }

  function getC() public view returns (uint) {
    return c;
  }

  function add(uint _a, uint _b) public {
    c = _a + _b;
  }
}

React

  async function add() {
    await requestAccount();
    await sendETH();
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
    const contract = new ethers.Contract(
      calculateAddress,
      Calculate.abi,
      signer
    );
    const tx = await contract.add(a, b);
    await tx.wait();
    getC();
  }

  async function sendETH() {
    await requestAccount();
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
    const tx = {
      from: connectedAccount,
      to: fundAcct,
      value: ethers.utils.parseEther(fee.toString()),
    };
    signer.sendTransaction(tx).then((transaction) => {
      console.dir(transaction);
      alert("Send finished!");
    });
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In case if incoming transfer amount is any, and you charge 1%, you have to call add function with value*(1+fee) amount.

Considering fee is in range 0-1 and there is a total variable for a total transaction cost without fee.

const tx = await contract.add(a, b, {value: ethers.utils.parseEther(fee.toString()).mul(total)});
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Calculate {
  uint private c = 123;
  address separateContract;

  constructor(address _separateContract) {
    getC();
    separateContract = _separateContract;
  }

  function getC() public view returns (uint) {
    return c;
  }

  function add(uint _a, uint _b) public payable {
    separateContract.send(msg.value / 100); // send 1% to a separate contract
    c = _a + _b;
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda