Best way to interact with a deployed contract

Hi all!

I’m looking for the best way to interact with a contract in react using the moralis sdk/api. Previously, one of the best ways I’ve interacted with a contract is with useDapps’s useContractFunction hook, which allows me to pass a contract object (created from an ABI and address), and it returns the state of the transaction, and a function to “send” the transaction, which I quite like.

What’s the best way to do this with moralis? I know I can do something like:

const contract = new web3.eth.Contract(contractAbi, contractAddress);

And then make a raw call to the contract, but it looks like I might have to setup some custom subscriptions to listen for the transaction to finish.

Would it be my best play to:

  1. Just useUseDapp w/ Moralis (seems like they overlap a lot…)
  2. Setup my own custom event listeners & such

Let me know!

Hey @PatrickAlphaC

Currently we do not have a universal solution for your problem. Therefore, you will have to use standard web3 methods, like:

const contract = new web3.eth.Contract(contractAbi, contractAddress);
await contract.methods
          .approve(contractAddress, amount)
          .send({ from: userAddress })
          .then(function (receipt) {
            console.log(receipt);
          })
          .catch(function (error) {
            console.log(error);
          });

Something similar to what you showed will be in our SDK later.

We have a universal solution for sending native crypto, tokens and NFTs docs .

In case if you want get only read data you can use useMoralisWeb3Api() . It has runContractFunction function which runs a function of a contract and returns readonly data.

Let me know if you have any questions :raised_hands:

1 Like