<commit_before>import { CoinStorage, ICoin } from './coin';
import { TransformOptions } from '../types/TransformOptions';
import { ObjectID } from 'mongodb';
import { BaseModel } from './base';
import { IWallet } from './wallet';
import { TransactionStorage } from './transaction';
import { StorageService } from '../services/storage';

export type IWalletAddress = {
  wallet: ObjectID;
  address: string;
  chain: string;
  network: string;
};

export class WalletAddressModel extends BaseModel<IWalletAddress> {
  constructor(storage?: StorageService) {
    super('walletaddresses', storage);
  }

  allowedPaging = [];

  onConnect() {
    this.collection.createIndex({ address: 1, wallet: 1 }, { background: true });
    this.collection.createIndex({ wallet: 1 }, { background: true });
  }

  _apiTransform(walletAddress: { address: string }, options: TransformOptions) {
    let transform = { address: walletAddress.address };
    if (options && options.object) {
      return transform;
    }
    return JSON.stringify(transform);
  }

  getUpdateCoinsObj(params: { wallet: IWallet; address: string }) {
    const { wallet, address } = params;
    const { chain, network } = wallet;

    return {
      updateMany: {
        filter: { chain, network, address },
        update: {
          $addToSet: { wallets: wallet._id }
        }
      }
    };
  }

  getUpdateWalletAddressObj(params: { wallet: IWallet; address: string }) {
    const { wallet, address } = params;
    const { chain, network } = wallet;

    return {
      updateOne: {
        filter: { wallet: wallet._id, address: address },
        update: { $setOnInsert: { wallet: wallet._id, address: address, chain, network } },
        upsert: true
      }
    };
  }

  async updateCoins(params: { wallet: IWallet; addresses: string[] }) {
    const { wallet, addresses } = params;
    const { chain, network } = wallet;

    return new Promise(async resolve => {
      let batch = new Array<string>();
      const AddAddresses = addresses => {
        const addressOps = addresses.map(address => this.getUpdateWalletAddressObj({ wallet, address }));
        return WalletAddressStorage.collection.bulkWrite(addressOps);
      };
      const UpdateCoins = addresses => {
        return CoinStorage.collection.updateMany(
          { chain, network, address: { $in: addresses } },
          { $addToSet: { wallets: wallet._id } }<commit_msg>chain and network on wallet address filter<commit_after>import { CoinStorage, ICoin } from './coin';
import { TransformOptions } from '../types/TransformOptions';
import { ObjectID } from 'mongodb';
import { BaseModel } from './base';
import { IWallet } from './wallet';
import { TransactionStorage } from './transaction';
import { StorageService } from '../services/storage';

export type IWalletAddress = {
  wallet: ObjectID;
  address: string;
  chain: string;
  network: string;
};

export class WalletAddressModel extends BaseModel<IWalletAddress> {
  constructor(storage?: StorageService) {
    super('walletaddresses', storage);
  }

  allowedPaging = [];

  onConnect() {
    this.collection.createIndex({ address: 1, wallet: 1 }, { background: true });
    this.collection.createIndex({ wallet: 1 }, { background: true });
  }

  _apiTransform(walletAddress: { address: string }, options: TransformOptions) {
    let transform = { address: walletAddress.address };
    if (options && options.object) {
      return transform;
    }
    return JSON.stringify(transform);
  }

  getUpdateCoinsObj(params: { wallet: IWallet; address: string }) {
    const { wallet, address } = params;
    const { chain, network } = wallet;

    return {
      updateMany: {
        filter: { chain, network, address },
        update: {
          $addToSet: { wallets: wallet._id }
        }
      }
    };
  }

  getUpdateWalletAddressObj(params: { wallet: IWallet; address: string }) {
    const { wallet, address } = params;
    const { chain, network } = wallet;

    return {
      updateOne: {
        filter: { wallet: wallet._id, address: address, chain, network },
        update: { $setOnInsert: { wallet: wallet._id, address: address, chain, network } },
        upsert: true
      }
    };
  }

  async updateCoins(params: { wallet: IWallet; addresses: string[] }) {
    const { wallet, addresses } = params;
    const { chain, network } = wallet;

    return new Promise(async resolve => {
      let batch = new Array<string>();
      const AddAddresses = addresses => {
        const addressOps = addresses.map(address => this.getUpdateWalletAddressObj({ wallet, address }));
        return WalletAddressStorage.collection.bulkWrite(addressOps);
      };
      const UpdateCoins = addresses => {
        return CoinStorage.collection.updateMany(
          { chain, network, address: { $in: addresses } },
          { $addToSet: { wallets: wallet._id } }
{'fraction_non_alphanumeric': 0.129283808742048, 'fraction_numerical': 0.001231274368971886, 'mean_word_length': 3.0923593618807725, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '1079279', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1354, 'n_words': 364}