From 470c2011f1b045ed5427dd2084896759d0727a60 Mon Sep 17 00:00:00 2001 From: raviycoder <135521192+raviycoder@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:10:55 +0530 Subject: [PATCH] first commit --- scripts/ethOperator.min.js | 239 ++++++++++++++++++++++++++++++++++++- 1 file changed, 238 insertions(+), 1 deletion(-) diff --git a/scripts/ethOperator.min.js b/scripts/ethOperator.min.js index 378c16c..50e78c5 100644 --- a/scripts/ethOperator.min.js +++ b/scripts/ethOperator.min.js @@ -1 +1,238 @@ -!function(EXPORTS){if(!window.ethers)return console.error("ethers.js not found");const ethOperator=EXPORTS,isValidAddress=ethOperator.isValidAddress=address=>{try{const isValidChecksum=ethers.utils.isAddress(address),isValidNonChecksum=ethers.utils.getAddress(address)===address.toLowerCase();return isValidChecksum||isValidNonChecksum}catch(error){return!1}},ERC20ABI=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}],CONTRACT_ADDRESSES={usdc:"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",usdt:"0xdac17f958d2ee523a2206206994597c13d831ec7"};function getProvider(){return window.ethereum?new ethers.providers.Web3Provider(window.ethereum):new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/6e12fee52bdd48208f0d82fb345bcb3c")}ethOperator.getBalance=async address=>{try{if(!address||!isValidAddress(address))return new Error("Invalid address");const provider=getProvider(),balanceWei=await provider.getBalance(address);return parseFloat(ethers.utils.formatEther(balanceWei))}catch(error){return console.error("Error:",error.message),error}},ethOperator.getTokenBalance=async(address,token,{contractAddress:contractAddress}={})=>{try{if(!token)return new Error("Token not specified");if(!CONTRACT_ADDRESSES[token]&&contractAddress)return new Error("Contract address of token not available");const usdcContract=new ethers.Contract(CONTRACT_ADDRESSES[token]||contractAddress,ERC20ABI,getProvider());let balance=await usdcContract.balanceOf(address);return balance=parseFloat(ethers.utils.formatUnits(balance,6)),balance}catch(e){console.error(e)}};const estimateGas=ethOperator.estimateGas=async({privateKey:privateKey,receiver:receiver,amount:amount})=>{try{const provider=getProvider(),signer=new ethers.Wallet(privateKey,provider);return provider.estimateGas({from:signer.address,to:receiver,value:ethers.utils.parseUnits(amount,"ether")})}catch(e){throw new Error(e)}};ethOperator.sendTransaction=async({privateKey:privateKey,receiver:receiver,amount:amount})=>{try{const provider=getProvider(),signer=new ethers.Wallet(privateKey,provider),limit=await estimateGas({privateKey:privateKey,receiver:receiver,amount:amount});return signer.sendTransaction({to:receiver,value:ethers.utils.parseUnits(amount,"ether"),gasLimit:limit,nonce:signer.getTransactionCount(),maxPriorityFeePerGas:ethers.utils.parseUnits("2","gwei")})}catch(e){throw new Error(e)}},ethOperator.sendToken=async({token:token,privateKey:privateKey,amount:amount,receiver:receiver,contractAddress:contractAddress})=>{const wallet=new ethers.Wallet(privateKey,getProvider()),tokenContract=new ethers.Contract(CONTRACT_ADDRESSES[token]||contractAddress,ERC20ABI,wallet),amountWei=ethers.utils.parseUnits(amount.toString(),6);return tokenContract.transfer(receiver,amountWei)}}("object"==typeof module?module.exports:window.ethOperator={}); \ No newline at end of file +!(function (EXPORTS) { + if (!window.ethers) return console.error("ethers.js not found"); + const ethOperator = EXPORTS, + isValidAddress = (ethOperator.isValidAddress = (address) => { + try { + const isValidChecksum = ethers.utils.isAddress(address), + isValidNonChecksum = + ethers.utils.getAddress(address) === address.toLowerCase(); + return isValidChecksum || isValidNonChecksum; + } catch (error) { + return !1; + } + }), + ERC20ABI = [ + { + constant: !0, + inputs: [], + name: "name", + outputs: [{ name: "", type: "string" }], + payable: !1, + stateMutability: "view", + type: "function", + }, + { + constant: !1, + inputs: [ + { name: "_spender", type: "address" }, + { name: "_value", type: "uint256" }, + ], + name: "approve", + outputs: [{ name: "", type: "bool" }], + payable: !1, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: !0, + inputs: [], + name: "totalSupply", + outputs: [{ name: "", type: "uint256" }], + payable: !1, + stateMutability: "view", + type: "function", + }, + { + constant: !1, + inputs: [ + { name: "_from", type: "address" }, + { name: "_to", type: "address" }, + { name: "_value", type: "uint256" }, + ], + name: "transferFrom", + outputs: [{ name: "", type: "bool" }], + payable: !1, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: !0, + inputs: [], + name: "decimals", + outputs: [{ name: "", type: "uint8" }], + payable: !1, + stateMutability: "view", + type: "function", + }, + { + constant: !0, + inputs: [{ name: "_owner", type: "address" }], + name: "balanceOf", + outputs: [{ name: "balance", type: "uint256" }], + payable: !1, + stateMutability: "view", + type: "function", + }, + { + constant: !0, + inputs: [], + name: "symbol", + outputs: [{ name: "", type: "string" }], + payable: !1, + stateMutability: "view", + type: "function", + }, + { + constant: !1, + inputs: [ + { name: "_to", type: "address" }, + { name: "_value", type: "uint256" }, + ], + name: "transfer", + outputs: [{ name: "", type: "bool" }], + payable: !1, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: !0, + inputs: [ + { name: "_owner", type: "address" }, + { name: "_spender", type: "address" }, + ], + name: "allowance", + outputs: [{ name: "", type: "uint256" }], + payable: !1, + stateMutability: "view", + type: "function", + }, + { payable: !0, stateMutability: "payable", type: "fallback" }, + { + anonymous: !1, + inputs: [ + { indexed: !0, name: "owner", type: "address" }, + { indexed: !0, name: "spender", type: "address" }, + { indexed: !1, name: "value", type: "uint256" }, + ], + name: "Approval", + type: "event", + }, + { + anonymous: !1, + inputs: [ + { indexed: !0, name: "from", type: "address" }, + { indexed: !0, name: "to", type: "address" }, + { indexed: !1, name: "value", type: "uint256" }, + ], + name: "Transfer", + type: "event", + }, + ], + CONTRACT_ADDRESSES = { + usdc: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", + usdt: "0xdac17f958d2ee523a2206206994597c13d831ec7", + }; + function getProvider() { + const bscMainnet = { + chainId: 56, + name: 'binance', + rpc: 'https://bsc-dataseed.binance.org/', + explorer: 'https://bscscan.com' + }; + return window.ethereum + ? new ethers.providers.Web3Provider(window.ethereum) + : new ethers.providers.JsonRpcProvider( + bscMainnet.rpc, bscMainnet + ); + } + (ethOperator.getBalance = async (address) => { + try { + if (!address || !isValidAddress(address)) + return new Error("Invalid address"); + const provider = getProvider(), + balanceWei = await provider.getBalance(address); + return parseFloat(ethers.utils.formatEther(balanceWei)); + } catch (error) { + return console.error("Error:", error.message), error; + } + }), + (ethOperator.getTokenBalance = async ( + address, + token, + { contractAddress: contractAddress } = {} + ) => { + try { + if (!token) return new Error("Token not specified"); + if (!CONTRACT_ADDRESSES[token] && contractAddress) + return new Error("Contract address of token not available"); + const usdcContract = new ethers.Contract( + CONTRACT_ADDRESSES[token] || contractAddress, + ERC20ABI, + getProvider() + ); + let balance = await usdcContract.balanceOf(address); + return ( + (balance = parseFloat(ethers.utils.formatUnits(balance, 6))), balance + ); + } catch (e) { + console.error(e); + } + }); + const estimateGas = (ethOperator.estimateGas = async ({ + privateKey: privateKey, + receiver: receiver, + amount: amount, + }) => { + try { + const provider = getProvider(), + signer = new ethers.Wallet(privateKey, provider); + return provider.estimateGas({ + from: signer.address, + to: receiver, + value: ethers.utils.parseUnits(amount, "ether"), + }); + } catch (e) { + throw new Error(e); + } + }); + (ethOperator.sendTransaction = async ({ + privateKey: privateKey, + receiver: receiver, + amount: amount, + }) => { + try { + const provider = getProvider(), + signer = new ethers.Wallet(privateKey, provider), + limit = await estimateGas({ + privateKey: privateKey, + receiver: receiver, + amount: amount, + }); + return signer.sendTransaction({ + to: receiver, + value: ethers.utils.parseUnits(amount, "ether"), + gasLimit: limit, + nonce: signer.getTransactionCount(), + maxPriorityFeePerGas: ethers.utils.parseUnits("2", "gwei"), + }); + } catch (e) { + throw new Error(e); + } + }), + (ethOperator.sendToken = async ({ + token: token, + privateKey: privateKey, + amount: amount, + receiver: receiver, + contractAddress: contractAddress, + }) => { + const wallet = new ethers.Wallet(privateKey, getProvider()), + tokenContract = new ethers.Contract( + CONTRACT_ADDRESSES[token] || contractAddress, + ERC20ABI, + wallet + ), + amountWei = ethers.utils.parseUnits(amount.toString(), 6); + return tokenContract.transfer(receiver, amountWei); + }); +})("object" == typeof module ? module.exports : (window.ethOperator = {}));