How to open MetaMask for confirming a transaction using Moralis?

I had assumed that the following code would open MetaMask with the amount of ETH requested to send so that the user could confirm the transaction. Is this still possible?

javascript

import { Moralis } from "@moralisweb3/next";

const options = {
  type: "native",
  amount: Moralis.Units.ETH("0.5"),
  receiver: "0x807546A7cfE7140cd8ef92c9949FDFc7Bc9ac92f"
};

let result = await Moralis.transfer(options);

Iā€™m using the Moralis library to interact with MetaMask for a transaction. However, when executing the code, I encountered an error: ā€œTypeError: moralisweb3_next__WEBPACK_IMPORTED_MODULE_20_.Moralis is undefinedā€. It seems that the Moralis object is not properly defined or imported.

I have already installed the @moralisweb3/next package and imported the Moralis object as shown in the code. Is there something Iā€™m missing or any additional steps required to open MetaMask and prompt the user to confirm the transaction?

Any guidance or insight would be greatly appreciated. Thank you!

Hey @MetalHorse233,

I see that youā€™re using @moralisweb3/next library which no longer have the .transfer function. If youā€™d like to use the .transfer, then install moralis-v1 instead with NPM where the function will exist,

Isnā€™t that deprecated though? I canā€™t get a server URL.

I imagine this is how you would do this. But it doesnā€™t make sense to me to use two SDKs in the same project because Iā€™m using the new web3 SDK for authentication with Firebase. Is there really no way to do this in the current sdk?

// Replace 'YOUR_APPLICATION_ID' with your actual Moralis application ID
Moralis.start({ serverUrl: 'https://YOUR_SERVER_URL/parse', appId: 'YOUR_APPLICATION_ID' });

try {
        // Enable web3 and authenticate the user
        await Moralis.enableWeb3();
        await Moralis.authenticate();
    
        // ... your code ...
    
        const options = {
          type: "native",
          amount: Moralis.Units.ETH("0.5"),
          receiver: "0x807546A7cfE7140cd8ef92c9949FDFc7Bc9ac92f"
        };
    
        let transaction = await Moralis.transfer(options);
        console.log(transaction, 'transaction details');
        const receipt = await transaction.wait();
        console.log(receipt, 'transaction finished');
    
        // ... your code ...
      } catch (error) {
        console.error("Error while creating the order:", error);
        // You can show an error message to the user here
      }

How do people send transactions with MetaMask now or is this just not a supported function?

Hey @MetalHorse233,

Yes, it is and true youā€™ll need a server URL. However, your code of using .enableWeb3 and .transfer are all properties of the moralis-v1 and not the latest @moralisweb3/next SDK. We have basically deprecated a lot of the function used for interacting with tokens or contracts in general in the newest SDK, so no way to do those transfer functions through our latest SDKs.

Alternatively, if you donā€™t want to use multiple version of Moralis then you can use library such as web3js or etherjs to do so.

Is it possible to do those transaction functions without authenticating the user again using the V1 SDK? Thatā€™s my biggest objection. If thatā€™s the case thereā€™s no point of using either one.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.