Need help with reading/writing to contract NextJS

hey guys,
i have installed and am using NextJS and react-moralis for my app. Everything is working fine so far. now i want to connect to the smart contract but nowhere is the code to do this with NextJS.

I dont know where to start, which function should i use?


i came across this, but still have no clue how to use itā€¦

i have the contract abi in a variable and the contract address tooā€¦ please somebody give me an example of how i can connect to a smart contract and use the say ā€˜mintā€™ function to mint 1 numberOfTokens

i have moralis wrapping the nextjs and everything else is working fine.

in vanilla js you can use Moralis.executeFunction

hey ā€¦ im using nextJS with moralis-reactā€¦

this is my contract address : https://testnet.bscscan.com/address/0xDe011a8221791d57394AB06687061aE414F1A302#code

any chance you or someone can help code me an example of using the mint function from my contract on a button please or guide me on how the function should appear please

Hey @Koolguy69 NextJS is a react-based framework, anything that works in React should work in NextJS unless specified, from calling samrt contract you can:

  1. do as @cryptokid says use Moralis.executeFunction
  2. use useWeb3ExecuteFunction or useWeb3Contract hooks for read or write smart contracts
  3. use useAPIContract to do only read smart contract data

hi guys thanks a thanks a lot for the really helpful repliesā€¦ now i understand it a lot moreā€¦
so if i use the below code, does it mean that when i call { fetch() } it will execute the function with the functionName and parameters?

should i need to change fetch() to another function depending on what type of function i am interacting with in the smart contract? or is fetch() general purpose execute functionName with parameters?

anyway i am going to experiment now and see what happens thanks for getting me some progress!

const ShowUniswapObserveValues = () => {
const { data, error, fetch, isFetching, isLoading } = useWeb3ExecuteFunction({
abi: usdcEthPoolAbi,
contractAddress: usdcEthPoolAddress,
functionName: ā€œobserveā€,
params: {
secondsAgos: [0, 10],
},
});

oookay i got it to work thanks to you guys!
thanks a lot a lot!!

my code is below:

const { data, error, fetch, isFetching, isLoading } = useWeb3ExecuteFunction({
abi: contract.abi,
contractAddress: contractAddress,
functionName: ā€œmintHeroā€,
params: {
numberOfTokens: 1,
msgValue: Moralis.Units.ETH(0.38),
}
})

the problem i run into now is the msgValue part because when i click mint, its telling me the transaction will fail and the value is wrongā€¦ how can i specify the value for the transaction, maybe i followed an incorrect guide and added it in the wrong place.

edit: keeps showing me the wrong value still

Total

0.285

0.285BNB

Amount + gas fee
Max amount:

0.285BNB

in metamask

You can set msgValue outside of params field

thanks boss let me try it now

awesome bro it worked!

but another problem now im not sure whatā€¦ i have 0.45bnb in my wallet and i need to spend 0.38 so amount insufficient is not the issue but the other one dont know whats causing it , cant get a clue even, below have attached screens:


any help is appreciated ā€¦ thanks!

It may be ok, in particular you need only the abi for that function.

From that error it looks like the transaction will fails now you have to make sure that the right chain was selected, that the smart contract address is the right one.

straight beasting boss!!!
god bless youā€¦ i have finally completed a task :smiley:
happy new year

for anyone that reads this later and wants to know, i had a toggle set to false so people can not mint anymore.