Get event with Moralis.execute

Hi! I want to get the token id from default Transfer event in an standard erc721 after minting with Moralis.execute()
I’m using new the Moralis beta which as we know now uses ether.js instead of web3.
With web3 I used the transfer event to get the ID of the token, I used to do this:

window.web3 = await Moralis.enableWeb3();
window.tokenContract = new web3.eth.Contract( ABI, TOKEN_CONTRACT_ADDRESS );
const receipt = await tokenContract.methods
			.createItem(uriInfo)
			.send({ from: ethereum.selectedAddress })
			.then(function(result) {
			console.log("RECEIPT RECEIVED!: ", result.events.Transfer.returnValues.tokenId);
	return result;
			})

Now with the new moralis beta I use the Moralis.execute like this:

const options = {
contractAddress: "0xE52,,,D39f1", 
functionName: "createItem",
abi: ABI,
params: {uri: uriInfo,},
};
const transaction = await Moralis.executeFunction(options);
const receipt = await transaction.wait(3);
console.log(receipt);

BUT … i cannot figure out how to get the transfer event to then get the token ID, or the content of any other custom event aswell.

The only thing I get is the name of the event (or any other custom event) but not the content…

Any ideas? thanks in advance!

I’m not really sure but do you get the transaction hash in the receipt?

yes. I guess I can get the token Id with the tx hash (i didnt find out how to do that yet), but its easier just to get the token Id from the event itself. That is what im missing using Moralis.execute()…
Or i didnt find it yet…

in that receipt, there is no event info?

1 Like

Ok, I made some progress here.
Yes i get event info, like this:
1- to get the name of the event:

receipt.events[0].event;

In this case 0 is the default ‘Transfer’ event (Im using an erc721)
2- Now I can guess i can get the token id form the default ‘transfer’, but its weird formating (I think).
I do it with this:

receipt.events[0].args[0];

and I get

0: {type: 'BigNumber', hex: '0x08'}

For the moment Im asuming that tha is the token ID and I should convert that number…
Ill post my findings soon!..

OK, I finally got this working!!!

This is how I got the token id from default Transfer event in an standard erc721 after minting with Moralis.execute()

const transaction = await Moralis.executeFunction(options);
const receipt = await transaction.wait();
let tokenid= parseInt(receipt.events[0].args[2]._hex, 16);
console.log(tokenid);

I had to convert the hex output to decimal.

hope that helps!

3 Likes