Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
Ethers pass method name with parameters instead of hexdata

I am using ethers to interact with contracts on Ethereum. This is how my method looks like:

const tx = await signer
      .sendTransaction({
        to: contractAddress,
        value: ethers.utils.parseEther(price),
        data: hex,
        gasPrice: ethers.utils.parseUnits(gas, 9),
      })

And it works fine. But every time I have to look on Etherscan to find transaction hex and pass it to the data parameter. Is it possible to just pass method name from contract and parameters to it instead of passing this hex value?

Maybe there is any method in ethers to encode that method with parameters into hex value?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What you need is the contract ABI. Generally it's written as a json object you can import in your js script, or it can be written like this (human-readable-ABI):

const contractABI = [
  "function foo(uint256 bar) external",
  // other functions or events ...
]

The ABI contains all (or some) methods of a contract and how to encode/decode them.

With the ABI you can create a contract object like this:

const contract = new ethers.Contract(contractAddress, contractABI, provider);

which can be used to send transactions like this:

const my_bar = 123  // example
const tx = await contract.connect(signer).foo(my_bar, {
  value: ethers.utils.parseEther(price),
  gasPrice: ethers.utils.parseUnits(gas, 9),
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda