forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transactions): enable button to add tokens to metamask (Uniswap#…
…1311) * start on adding button for watching tokens * add tokens to metamask * add confirmation view * reset modal view
- Loading branch information
Showing
5 changed files
with
88 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { getTokenLogoURL } from './../components/CurrencyLogo/index' | ||
import { wrappedCurrency } from 'utils/wrappedCurrency' | ||
import { Currency, Token } from '@uniswap/sdk' | ||
import { useCallback, useState } from 'react' | ||
import { useActiveWeb3React } from 'hooks' | ||
|
||
export default function useAddTokenToMetamask( | ||
currencyToAdd: Currency | undefined | ||
): { addToken: () => void; success: boolean | undefined } { | ||
const { library, chainId } = useActiveWeb3React() | ||
|
||
const token: Token | undefined = wrappedCurrency(currencyToAdd, chainId) | ||
|
||
const [success, setSuccess] = useState<boolean | undefined>() | ||
|
||
const addToken = useCallback(() => { | ||
if (library && library.provider.isMetaMask && library.provider.request && token) { | ||
library.provider | ||
.request({ | ||
method: 'wallet_watchAsset', | ||
params: { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
//@ts-ignore // need this for incorrect ethers provider type | ||
type: 'ERC20', | ||
options: { | ||
address: token.address, | ||
symbol: token.symbol, | ||
decimals: token.decimals, | ||
image: getTokenLogoURL(token.address) | ||
} | ||
} | ||
}) | ||
.then(success => { | ||
setSuccess(success) | ||
}) | ||
.catch(() => setSuccess(false)) | ||
} else { | ||
setSuccess(false) | ||
} | ||
}, [library, token]) | ||
|
||
return { addToken, success } | ||
} |
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