-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from kleros/feat/xdai-activation
Feat: xDAI activation
- Loading branch information
Showing
37 changed files
with
1,780 additions
and
999 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
], | ||
}); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.