MetaMask Polygon/Matic Payments + Possible BNB Payments

Hello, I want to know how I’d be able to allow my users to pay with ETH on polygon and BNB on BSC through MetaMask.(So if they are on network 56 or 137 it’ll allow them to pay in BNB or ETH) This is how I did it for ETH on the Ethereum Mainnet and it works but how can I do it for the other chains? Looking forward to hearing back hopefully I can get response soon :D. Lastly I’m using more of an order book model so id prefer if the solution had the .on handlers so i could run the same functions in them. I also want the Polygon payments to work more, if BNB requires a new smart contract we can skip it.

await axios
.get(“https://min-api.cryptocompare.com/data/pricemulti?fsyms=MATIC,BNB,ETH,IOT&tsyms=USD”)
.then((res) => {
const cryptos = res.data;
setETHRate(cryptos[“ETH”].USD)
});

        const sum = text1 / ethRate
        const ether = `${sum.toFixed(10)}`
        const networkId = await web3.eth.net.getId()
        if(networkId === 1){
            try {
                const trans = web3.eth.sendTransaction({
                    from: account,
                    to: '0x0000000000',
                    value: web3.utils.toWei(ether, "ether")
                })
               .on("transactionHash", async (hash) => {
                    //pending 
                })
                .on("receipt", async (receipt) => {
                    //completed action
                })
                .on("error", async (error) => {
                    //error action
                }) .....

Yes, This would be tremendously helpful, I’ve been stuck for a week. I want to allow ETH purchases on Polygon through MM.

Does anyone have any advice or an easier way to do it through Moralis?

I’m not sure what you are specifically trying to do, but my app based on Moralis works on Ethereum, Polygon and BSC without any changes in the Moralis code except names of database tables.
I auto detect the network currently chosen in Metamask with this:

        const web3 = await Moralis.Web3.enable()
        const chainId = web3.currentProvider.chainId

This will give you the chainId, for example ‘0x1’ is indeed Eth Mainnet, ‘0x13881’ Polygon Mumbai, ‘0x38’ BSC Mainnet, etc.
Based on that it set some params for the app, for example the database name like:

const tokenQuery = new Moralis.Query(`${chainId}Balance`)

For the rest I think all the same, although I’m working on NFT’s and not payments.
I interact with my own contracts and for each chain I set a different contract address. Contracts for all chains are exactly the same, just deployed on different EVM compatible networks.

Best of luck!

1 Like

Whats Balance? Just the USD equivalent to the ETH, or BSC?

Also how do you send the transaction for Polygon/BSC/ETH, the full boilerplate code would help a lot

Sorry for the confusion, I was just explaining how different networks relate to the use of Moralis. Your question is actually not related to Moralis, only to using web3 / metamask with other EVM chains like Polygon and BSC.

1 Like

How could you create a smart contract that will receive coins so my users could send BNB or any other ERC20 tokens, ive been searching but i dont see much about this.