Search code examples
ethereumvyperethers.js

How to test payable/external method with waffle and ethers.js


Here is the smart contract, written in vyper, to be tested

owner: public(address) 
name: public(String[100])
total_amount: uint256

@external
def __init__(_name: String[100]):
    self.owner = tx.origin 
    self.name = _name

@external
@payable
def donate():
#anyone can donate to the piggy bank
    self.total_amount += msg.value

@external
@view
def get_total_amount() -> uint256:
    return self.total_amount

What is the right way to test the donate() method of the smart contract with waffle and ethers.js?

Is there a sendTransaction method for ethers.js that can be called on contract's side like in this example of web3+truffle ?


Solution

  • You can use the overrides parameter. It's used after all contract function arguments.

    In your case, the contract function has 0 arguments so the overrides is on position 1.

    let overrides = {
        value: ethers.utils.parseEther("1.0")
    };
    let tx = await myContract.donate(overrides);
    

    See this issue for more details.