Get the EthersJs library in nodejs

Hello.

Please provide me with correct code for EthersJs library.

Am I right with this example:

await Moralis.start({moralisSecret});
const ethers = Moralis.web3Library;
const daiAddress = "dai.tokens.ethers.eth";
const daiAbi = [
    "function name() view returns (string)",
    "function symbol() view returns (string)",
    "function balanceOf(address) view returns (uint)",
    "function transfer(address to, uint amount)",
    "event Transfer(address indexed from, address indexed to, uint amount)",
];
const daiContract = new ethers.Contract(daiAddress, daiAbi);

or I need use:

const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);

What is provider in this case?

Or provider in this case (not common):

const provider = await Moralis.enableWeb3();

and correct code is:

await Moralis.start({moralisSecret});
const provider = await Moralis.enableWeb3();
const ethers = Moralis.web3Library;
const daiAddress = "dai.tokens.ethers.eth";
const daiAbi = [
    "function name() view returns (string)",
    "function symbol() view returns (string)",
    "function balanceOf(address) view returns (uint)",
    "function transfer(address to, uint amount)",
    "event Transfer(address indexed from, address indexed to, uint amount)",
];
const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);

Thanks.

I know how to do it in cloud code, in case that it helps:

https://docs.moralis.io/moralis-dapp/cloud-code/cloud-functions#example-of-how-to-use-a-custom-rpc-url

Thanks. But I don’t understand anything … Why did u give a response that doesn’t apply to my question? I didn’t ask about RPC, I asked about a specific example, which is specified in your documentation: https://docs.moralis.io/muralis-dapp/web3/web3#get-the-ethersjs-library. I decided to clarify the inaccuracies that there is in it.

Thanks.

you want that server side, in node js?

can you use directly ethers in nodejs?

you can look at this example on how to use enableWeb3 in nodejs:

Yes. I want to use example and modify it.

No. In this case I need provider.

Thanks. But this example not helps me and not answered for my question (((

can you rephrase the question (probably I didn’t understand it)?

what you mean by the fact that you need provider?
being in nodejs you can use ethers or web3 directly with a speedy node RCP url

I need EthersJs from Moralis. Ie for:

new ethers.Contract

I mean that in example https://docs.moralis.io/muralis-dapp/web3/web3#get-the-ethersjs-library I saw this:

const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);

and my question is:

What is provider in this case?

Then I ask about code which I took from example and change it … That’s all.

there is a difference on how to use it on nodejs compared to how to use it in a browser.

you may need to read more about how providers work in ethers:
https://docs.ethers.io/v5/api/providers/
https://docs.ethers.io/v5/api/providers/jsonrpc-provider/

I know about it. That’s why I am asked about it.

In diff way I use code like this:

const web3Provider = new Web3.providers.WebsocketProvider(sProviderUrl, arProviderOptions);
const web3 = new Web3(web3Provider);
const oContract = new web3.eth.Contract(sDefaultContractAbi, sTokenContractAddress);

Everything is clear here)

And then I open this link https://docs.moralis.io/moralis-dapp/web3/web3#web3.js and see:

import Web3 from "web3"; // Only when using npm/yarn
// Enable web3 and get the initialized web3 instance from Web3.js
await Moralis.enableWeb3();
const web3Js = new Web3(Moralis.provider);

and below (https://docs.moralis.io/muralis-dapp/web3/web3#get-the-ethersjs-library) I see:

await Moralis.start({moralisSecret});
const ethers = Moralis.web3Library;
const daiAddress = "dai.tokens.ethers.eth";
const daiAbi = [
    "function name() view returns (string)",
    "function symbol() view returns (string)",
    "function balanceOf(address) view returns (uint)",
    "function transfer(address to, uint amount)",
    "event Transfer(address indexed from, address indexed to, uint amount)",
];
const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);

So I am asked:

What is provider in this case?

Is it:

Moralis.provider

from above (https://docs.moralis.io/moralis-dapp/web3/web3#web3.js) or some else?

Or can I use:

const daiContract = new ethers.Contract(daiAddress, daiAbi);

instead of:

const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);

Is this clear for now?

Thanks.

I don’t know the exact answers now

this may be something from the source code that could help you:
https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/9455b3e72cf66bb9c12b719dfa59f8bd94207087/src/InternalWeb3Provider.js

If you use moralisSecret in nodejs for Moralis.start then it will use your speedy node url

for the question with provider in

it seems to be a generic provider variable that you can replace with something that you have as provider

The point is that I didn’t want to change anything ) I want to use only Moralis service. That’s why I am asking which way will be correct with example from https://docs.moralis.io/muralis-dapp/web3/web3#get-the-ethersjs-library.

Thanks.

1 Like

this seems to work:

x = async () => {
    const  Moralis = require('moralis/node')
    console.log(Moralis.CoreManager.get("VERSION"))
    await Moralis.start({ moralisSecret: "asfasdf" });

    await Moralis.enableWeb3({
        chainId: 0x1,
        //privateKey:
        //   "private_key",
    });

    const ethers = Moralis.web3Library;

    const daiAddress = "dai.tokens.ethers.eth";
    const daiAbi = [
      "function name() view returns (string)",
      "function symbol() view returns (string)",
      "function balanceOf(address) view returns (uint)",
      "function transfer(address to, uint amount)",
      "event Transfer(address indexed from, address indexed to, uint amount)",
    ];

    customHttpProvider = new ethers.providers.JsonRpcProvider(Moralis.provider)

    customHttpProvider.getBlockNumber().then((result) => {
        console.log("Current block number: " + result);
    });

    const daiContract = new ethers.Contract(daiAddress, daiAbi, customHttpProvider);

    const name = await daiContract.name();
    console.log(name);
    // 'Dai Stablecoin'
}

x();

1 Like