Skip to content

Commit

Permalink
Merge pull request #201 from kleros/feat/xdai-activation
Browse files Browse the repository at this point in the history
Feat: xDAI activation
  • Loading branch information
hbarcelos authored Jul 21, 2021
2 parents 3684ca5 + 62b0b96 commit 4fba2ce
Show file tree
Hide file tree
Showing 37 changed files with 1,780 additions and 999 deletions.
8 changes: 4 additions & 4 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ REACT_APP_WEB3_FALLBACK_SOKOL_URL=wss://sokol.poa.network/wss
REACT_APP_WEB3_FALLBACK_SOKOL_HTTPS_URL=https://sokol.poa.network

REACT_APP_KLEROS_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_KLEROS_LIQUID_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_KLEROS_LIQUID_EXTRA_VIEWS_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_PINAKION_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_KLEROS_LIQUID_XDAI_ADDRESS=0x9C1dA9A04925bDfDedf0f6421bC7EEa8305F9002
REACT_APP_KLEROS_LIQUID_EXTRA_VIEWS_XDAI_ADDRESS=0xFA71f907B48f27d22f670d9E446f8137b0769e4B
REACT_APP_PINAKION_XDAI_ADDRESS=0xcb3231aBA3b451343e0Fddfc45883c842f223846
REACT_APP_RAW_PINAKION_XDAI_ADDRESS=0x37b60f4e9a31a64ccc0024dce7d0fd07eaa0f7b3
REACT_APP_POLICY_REGISTRY_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_POLICY_REGISTRY_XDAI_ADDRESS=0x9d494768936b6bDaabc46733b8D53A937A6c6D7e
REACT_APP_UNISWAP_V2_ROUTER_02_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_UNISWAP_V2_FACTORY_XDAI_ADDRESS=0x0000000000000000000000000000000000000000
REACT_APP_TOKEN_BRIDGE_XDAI_ADDRESS=0xf6A78083ca3e2a662D6dd1703c939c8aCE2e268d
Expand Down
8 changes: 4 additions & 4 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
REACT_APP_WEB3_FALLBACK_SOKOL_HTTPS_URL='https://sokol.poa.network'

REACT_APP_KLEROS_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_KLEROS_LIQUID_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_KLEROS_LIQUID_EXTRA_VIEWS_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_PINAKION_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_KLEROS_LIQUID_XDAI_ADDRESS='0x9C1dA9A04925bDfDedf0f6421bC7EEa8305F9002'
REACT_APP_KLEROS_LIQUID_EXTRA_VIEWS_XDAI_ADDRESS='0xFA71f907B48f27d22f670d9E446f8137b0769e4B'
REACT_APP_PINAKION_XDAI_ADDRESS='0xcb3231aBA3b451343e0Fddfc45883c842f223846'
REACT_APP_RAW_PINAKION_XDAI_ADDRESS='0x37b60f4e9a31a64ccc0024dce7d0fd07eaa0f7b3'
REACT_APP_POLICY_REGISTRY_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_POLICY_REGISTRY_XDAI_ADDRESS='0x9d494768936b6bDaabc46733b8D53A937A6c6D7e'
REACT_APP_UNISWAP_V2_ROUTER_02_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_UNISWAP_V2_FACTORY_XDAI_ADDRESS='0x0000000000000000000000000000000000000000'
REACT_APP_TOKEN_BRIDGE_XDAI_ADDRESS='0xf6A78083ca3e2a662D6dd1703c939c8aCE2e268d'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@
"react-time-ago": "^3.0.3",
"shallowequal": "^1.1.0",
"styled-components": "^5.3.0",
"use-persisted-state": "^0.3.3",
"web3": "1.3.6"
},
"volta": {
"node": "10.23.2",
"node": "10.24.1",
"yarn": "1.22.10"
}
}
27 changes: 15 additions & 12 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="theme-color" content="#000000" />
<!--
manifest.json provides metadata used when your web app is added to the
Expand All @@ -25,14 +22,19 @@
<title>Kleros · Court</title>
<!-- Hotjar Tracking Code for court.kleros.io -->
<script>
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:1844958,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
(function (h, o, t, j, a, r) {
h.hj =
h.hj ||
function () {
(h.hj.q = h.hj.q || []).push(arguments);
};
h._hjSettings = { hjid: 1844958, hjsv: 6 };
a = o.getElementsByTagName("head")[0];
r = o.createElement("script");
r.async = 1;
r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
a.appendChild(r);
})(window, document, "https://static.hotjar.com/c/hotjar-", ".js?sv=");
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<!-- <script async src="https://www.googletagmanager.com/gtag/js?id=UA-169659909-2"></script> -->
Expand All @@ -45,6 +47,7 @@
</head>
<body>
<noscript> You need to enable JavaScript to run this app. </noscript>
<div id="banner-root"></div>
<div id="root"></div>
<!--
This HTML file is a template.
Expand Down
54 changes: 45 additions & 9 deletions src/api/side-chain/chain-params.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import logoXPNK from "../../assets/images/xPNK.png";
import logoStPNK from "../../assets/images/stPNK.png";

