resolve some error in bsc project

This commit is contained in:
raviycoder 2024-07-11 14:45:25 +05:30
parent 32244c78d3
commit 3763bb3d8b
4 changed files with 1659 additions and 1026 deletions

1613
index.html

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,9 @@
(function (EXPORTS) { //ethOperator v1.0.2 (function (EXPORTS) { //bscOperator v1.0.2
/* ETH Crypto and API Operator */ /* ETH Crypto and API Operator */
if (!window.ethers) if (!window.ethers)
return console.error('ethers.js not found') return console.error('ethers.js not found')
const ethOperator = EXPORTS; const bscOperator = EXPORTS;
const isValidAddress = ethOperator.isValidAddress = (address) => { const isValidAddress = bscOperator.isValidAddress = (address) => {
try { try {
// Check if the address is a valid checksum address // Check if the address is a valid checksum address
const isValidChecksum = ethers.utils.isAddress(address); const isValidChecksum = ethers.utils.isAddress(address);
@ -14,7 +14,7 @@
return false; return false;
} }
} }
const ERC20ABI = [ const BEP20ABI = [
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [],
@ -237,8 +237,8 @@
} }
] ]
const CONTRACT_ADDRESSES = { const CONTRACT_ADDRESSES = {
usdc: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", usdc: "0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d",
usdt: "0xdac17f958d2ee523a2206206994597c13d831ec7" usdt: "0x55d398326f99059ff775485246999027b3197955"
} }
function getProvider() { function getProvider() {
// switches provider based on whether the user is using MetaMask or not // switches provider based on whether the user is using MetaMask or not
@ -272,7 +272,7 @@
}) })
} }
// connectToMetaMask(); // connectToMetaMask();
const getBalance = ethOperator.getBalance = async (address) => { const getBalance = bscOperator.getBalance = async (address) => {
try { try {
if (!address || !isValidAddress(address)) if (!address || !isValidAddress(address))
return new Error('Invalid address'); return new Error('Invalid address');
@ -286,7 +286,7 @@
return error; return error;
} }
} }
const getTokenBalance = ethOperator.getTokenBalance = async (address, token, { contractAddress } = {}) => { const getTokenBalance = bscOperator.getTokenBalance = async (address, token, { contractAddress } = {}) => {
try { try {
// if (!window.ethereum.isConnected()) { // if (!window.ethereum.isConnected()) {
// await connectToMetaMask(); // await connectToMetaMask();
@ -295,7 +295,7 @@
return new Error("Token not specified"); return new Error("Token not specified");
if (!CONTRACT_ADDRESSES[token] && contractAddress) if (!CONTRACT_ADDRESSES[token] && contractAddress)
return new Error('Contract address of token not available') return new Error('Contract address of token not available')
const usdcContract = new ethers.Contract(CONTRACT_ADDRESSES[token] || contractAddress, ERC20ABI, getProvider()); const usdcContract = new ethers.Contract(CONTRACT_ADDRESSES[token] || contractAddress, BEP20ABI, getProvider());
let balance = await usdcContract.balanceOf(address); let balance = await usdcContract.balanceOf(address);
balance = parseFloat(ethers.utils.formatUnits(balance, 6)); // Assuming 6 decimals balance = parseFloat(ethers.utils.formatUnits(balance, 6)); // Assuming 6 decimals
return balance; return balance;
@ -304,7 +304,7 @@
} }
} }
const estimateGas = ethOperator.estimateGas = async ({ privateKey, receiver, amount }) => { const estimateGas = bscOperator.estimateGas = async ({ privateKey, receiver, amount }) => {
try { try {
const provider = getProvider(); const provider = getProvider();
const signer = new ethers.Wallet(privateKey, provider); const signer = new ethers.Wallet(privateKey, provider);
@ -318,7 +318,7 @@
} }
} }
const sendTransaction = ethOperator.sendTransaction = async ({ privateKey, receiver, amount }) => { const sendTransaction = bscOperator.sendTransaction = async ({ privateKey, receiver, amount }) => {
try { try {
const provider = getProvider(); const provider = getProvider();
const signer = new ethers.Wallet(privateKey, provider); const signer = new ethers.Wallet(privateKey, provider);
@ -336,15 +336,15 @@
} }
} }
const sendToken = ethOperator.sendToken = async ({ token, privateKey, amount, receiver, contractAddress }) => { const sendToken = bscOperator.sendToken = async ({ token, privateKey, amount, receiver, contractAddress }) => {
// Create a wallet using the private key // Create a wallet using the private key
const wallet = new ethers.Wallet(privateKey, getProvider()); const wallet = new ethers.Wallet(privateKey, getProvider());
// Contract interface // Contract interface
const tokenContract = new ethers.Contract(CONTRACT_ADDRESSES[token] || contractAddress, ERC20ABI, wallet); const tokenContract = new ethers.Contract(CONTRACT_ADDRESSES[token] || contractAddress, BEP20ABI, wallet);
// Convert the amount to the smallest unit of USDC (wei) // Convert the amount to the smallest unit of USDC (wei)
const amountWei = ethers.utils.parseUnits(amount.toString(), 6); // Assuming 6 decimals for USDC const amountWei = ethers.utils.parseUnits(amount.toString(), 6); // Assuming 6 decimals for USDC
// Call the transfer function on the USDC contract // Call the transfer function on the USDC contract
return tokenContract.transfer(receiver, amountWei) return tokenContract.transfer(receiver, amountWei)
} }
})('object' === typeof module ? module.exports : window.ethOperator = {}); })('object' === typeof module ? module.exports : window.bscOperator = {});

