<commit_before>import { crypto, Address, ByteArray, ethereum, BigInt, BigDecimal } from '@graphprotocol/graph-ts';
import { RevenuePool } from '../generated/RevenuePool/RevenuePool';
import { log } from '@graphprotocol/graph-ts';
import { ERC20 } from './erc-20/ERC20';
import { ERC20SymbolBytes } from './erc-20/ERC20SymbolBytes';
import { ERC20NameBytes } from './erc-20/ERC20NameBytes';
import { ZERO_ADDRESS } from '@protofire/subgraph-toolkit';
import { PrepaidCardManager } from '../generated/PrepaidCard/PrepaidCardManager';
import { Exchange } from '../generated/PrepaidCard/Exchange';
import {
  EOATransaction,
  Transaction,
  MerchantRevenue,
  PrepaidCard,
  PrepaidCardPayment,
  RevenueEarningsByDay,
  Token,
  Account,
  MerchantSafe,
  MerchantRevenueEvent,
  SafeOwner,
} from '../generated/schema';
import { GnosisSafe } from '../generated/Gnosis/GnosisSafe';
import { StaticToken } from './static-tokens';
import { addresses } from './generated/addresses';
import { dayMonthYearFromEventTimestamp } from './dates';

export let protocolVersions = new Map<string, i32>();

export function makeToken(address: Address): string {
  let token = toChecksumAddress(address);
  let tokenEntity = new Token(token);
  tokenEntity.decimals = fetchTokenDecimals(address);
  tokenEntity.symbol = fetchTokenSymbol(address);
  tokenEntity.name = fetchTokenName(address);
  tokenEntity.save();
  return token;
}

export function makeTransaction(event: ethereum.Event): void {
  let txEntity = new Transaction(event.transaction.hash.toHex());
  txEntity.timestamp = event.block.timestamp;
  txEntity.blockNumber = event.block.number;
  txEntity.gasUsed = event.transaction.gasUsed;
  txEntity.save();
}

export function makeEOATransaction(event: ethereum.Event, address: string, safe: string | null = null): void {
  makeTransaction(event);
  makeAccount(address);

  let txnHash = event.transaction.hash.toHex();
  let entity = new EOATransaction(txnHash + '-' + address);
  entity.transaction = txnHash;
  entity.account = address;
  entity.safe = safe;
  entity.timestamp = event.block.timestamp;
  entity.blockNumber = event.block.number;
  entity.save();
}

export function makeEOATransactionForSafe(event: ethereum.Event, safe: string): void {
  let safeContract = GnosisSafe.bind(Address.fromString(safe));
  let owners = safeContract.getOwners();
  for (let i = 0; i < owners.length; i++) {
    makeEOATransaction(event, toChecksumAddress(owners[i]), safe);
  }
}

export function setSafeType(safe: string, type: string): void {
  let safeContract = GnosisSafe.bind(Address.fromString(safe));
  let owners = safeContract.getOwners();
  for (let i = 0; i < owners.length; i++) {
    let ownerAddress = toChecksumAddress(owners[i]);
    let safeOwnerEntityId = safe + '-' + ownerAddress;
    let safeOwnerEntity = SafeOwner.load(safeOwnerEntityId);
    if (safeOwnerEntity == null) {
      log.warning(
        'Cannot set safe type {} for safe {} with owner {}: SafeOwner entity does not exist for id {}. This is likely due to the subgraph having a startBlock that is higher than the block the SafeOwner was created in.',
        [type, safe, ownerAddress, safeOwnerEntityId]
      );
      return;
    }
    safeOwnerEntity.type = type;
    safeOwnerEntity.save();
  }
}

export function makeMerchantRevenue(merchantSafe: string, token: string): MerchantRevenue {
  let id = merchantSafe + '-' + token;
  let entity = MerchantRevenue.load(id);
  if (entity == null) {
    entity = new MerchantRevenue(merchantSafe + '-' + token);
    entity.token = token;
    entity.merchantSafe = merchantSafe;
    entity.lifetimeAccumulation = new BigInt(0);
    entity.unclaimedBalance = new BigInt(0);
    entity.save();
  }
  return entity as MerchantRevenue;
}

