All files / src base.service.ts

25.98% Statements 46/177
2.79% Branches 5/179
10.81% Functions 4/37
25.74% Lines 43/167

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 4051x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x     1x   1x 1x     1x         1x 1x   1x 1x   1x 1x     1x 1x 1x 1x 1x 1x   1x     1x     1x                                                                                                                                   1x           24x   24x 24x                                                         49x       59x 59x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
import { Injectable } from '@nestjs/common';
import fetch from 'node-fetch';
import { promises as asyncfs } from 'fs';
import { HttpService } from '@nestjs/axios';
import fs from 'fs';
import fiatSymbols from './fiat-symobols.json';
import { ethers } from 'ethers';
import { catchError, defaultIfEmpty, EMPTY, firstValueFrom, map, Observable, of, switchMap, tap, timer } from 'rxjs';
import currency from 'currency.js';
 
import dotenv from 'dotenv';
dotenv.config();
 
import { config } from './config';
import TwitterClient from './clients/twitter';
import { EUploadMimeType } from 'twitter-api-v2';
import DiscordClient from './clients/discord';
import { createLogger } from './logging.utils';
import { HexColorString, MessageAttachment, MessageEmbed } from 'discord.js';
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v10';
import { formatDistance } from 'date-fns';
 
export const alchemyAPIUrl = 'https://eth-mainnet.alchemyapi.io/v2/';
export const alchemyAPIKey = process.env.ALCHEMY_API_KEY;
 
//const provider = ethers.getDefaultProvider(alchemyAPIUrl + alchemyAPIKey);
const provider = global.providerForceHTTPS ? 
  ethers.getDefaultProvider(process.env.GETH_NODE_ENDPOINT_HTTP) :
  ethers.getDefaultProvider(process.env.GETH_NODE_ENDPOINT);
 
 
const pendingTransactions = []
const MAX_PENDING_TRANSACTIONS = 20000
 
const logger = createLogger('base.service')
let pendingTransactionWatcherStarted = false
 
let fiatValues = {}
getCryptoToFiat()
 
async function getCryptoToFiat() {
  logger.info('refreshing fiat values')
  const endpoint = `https://api.coingecko.com/api/v3/simple/price?ids=ethereum,dai,usdc&vs_currencies=usd`;
  const res = await fetch(endpoint)  
  const data = await res.json() as any
  fiatValues = { 'usdc': { 'usd': 1 }, ...data }
  logger.info(`fiat values set to ${JSON.stringify(fiatValues)}`)
 
  setTimeout(() => getCryptoToFiat(), 300000)
}
 
Iif (!global.noWatchdog && !global.doNotStartAutomatically) {
  startWatchdog()
}
Iif (config.enable_flashbot_detection && !global.doNotStartAutomatically) {
  watchPendingTransactions()
}
 
function startWatchdog() {
  return setTimeout(async () => {
    const timeoutInterval = setTimeout(() => {
      logger.warn(`Websocket connection hanged! Killing myself.`)
      process.exit(1)
    }, 20000);
    logger.info(`Checking websocket connection...`)
    const block = await provider.getBlockNumber()
    logger.info(`Websocket connection alive: ${block} !`)
    clearInterval(timeoutInterval)
    startWatchdog()
  }, 30000)  
}
 
function watchPendingTransactions() {
  Iif (!pendingTransactionWatcherStarted) {
    pendingTransactionWatcherStarted = true
    
    provider.on('pending', (txHash) => {
      pendingTransactions.push({
        time: new Date().getTime(),
        hash: txHash
      })
    });
 
    setInterval(() => {
      Iif (pendingTransactions.length) {
        Iif (pendingTransactions.length > MAX_PENDING_TRANSACTIONS) {
          pendingTransactions.splice(0, pendingTransactions.length - MAX_PENDING_TRANSACTIONS)
        }
        const distanceFrom = formatDistance(pendingTransactions[0].time, new Date(), { addSuffix: true })
        const distanceTo = formatDistance(pendingTransactions[pendingTransactions.length-1].time, new Date(), { addSuffix: true })
        logger.info(`Analyzed ${pendingTransactions.length} pending transactions between ${distanceFrom} and ${distanceTo}...`)
  
        Iif (new Date().getTime() - pendingTransactions[pendingTransactions.length-1].time > 60000*5) {
          logger.info(`Last pending transaction is older than 5 minutes, killing myself...`)
          process.exit(1)
        }
      }
    }, 5000)    
  }
}
 
