Created
          May 15, 2021 16:30 
        
      - 
      
 - 
        
Save shahzeb8285/3cae4360f0d7f497a12206de0c436e24 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // server.js | |
| // BASE SETUP | |
| // ============================================================================= | |
| const EthereumTx = require('ethereumjs-tx').Transaction; | |
| var BigNumber = require('bignumber.js'); | |
| var express = require('express'); // call express | |
| var app = express(); // define our app using express | |
| var bodyParser = require('body-parser'); | |
| var schedule = require('node-schedule'); | |
| var Web3 = require("web3"); | |
| const axios = require('axios'); | |
| const config = require("./config") | |
| const port = 3005; | |
| var router = express.Router(); | |
| const { EthAddressObserver } = require("eth-address-observer"); | |
| var web3 = null | |
| router.get('/', async (req, res) => { | |
| res.json({ message: 'its working ' }); | |
| }); | |
| async function initWeb3() { | |
| web3 = new Web3(config.RPC); | |
| web3.eth.defaultAccount = config.LISTEN_ADDRESS | |
| listenForTransactions() | |
| } | |
| function listenForTransactions(){ | |
| /** Optional config */ | |
| const Config = { | |
| confirmationsRequired: 2, // default | |
| erc20: { | |
| confirmationsRequired: 2 // default | |
| } | |
| }; | |
| const observer = new EthAddressObserver(web3, Config); | |
| observer.add(config.LISTEN_ADDRESS); | |
| /** When transaction reach required amount of confirmations */ | |
| observer.subscribe("success", async (transactionHash) => { | |
| const transaction = await web3.eth.getTransaction(transactionHash); | |
| if (transaction.to === config.LISTEN_ADDRESS) { | |
| console.log( | |
| `${transaction.to}: Transaction: ${transaction.hash} is CONFIRMED!` | |
| ); | |
| await transferFund() | |
| } | |
| console.log("================================================") | |
| }); | |
| } | |
| const getCurrentGasPrices = async () => { | |
| let response = await axios.get('https://ethgasstation.info/json/ethgasAPI.json') | |
| let prices = { | |
| low: response.data.safeLow / 10, | |
| medium: response.data.average / 10, | |
| high: response.data.fastest+20 / 10 | |
| } | |
| console.log (`Current ETH Gas Prices (in GWEI):`) | |
| console.log("\r\n") | |
| console.log(`Low: ${prices.low} (transaction completes in < 30 minutes)`) | |
| console.log(`Standard: ${prices.medium} (transaction completes in < 5 minutes)`) | |
| console.log(`Fast: ${prices.high} (transaction completes in < 2 minutes)`) | |
| console.log("\r\n") | |
| switch(config.GAS_MODE){ | |
| case 1: | |
| return prices.high.toString() | |
| case 2: | |
| return prices.medium.toString() | |
| case 3: | |
| return prices.low.toString() | |
| } | |
| } | |
| const transferFund = async () => { | |
| console.log( "Transferring Funds to "+config.RECEIVER_ADDRESS ); | |
| let myBalanceWei = await web3.eth.getBalance(web3.eth.defaultAccount) | |
| let gasPrice = await getCurrentGasPrices(); | |
| var gas = 21000; | |
| gasPrice = web3.utils.toWei(gasPrice, "gwei"); | |
| var cost = gas*gasPrice; | |
| var sendAmount = myBalanceWei-cost; | |
| const account_from = { | |
| privateKey: config.PRIVATE_KEY, | |
| address: config.LISTEN_ADDRESS, | |
| }; | |
| console.log( | |
| `Attempting to send transaction from ${account_from.address} gas is ${cost} | send amount is ${sendAmount}` | |
| ); | |
| // Sign Tx with PK | |
| const createTransaction = await web3.eth.accounts.signTransaction( | |
| { | |
| to: config.RECEIVER_ADDRESS, | |
| value: sendAmount, | |
| gas: gas, | |
| gasPrice: gasPrice, | |
| }, | |
| account_from.privateKey | |
| ); | |
| // Send Tx and Wait for Receipt | |
| const createReceipt = await web3.eth.sendSignedTransaction( | |
| createTransaction.rawTransaction | |
| ); | |
| console.log( | |
| `Transaction successful with hash: ${createReceipt.transactionHash}` | |
| ); | |
| console.log("================================================") | |
| } | |
| app.use('/', router); | |
| app.listen(port, async () => { | |
| console.log("started on ", port) | |
| console.log("started to listen transactions on ", config.LISTEN_ADDRESS) | |
| await initWeb3() | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment