Skip to content

Commit

Permalink
work paused until unblock
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Nov 29, 2022
1 parent c7ec0cf commit bade728
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/admin-ui/src/common/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export interface Routes {
/**
* Return array of available clients to connect a wallet (i.e metmask)
*/
ethClientsGet: () => Promise<EthClient[]>;
ethClientsGet: () => Promise<EthClientWallet[]>;

/**
* Return formated core update data
Expand Down
4 changes: 4 additions & 0 deletions packages/admin-ui/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,10 +1022,14 @@ export enum EthClientRemote {

export type EthClientStatus = EthClientStatusOk | EthClientStatusError;

export type EthClientWallet = EthClientWalletOk | EthClientStatusError;

export type EthClientStatusOk =
// All okay, client is functional
{ ok: true; url: string; dnpName: string };

export type EthClientWalletOk = EthClientStatusOk & { chainId: string };

export type EthClientStatusError =
// Unexpected error
| { ok: false; code: "UNKNOWN_ERROR"; error: ErrorSerialized }
Expand Down
54 changes: 46 additions & 8 deletions packages/admin-ui/src/pages/dashboard/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";
import { ethers } from "ethers";
import CardList from "components/CardList";
import { useApi } from "api";
import ErrorView from "components/ErrorView";
import Ok from "components/Ok";
import Alert from "react-bootstrap/esm/Alert";

import { EthClientWalletOk } from "common";
import Button from "components/Button";
import { prettyDnpName } from "utils/format";
declare global {
interface Window {
ethereum: any;
Expand All @@ -15,9 +16,35 @@ declare global {
export default function ConnectWallet() {
const ethClients = useApi.ethClientsGet();

async function connectWallet() {
await window.ethereum.enable();
const provider = new ethers.providers.Web3Provider(window.ethereum);
async function walletConnect(ethClient: EthClientWalletOk) {
// TODO: add testnets ethclients. Chain ID will change: https://chainlist.org/
if (ethClient.ok) {
try {
// https://eips.ethereum.org/EIPS/eip-3326
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0xf00" }]
});
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
try {
// https://eips.ethereum.org/EIPS/eip-3085
await window.ethereum.request({
method: "wallet_addEthereumChain",
// IMPORTANT! RPC without HTTPs is not allowed
// IMPORTANT! Add new chains with a default chain ID in metamask is not allowed
params: [{ chainId: ethClient.chainId, rpcUrls: [ethClient.url] }]
});
} catch (addError) {
// handle "add" error
throw addError;
}
}
// handle other "switch" errors
throw switchError;
}
}
}

if (ethClients.error)
Expand All @@ -34,9 +61,20 @@ export default function ConnectWallet() {
</Alert>
) : (
<CardList className="connect-wallet">
{ethClients.data.map(ethClient => (
<span></span>
))}
{ethClients.data.map(
ethClient =>
ethClient.ok && (
<div className="connect-wallet-item">
<span>{prettyDnpName(ethClient.dnpName)}</span>
<Button
variant="dappnode"
onClick={() => walletConnect(ethClient)}
>
Connect
</Button>
</div>
)
)}
</CardList>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SubTitle from "components/SubTitle";
import Title from "components/Title";

import "./dashboard.scss";
import ConnectWallet from "./ConnectWallet";

export default function Dashboard() {
return (
Expand All @@ -19,6 +20,9 @@ export default function Dashboard() {
<div className="dashboard-right">
<SubTitle>Package updates</SubTitle>
<PackageUpdates />

<SubTitle>Connect wallet</SubTitle>
<ConnectWallet />
</div>

<div className="dashboard-left">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
grid-template-columns: repeat(auto-fill, minmax(7.5em, 1fr));
}

.package-updates {
.package-updates,
.connect-wallet {
// Give the single card a max width that matches the other cards
grid-column: 1 / 4;
}
Expand Down Expand Up @@ -76,7 +77,8 @@

// Package updates card

.package-update-item {
.package-update-item,
.connect-wallet-item {
display: flex;
justify-content: space-between;
align-items: center;
Expand Down
6 changes: 4 additions & 2 deletions packages/dappmanager/src/calls/ethClientsGet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EthClient } from "../types";
import { EthClientWallet } from "../types";

export async function ethClientsGet(): Promise<EthClient[]> {}
export async function ethClientsGet(): Promise<EthClientWallet[]> {
return [];
}

0 comments on commit bade728

Please sign in to comment.