How to call a contract from back-end?

Hi there, wondering about the best way to call a smart-contract setter function from a backend.

Basically, Iā€™ve got a react front-end, and a smart-contract on the BSC, that I must keep up to date with some data from Ethereum (NFT ownership). The simplest way I found is to listen for a transfer event on Ethereum, and when that happens, to update the NFT owner in my contract on the BSC (with a simple setter function). But I donā€™t know how to sign the TX from my react app since itā€™s not like I have any user connected to metamask. I would like this process to be automatic.

event listener: if event triggered => setNewValue in contract (keep the contract up to date)

Seems like I must set a Node/Express backend in my react app that will use my private key to sign the transactions, but donā€™t really have a clue about where to start!

Any help appreciated!

You can try the syntax from this thread and to use Moralis.executeFunction

If it doesnā€™t work you will have to use web3 or ethers directly

1 Like

I did have an idea for this but I see now youā€™re looking at cross-chain compared to just the one chain. Not sure now. Maybe you can use multiple providers in ethers.js/web3js. It sounds like youā€™ve been able to do this manually?

Otherwise you may have to look at using an oracle like Chainlink for this. So your contract gets info from the oracle about that contract on another chain.

1 Like

Thanks for the suggestion @cryptokid. Using web3 or ether.js isnā€™t an issue. Iā€™m just not sure how to set the server up, access the private key in a secure way (environment var on AWS?), and then communicate either with my react app, or Moralis Cloud. Iā€™ll try your code out once Iā€™m done setting the node/express server up. Wonā€™t take too long hopefully ahah!

Hey @alex, yeah this cross-chain thing is givin me headaches! Would be much easier otherwise. For now, (thanks to Moralis web API), I can quickly fetch all NFT owners on ETH and add this to the Moralis DB. But then, I need to keep track of NFT ownership (in case of transfer), and to update my smart-contract accordingly.
Did look at Chainlink too, but didnā€™t seem much easier. Will try this Node/express server first.

So look at getting a basic app running. Express ā€œHello Worldā€ example (expressjs.com)

You can install nodemon to run your server file so it will reload when you make changes.

Donā€™t worry about deployment details, private keys, etc. until you get to such stages.

One theoretical way, maybe it can work. You would change out the parameters, methods, events based on the contracts youā€™re using.

const express = require('express')
const app = express()
const port = 3001

const ethers = require('ethers')

// set provider for each chain
const ethprovider = new ethers.providers.JsonRpcProvider("https://speedy-nodes-nyc.moralis.io/***/eth/mainnet");
const polygonprovider = new ethers.providers.JsonRpcProvider("https://speedy-nodes-nyc.moralis.io/***/polygon/mainnet");

// signer for your transactions 
const signer = new ethers.Wallet(privateKey, polygonprovider)

const ABI = require('./abi.json')
const wethabi = require('./weth.json')

// PolygonPunks Polygon
const nftContract = new ethers.Contract("0x9498274b8c82b4a3127d67839f2127f2ae9753f4", ABI, signer)

// wETH Ethereum
const ethContract = new ethers.Contract("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", wethabi, ethprovider)

ethContract.on("Transfer", (src, dst, wad) => {
    set(dst)
})

async function set(dst) {
    const tx = await nftContract.set(dst);
    await tx.wait()
}

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
1 Like

Wow, amazing @alex! Just got a back-end up and running, have added nodemon as suggested too (nice addition by the way!). And even managed some basic get request to communicate with react app. Now weā€™re talking haha!

1 Like