All files / src/store/web3 actions.ts

73.33% Statements 33/45
50% Branches 1/2
100% Functions 0/0
67.57% Lines 25/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 67 68 691x 1x         1x         4x   2x   2x   2x   2x 2x 1x 1x       1x         2x 1x 1x 1x 1x 1x                                       1x 1x 1x     2x 2x 2x       1x  
import { web3Instance } from "src/boot/web3";
import {
  errorNotification,
  successNotification
} from "src/helper/notifications";
import { ActionTree } from "vuex";
import { Web3ActionName, Web3MutationName } from "./names";
import { Web3State } from "./state";
 
const actions: ActionTree<Web3State, any> = {
  /** Required to run this after browser loaded */
  async [Web3ActionName.initializeWeb3]({ state, commit }, Eforce = false) {
    // true/false => true
    commit(Web3MutationName.setLoading, true);
    // false/true => true
    commit(Web3MutationName.setInitialized, true);
    // "login"/"logout" => "loading"
    commit(Web3MutationName.setStatus, "loading");
    // "" => MESSAGE
    commit(Web3MutationName.setMessage, "Loading web3...");
    const w3 = window.web3;
    if (window.ethereum) {
      web3Instance.setWeb3(window.ethereum);
      // if ethereum exist
      try {
        // LOADING MESSAGE => APPROVAL_MESSAGE
        commit(
          Web3MutationName.setMessage,
          "Waiting for approval to connect to your account..."
        );
        // Request account access if needed
        await window.ethereum.enable();
        const network = await web3Instance.getId();
        successNotification("web3.success.sign_in");
        commit(Web3MutationName.setNetwork, network);
        commit(Web3MutationName.setLoading, false);
        commit(Web3MutationName.setStatus, "login");
      } catch (err) {
        commit(Web3MutationName.setLoading, false);
        commit(Web3MutationName.setStatus, "logout");
        errorNotification("web3.error.connect_eth_acc");
        errorNotification(err.message, false);
      }
    } else if (typeof w3 !== "undefined") {
      try {
        web3Instance.setWeb3(w3.currentProvider);
        successNotification("web3.success.sign_in");
        commit(Web3MutationName.setLoading, false);
        commit(Web3MutationName.setStatus, "login");
      } catch (err) {
        commit(Web3MutationName.setLoading, false);
        commit(Web3MutationName.setStatus, "logout");
        errorNotification("web3.error.connect_eth_acc");
        errorNotification(err.message, false);
      }
    } else {
      commit(Web3MutationName.setLoading, false);
      commit(Web3MutationName.setStatus, "logout");
      errorNotification("web3.error.web3_err");
    }
  },
  async [Web3ActionName.setNetwork]({ state, commit }, network?: string) {
    const selectedNetwork = network || (await web3Instance.getId());
    commit(Web3MutationName.setNetwork, selectedNetwork);
  }
};
 
export default actions;