Skip to content

Instantly share code, notes, and snippets.

@nCally
Last active August 17, 2023 08:25
Show Gist options
  • Select an option

  • Save nCally/0011ad05fbc9fb5b4ad757d932b5ae6a to your computer and use it in GitHub Desktop.

Select an option

Save nCally/0011ad05fbc9fb5b4ad757d932b5ae6a to your computer and use it in GitHub Desktop.
export const setupWeb3 = (networkId: number) => {
let provider = '';
if (networkId === 97) {
provider = 'https://data-seed-prebsc-1-s1.binance.org:8545/';
}
if (networkId === 56) {
provider = 'https://bsc-dataseed1.ninicoin.io';
}
if (networkId === 137) {
provider = 'https://polygon-rpc.com/';
}
// if (network === 'eth') {
// provider = 'https://mainnet.infura.io/v3/c9a2fe300dd9496f9ee19bc4cb2c4689';
// }
return new Web3(provider);
};
const web3 = setupWeb3(Number(blockchainNetworkId?.networkId));
const validAddress = web3.utils.isAddress(
destinationAddress,
Number(blockchainNetworkId?.networkId) || 56
);
if (!validAddress) {
return {
status: false,
message:
'The destination address seems not be a valid address. Please verify.',
};
}
const receipt = await web3.eth.getTransactionReceipt(hash);
if (!receipt) {
return {
status: false,
message: 'Transaction receipt does not exist',
};
}
if (receipt.status !== true) {
return {
status: false,
message:
'This is a failed transaction. Only successfully transactions are processed. Please contact support.',
};
}
// check toAddress
const ReceiverBn = web3.utils
.toBN(web3.utils.toHex(receipt.logs[0].topics[2]))
.toString();
const destinationBN = web3.utils
.toBN(destinationAddress.toString())
.toString();
if (ReceiverBn !== destinationBN) {
return {
status: false,
message: `inconsistent destination address ${ReceiverBn}---${destinationBN}`,
};
}
// check amount
const contractAddress = await ContractAddress.findOne({
blockchainNetworkId,
currencyId,
});
const amountSentInBlockchain = web3.utils.fromWei(
web3.utils.toBN(receipt.logs[0].data).toString(),
contractAddress?.decimals || 'ether'
);
if (Number(amountSentInBlockchain) + 5 < Number(amount)) {
return {
status: false,
message: `Amount Deposited in Blockchain is less: ${amountSentInBlockchain}>>>>${amount}`,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment