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?