0

I am trying to use Web3.js (Ethereum JavaScript API) in a ReactJS app with Redux / Saga.

This is my GetWeb3 function:

const GetWeb3 = () =>
  new Promise((resolve, reject) => {
    window.addEventListener("load", () => {
      let web3 = window.web3;
      const alreadyInjected = typeof web3 !== "undefined";
      if (alreadyInjected) {
        web3 = new Web3(web3.currentProvider);
        console.log("Injected web3 detected.");
        resolve(web3);
      } else {
        const provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");
        web3 = new Web3(provider);
        console.log("No web3 instance injected, using Local web3.");
        resolve(web3);
      }
    });
  });

export default GetWeb3;

and I am trying to use this inside a saga like this:

import { call, put } from "redux-saga/effects";
import truffleContract from "truffle-contract";

import GetWeb3 from "../../../Utils/GetWeb3";
import SimpleStorageContract from "../../../Data/SimpleStorageContract.json";

import { loadWeb3Done, loadWeb3Error } from "./Web3.actionCreators";

export function* loadWeb3(action) {
  try {
    const web3 = yield call(GetWeb3());
    const accounts = yield call(web3.eth.getAccounts());

    console.log('accounts', accounts);

    const contract = truffleContract(SimpleStorageContract);
    contract.setProvider(web3.currentProvider);
    const instance = yield call(contract.deployed());

    yield put(
      loadWeb3Done({
        web3,
        accounts,
        contract: instance
      })
    );
  } catch (e) {
    yield put(loadWeb3Error(e));
  }
}

When my app runs with the above, I am seeing this error:

index.js:2178 uncaught at check call: argument [object Promise] is not a function stack_frame_overlay_proxy_console @ index.js:2178

GetWeb3.js:14 Injected web3 detected.

and I am not seeing the console log of accounts.

Any ideas what I might be doing wrong here?

0
const web3 = yield call(GetWeb3);

call should receive function as a parameter, not result of function's execution. Same applies for other calls

  • Ah, I see. I made the change to all other call functions, but it looks like the second or third call is not executing, i.e. loading the accounts or console logging it. I converted this original example code: pastebin.com/pfWQCzbj (this works as intended on it's own, I just need it to work inside saga). – Latheesan Oct 13 '18 at 22:35
  • have you tried const accounts = yield cps(web3.eth.getAccounts);? – Aquila Oct 13 '18 at 22:53
  • I had already done that. The reason why it wasn't working (i.e. loading the accounts) after changing all the call function was because I was not logged into MetaMask. Thanks. – Latheesan Oct 14 '18 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.