[SOLVED] Can't get return value from executeFunction

Hello!

I have next code:
const abi = [
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “biddingTime”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “revealTime”,
“type”: “uint256”
},
{
“internalType”: “address”,
“name”: “token”,
“type”: “address”
}
],
“name”: “newGame”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “id”,
“type”: “uint256”
}
],
“stateMutability”: “nonpayable”,
“type”: “function”
}
]
const newGame = async () => {
const options = {
contractAddress: contractAddress,
functionName: “newGame”,
abi: abi,
params: {
biddingTime: “1641793392”,
revealTime: “1642225392”,
token: “0xe9e7cea3dedca5984780bafc599bd69add087d56”
},
};
const web3 = await $moralis.enableWeb3();
const exec = await $moralis.executeFunction(options);
console.log(exec);
}

All work well, but there isn’t return value.
Result is (no returned id):

{
“blockHash”: “0x8792fb87a6f202d97e9b55f11682b38a9a0a5e5b5287105ade09cab4b4128118”,
“blockNumber”: 15608278,
“contractAddress”: null,
“cumulativeGasUsed”: 6487709,
“from”: “0x03dd4a084f2b4ba0ccab37639222d1508d2ea5dc”,
“gasUsed”: 113530,
“logsBloom”: “0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”,
“status”: true,
“to”: “0xd731361f848ee8ed734be1b6baf3b8f3ca127ae0”,
“transactionHash”: “0x92d721348a826fdc022e51f5fa287ec7fcc33f3712712231058d0613d800d62d”,
“transactionIndex”: 8,
“type”: “0x0”,
“events”: {}
}

Can you tell me please, what could be wrong?
BR,
Andrey

What do you expect to receive as a returned value?

My contract is:

function newGame(
uint biddingTime,
uint revealTime,
address token
)
external
onlyOwner
returns (uint id)
{
_gameIds.increment();
uint gameId = _gameIds.current();
Game storage game = games[gameId];
game.id = gameId;
game.biddingEnd = block.timestamp + biddingTime;
game.revealEnd = game.biddingEnd + revealTime;
game.gameToken= token;
game.initialPrice = 0;
return gameId;
}

So, I expect to receive gameId.

I don’t know now in what circumstances you can get that id. Usually you have to parse events to get a result, In case that events were used.

So, why using “runContractFunction” I can get retirn value, but can’t change state of the contract?

runContractFunction only calls read only functions, no transactions is sent/processed to the chain, the chain is only queried

1 Like

Thanx!
Should I use native Web3 Api to listen transaction events?
I didn’t found example with special Moralis API to do it.

in that result that you pasted above, it also included events, but that transaction didn’t have events

I’ve changed contract

event GameStarted(uint gameId);

function newGame(
uint biddingTime,
uint revealTime,
address token
)
external
onlyOwner
{
_gameIds.increment();
uint gameId = _gameIds.current();
Game storage game = games[gameId];
game.id = gameId;
game.biddingEnd = block.timestamp + biddingTime;
game.revealEnd = game.biddingEnd + revealTime;
game.gameToken = token;
game.initialPrice = 0;
emit GameStarted(gameId);
}

but anyway didn’t see any event in events list…

how does the received data look now?

I copied example from documentation:
const newGame = async () => {
const options = {
contractAddress: contractAddress,
functionName: “newGame”,
abi: contract[“abi”],
params: {
biddingTime: “1641793392”,
revealTime: “1642225392”,
token: “0xe9e7cea3dedca5984780bafc599bd69add087d56”
},
};
const exec = await $moralis.executeFunction(options);

exec.events is empty…

can you paste the entire output, as you did on first post? you can also see here how to post code on forum: READ BEFORE POSTING - How to post code in the forum

Sorry, of course.
But it didn’t change…

{
“blockHash”: “0xf5d4263998d88f758d1d1d3b472d9ebbc24bf5ba47f69288f0c0b9a033a92f11”,
“blockNumber”: 15631285,
“contractAddress”: null,
“cumulativeGasUsed”: 2831220,
“from”: “0x03dd4a084f2b4ba0ccab37639222d1508d2ea5dc”,
“gasUsed”: 113530,
“logsBloom”: “0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”,
“status”: true,
“to”: “0xd731361f848ee8ed734be1b6baf3b8f3ca127ae0”,
“transactionHash”: “0x889c99b7201120717498baf6b19fbb6916e0472ed9288c6ef6c7ceae3637fba3”,
“transactionIndex”: 18,
“type”: “0x0”,
“events”: {}
}

maybe it still uses the previous smart contract and not the new one

I’ve triple time called “truffle migrate --network testnet”

on what network was that contract deployed?

On the bsc testnet

I changed contract long time ago…

it looks like that contract was not deployed today, so probably it doesn’t have the updated code with events

You were wright!
after using --reset opitons I can see event in list!
Could you point to example of watching event&
{
“blockHash”: “0x2c917cdbfad2e5594d6f45c27e3e9d063f97227127ed9cc5c5c2aff323cff296”,
“blockNumber”: 15632187,
“contractAddress”: null,
“cumulativeGasUsed”: 1060818,
“from”: “0x03dd4a084f2b4ba0ccab37639222d1508d2ea5dc”,
“gasUsed”: 129715,
“logsBloom”: “0x00010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000040000000000000000000000000000000020000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”,
“status”: true,
“to”: “0x96f62179fafc07a1895cda38f264253e4cf7fc9e”,
“transactionHash”: “0x00e4114161b40de1d4c53189ed2c1eb4ad953369d9c0e7d0ac4d7e7c7a5b15f5”,
“transactionIndex”: 7,
“type”: “0x0”,
“events”: {
“GameStarted”: {
“address”: “0x96f62179faFc07a1895cdA38F264253e4cf7FC9e”,
“blockNumber”: 15632187,
“transactionHash”: “0x00e4114161b40de1d4c53189ed2c1eb4ad953369d9c0e7d0ac4d7e7c7a5b15f5”,
“transactionIndex”: 7,
“blockHash”: “0x2c917cdbfad2e5594d6f45c27e3e9d063f97227127ed9cc5c5c2aff323cff296”,
“logIndex”: 29,
“removed”: false,
“id”: “log_571f39cd”,
“returnValues”: {
“0”: “1”,
“gameId”: “1”
},
“event”: “GameStarted”,
“signature”: “0x50ad08f58a27f2851d7e3a1b3a6a46b290f2ce677e99642d30ff639721e77790”,
“raw”: {
“data”: “0x0000000000000000000000000000000000000000000000000000000000000001”,
“topics”: [
“0x50ad08f58a27f2851d7e3a1b3a6a46b290f2ce677e99642d30ff639721e77790”
]
}
}
}
}

you still need some help now?