import { getBaseUrl } from "../../helpers/block-explorer";

export const Tokens = {
PNK: "PNK",
stPNK: "stPNK",
};

const supportedSideChains = {
// xDai
// 100: {
// chainId: 100,
// chainName: "xDAI",
// nativeCurrency: { name: "xDAI", symbol: "xDAI", decimals: 18 },
// rpcUrls: [ensureEnv("REACT_APP_WEB3_FALLBACK_XDAI_HTTPS_URL")],
// blockExplorerUrls: [getBaseUrl(100)],
// bridgeAppUrl: `https://omni.xdaichain.com/bridge?from=100&to=1&token=${ensureEnv("REACT_APP_PINAKION_ADDRESS")}`,
// mainChainId: 1,
// },
100: {
chainId: 100,
chainName: "xDAI",
nativeCurrency: { name: "xDAI", symbol: "xDAI", decimals: 18 },
rpcUrls: [ensureEnv("REACT_APP_WEB3_FALLBACK_XDAI_HTTPS_URL")],
blockExplorerUrls: [getBaseUrl(100)],
bridgeAppUrl: `https://omni.xdaichain.com/bridge?from=1&to=100&token=${ensureEnv("REACT_APP_PINAKION_ADDRESS")}`,
mainChainId: 1,
tokens: {
[Tokens.PNK]: {
address: ensureEnv("REACT_APP_RAW_PINAKION_XDAI_ADDRESS"),
symbol: "PNK",
decimals: 18,
image: `${window.location.origin}${logoXPNK}`,
},
[Tokens.stPNK]: {
address: ensureEnv("REACT_APP_PINAKION_XDAI_ADDRESS"),
symbol: "stPNK",
decimals: 18,
image: `${window.location.origin}${logoStPNK}`,
},
},
},
// Sokol
77: {
chainId: 77,
Expand All @@ -22,6 +44,20 @@ const supportedSideChains = {
"REACT_APP_PINAKION_KOVAN_ADDRESS"
)}`,
mainChainId: 42,
tokens: {
[Tokens.PNK]: {
address: ensureEnv("REACT_APP_RAW_PINAKION_SOKOL_ADDRESS"),
symbol: "PNK",
decimals: 18,
image: `${window.location.origin}${logoXPNK}`,
},
[Tokens.stPNK]: {
address: ensureEnv("REACT_APP_PINAKION_SOKOL_ADDRESS"),
symbol: "stPNK",
decimals: 18,
image: `${window.location.origin}${logoStPNK}`,
},
},
},
};

Expand Down
4 changes: 1 addition & 3 deletions src/api/side-chain/create-side-chain-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import * as xDai from "./xdai-api";

export default async function createSideChainApi(provider) {
const web3 = new Web3(provider);
// Accounts is ignored because it's purpose is only to check whether
// the provider can sign transactions or not.
const [chainId] = await Promise.all([web3.eth.getChainId(), web3.eth.requestAccounts()]);
const chainId = await web3.eth.getChainId();

if (!isSupportedSideChain(chainId)) {
throw new Error(`Unsuported chain ID: ${chainId}`);
Expand Down
28 changes: 0 additions & 28 deletions src/api/side-chain/create-switch-to-side-chain.js

This file was deleted.

72 changes: 72 additions & 0 deletions src/api/side-chain/create-watch-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Tokens } from "./chain-params";

export default function createWatchToken({ getChainParams }) {
return async function requestWatchToken(provider, token) {
if (![Tokens.stPNK, Tokens.PNK].includes(token)) {
throw new Error(`Invalid token: ${token}`);
}

const chainId = Number.parseInt(
await provider.request({
method: "eth_chainId",
}),
16
);

const tokenParams = getChainParams(chainId)?.tokens ?? {};
const tokenData = tokenParams[token];

if (tokenData && !isAssetWatched({ ...tokenData, chainId })) {
try {
await addToken(provider, tokenData);
storeWatchedAsset({ ...tokenData, chainId });
} catch (err) {
console.warn(`Error when adding token ${token}:`, err);
}
}
};
}

async function addToken(provider, { address, symbol, decimals = 18, image }) {
/**
* FIXME: Apparently this call is broken and the promise will resolve even if the user
* rejects the request to watch the asset.
*
* @see { @link https://github.com/MetaMask/metamask-extension/issues/11377 }
*/
return await provider.request({
method: "wallet_watchAsset",
params: {
type: "ERC20",
options: {
address,
symbol,
decimals,
image,
},
},
});
}

const getStorageKey = ({ chainId, symbol, address }) => `@@kleros/court/tokens/${symbol}/${chainId}/${address}`;

function isAssetWatched({ chainId, symbol, address }) {
const key = getStorageKey({ chainId, symbol, address });

try {
return JSON.parse(window.localStorage.getItem(key)) === true;
} catch (err) {
console.warn("Error in isAssetWatched", err);
return false;
}
}

function storeWatchedAsset({ chainId, symbol, address }) {
const key = getStorageKey({ chainId, symbol, address });

try {
window.localStorage.setItem(key, "true");
} catch (err) {
console.warn("Error in isAssetWatched", err);
}
}
10 changes: 6 additions & 4 deletions src/api/side-chain/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import createSwitchToSideChain from "./create-switch-to-side-chain";
import { getSideChainParamsFromMainChainId } from "./chain-params";
import createWatchToken from "./create-watch-token";
import { getSideChainParams } from "./chain-params";

export { SideChainApiProvider, useSideChainApi } from "./react-adapters";
export * from "./chain-params";

export const requestSwitchToSideChain = createSwitchToSideChain({
getCounterPartyChainParams: getSideChainParamsFromMainChainId,
export { default as requestSwitchNetwork } from "./request-switch-network";

export const requestWatchToken = createWatchToken({
getChainParams: getSideChainParams,
});
10 changes: 9 additions & 1 deletion src/api/side-chain/react-adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ const defaultRenderOnLoading = (
</Spin>
);

const defaultRenderOnError = (error) => <Alert type="error" message={error.message} />;
const defaultRenderOnError = (error) => (
<Alert
css={`
width: 100%;
`}
type="error"
message={error.message}
/>
);

SideChainApiProvider.defaultProps = {
renderOnLoading: defaultRenderOnLoading,
Expand Down
44 changes: 44 additions & 0 deletions src/api/side-chain/request-switch-network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Web3 from "web3";
import { getSideChainParams, isSupportedSideChain } from "./chain-params";

const { toHex } = Web3.utils;

export default async function requestSwitchNetwork(provider, destinationChainId) {
try {
return await switchNetwork(provider, { chainId: destinationChainId });
} catch (err) {
// This error code indicates that the chain has not been added to MetaMask
// if it is not, then add it into the user MetaMask
if (err.code === 4902 && isSupportedSideChain(destinationChainId)) {
return await addNetwork(provider, getSideChainParams(destinationChainId));
}

throw err;
}
}

async function addNetwork(provider, { chainId, chainName, nativeCurrency, rpcUrls, blockExplorerUrls }) {
return await provider.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: toHex(chainId),
chainName: chainName,
nativeCurrency: nativeCurrency,
rpcUrls: rpcUrls,
blockExplorerUrls: blockExplorerUrls,
},
],
});
}

async function switchNetwork(provider, { chainId }) {
return await provider.request({
method: "wallet_switchEthereumChain",
params: [
{
chainId: toHex(chainId),
},
],
});
}
21 changes: 21 additions & 0 deletions src/assets/images/kleros-logo-flat-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/images/stPNK.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/images/xPNK.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4fba2ce

Please sign in to comment.