Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Ethers.js #167

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "0.9.1",
"version": "0.10.0",
"homepage": "/",
"private": true,
"dependencies": {
Expand All @@ -13,6 +13,11 @@
"@near-wallet-selector/near-wallet": "^7.9.0",
"@near-wallet-selector/neth": "^7.9.0",
"@near-wallet-selector/sender": "^7.9.0",
"@web3-onboard/core": "^2.16.2",
"@web3-onboard/injected-wallets": "^2.8.4",
"@web3-onboard/ledger": "^2.4.4",
"@web3-onboard/react": "^2.7.2",
"@web3-onboard/walletconnect": "^2.3.5",
"big.js": "^6.1.1",
"bn.js": "^5.1.1",
"bootstrap": "^5.2.1",
Expand All @@ -21,7 +26,7 @@
"error-polyfill": "^0.1.2",
"local-storage": "^2.0.0",
"near-api-js": "^0.45.1",
"near-social-vm": "git+https://github.com/NearSocial/VM.git#1.2.0",
"near-social-vm": "git+https://github.com/NearSocial/VM.git#1.3.0",
"prettier": "^2.7.1",
"react": "^18.2.0",
"react-bootstrap": "^2.5.0",
Expand Down
43 changes: 27 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
import { setupNeth } from "@near-wallet-selector/neth";
import { setupModal } from "@near-wallet-selector/modal-ui";
import EmbedPage from "./pages/EmbedPage";
import { useAccount, useInitNear, useNear, utils } from "near-social-vm";
import {
useAccount,
useInitNear,
useNear,
utils,
EthersProviderContext,
} from "near-social-vm";
import Big from "big.js";
import { NavigationWrapper } from "./components/navigation/NavigationWrapper";
import { NetworkId, Widgets } from "./data/widgets";
import { useEthersProviderContext } from "./data/web3";

export const refreshAllowanceObj = {};
const documentationHref = "https://social.near-docs.io/";
Expand All @@ -32,6 +39,8 @@ function App(props) {
const [walletModal, setWalletModal] = useState(null);
const [widgetSrc, setWidgetSrc] = useState(null);

const ethersProviderContext = useEthersProviderContext();

const { initNear } = useInitNear();
const near = useNear();
const account = useAccount();
Expand Down Expand Up @@ -143,21 +152,23 @@ function App(props) {

return (
<div className="App">
<Router basename={process.env.PUBLIC_URL}>
<Switch>
<Route path={"/embed/:widgetSrc*"}>
<EmbedPage {...passProps} />
</Route>
<Route path={"/edit/:widgetSrc*"}>
<NavigationWrapper {...passProps} />
<EditorPage {...passProps} />
</Route>
<Route path={"/:widgetSrc*"}>
<NavigationWrapper {...passProps} />
<ViewPage {...passProps} />
</Route>
</Switch>
</Router>
<EthersProviderContext.Provider value={ethersProviderContext}>
<Router basename={process.env.PUBLIC_URL}>
<Switch>
<Route path={"/embed/:widgetSrc*"}>
<EmbedPage {...passProps} />
</Route>
<Route path={"/edit/:widgetSrc*"}>
<NavigationWrapper {...passProps} />
<EditorPage {...passProps} />
</Route>
<Route path={"/:widgetSrc*"}>
<NavigationWrapper {...passProps} />
<ViewPage {...passProps} />
</Route>
</Switch>
</Router>
</EthersProviderContext.Provider>
</div>
);
}
Expand Down
130 changes: 130 additions & 0 deletions src/data/web3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { singletonHook } from "react-singleton-hook";
import { useEffect, useState } from "react";
import { init, useConnectWallet } from "@web3-onboard/react";
import injectedModule from "@web3-onboard/injected-wallets";
import walletConnectModule from "@web3-onboard/walletconnect";
import ledgerModule from "@web3-onboard/ledger";
import { ethers } from "ethers";
import ls from "local-storage";
import icon from "../images/near_social_icon.svg";

const web3onboardKey = "web3-onboard:connectedWallets";

const wcV1InitOptions = {
qrcodeModalOptions: {
mobileLinks: ["metamask", "argent", "trust"],
},
connectFirstChainId: true,
};

const walletConnect = walletConnectModule(wcV1InitOptions);
const ledger = ledgerModule();
const injected = injectedModule();

// initialize Onboard
export const onboard = init({
wallets: [injected, walletConnect, ledger],
chains: [
{
id: 1,
token: "ETH",
label: "Ethereum Mainnet",
rpcUrl: "https://rpc.ankr.com/eth",
},
{
id: 3,
token: "ETH",
label: "Ropsten - Ethereum Testnet",
rpcUrl: "https://rpc.ankr.com/eth_ropsten",
},
{
id: 5,
token: "ETH",
label: "Goerli - Ethereum Testnet",
rpcUrl: "https://rpc.ankr.com/eth_goerli",
},
{
id: "0x4e454152",
token: "ETH",
label: "Aurora Mainnet",
rpcUrl: "https://mainnet.aurora.dev",
},
{
id: 137,
token: "MATIC",
label: "Matic Mainnet",
rpcUrl: "https://rpc.ankr.com/polygon",
},
{
id: 324,
token: "ETH",
label: "zkSync",
rpcUrl: "https://zksync2-mainnet.zksync.io",
},
{
id: 56,
token: "BNB",
label: "Binance Smart Chain Mainnet",
rpcUrl: "https://bsc.publicnode.com",
},
{
id: 42161,
token: "ETH",
label: "Arbitrum One Mainnet",
rpcUrl: "https://endpoints.omniatech.io/v1/arbitrum/one/public",
},
],
appMetadata: {
name: "NEAR Social",
icon,
description: "NEAR Social",
},
theme: "dark",
containerElements: {
// connectModal: '#near-social-navigation-bar',
// accountCenter: "#near-social-web3-account",
},
});

const defaultEthersProviderContext = { useConnectWallet };

export const useEthersProviderContext = singletonHook(
defaultEthersProviderContext,
() => {
const [{ wallet }] = useConnectWallet();
const [ethersProvider, setEthersProvider] = useState(
defaultEthersProviderContext
);

useEffect(() => {
(async () => {
const walletsSub = onboard.state.select("wallets");
const { unsubscribe } = walletsSub.subscribe((wallets) => {
const connectedWallets = wallets.map(({ label }) => label);
ls.set(web3onboardKey, connectedWallets);
});

const previouslyConnectedWallets = ls.get(web3onboardKey) || [];

if (previouslyConnectedWallets) {
// You can also auto connect "silently" and disable all onboard modals to avoid them flashing on page load
await onboard.connectWallet({
autoSelect: {
label: previouslyConnectedWallets[0],
disableModals: true,
},
});
}
})();
}, []);

useEffect(() => {
setEthersProvider({
provider: wallet?.provider,
useConnectWallet,
});
}, [wallet]);

return ethersProvider;
}
);
8 changes: 7 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@

--font-weight-medium: 500;
--font-weight-bold: 600;
}

/* For web3 onboard */
--account-center-z-index: 999;
--account-center-position-top: 64px;
--account-center-position-right: -10px;
}

2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ module.exports = function (env) {
fallback: {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
http: require.resolve("stream-http"),
https: require.resolve("https-browserify"),
},
// Fix for using `yarn link "near-social-vm"`
alias: {
Expand Down
Loading