Triggering receive() external payable function with Moralis.transfer() - mumbai, MATIC [SOLVED]

Hello Everyone, I have deployed a smart conract to Polygon Mumbai using MetaMask. My smart contract has a receive() external payable function for empty calldata (and any value). I want to do a simple transaction with the smart contract as I can do in Remix by entering a value and then clicking the transact button. Can “Moralis.transfer()” function be used for this transaction or do I need to use something else?

I couldn’t find information on this so I assumed transfer function could be used. However, I also couldn’t figure out how to use matic in mumbai testnet for this. I don’t know what to put for the “contractAddress”. When the type is erc20 it doesn’t even interact with the MetaMask and If i try native for the type It just fails to complete the transaction ->>
image
–>>

->>>

 async function buy(amount){
            // const tokenInfo = { chain: "mumbai", symbols: "MATIC" };
            // const tokenMetadata = await Moralis.Web3API.token.getTokenMetadataBySymbol(tokenInfo);
            // const contractAddress = tokenMetadata?.address;
            const options = {
                type: "erc20", 
                amount:Moralis.Units.Token("0.001", "18"), 
                receiver: "0x..", 
                contractAddress: "0x.." //???
            }
            let result = await Moralis.transfer(options)
            alert(result);
        }

------------------------------------------------EDIT------------------------------------------------------
I have solved the issue. Moralis.transfer() with native type works perfectly. I had an issue with the logic in the smart contract. If theres is a problem with the logic in the receive() function it fails and reverts the execution!

Hey @beng are you trying to transfer ERC20 or interact with the contract here? :raised_hands: Keep in mind that Moralis.transfer has nothing to do with contract interaction and only send ERC20 token to another address.

Also making the function payable means that you are accepting MATIC (native token) but not necessarily ERC20 tokens.

To interact with smart contract, use Moralis.executeFunction instead https://docs.moralis.io/moralis-server/web3/web3#executefunction

1 Like

Hi @YosephKS, thank you for your reply. I was confused about the types I understand now that I need to use native type for MATIC. To call the receive() function, do you know what should be the paramater for the function name? Do I do something like this? -> functionName: “receive”. I assumed it cannot be called as a normal function since I do not decleare it as a regular function in the smart contract.

I think that there is a standard for that to be able to receive native currency in a smart contract as any other address

I see. I have been searching for a while now. I think Moralis does not have such feature for this kind of low level interaction. Using remix I can simply enter a value for the transaction and transact with empty call data and it by default calls the receive method. Moralis.executeFunction() requires a functionName parameter which does not work for receive() function.

    receive() external payable {
        
        require(ticketPrice != 0 && msg.value  >= ticketPrice, "Incorrect Ether amount!");
        
        require(msg.sender != admin);
        uint i;
        uint j = msg.value / ticketPrice;
        for (i=0; i < j; i++) {
             players.push(payable(msg.sender));
        }
       
    }

You can use transfer function maybe, or write directly with web3

1 Like