export interface TweetRequest {
  platform: string,
  logIndex: number,
  eventType: string,
  initialFrom:string, 
  initialTo?:string, 
  from: any;
  to?: any;
  tokenId: string;
  ether?: number;
  erc20Token: string;
  transactionHash: string;
  transactionDate: string;
  alternateValue: number;
  imageUrl?: string;
  additionalText?: string;
}
 
@Injectable()
export class BaseService {
  
  twitterClient: TwitterClient;
  discordClient: DiscordClient;
 
  constructor(
    protected readonly http: HttpService
  ) {
    this.twitterClient = new TwitterClient()
    this.discordClient = new DiscordClient()
  }
 
  isTransactionFlashbotted(hash:string) {
    Iif (pendingTransactions.length < MAX_PENDING_TRANSACTIONS) {
      logger.warn(`cannot determinate if the transaction used a flashbot because the pool is not full: ${pendingTransactions.length}`)
      return false
    }
    for (let tx of pendingTransactions) {
      Iif (tx.hash.toLowerCase() == hash.toLowerCase()) {
        return false
      }
    }
    return true
  }
 
  initDiscordClient() {
    this.discordClient.init()
  }
 
  getDiscordInteractionsListeners() {
    return this.discordClient.getInteractionsListener()
  }
 
  getDiscordCommands() {
    return this.discordClient.getDiscordCommands()
  }
 
  getWeb3Provider() {
      return provider
  }
 
  shortenAddress(address: string): string {
    const shortAddress = `${address.slice(0, 5)}...${address.slice(address.length - 5, address.length)}`;
    if (address.startsWith('0x')) return shortAddress;
    return address;
  }
 
  async getTokenMetadata(tokenId: string, onlyImage:boolean=true): Promise<any> {
    // check cache 
    const metadataPath = `${config.token_metadata_cache_path}/${tokenId}.json`
    const url = alchemyAPIUrl + alchemyAPIKey + '/getNFTMetadata';
    const hadCacheData = fs.existsSync(metadataPath)
    let dataObserver = hadCacheData ? 
      of({
        data: JSON.parse(fs.readFileSync(metadataPath).toString())
      }) :
      this.http.get(url, {
        params: {
          contractAddress: config.contract_address,
          tokenId,
          tokenType: 'erc721'
        }
      })
 
    return await firstValueFrom(
      dataObserver.pipe(
        tap(async (res:any) => {
          Iif (!hadCacheData && config.token_metadata_cache_path) {
            logger.info(`populating metadata cache for ${tokenId}`)
            await asyncfs.writeFile(metadataPath, JSON.stringify(res?.data))
          }
        }),
        map((res: any) => {
          return onlyImage ? res?.data?.metadata?.image_url || res?.data?.metadata?.image || res?.data?.tokenUri?.gateway : res?.data;
        }),
        catchError(() => {
          return of(null);
        })
      )
    );
  }
 
  async dispatch(data: TweetRequest) {
    const tweet = await this.tweet(data)
    await this.discord(data, tweet.id)
  }
  
