Failed running cloud function runContractFunction for user undefined

Get this error Failed running cloud function runContractFunction for user undefined

const options = {
chain: CHAIN,
address: data.contract,
function_name: ‘getListing’,
abi: ABI_MARKET,
params: { listingId: 3 },
};
await Moralis.Web3API.native.runContractFunction(options);

Should that be possible, after being connected

Is it a read-only function?

It is only possible to call the read function through runContractFunction. It should also work in cloud code.

Can we see your full cloud function code? Test first with static values for each of the options (chain, address, etc.).

Yes, its a read function
0x195642d09A00574daC97B77C9Bf1E85790C4E2C6

getListing, using ID 2 should give a response

we need the full options part to test, it should work fine if the right parameters are provided in options

you can also test it directly in web3api tab in admin interface

{
chain: ‘polygon’,
address: ‘0x195642d09A00574daC97B77C9Bf1E85790C4E2C6’,
function_name: ‘getListing’,
abi: ‘[{“anonymous”:false,“inputs”:[{“indexed”:false,“internalType”:“uint256”,“name”:“listingId”,“type”:“uint256”},{“indexed”:false,“internalType”:“address”,“name”:“seller”,“type”:“address”}],“name”:“Cancel”,“type”:“event”},{“anonymous”:false,“inputs”:[{“indexed”:false,“internalType”:“uint256”,“name”:“listingId”,“type”:“uint256”},{“indexed”:false,“internalType”:“address”,“name”:“seller”,“type”:“address”},{“indexed”:false,“internalType”:“address”,“name”:“token”,“type”:“address”},{“indexed”:false,“internalType”:“uint256”,“name”:“tokenId”,“type”:“uint256”},{“indexed”:false,“internalType”:“uint256”,“name”:“price”,“type”:“uint256”}],“name”:“Listed”,“type”:“event”},{“anonymous”:false,“inputs”:[{“indexed”:false,“internalType”:“uint256”,“name”:“listingId”,“type”:“uint256”},{“indexed”:false,“internalType”:“address”,“name”:“buyer”,“type”:“address”},{“indexed”:false,“internalType”:“address”,“name”:“token”,“type”:“address”},{“indexed”:false,“internalType”:“uint256”,“name”:“tokenId”,“type”:“uint256”},{“indexed”:false,“internalType”:“uint256”,“name”:“price”,“type”:“uint256”}],“name”:“Sale”,“type”:“event”},{“inputs”:[{“internalType”:“uint256”,“name”:“listingId”,“type”:“uint256”}],“name”:“buyToken”,“outputs”:[],“stateMutability”:“payable”,“type”:“function”},{“inputs”:[{“internalType”:“uint256”,“name”:“listingId”,“type”:“uint256”}],“name”:“cancel”,“outputs”:[],“stateMutability”:“nonpayable”,“type”:“function”},{“inputs”:[{“internalType”:“uint256”,“name”:“listingId”,“type”:“uint256”}],“name”:“getListing”,“outputs”:[{“components”:[{“internalType”:“enum Market.ListingStatus”,“name”:“status”,“type”:“uint8”},{“internalType”:“address”,“name”:“seller”,“type”:“address”},{“internalType”:“address”,“name”:“token”,“type”:“address”},{“internalType”:“uint256”,“name”:“tokenId”,“type”:“uint256”},{“internalType”:“uint256”,“name”:“price”,“type”:“uint256”}],“internalType”:“struct Market.Listing”,“name”:"",“type”:“tuple”}],“stateMutability”:“view”,“type”:“function”},{“inputs”:[{“internalType”:“address”,“name”:“token”,“type”:“address”},{“internalType”:“uint256”,“name”:“tokenId”,“type”:“uint256”},{“internalType”:“uint256”,“name”:“price”,“type”:“uint256”}],“name”:“listToken”,“outputs”:[],“stateMutability”:“nonpayable”,“type”:“function”}]’,
params: { listingId: 2 }
}

you can see here how to post code on forum:

I can not copy this easily and test it, it could be easier if it would be formatted as code.

const options = {chain: 'polygon',
 address: '0x195642d09A00574daC97B77C9Bf1E85790C4E2C6',
 function_name: 'getListing',
 abi: '[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"listingId","type":"uint256"},{"indexed":false,"internalType":"address","name":"seller","type":"address"}],"name":"Cancel","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"listingId","type":"uint256"},{"indexed":false,"internalType":"address","name":"seller","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"price","type":"uint256"}],"name":"Listed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"listingId","type":"uint256"},{"indexed":false,"internalType":"address","name":"buyer","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"price","type":"uint256"}],"name":"Sale","type":"event"},{"inputs":[{"internalType":"uint256","name":"listingId","type":"uint256"}],"name":"buyToken","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"listingId","type":"uint256"}],"name":"cancel","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"listingId","type":"uint256"}],"name":"getListing","outputs":[{"components":[{"internalType":"enum Market.ListingStatus","name":"status","type":"uint8"},{"internalType":"address","name":"seller","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"struct Market.Listing","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"price","type":"uint256"}],"name":"listToken","outputs":[],"stateMutability":"nonpayable","type":"function"}]',
 params: { listingId: 2 }
}            

const list = await Moralis.Web3API.native.runContractFunction(options);
console.log(list);                        

https://lves6ea06v2a.usemoralis.com:2053/server

1 Like

turns out that REST API directly works fine:

curl -X 'POST' \
  'https://deep-index.moralis.io/api/v2/0x195642d09A00574daC97B77C9Bf1E85790C4E2C6/function?chain=polygon&function_name=getListing' \
  -H 'accept: application/json' \
  -H 'X-API-Key: 4234324' \
  -H 'Content-Type: application/json' \
  -d '{
  "abi": [{"inputs":[{"internalType":"uint256","name":"listingId","type":"uint256"}],"name":"getListing","outputs":[{"components":[{"internalType":"enum Market.ListingStatus","name":"status","type":"uint8"},{"internalType":"address","name":"seller","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"struct Market.Listing","name":"","type":"tuple"}],"stateMutability":"view","type":"function"}],
  "params": { "listingId": 2}
}'

=>

[
  "0",
  "0xB97d6aCCd9Bd5dFD50B9273d80fa0d4302429bEF",
  "0xE59d02ADDe7ac904277fa3c23604e2bDAAe1ce24",
  "6",
  "100"
]

so I should use that instead? But should runContract also work?

it looks like you have to use this syntax:

options = {chain: 'polygon',
 address: '0x195642d09A00574daC97B77C9Bf1E85790C4E2C6',
 function_name: 'getListing',
 abi: [{"inputs":[{"internalType":"uint256","name":"listingId","type":"uint256"}],"name":"getListing","outputs":[{"components":[{"internalType":"enum Market.ListingStatus","name":"status","type":"uint8"},{"internalType":"address","name":"seller","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"struct Market.Listing","name":"","type":"tuple"}],"stateMutability":"view","type":"function"}],
 "params": { "listingId": 2 }
}     
1 Like

Yes, that worked, thanks. Is REST , a better choice, given that now both works?

it depends on your use case, if you have your own server, you can also use REST directly

The mistake you did there is wrapping your abi in quotes making it a string which isn’t a proper syntax for the options.

As @cryptokid mentioned, depends on your case. You need to be careful with the syntax