How would I send an eth value to specific smart contract function that is payable in ethers.js?
Asked Answered
B

1

22

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.

Bethesda answered 30/6, 2021 at 16:47 Comment(2)
Does this answer your question? How to test payable/external method with waffle and ethers.jsNelle
With unit testing, the notation is different as you're working within chai's framework, thanks for the suggestion thoughBethesda
T
44
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

Turkic answered 30/6, 2021 at 20:58 Comment(4)
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 approachBethesda
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.Survivor
I'm trying something simiar, but it's returning Error: non-payable method cannot override value. Any idea on how to fix this?Unchurch
I was trying to find this solution for many hours, thanks pal!Turkestan

© 2022 - 2024 — McMap. All rights reserved.