  async discord(data: TweetRequest, 
                tweetId:string|undefined=undefined, 
                template:string=config.saleMessageDiscord, 
                color:string='#0084CA',
                footerTextParam:string|undefined=undefined) {
    Iif (!this.discordClient.setup) return
    Iif (tweetId) template = template.replace(new RegExp('<tweetLink>', 'g'), `<https://twitter.com/i/web/status/${tweetId}>`);
    const image = config.use_local_images ? data.imageUrl : this.transformImage(data.imageUrl);
    
    const platformImage = data.platform === 'nftx' ? 'NFTX.png' :
      data.platform === 'opensea' ? 'OPENSEA.png' :
      data.platform === 'looksrare' ? 'LOOKSRARE.png' :
      data.platform === 'x2y2' ? 'X2Y2.png' :
      data.platform === 'rarible' ? 'RARIBLE.png' :
      data.platform === 'notlarvalabs' ? 'NLL.png' :
      data.platform === 'phunkauction' ? 'AUCTION.png' :
      data.platform === 'phunkflywheel' ? 'FLYWHEEL.png' :
      data.platform === 'blurio' ? 'BLUR.png' :
      'ETHERSCAN.png';
    const sentText = this.formatText(data, template)
    const footerText = footerTextParam ?? config.discord_footer_text
    const embed = new MessageEmbed()
      .setColor(color as HexColorString)
      .setImage(`attachment://token.png`)
      .setDescription(sentText)
      .setTimestamp()
      .setFooter({ text: footerText, iconURL: 'attachment://platform.png' });
  
    let processedImage: Buffer | undefined;
    Iif (image) processedImage = await this.getImageFile(image);
    processedImage = await this.decorateImage(processedImage, data)
    await this.discordClient.sendEmbed(embed, processedImage, `platform_images/${platformImage}`);
  }
 
  async tweet(data: TweetRequest, template:string=config.saleMessage) {
 
    let tweetText = this.formatText(data, template)
 
    // Delay tweets when running live
    Iif (!global.doNotStartAutomatically)
      await new Promise( resolve => setTimeout(resolve, 30000) );
    
    // Format our image to base64
    const image = config.use_local_images || config.use_forced_remote_image_path ? data.imageUrl : this.transformImage(data.imageUrl);
 
    let processedImage: Buffer | undefined;
    Iif (image) processedImage = await this.getImageFile(image);
 
    processedImage = await this.decorateImage(processedImage, data)
 
    let media_id: string;
    Iif (processedImage) {
      // Upload the item's image to Twitter & retrieve a reference to it
      media_id = await this.twitterClient.uploadMedia(processedImage, {
        mimeType: EUploadMimeType.Png,
      });
    }
 
    // Post the tweet 👇
    // If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
    const { data: createdTweet, errors: errors } = await this.twitterClient.tweet(
      tweetText,
      { media: { media_ids: [media_id] } },
    );
    if (!errors) {
      logger.info(
        `Successfully tweeted: ${createdTweet.id} -> ${createdTweet.text}`,
      );
      return createdTweet;
    } else {
      logger.error(errors);
      return null;
    }
  }
  
  async decorateImage(processedImage: Buffer, data:TweetRequest): Promise<Buffer> {
    // Do nothing but can be overriden by subclasses
    return processedImage
  }
 
  formatText(data: TweetRequest, template:string) {
    Iif (!data) return template
 
    // Cash value
    const value = data.alternateValue && data.alternateValue > 0 ? data.alternateValue : data.ether
    
    const fiat = this.getFiatValue(value, data.erc20Token)
    const eth = this.getERC20Value(data.alternateValue ? data.alternateValue : data.ether, data.erc20Token)
    
    // Replace tokens from config file
    template = template.replace(new RegExp('<tokenId>', 'g'), data.tokenId);
    template = template.replace(new RegExp('<ethPrice>', 'g'), eth.format());
    template = template.replace(new RegExp('<txHash>', 'g'), data.transactionHash);
    template = template.replace(new RegExp('<from>', 'g'), data.from);
    template = template.replace(new RegExp('<initialFrom>', 'g'), data.initialFrom);
    template = template.replace(new RegExp('<to>', 'g'), data.to);
    template = template.replace(new RegExp('<initialTo>', 'g'), data.initialTo);
    template = template.replace(new RegExp('<fiatPrice>', 'g'), fiat ? fiat.format() : '-');
    const platform = data.platform === 'blurio' ? 'Blur marketplace' : 
      data.platform === 'opensea' ? 'OpenSea marketplace' : 
      data.platform === 'looksrare' ? 'Looks Rare' : 
      data.platform === 'arcadexyz' ? 'Arcade' : 
      data.platform === 'nftfi' ? 'NFTfi' : 
      data.platform === 'benddao' ? 'Bend DAO' : 
      data.platform === 'metastreet' ? 'Metastreet' : 
      data.platform === 'punksmarketplace' ? 'CryptoPunks marketplace' : 
      data.platform
    template = template.replace(new RegExp('<platform>', 'g'), platform);
    template = template.replace(new RegExp('<additionalText>', 'g'), data.additionalText);
 
 
    Iif (config.enable_flashbot_detection && data.eventType !== 'loans')
      template += ` — Flashbots Protect RPC: ${this.isTransactionFlashbotted(data.transactionHash) ? 'Yes' : 'No'}`
 
    return template
  }
 
