[SOLVED] Run contract function

Hello.

I am try to use this code with NodeJs:

import Moralis from 'moralis/node.js';
import { EvmChain } from '@moralisweb3/evm-utils';

import oConfig from '../config/index.js';
import { sv, empty, addDebugData, ucfirst, writeToLog, getDateTimeDiff } from '../lib/helpers.js';

const sTokenPairContractAddress = '0xAC6C2a9657Dc4192d85179876E47cd1DE159f616';
const sStreamKey = oConfig.services.moralis.keys.sStreamKey;
const abi = oConfig.oPancakeSwap.oABI.sDefaultContract;
const functionName = 'token0';
const address = sTokenPairContractAddress;
const chain = EvmChain.BSC;

await Moralis.start({apiKey: sStreamKey});

const oResponse = await Moralis.EvmApi.utils.runContractFunction(
{
	abi,
	functionName,
	address,
	chain
});

console.log(' ');
console.log('runContractFunction');
console.log(oResponse.result);
console.log(' ');

and got this error:

/.../@moralisweb3/core/lib/MoralisCoreProvider.js:10
            throw new Error_1.MoralisCoreError({
                  ^

MoralisError [Moralis SDK Core Error]: [C0008] Default instance of MoralisCore is not set
    at Function.MoralisCoreProvider.getDefault (/.../@moralisweb3/core/lib/MoralisCoreProvider.js:10:19)
    at Function.EvmChain.create (/.../@moralisweb3/evm-utils/lib/dataTypes/EvmChain/EvmChain.js:172:52)
    at Function.get (/.../@moralisweb3/evm-utils/lib/dataTypes/EvmChain/EvmChain.js:82:29)
    at file:///.../token-get-address-from-pair-address.js:12:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  isMoralisError: true,
  code: 'C0008',
  details: undefined,
  [cause]: undefined
}

How I can fix it?

package.json

	"dependencies": {
		...
		"@moralisweb3/core": "^2.7.4",
		"@moralisweb3/evm-utils": "^2.7.4",
		"moralis": "^2.6.7",
		...
	}

Thanks.

It could be a different syntax for using Moralis sdk based on what you are using. You can try to check the documentation for 2 different syntaxes for js and typescript.

I mean the other documentation with the tutorials

Which one? I don’t see another docs …

Another tab on those same docs page.

changed to:

import Moralis from 'moralis';
import { EvmChain } from '@moralisweb3/evm-utils';

import oConfig from '../config/index.js';
import { sv, empty, addDebugData, ucfirst, writeToLog, getDateTimeDiff } from '../lib/helpers.js';

const sTokenPairContractAddress = '0xAC6C2a9657Dc4192d85179876E47cd1DE159f616';
const moralisSecret = oConfig.services.moralis.keys.sSecretKey;
const sStreamKey = oConfig.services.moralis.keys.sStreamKey;
const abi = oConfig.oPancakeSwap.oABI.sDefaultContract;
const functionName = 'token0';
const address = sTokenPairContractAddress;
const chain = EvmChain.BSC;

const runContractFunction = async () => {
	await Moralis.start({apiKey: sStreamKey});

	const oResponse = await Moralis.EvmApi.utils.runContractFunction(
	{
		abi,
		functionName,
		address,
		chain
	});	

	console.log(' ');
	console.log('runContractFunction');
	console.log(oResponse.result);
	console.log(' ');
};

runContractFunction();

The same error.

What are you using to run the script? JavaScript, a module, typescript?

NodeJS vs Module type.

I don’t know what that means. You can try the examples from documentation to see if any of them works for you. For a module it should be a different syntax and the error should be different.

It means that in package.json:

{
    ...
    "type": "module",
    ...
}

and I run this script like:

node /path/to/script.js

I already do it - sent to you screenshot above where I choose example for NodeJS.

The same which in my example above.

That didn’t look like the nodejs

example[quote=“tcolonel, post:7, topic:21186”]
import Moralis from 'moralis';
[/quote]

This looks like typescript

There is another forum post with all the possible versions of importing and using the sdk. There are 3 of them.

https://nodejs.org/api/esm.html#import-expressions

Where? Can u send me the links?

Here it is: Cannot find package '@moralis/sdk'

Didn’t help like this one - [SOLVED] Streams API get Error : Default instance of MoralisCore is not set.

So can you ask support team or give the email address for solve this problem? I have a Business plan if that matter.

Thanks.

You need to do something similar to this (Moralis.default) again in your environment.

If you have, can you post your current code and current error.

bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)

You can ignore this, your script should still work.

1 Like