All files / src/parsers nftx.parser.ts

95.23% Statements 40/42
83.33% Branches 10/12
100% Functions 12/12
94.59% Lines 35/37

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 671x   1x 1x   1x   1x   1x     29x     490x 4x 4x 4x 486x 6x     18x         168x   6x 6x 6x 6x     6x 6x     6x   490x   29x   266x 10x 10x 6x 6x     4x 98x   8x   8x     10x   19x      
import { Log, TransactionResponse, ethers } from "ethers";
import { LogParser } from "./parser.definition";
import nftxABI from '../abi/nftxABI.json';
import { config } from "../config";
 
const nftxInterface = new ethers.Interface(nftxABI);
 
export class NFTXParser implements LogParser {
    
    platform: string = 'nftx';
    
    parseLogs(transaction:TransactionResponse, logs: Log[], tokenId: string): number {
        const result = logs.map((log: any) => {
 
          // direct buy from vault
          if (log.topics[0].toLowerCase() === '0x1cdb5ee3c47e1a706ac452b89698e5e3f2ff4f835ca72dde8936d0f4fcf37d81') {  
            const relevantData = log.data.substring(2);
            const relevantDataSlice = relevantData.match(/.{1,64}/g);
            return BigInt(`0x${relevantDataSlice[1]}`) / BigInt('1000000000000000');
          } else if (log.topics[0].toLowerCase() === '0x63b13f6307f284441e029836b0c22eb91eb62a7ad555670061157930ce884f4e') {
            const parsedLog = nftxInterface.parseLog(log)
            
            // check that the current transfer is NFTX related
            Iif (!parsedLog.args.nftIds.filter(n => BigInt(n).toString() === tokenId).length) {
              return
            }
            
            // redeem, find corresponding token bought
            const swaps = logs.filter((log2: any) => log2.topics[0].toLowerCase() === '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822')
              .map(b => {
                const relevantData = b.data.substring(2);
                const relevantDataSlice = relevantData.match(/.{1,64}/g);
                const moneyIn = BigInt(`0x${relevantDataSlice[0]}`)
                Iif (moneyIn > BigInt(0))
                  return moneyIn / BigInt('1000000000000000');
                else {
                  const moneyIn2 = BigInt(`0x${relevantDataSlice[1]}`)
                  return moneyIn2 / BigInt('1000000000000000');
                }
              })
            if (swaps.length) return swaps.reduce((previous, current) => previous + current, BigInt(0))
          }
        }).filter(n => n !== undefined)
 
        if (result.length) {
          // find the number of token transferred to adjust amount per token
          const redeemLog = logs.filter((log: any) => log.topics[0].toLowerCase() === '0x63b13f6307f284441e029836b0c22eb91eb62a7ad555670061157930ce884f4e')[0] as any
          let tokenCount = 1
          if (redeemLog) {
            const parsedLog = nftxInterface.parseLog(redeemLog)
            tokenCount = Math.max(parsedLog.args.nftIds.length, 1)
          } else {
            // count the number of tokens transfered
            tokenCount = logs
              .filter(l => l.address.toLowerCase() === config.contract_address.toLowerCase() && 
                l.topics[0].toLowerCase() === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef')
                .map(l => l.topics[3])
                // take unique value
                .filter((value, index, array) => array.indexOf(value) === index)
                .length
          }
          return parseFloat(result[0].toString())/tokenCount/1000;
        }
        return undefined
    }
 
}