Advertisement
lscofield

contract.js

Aug 11th, 2022 (edited)
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Web3 = require('web3')
  2. const { Random } = require('./decode')
  3. const types = require('./types')
  4. const chalk = require('chalk')
  5. const { config } = require('./getConfig')
  6.  
  7. const Web3Instance = async () => {
  8.     const rpc = Random(config.rpcURL)
  9.     console.log(chalk.cyan(`Call:::RPC Provider: ${rpc}`))
  10.  
  11.     const _web3 = await new Web3(rpc)
  12.     _web3.eth.accounts.wallet.clear()
  13.     _web3.eth.accounts.wallet.add(config.secretPhrase, config.userAddress)
  14.     _web3.eth.defaultAccount = config.userAddress
  15.  
  16.     return _web3
  17. }
  18.  
  19. const TokenDecimals = async (tokenAddress) => {
  20.     const token = (await Instance(types.TOKEN, tokenAddress)).methods
  21.     return parseInt((await token.decimals().call()))
  22. }
  23.  
  24. const Instance = async (contractType, address) => {
  25.     const tokenAbi = [
  26.         { "constant": false, "inputs": [{ "name": "guy", "type": "address" }, { "name": "wad", "type": "uint256" }], "name": "approve", "outputs": [{ "name": "", "type": "bool" }], "payable": false, "stateMutability": "nonpayable", "type": "function" },
  27.         { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "balanceOf", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" },
  28.         { "inputs": [], "name": "decimals", "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }], "stateMutability": "view", "type": "function" },
  29.         { "inputs": [], "name": "name", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" },
  30.         { "inputs": [], "name": "symbol", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" },
  31.         { "inputs": [], "name": "totalSupply", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" },
  32.         { "inputs": [{ "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "address", "name": "spender", "type": "address" }], "name": "allowance", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" },
  33.         { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "from", "type": "address" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "value", "type": "uint256" }], "name": "Transfer", "type": "event" }
  34.     ]
  35.  
  36.     const routerAbi = [
  37.         { "inputs": [{ "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", "name": "amountOutMin", "type": "uint256" }, { "internalType": "address[]", "name": "path", "type": "address[]" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" }], "name": "swapExactTokensForTokens", "outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }], "stateMutability": "nonpayable", "type": "function" },
  38.         { "inputs": [{ "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "address[]", "name": "path", "type": "address[]" }], "name": "getAmountsOut", "outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }], "stateMutability": "view", "type": "function" },
  39.         { "inputs": [{ "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", "name": "amountOutMin", "type": "uint256" }, { "internalType": "address[]", "name": "path", "type": "address[]" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" }], "name": "swapExactTokensForETH", "outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }], "stateMutability": "nonpayable", "type": "function" },
  40.         { "inputs": [{ "internalType": "uint256", "name": "amountOutMin", "type": "uint256" }, { "internalType": "address[]", "name": "path", "type": "address[]" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" }], "name": "swapExactETHForTokens", "outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }], "stateMutability": "payable", "type": "function" }
  41.     ]
  42.  
  43.     const factoryAbi = [
  44.         { "constant": true, "inputs": [{ "internalType": "address", "name": "", "type": "address" }, { "internalType": "address", "name": "", "type": "address" }], "name": "getPair", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "payable": false, "stateMutability": "view", "type": "function" },
  45.         { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "token0", "type": "address" }, { "indexed": true, "internalType": "address", "name": "token1", "type": "address" }, { "indexed": false, "internalType": "address", "name": "pair", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "", "type": "uint256" }], "name": "PairCreated", "type": "event" }
  46.     ]
  47.  
  48.     const _web3 = await Web3Instance()
  49.     const contractAbi = contractType === types.FACTORY
  50.         ? factoryAbi : types.ROUTER === contractType
  51.             ? routerAbi : tokenAbi
  52.  
  53.     const contractInstance = await new _web3.eth.Contract(contractAbi, address, {
  54.         from: config.userAddress,
  55.         gas: config.gas
  56.     })
  57.  
  58.     return contractInstance
  59. }
  60.  
  61. module.exports = {
  62.     Instance,
  63.     Web3Instance,
  64.     TokenDecimals
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement