23

I'm trying to call a payable function on a smart contract that only accepts one argument.

How would I send an eth value to this function in ethers.js along with the function call? The docs don't seem to give much examples on the best way to do this.

My function call

const reciept = await contract.buyPunk(1001);

all other read and write function calls work as expected, but its calling a payable function that I have yet to solve.

  • Does this answer your question? [How to test payable/external method with waffle and ethers.js](https://stackoverflow.com/questions/67121007/how-to-test-payable-external-method-with-waffle-and-ethers-js) – Petr Hejda Jun 30 '21 at 18:08
  • With unit testing, the notation is different as you're working within chai's framework, thanks for the suggestion though – Richard Vartan Melkonian Jul 02 '21 at 11:06

1 Answers1

43
const options = {value: ethers.utils.parseEther("1.0")}
const reciept = await contract.buyPunk(1001, options);

When calling a contract function through ethers.js you can pass along an object of options at the end of your arguments. This object can set the the value to send along with the transaction.

Documentation here: https://docs.ethers.io/v5/api/contract/contract/#Contract-functionsCall

Daniel Von Fange
  • 857
  • 9
  • 10
  • 1
    Thanks a bunch! This solved it, yes I did see that in the documentation, but wasn't sure what properties the object should contain. Pretty cool that you can specific GasLimit and price also within the object. Makes for a nice modular approach – Richard Vartan Melkonian Jul 01 '21 at 12:42
  • Is there any option to use something similar to specify which erc20 does the value net to be paid in? Let's say I would like to force the user to pay 100 USDC. – Raül Jun 18 '22 at 16:12
  • I'm trying something simiar, but it's returning `Error: non-payable method cannot override value`. Any idea on how to fix this? – Ciprian Jun 27 '22 at 08:51
  • I was trying to find this solution for many hours, thanks pal! – user3417479 Dec 31 '22 at 19:49