msgValue problem

Hello everyone,
is there a way we can send msgValue in runcontractFunction in eth instead of wei or gwei. In my smart contract it expects to send value in eth

but when i am sending it from msgValue function it is going in form of wei and it is giving unexpected result.
when i am send 0.2 number for eth. it is transforming into it’s wei form that is 2e17 something and this according to my contract means that i am sending 2e17 eth which i don’t have in my account so reverting with not suffiecient balance in account.
please help.

Did you try with an exact value in wei hardcoded to see what happens?

Is that a read only function that uses msgValue?

Yes i tried using manual value also but not working.
It’s not readonly.

Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="execution reverted", method="estimateGas", transaction={"from":"0x6A00422C67579157C48A574E7064c2B856fc14f3","to":"0xD6a31697eefE202fFc673BbDf58d2f16F168910b","value":{"type":"BigNumber","hex":"0x02c68af0bb140000"},"data":"0x681a1a160000000000000000000000004450c8e799d036de36e9ec096c2d14a33abc96f7000000000000000000000000326c977e6efc84e512bb9c30f76e30c160ed06fb","accessList":null}, error={"code":-32603,"message":"execution reverted","data":{"originalError":{"code":3,"data":"0xb8c67e600000000000000000000000000000000000000000000000026abc44bafddc9ab0","message":"execution reverted"}}}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.7.0)
    at Logger.makeError (index.js?dd68:224:1)
    at Logger.throwError (index.js?dd68:233:1)
    at checkError (json-rpc-provider.js?8679:76:1)
    at Web3Provider.eval (json-rpc-provider.js?8679:575:1)
    at Generator.throw (<anonymous>)
    at rejected (json-rpc-provider.js?8679:6:42)

is the error that i am getting

You can use web3 or ethers directly to see if it works.

Yes i also wanted to use ethers js. But when i am installing it is also giving an error like ./cache or something file missing. So i had to remove it.

What type of application it is? Vanilla js, react, next js, something else

i am using nextjs for the application

It should be possible to make it work with web3 or ethers directly

  const { runContractFunction: swapWethToToken } = useWeb3Contract({
    contractAddress: DEX_CONTRACT_ADDRESS,
    abi: dexAbi,
    functionName: "swapWETHToToken",
    msgValue:
      fromValue != 0 &&
      Moralis.Units.FromWei(
        BigNumber(fromValue).multipliedBy(Number(roundOff)).toFixed().toString()
      ),
    params: {
      from: contractAddresses[activeFromToken],
      to: contractAddresses[activeToToken],
    },
  });

The problem is when i am sending 0.2 as msgValue it is saying that it is not a BigNumber. Is there a way to go past this check :thinking:.

try to add some logging to see what you have there.
there are also Utils functions that make that transformation from eth to Wei and the other way around


image

0.1 in wei form what i am getting
The same thing is happening when i am doing it with code.

what is the expected value?

like if i want to provide 0.2 in eth as liquidity it should be also be seen 0.2 eth in metamask. Since 0.2 is not a bigNumber i have to transform it into wei form or non float form which is giving the same result mentioned above.

What is the value of fromValue here?
What does that multiplication do?

fromValue is the value from the input tag.
if fromValue = 2
roundOff = 10e18
answer = 2e18
toFixed is transforming 2e18 into 2000000000000000000 form.

I was only using this line

BigNumber(fromValue).multipliedBy(Number(roundOff)).toFixed().toString()

i used Moralis.Units.FromWei to check if it is making any difference. But no.

Also i am using bigNumber.js library for the above calculation.

can you give an example for input value and expected output value?


in the above transaction i want to swap 1.2 eth. And i am using above mentioned formula to calculate the wei form of it(12000000000000000000). So my function should understand that i am sending eth in wei not in ether itself. But it is failing to do so and showing that i am trying to send 12000000000000000000 ethers not wei.

ok, for your code, what you sent it as input value and what is the expected output?

same that mentioned above.
1.2 eth should show 1.2 eth
not 1200000… eth
my input value is 1.2 eth and output value in 1200000… eth.