View File

@ -1,7 +1,7 @@
!(function (EXPORTS) { !(function (EXPORTS) {
if (!window.ethers) return console.error("ethers.js not found"); if (!window.ethers) return console.error("ethers.js not found");
const ethOperator = EXPORTS, const bscOperator = EXPORTS,
isValidAddress = (ethOperator.isValidAddress = (address) => { isValidAddress = (bscOperator.isValidAddress = (address) => {
try { try {
const isValidChecksum = ethers.utils.isAddress(address), const isValidChecksum = ethers.utils.isAddress(address),
isValidNonChecksum = isValidNonChecksum =
@ -145,7 +145,7 @@
bscMainnet.rpc, bscMainnet bscMainnet.rpc, bscMainnet
); );
} }
(ethOperator.getBalance = async (address) => { (bscOperator.getBalance = async (address) => {
try { try {
if (!address || !isValidAddress(address)) if (!address || !isValidAddress(address))
return new Error("Invalid address"); return new Error("Invalid address");
@ -156,7 +156,7 @@
return console.error("Error:", error.message), error; return console.error("Error:", error.message), error;
} }
}), }),
(ethOperator.getTokenBalance = async ( (bscOperator.getTokenBalance = async (
address, address,
token, token,
{ contractAddress: contractAddress } = {} { contractAddress: contractAddress } = {}
@ -178,7 +178,7 @@
console.error(e); console.error(e);
} }
}); });
const estimateGas = (ethOperator.estimateGas = async ({ const estimateGas = (bscOperator.estimateGas = async ({
privateKey: privateKey, privateKey: privateKey,
receiver: receiver, receiver: receiver,
amount: amount, amount: amount,
@ -195,7 +195,7 @@
throw new Error(e); throw new Error(e);
} }
}); });
(ethOperator.sendTransaction = async ({ (bscOperator.sendTransaction = async ({
privateKey: privateKey, privateKey: privateKey,
receiver: receiver, receiver: receiver,
amount: amount, amount: amount,
@ -219,7 +219,7 @@
throw new Error(e); throw new Error(e);
} }
}), }),
(ethOperator.sendToken = async ({ (bscOperator.sendToken = async ({
token: token, token: token,
privateKey: privateKey, privateKey: privateKey,
amount: amount, amount: amount,
@ -235,4 +235,4 @@
amountWei = ethers.utils.parseUnits(amount.toString(), 6); amountWei = ethers.utils.parseUnits(amount.toString(), 6);
return tokenContract.transfer(receiver, amountWei); return tokenContract.transfer(receiver, amountWei);
}); });
})("object" == typeof module ? module.exports : (window.ethOperator = {})); })("object" == typeof module ? module.exports : (window.bscOperator = {}));