[SOLVED] How to send native crypto to a smart contract

Hey !

I am new on Moralis and blockchain development but I learned thanks to moralis.

I am stuck because I donā€™t know how to send token on my smart contract with moralis. I tried to do a transfer like this :

const options = {type: "native", amount: Moralis.Units.ETH("0.1"), receiver: "0x9...2Cb"} let result = await Moralis.transfer(options)

in receiver I put the smart contract adress, when I press button metamask open to accept the transacttion but there is an error. I donā€™t know what to do now.

Can I do it with moralis ?

Thanks !

What is the error that you get?

Warning! Error encountered during contract execution [execution reverted]

The error is not clear so I donā€™t know what to do.

is it the right way to do it ?

Did you have 0.1 eth in your address? On what chain did you test it?

I use bsc testnet, on my bsc testnet metamask I have 1 BNB.

the full error in console is:

Uncaught (in promise) Error: Transaction has been reverted by the EVM:
{
  "blockHash": "0xa09ecc4c50bc9230ffb73e3a4a8f918cb93e40cc16ea14dac1ca9287582ca2f2",
  "blockNumber": 12820592,
  "contractAddress": null,
  "cumulativeGasUsed": 189498,
  "from": "0xf...74",
  "gasUsed": 21046,
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x...bc",
  "transactionHash": "0xc24d529c058decb3697b607bba36bf706d29c89a3b439539878a183ab6fa6e89",
  "transactionIndex": 3,
  "type": "0x0"
}
    at Object.TransactionError (errors.js:87)
    at Object.TransactionRevertedWithoutReasonError (errors.js:98)
    at index.js:393
    at c (runtime.js:63)
    at Generator._invoke (runtime.js:294)
    at Generator.next (runtime.js:119)
    at n (asyncToGenerator.js:3)
    at s (asyncToGenerator.js:25)
    at asyncToGenerator.js:32
    at new Promise (<anonymous>)

The reason is that your smart contract is not payable

Even if you try to send crypto directly from your metamask it will fail

contract HelloWorld {
    
    function invest() external payable {
    }
    
    function balanceOf() external view returns (uint) {
        return address(this).balance;
    }
}

The smart contract is like this. When I use it from Remix I can change the value and call invest function so the smart contract receive bnb correctly but I donā€™t know how to do it with moralis.

Even if you try to send crypto directly from your metamask it will fail

Iā€™ve tried to send bnb directly from my metamask - transaction failed. Itā€™s not a problem on the Moralis side

Invest function is a contract method. And you should to use Moralis.executeFunction() for calling smart contract methods. Itā€™s not a simple transfer to the contract

I tried like this too:

   const options = {
        contractAddress: "0x...Ec",
        functionName: "invest",
        abi: ABI
      };

    const contract = await Moralis.executeFunction(options);

but I did not found in documentation where to add the value I want to transfer.

Sorry for this and thanks for your help !

1 Like

No problem, I should add more info to the docs, itā€™s true. Okay, let me show you a ready solution

1 Like

Could you send me the ABI or verify your contract?

[
        {
            "inputs": [],
            "name": "balanceOf",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [],
            "name": "invest",
            "outputs": [],
            "stateMutability": "payable",
            "type": "function"
        }
    ];

and contract adress is : 0x5be25A21192c14Da2C3fdD9cD8a66954D56535Ec

1 Like

Hey @luca

Iā€™ve opened PR for adding an ability to provide ā€œamountā€ to send.

Iā€™ll let you know when itā€™ll be merged

1 Like

Hey @luca

Update your SDK version and you will be able to send native crypto to a smartc contract functin this way:

const options = {
        contractAddress: "0x...Ec",
        functionName: "invest",
        abi: ABI,
        msgValue: Moralis.Units.ETH("0.5") //sending 0.5 Bnb
      };

    const contract = await Moralis.executeFunction(options);
2 Likes