addEtherNetwork
import { addEtherNetwork } from "@forta/usemetamask"// ..
const Matic = { chainId: "0x89", chainName: "Matic Mainnet", rpcUrl: "https://rpc-mainnet.maticvigil.com", blockExplorerUrl: "https://explorer.matic.network", symbol: "MATIC",}
// invoke anywhere, will execute only if provider exists and isMetamaskaddEtherNetwork(Matic)
addEtherToken
import { addEtherToken } from "@forta/usemetamask"// ..
addEtherToken({ address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", symbol: "USDT",})
getMetamaskProvider
import { getMetamaskProvider } from "@forta/usemetamask"// ..
// returns window.ethereum API with ts definitionsconst provider = getMetamaskProvider()if (provider) { // null if window.ethereum `undefined` Or `isMetamask == false` console.log(`isMetamask: ${provider.isMetaMask}`)}
switchEtherNetwork
import { switchEtherNetwork } from "@forta/usemetamask"// ..
const GoerliChainId = "0x5"switchEtherNetwork(GoerliChainId)
switchOrAppendNetwork
This helper does the following:
- Tries to Switch to the Network
chainId
- If Switch fails then we try to append Network to Metamask
- If Network was added succesfully it switches to the desired Network
import { switchOrAppendNetwork } from "@forta/usemetamask"// ..
const Matic = { chainId: "0x89", chainName: "Matic Mainnet", rpcUrl: "https://rpc-mainnet.maticvigil.com", blockExplorerUrl: "https://explorer.matic.network", currencySymbol: "MATIC",}switchOrAppendNetwork(Matic)
sendEther
import { sendEther } from "@forta/usemetamask"// ..
sendEther({ from: "0x3c0e20fCA6d2E084127D056377a5f35294503447", to: "0x3c0e20fCA6d2E084127D056377a5f35294503447", value: parse.toTxWei(0.5) /* 0.5 in ETH */,})
metamaskRequest
It validates if Metamask exists in global and resolves with it's window.ethereum.request
function.
import { metamaskRequest } from "@forta/usemetamask"// ..
metamaskRequest({ method: "eth_requestAccounts",})
connectToMetamask
Will request accounts to MetaMask and resolve with the connected account string.
import { connectToMetamask } from "@forta/usemetamask"// ..
connectToMetamask().then((account) => { console.log(account)})
getFIATValue
Returns a formatted string with a token FIAT value.
import { getFIATValue } from "@forta/usemetamask"// ..
const tokenBalance = 1000const tokenPriceInUSD = 22.32// you can replace USD with EUR or JPYconst balanceInUSD = getFIATValue(tokenBalance, tokenPriceInUSD, "USD") // 22,320
⚠️
Note: In versions before 0.2.0
the method was named getFIATBalance.
See release notes