ETH value is always 0 - How to Build Serverless Dapps

I’m going through this tutorial - https://www.youtube.com/watch?v=rd0TTLjQLy4

Everything seems to work fine. After clicking Play Heads / Play Tails I’m getting a Metamask confirmation and after confirming, I’m getting the smart contract receipt. The problem is that regardless of the amount in the number input, the transaction price is always equal to just the gas fee. I’m sure that I’m getting the amount from the input correctly because it’s showing up in the console log.

Here’s my code:

Moralis.initialize("************"); // Application id from moralis.io
Moralis.serverURL = "************"; //Server url from moralis.io

async function login() {
    try {
        user = await Moralis.User.current();
        if(!user) {
            user = await Moralis.Web3.authenticate();
        }
        
        console.log(user);
        alert("User logged in")
        document.getElementById("login_button").style.display = "none";
        document.getElementById("game").style.display = "block";
    } catch (error) {
        console.log(error);
    }
}

async function flip(side) {
    let sideNumber;
    let amount = document.getElementById("amount").value;

    if(side == "heads") {
        sideNumber = 0;
    } else {
        sideNumber = 1;
    }

    window.web3 = await Moralis.Web3.enable();
    let contractInstance = new web3.eth.Contract(window.abi, '0xf09274736c50a7497D84940b9FF14A140Ef3333C');
    contractInstance.methods.flip(sideNumber).send({value: amount.toString(), from: ethereum.selectedAddress})
    .on('receipt', function(receipt) {
        console.log(receipt.events.bet.returnValues.win);
    })
}

document.getElementById("login_button").onclick = login;
document.getElementById("heads_button").onclick = function(){flip("heads")};
document.getElementById("tails_button").onclick = function(){flip("tails")};

And a screenshot:

1 Like

Hey @ApesTogetherStrong

I guess the problem is that you are inputting too little value in the Wei.
10000Wei from the screenshot = 0.00000000000001Eth

Do you have a value 10000 on the receipt?

To easily convert from wei to ETH you can use Moralis.Units.ETH(amountWei)

Hey @Yomoo, thanks for the reply. Sadly, regardless of how much I put there, the transaction value stays the same. Here’s the receipt:

Here is a full code from the tutotial https://github.com/MoralisWeb3/demo-apps/blob/main/casino-dapp/main.js

Please use https://www.diffchecker.com/ to find the differences. Your code is unfinished and apparently somewhere very different from the tutorial. The code in the tutorial works correctly.

I guess there is no need to value: amount.toString() you can use value: amount

Share your smart contract please

Also make sure that this line let amount = document.getElementById("amount").value; gets the right number.

I know it’s not complete, but at this stage in the tutorial, it was working already. Removing toString() didn’t change anything. Here’s the smart contract:

import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
import "../node_modules/@openzeppelin/contracts/utils/math/SafeMath.sol";

pragma solidity ^0.8.0;

contract FlipContract is Ownable {
    
    using SafeMath for uint256;

    uint public ContractBalance;

    event bet(address indexed user, uint indexed bet, bool indexed win, uint8 side);
    event funded(address owner, uint funding);

    // Function to simulate coin flip 50/50 randomnes
    function flip(uint8 side) public payable returns(bool){
        require(address(this).balance >= msg.value.mul(2), "The contract hasn't enought funds");
        require(side == 0 || side == 1, "Incorrect side, needs to be 0 or 1");
        bool win;
        if(block.timestamp % 2 == side){
            ContractBalance -= msg.value;
            payable(msg.sender).transfer(msg.value * 2);
            win = true;
        }
        else{
            ContractBalance += msg.value;
            win = false;
        }
        emit bet(msg.sender, msg.value, win, side);
    }
    // Function to Withdraw Funds
    function withdrawAll() public onlyOwner returns(uint){
        payable(msg.sender).transfer(address(this).balance);
        assert(address(this).balance == 0);
        return address(this).balance;
    }
    // Function to get the Balance of the Contract
    function getBalance() public view returns (uint) {
        return ContractBalance;
    }
    // Fund the Contract
    function fundContract() public payable onlyOwner {
        require(msg.value != 0);
        ContractBalance = ContractBalance.add(msg.value);
        emit funded(msg.sender, msg.value);
        assert(ContractBalance == address(this).balance);
    }

}

It does :slight_smile:

How do you know that?

I’ve consol logged it and the number showed up.

Try to fund the contract:
in smart contract:

    event funded(address sender, uint256 funding);
    function fundContract() public payable {
        emit funded(msg.sender, msg.value);
    }

in frontend:

    window.web3 = await Moralis.Web3.enable();
    let contract_instance = new web3.eth.Contract(
        abi, SMART_CONTRACT);
    await contract_instance.methods.fundContract()
        .send({ value: 100000, from: ethereum.selectedAddress });

@Yomoo @cryptokid even after cloning the repository of the full app https://github.com/MoralisWeb3/demo-apps/tree/main/casino-dapp I’m still running into the same issue:

I’m investigating, it looks like I have a similar problem.

1 Like

It works in my case, I was confused because I didn’t see in metamask the sent value, but that was because the sent value was too small to influence the final gas transaction cost.
You will not be able to see that small difference in metamask unless you send much more than the cost of gas.
I did this transaction for test: https://testnet.bscscan.com/tx/0x995a253dcfce62a04b3ab418469128069af976b76876d092199f8d8a07ef3142

@ApesTogetherStrong, you can check in ganache interface (or what you are using) for that transaction details. Probably is everything ok on your side too.

@cryptokid but does your balance change? In my case, I can see the transaction in Ganache, but the ETH value is 0:

image

It’s not sending eth transaction. It’s an interaction with smart contract - which has value in eth