Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract events emitting on local server but not on vercel deplyed server (node.js) #4636

Open
umerfarooq777 opened this issue Mar 6, 2024 · 0 comments
Assignees
Labels
investigate Under investigation and may be a bug. v5 Issues regarding legacy-v5

Comments

@umerfarooq777
Copy link

umerfarooq777 commented Mar 6, 2024

Ethers Version

5.7.2

Search Terms

No response

Describe the Problem

I have created an ERC20 token with a function depositTokens which emits a "Trade" I'm listening to those events and running my task on that, all things are working fine on the local server but as I deploy on Vercel it doesn't listen to events or doing task on emission of event

Contract Code Snippet

event Trade( address indexed from, address indexed to, Mode indexed mode, uint256 amount, uint256 value, uint256 date, uint256 orderId );

I also tried the websocket

Server Code Snippet

`const express = require("express");
const cors = require("cors");
const ethers = require("ethers");
require('dotenv').config()

const { tokenAbi } = require("./utils/ABI");
const app = express();
const port = 5000;

app.use(cors());
app.use(express.json());
const _privateKey = String(process.env.PRIVATE_KEY);
const ethRpc = String(process.env.ETH_RPC_URL);
const ethRpcWs = String(process.env.ETH_RPC_URL_WS);
const maticRpc = String(process.env.MATIC_RPC_URL);
const maticRpcWs = String(process.env.MATIC_RPC_URL_WS);
const ethTokenAddress = String(process.env.ETH_TOKEN_ADDRESS);
const maticTokenAddress = String(process.env.MATIC_TOKEN_ADDRESS);

// console.log("DATA",{
// _privateKey,
// ethRpc,
// maticRpc,
// maticTokenAddress,
// ethTokenAddress
// })

app.get("/", async (req, res) => {
try {
res.status(200).json({ message: "Server is Live!" });
} catch (error) {
res.status(500).json(error);
}
});

const abi = tokenAbi; // Contract ABI

const ethTokenProviderWs = new ethers.providers.WebSocketProvider(ethRpcWs);
const ethTokenContractWs = new ethers.Contract(
ethTokenAddress,
abi,
ethTokenProviderWs
);

const ethTokenProvider = new ethers.providers.JsonRpcProvider(ethRpc);
const ethTokenWallet = new ethers.Wallet(_privateKey, ethTokenProvider);
const ethTokenContract = new ethers.Contract(
ethTokenAddress,
abi,
ethTokenWallet
);

const maticTokenProviderWs = new ethers.providers.WebSocketProvider(maticRpcWs);
const maticTokenContractWs = new ethers.Contract(
maticTokenAddress,
abi,
maticTokenProviderWs
);

const maticTokenProvider = new ethers.providers.JsonRpcProvider(maticRpc);
const maticTokenWallet = new ethers.Wallet(_privateKey, maticTokenProvider);
const maticTokenContract = new ethers.Contract(
maticTokenAddress,
abi,
maticTokenWallet
);

const init = async () => {

ethTokenContractWs.on(
"Trade",
async (from, to, mode, amount,_value, date, orderId, Event) => {
if (mode?.toString() == "0") {
console.log("Event received: eth");
try {
const tx = await maticTokenContract.fullfillOrder(
to,
amount.toString(),
orderId.toString(),
{
value:_value?.toString()
}
);
console.log("Transaction Hash:", tx.hash);
await tx.wait(); // Wait for the transaction to be mined
console.log("Token minted successfully!",{
to,
amount:amount.toString(),
orderId:orderId.toString(),
value:_value?.toString()
});
} catch (error) {
console.error("Error minting Token:", error);
}
}
}
);

maticTokenContractWs.on(
"Trade",
async (from, to, mode, amount,_value, date, orderId, Event) => {
if (mode?.toString() == "0") {
console.log("Event received: matic");
try {
const tx = await ethTokenContract.fullfillOrder(
to,
amount.toString(),
orderId.toString(),{
value:_value?.toString()
}
);
console.log("Transaction Hash:", tx.hash);
await tx.wait(); // Wait for the transaction to be mined
console.log("Token minted successfully!",{
to,
amount:amount.toString(),
orderId:orderId.toString(),
value:_value?.toString()
});
} catch (error) {
console.error("Error minting Token:", error);
}
}
}
);

}

init().catch((err) => {
console.log(err);
process.exit(1);
});

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

help me if I'm missing out on something. thanks.

@umerfarooq777 umerfarooq777 added investigate Under investigation and may be a bug. v5 Issues regarding legacy-v5 labels Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v5 Issues regarding legacy-v5
Projects
None yet
Development

No branches or pull requests

2 participants