<commit_before>import web3 from '../../../components/Web3.js'

export const GET_USER_INFO = 'GET_USER_INFO'
export const SET_USER_EVENTS = 'SET_USER_EVENTS'
export const SET_USER_TICKETS = 'SET_USER_TICKETS'
export const GET_USER_TICKETS = 'GET_USER_TICKETS'
export const GET_USER_EVENTS = 'GET_USER_EVENTS'

export const getUserInfo = () => {
  // return (dispatch, getState) => {
  //   web3.eth
  //
  //   .then((tickets) => {
  //     dispatch({
  //       type: GET_USER_INFO,
  //       payload: tickets
  //     })
  //   });
  //
  // }
};

export const getUserTickets = () => {
  const tickets = [{ id: "0x712982674F171933e0bcad11D6eEc6f3eE782A90", eventId: "0x712982674F171933e0bcad11D6eEc6f3eE782A90", name: "The String Cheese Incident", price: "100" },
  { id: "0x712982674F171933e0bcad11D6eEc6f3eE782A91", eventId: "0x712982674F171933e0bcad11D6eEc6f3eE782A90", name: "Phish", price: "100" },
  { id: "0x712982674F171933e0bcad11D6eEc6f3eE782A92", eventId: "0x712982674F171933e0bcad11D6eEc6f3eE782A90", name: "Widespread Panic", price: "100" },
  { id: "0x712982674F171933e0bcad11D6eEc6f3eE782A93", eventId: "0x712982674F171933e0bcad11D6eEc6f3eE782A90", name: "Greensky Bluegrass", price: "100" }];
  return (dispatch, getState) => {
    dispatch({
      type: SET_USER_TICKETS,
      payload: tickets
    });
  };
};

export const getUserEvents = () => {
  const events = [{ id: "0x712982674F171933e0bcad11D6eEc6f3eE782A90", name: "The String Cheese Incident", qty: 7, price: "100" },
  { id: "0x712982674F171933e0bcad11D6eEc6f3eE782A91", name: "Phish", qty: 7, price: "100" },
  { id: "0x712982674F171933e0bcad11D6eEc6f3eE782A92", name: "Widespread Panic", qty: 7, price: "100" },
  { id: "0x712982674F171933e0bcad11D6eEc6f3eE782A93", name: "Greensky Bluegrass", qty: 7, price: "100" }];
  return (dispatch, getState) => {
    console.log('events: ', events);
    dispatch({
      type: SET_USER_EVENTS,
      payload: events
    });
  };
};

export const getUserBalance = (privateKey) => {
  return (dispatch, getState) => {
    web3.eth.getBalance(getState().auth.user.walletAddress)
      .then((balance) => {
        dispatch({
          type: 'SET_USER_BALANCE',
          payload: balance
        });
      });
  }
}

export const actions = {
  getUserInfo,
  // getUserTickets,
  getUserEvents,
  getUserBalance
}

// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
  [GET_USER_INFO]  : (state, action) => {
    return {
      ...state
    }<commit_msg>user events and user tickets<commit_after>import web3 from '../../../components/Web3.js'
import pasync from 'pasync';

export const GET_USER_INFO = 'GET_USER_INFO'
export const SET_USER_EVENTS = 'SET_USER_EVENTS'
export const SET_USER_TICKETS = 'SET_USER_TICKETS'
export const GET_USER_TICKETS = 'GET_USER_TICKETS'
export const GET_USER_EVENTS = 'GET_USER_EVENTS'

let getContractInstance = (abi, address) => {
  const instance = new web3.eth.Contract(abi, address);
  return instance;
};

export const getUserInfo = () => {
  // return (dispatch, getState) => {
  //   web3.eth
  //
  //   .then((tickets) => {
  //     dispatch({
  //       type: GET_USER_INFO,
  //       payload: tickets
  //     })
  //   });
  //
  // }
};

export const getUserTickets = () => {
  return async (dispatch, getState) => {
    let { user } = getState().auth;

    let { abis, terrapinAddress } = getState().terrapin;
    let terrapinInstance = getContractInstance(abis.terrapin.abi, terrapinAddress);

    let eventAddresses = await terrapinInstance.methods.getEvents().call();

    let tickets = [];

    return pasync.eachSeries(eventAddresses, async (eventAddress) => {
      let eventInstance = getContractInstance(abis.event.abi, eventAddress);
      let ticketAddreses = await eventInstance.methods.getTickets().call();

      return pasync.eachSeries(ticketAddreses, async (ticketAddress) => {
        let ticketInstance = getContractInstance(abis.ticket.abi, ticketAddress);
        let owner = await ticketInstance.methods.owner().call();
        if (owner === user.walletAddress) {
          tickets.push({
            id: ticketInstance.options.address,
            eventId: eventInstance.options.address,
            name: web3.utils.toAscii(await eventInstance.methods.name().call()),
            price: await ticketInstance.methods.price().call()
          });
        }
      });
    })
    .then(() => {
      dispatch({
        type: SET_USER_TICKETS,
        payload: tickets
      });
    });
  };
};

export const getUserEvents = () => {
  return async (dispatch, getState) => {
    let { user } = getState().auth;

    let { abis, terrapinAddress } = getState().terrapin;
    let terrapinInstance = getContractInstance(abis.terrapin.abi, terrapinAddress);

    let eventAddresses = await terrapinInstance.methods.getEvents().call();

    let events = [];

    return pasync.eachSeries(eventAddresses, async (eventAddress) => {
      let eventInstance = getContractInstance(abis.event.abi, eventAddress);
      let owner = await eventInstance.methods.owner().call();
      if (owner === user.walletAddress) {
        let ticketAddreses = await eventInstance.methods.getTickets().call();

        let ticketInstance = getContractInstance(abis.ticket.abi, ticketAddreses[0]);

        events.push({
          id: eventInstance.options.address,
          name: web3.utils.toAscii(await eventInstance.methods.name().call()),
          qty: ticketAddreses.length,
          price: await (ticketInstance.methods.price().call())
        });
      }
    })
    .then(() => {
      dispatch({
        type: SET_USER_EVENTS,
        payload: events
      });
    });

  };
};

export const getUserBalance = (privateKey) => {
  return (dispatch, getState) => {
    web3.eth.getBalance(getState().auth.user.walletAddress)
      .then((balance) => {
        dispatch({
          type: 'SET_USER_BALANCE',
          payload: balance
        });
      });
  }
}

export const actions = {
  getUserInfo,
  // getUserTickets,
  getUserEvents,
  getUserBalance
}

// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
  [GET_USER_INFO]  : (state, action) => {
    return {
      ...state
    }
{'fraction_non_alphanumeric': 0.15902237740041264, 'fraction_numerical': 0.05776860815743533, 'mean_word_length': 2.8497251069028713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '6936985', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2133, 'n_words': 387}