0

I have a lottery kind of app for which users send money to a contract that at the time pays rewards. Simplified, the method I'm trying to test is something like this:

struct Bet {
    address payable user;
    bytes32 gameId;
    uint amount;
    uint num;
}

function placeBet(bytes32 _gameId, uint _num) public payable {
    ...
    // add the new bet
    Bet[] storage bets = gameBets[_gameId];
    bets.push(Bet(msg.sender, _gameId, msg.value, _num));

    // add the mapping
    bytes32[] storage userBets = userToBets[msg.sender];
    userBets.push(_gameId);
    ...
}

In order to test this properly I need to manually set the sender and value of the transaction I'm triggering, in this case placeBet().

I tried adding a hash with from and value keys after the method's params (like { from: addr, value: 5 }), which I understand works for truffle:

    before(async function () {
        LoteryApp = await ethers.getContractFactory("LoteryApp");
        [owner, addr1, addr2] = await ethers.getSigners();

        contract = await LoteryApp.deploy();
    });

    describe("it takes bets", function() {
        it("and assigns them properly", async function() {
            // user places Bet on 2
            await contract.placeBet(game.id, 2, { from: addr1, value: 5000 });

            // user places Bet on 1
            await contract.placeBet(game.id, 2, { from: addr2, value: 3000 });
            ...
        })
    })

And I also tried with connect method, taken from hardhat docs:

describe("it takes bets", function() {
    it("and assigns them properly", async function() {
        // user places Bet on 2
        await contract.connect(addr1).placeBet(game.id, 2);

        // admin places Bet on 1
        await contract.connect(addr2).placeBet(game.id, 1);
        ...
    });
})

But non of these options worked. Transactions always seem to be sent from the same address with a value of 0.

I'm using solidity 0.7.3, hardhat 2.3.3, ethers 5.4

0
2

It seems like the value you entered is a bit too small. I think this is why the transaction value is 0.

await contract.placeBet(game.id, 2, { value: ethers.utils.parseEther("0.5") }); // msg.value = 0.5 eth

To call the function from another account try:

await contract.connect(addr1).placeBet(game.id, 2, { value: ethers.utils.parseEther("0.5") }); 
2
0

I'm not sure why your first example didn't work. But using the connect method, you should be able to pass options in to the transaction in the same way you attempted using standard ethers format. Example here:

it(`should deposit 50 eth to weth contract:`, async () => {
      weth
        .connect(accounts[1])
        .deposit({ value: ethers.utils.parseEther("50") });
});

Note: The deposit function does not take any parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.