export function makeMerchantRevenueEvent(
  event: ethereum.Event,
  merchantSafe: string,
  token: string
): MerchantRevenueEvent {
  let txnHash = event.transaction.hash.toHex();
  let revenuePoolAddress = addresses.get('revenuePool') as string;
  let revenueEntity = makeMerchantRevenue(merchantSafe, token);
  let revenuePool = RevenuePool.bind(Address.fromString(revenuePoolAddress));
  revenueEntity.unclaimedBalance = revenuePool.revenueBalance(
    Address.fromString(merchantSafe),
    Address.fromString(token)
  );
  revenueEntity.save();

  let revenueEventEntity = new MerchantRevenueEvent(txnHash);
  revenueEventEntity.transaction = txnHash;
  revenueEventEntity.timestamp = event.block.timestamp;
  revenueEventEntity.merchantRevenue = revenueEntity.id;
  revenueEventEntity.historicLifetimeAccumulation = revenueEntity.lifetimeAccumulation;
  revenueEventEntity.historicUnclaimedBalance = revenueEntity.unclaimedBalance;
  return revenueEventEntity;
}

export function makePrepaidCardPayment(
  event: ethereum.Event,
  prepaidCard: string,
  merchantSafe: string | null,
  issuingToken: string,
  issuingTokenAmount: BigInt,
  spendAmount: BigInt | null
): void {
  let txnHash = event.transaction.hash.toHex();
  let timestamp = event.block.timestamp;
  let prepaidCardEntity = PrepaidCard.load(prepaidCard);
  let faceValue = getPrepaidCardFaceValue(prepaidCard);
  if (prepaidCardEntity != null) {
    let token = ERC20.bind(Address.fromString(issuingToken));
    prepaidCardEntity.faceValue = faceValue;
    prepaidCardEntity.issuingTokenBalance = token.balanceOf(Address.fromString(prepaidCard));
    prepaidCardEntity.save();
  } else {
    log.warning(
      'Cannot process payment txn {}: PrepaidCard entity does not exist for prepaid card {}. This is likely due to the subgraph having a startBlock that is higher than the block the prepaid card was created in.',
      [txnHash, prepaidCard]
    );
    return;<commit_msg>missing blockNumber in MerchantRevenueEvent entity (#2462)<commit_after>import { crypto, Address, ByteArray, ethereum, BigInt, BigDecimal } from '@graphprotocol/graph-ts';
import { RevenuePool } from '../generated/RevenuePool/RevenuePool';
import { log } from '@graphprotocol/graph-ts';
import { ERC20 } from './erc-20/ERC20';
import { ERC20SymbolBytes } from './erc-20/ERC20SymbolBytes';
import { ERC20NameBytes } from './erc-20/ERC20NameBytes';
import { ZERO_ADDRESS } from '@protofire/subgraph-toolkit';
import { PrepaidCardManager } from '../generated/PrepaidCard/PrepaidCardManager';
import { Exchange } from '../generated/PrepaidCard/Exchange';
import {
  EOATransaction,
  Transaction,
  MerchantRevenue,
  PrepaidCard,
  PrepaidCardPayment,
  RevenueEarningsByDay,
  Token,
  Account,
  MerchantSafe,
  MerchantRevenueEvent,
  SafeOwner,
} from '../generated/schema';
import { GnosisSafe } from '../generated/Gnosis/GnosisSafe';
import { StaticToken } from './static-tokens';
import { addresses } from './generated/addresses';
import { dayMonthYearFromEventTimestamp } from './dates';

export let protocolVersions = new Map<string, i32>();

export function makeToken(address: Address): string {
  let token = toChecksumAddress(address);
  let tokenEntity = new Token(token);
  tokenEntity.decimals = fetchTokenDecimals(address);
  tokenEntity.symbol = fetchTokenSymbol(address);
  tokenEntity.name = fetchTokenName(address);
  tokenEntity.save();
  return token;
}

export function makeTransaction(event: ethereum.Event): void {
  let txEntity = new Transaction(event.transaction.hash.toHex());
  txEntity.timestamp = event.block.timestamp;
  txEntity.blockNumber = event.block.number;
  txEntity.gasUsed = event.transaction.gasUsed;
  txEntity.save();
}

export function makeEOATransaction(event: ethereum.Event, address: string, safe: string | null = null): void {
  makeTransaction(event);
  makeAccount(address);

  let txnHash = event.transaction.hash.toHex();
  let entity = new EOATransaction(txnHash + '-' + address);
  entity.transaction = txnHash;
  entity.account = address;
  entity.safe = safe;
  entity.timestamp = event.block.timestamp;
  entity.blockNumber = event.block.number;
  entity.save();
}

export function makeEOATransactionForSafe(event: ethereum.Event, safe: string): void {
  let safeContract = GnosisSafe.bind(Address.fromString(safe));
  let owners = safeContract.getOwners();
  for (let i = 0; i < owners.length; i++) {
    makeEOATransaction(event, toChecksumAddress(owners[i]), safe);
  }
}

export function setSafeType(safe: string, type: string): void {
  let safeContract = GnosisSafe.bind(Address.fromString(safe));
  let owners = safeContract.getOwners();
  for (let i = 0; i < owners.length; i++) {
    let ownerAddress = toChecksumAddress(owners[i]);
    let safeOwnerEntityId = safe + '-' + ownerAddress;
    let safeOwnerEntity = SafeOwner.load(safeOwnerEntityId);
    if (safeOwnerEntity == null) {
      log.warning(
        'Cannot set safe type {} for safe {} with owner {}: SafeOwner entity does not exist for id {}. This is likely due to the subgraph having a startBlock that is higher than the block the SafeOwner was created in.',
        [type, safe, ownerAddress, safeOwnerEntityId]
      );
      return;
    }
    safeOwnerEntity.type = type;
    safeOwnerEntity.save();
  }
}

export function makeMerchantRevenue(merchantSafe: string, token: string): MerchantRevenue {
  let id = merchantSafe + '-' + token;
  let entity = MerchantRevenue.load(id);
  if (entity == null) {
    entity = new MerchantRevenue(merchantSafe + '-' + token);
    entity.token = token;
    entity.merchantSafe = merchantSafe;
    entity.lifetimeAccumulation = new BigInt(0);
    entity.unclaimedBalance = new BigInt(0);
    entity.save();
  }
  return entity as MerchantRevenue;
}

export function makeMerchantRevenueEvent(
  event: ethereum.Event,
  merchantSafe: string,
  token: string
): MerchantRevenueEvent {
  let txnHash = event.transaction.hash.toHex();
  let revenuePoolAddress = addresses.get('revenuePool') as string;
  let revenueEntity = makeMerchantRevenue(merchantSafe, token);
  let revenuePool = RevenuePool.bind(Address.fromString(revenuePoolAddress));
  revenueEntity.unclaimedBalance = revenuePool.revenueBalance(
    Address.fromString(merchantSafe),
    Address.fromString(token)
  );
  revenueEntity.save();

  let revenueEventEntity = new MerchantRevenueEvent(txnHash);
  revenueEventEntity.transaction = txnHash;
  revenueEventEntity.timestamp = event.block.timestamp;
  revenueEventEntity.blockNumber = event.block.number;
  revenueEventEntity.merchantRevenue = revenueEntity.id;
  revenueEventEntity.historicLifetimeAccumulation = revenueEntity.lifetimeAccumulation;
  revenueEventEntity.historicUnclaimedBalance = revenueEntity.unclaimedBalance;
  return revenueEventEntity;
}

export function makePrepaidCardPayment(
  event: ethereum.Event,
  prepaidCard: string,
  merchantSafe: string | null,
  issuingToken: string,
  issuingTokenAmount: BigInt,
  spendAmount: BigInt | null
): void {
  let txnHash = event.transaction.hash.toHex();
  let timestamp = event.block.timestamp;
  let prepaidCardEntity = PrepaidCard.load(prepaidCard);
  let faceValue = getPrepaidCardFaceValue(prepaidCard);
  if (prepaidCardEntity != null) {
    let token = ERC20.bind(Address.fromString(issuingToken));
    prepaidCardEntity.faceValue = faceValue;
    prepaidCardEntity.issuingTokenBalance = token.balanceOf(Address.fromString(prepaidCard));
    prepaidCardEntity.save();
  } else {
    log.warning(
      'Cannot process payment txn {}: PrepaidCard entity does not exist for prepaid card {}. This is likely due to the subgraph having a startBlock that is higher than the block the prepaid card was created in.',
      [txnHash, prepaidCard]
    );
    return;
{'fraction_non_alphanumeric': 0.10966510498004511, 'fraction_numerical': 0.0048585806003817456, 'mean_word_length': 4.775050100200401, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4213832', 'n_tokens_mistral': 3509, 'n_tokens_neox': 3449, 'n_words': 828}