ETH value is always 0 - How to Build Serverless Dapps

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

You could try to get the balance this way from console:

let balance = await web3.eth.getBalance(CONTRACT_ADDRESS);

This balance value is increasing every with every roll, so that’s good. It looks like the problem is with user not getting the ETH back after winning.