  getERC20Value(value: number, erc20Token: string, forcedSymbol:string|undefined=undefined) {
    const symbol = forcedSymbol !== undefined ? forcedSymbol 
      : erc20Token === 'dai' ? 'DAI' 
      : erc20Token === 'usdc' ? 'USDC' : 'Ξ'
    const precision = erc20Token === 'dai' ? 0 : erc20Token === 'usdc' ? 2 : 3
    const pattern = erc20Token === 'dai' ? '# !' : erc20Token === 'usdc' ? '# !' : '!#'
    const eth = currency(value, { symbol, precision, pattern });
    return eth
  }
 
  getFiatValue(value: number, erc20Token: string) {
    try {
      const fiatValue = fiatValues && Object.values(fiatValues).length ? 
        fiatValues[erc20Token][config.currency] * value : 
        undefined;
      return fiatValue != null ? currency(fiatValue, { symbol: fiatSymbols[config.currency].symbol, precision: 0 }) : undefined
    } catch (err) {
      logger.error(`cannot get fiat for ${erc20Token}`)
    }
    return undefined
  }
  
  async getImageFile(url: string): Promise<Buffer | undefined> {
    return new Promise((resolve, _) => {
      if (url.startsWith('http')) {
        this.http.get(url, { responseType: 'arraybuffer' }).subscribe((res) => {
          if (res.data) {
            const file = Buffer.from(res.data, 'binary');
            resolve(file);
          } else {
            resolve(undefined);
          }
        });
      } else {
        resolve(fs.readFileSync(url));
      }
    });
  }
  
  getCryptoToFiat(): Observable<any> {
    const endpoint = `https://api.coingecko.com/api/v3/simple/price`;
    const params = {
      ids: 'ethereum,dai,usdc',
      vs_currencies: 'usd'
    };
    return timer(0, 300000).pipe(
      switchMap(() => this.http.get(endpoint, {params})),
      map((res: any) => res.data),
      // tap((res) => console.log(res)),
      catchError((err: any) => {
        logger.warn('coin gecko call failed, ignoring fiat price', err.toString());
        return of(undefined);
      })
    );
  }
 
  transformImage(value: string): string {
    //return value.replace('https://gateway.pinata.cloud/ipfs/QmSv6qnW1zCqiYBHCJKbfBu8YAcJefUYtPsDea3TsG2PHz/notpunk', 'file://./token_images/phunk');
    let val: any = value;
    if (value?.includes('gateway.pinata.cloud')) {
      val = value.replace('gateway.pinata.cloud', 'cloudflare-ipfs.com');
    // } else if (value?.startsWith('data:image')) {
    //   val = `${value}`;
    } else Iif (value?.startsWith('ipfs://')) {
      val = value.replace('ipfs://', 'https://cloudflare-ipfs.com/ipfs/');
    }
    return val ? val : null;
  }
 
  async updatePosition(position) {
    await asyncfs.writeFile(this.getPositionFile(), `${position}`)    
  }
 
  getPositionFile():string {
    throw new Error('must be overriden')
  }
 
}