Skip to content

Instantly share code, notes, and snippets.

@nichanank
Last active June 28, 2021 10:36
Show Gist options
  • Save nichanank/737f064e81ab1a33929e449a12337f1d to your computer and use it in GitHub Desktop.
Save nichanank/737f064e81ab1a33929e449a12337f1d to your computer and use it in GitHub Desktop.
Barebones gists for Sablier dapp tutorial
import { InjectedConnector } from "@web3-react/injected-connector";
export const injected = new InjectedConnector({
supportedChainIds: [1, 3, 4, 5, 42]
});
import { useState, useEffect } from "react";
import { useWeb3React } from "@web3-react/core";
import { injected } from "./connectors";
export function useEagerConnect() {
const { activate, active } = useWeb3React();
const [tried, setTried] = useState(false);
useEffect(() => {
injected.isAuthorized().then(isAuthorized => {
if (isAuthorized) {
activate(injected, undefined, true).catch(() => {
setTried(true);
});
} else {
setTried(true);
}
});
}, [activate]); // intentionally only running on mount (make sure it's only mounted once :))
// if the connection worked, wait until we get confirmation of that to flip the flag
useEffect(() => {
if (!tried && active) {
setTried(true);
}
}, [tried, active]);
return tried;
}
export function useInactiveListener(suppress = false) {
const { active, error, activate } = useWeb3React();
useEffect(() => {
const { ethereum } = window;
if (ethereum && !active && !error && !suppress) {
const handleNetworkChanged = networkId => {
console.log("networkChanged", networkId);
activate(injected);
};
const handleAccountsChanged = accounts => {
console.log("accountsChanged", accounts);
if (accounts.length > 0) {
activate(injected);
}
};
ethereum.on("networkChanged", handleNetworkChanged);
ethereum.on("accountsChanged", handleAccountsChanged);
return () => {
ethereum.removeListener("networkChanged", handleNetworkChanged);
ethereum.removeListener("accountsChanged", handleAccountsChanged);
};
}
return () => {};
}, [active, error, suppress, activate]);
}
import React from "react";
import ReactDOM from "react-dom";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "@apollo/react-hooks";
import { ethers } from "ethers";
import { Web3ReactProvider } from '@web3-react/core';
import "./index.css";
import App from "./App";
function getLibrary(provider) {
const library = new ethers.providers.Web3Provider(provider)
library.pollingInterval = 10000
return library
}
// This is the official Sablier subgraph. You can replace it with your own, if you need to.
// See all subgraphs: https://thegraph.com/explorer/
const client = new ApolloClient({
uri: "https://api.thegraph.com/subgraphs/name/sablierhq/sablier",
});
ReactDOM.render(
<Web3ReactProvider getLibrary={getLibrary}>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</Web3ReactProvider>,
document.getElementById("root"),
);
import * as ethers from 'ethers'
export default class UncheckedJsonRpcSigner extends ethers.Signer {
constructor(signer) {
super()
ethers.utils.defineReadOnly(this, 'signer', signer)
ethers.utils.defineReadOnly(this, 'provider', signer.provider)
}
getAddress() {
return this.signer.getAddress()
}
sendTransaction(transaction) {
return this.signer.sendUncheckedTransaction(transaction).then(hash => {
return {
hash: hash,
nonce: null,
gasLimit: null,
gasPrice: null,
data: null,
value: null,
chainId: null,
confirmations: 0,
from: null,
wait: confirmations => {
return this.signer.provider.waitForTransaction(hash, confirmations)
}
}
})
}
signMessage(message) {
return this.signer.signMessage(message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment