2021-10-19 14:06:16 +02:00
|
|
|
import { getNetworkDisplayName } from '@hooks/useNetworkMetadata'
|
2021-12-10 12:33:47 +01:00
|
|
|
import { LoggerInstance } from '@oceanprotocol/lib'
|
2021-08-31 16:56:48 +02:00
|
|
|
import { getOceanConfig } from './ocean'
|
2021-03-30 15:37:30 +02:00
|
|
|
|
2021-03-17 11:44:26 +01:00
|
|
|
export function accountTruncate(account: string): string {
|
2021-11-23 13:53:09 +01:00
|
|
|
if (!account || account === '') return
|
2021-03-17 11:44:26 +01:00
|
|
|
const middle = account.substring(6, 38)
|
|
|
|
const truncated = account.replace(middle, '…')
|
|
|
|
return truncated
|
|
|
|
}
|
|
|
|
|
2021-07-15 17:03:03 +02:00
|
|
|
export async function addCustomNetwork(
|
2021-03-30 15:37:30 +02:00
|
|
|
web3Provider: any,
|
2021-09-16 19:44:34 +02:00
|
|
|
network: EthereumListsChain
|
2021-07-15 17:03:03 +02:00
|
|
|
): Promise<void> {
|
2021-10-06 11:30:17 +02:00
|
|
|
// Always add explorer URL from ocean.js first, as it's null sometimes
|
|
|
|
// in network data
|
|
|
|
const blockExplorerUrls = [
|
|
|
|
getOceanConfig(network.networkId).explorerUri,
|
|
|
|
network.explorers && network.explorers[0].url
|
|
|
|
]
|
|
|
|
|
|
|
|
const newNetworkData = {
|
2021-03-30 15:37:30 +02:00
|
|
|
chainId: `0x${network.chainId.toString(16)}`,
|
2021-09-16 19:44:34 +02:00
|
|
|
chainName: getNetworkDisplayName(network, network.chainId),
|
|
|
|
nativeCurrency: network.nativeCurrency,
|
|
|
|
rpcUrls: network.rpc,
|
2021-10-06 11:30:17 +02:00
|
|
|
blockExplorerUrls
|
2021-03-30 15:37:30 +02:00
|
|
|
}
|
2021-07-15 17:03:03 +02:00
|
|
|
try {
|
|
|
|
await web3Provider.request({
|
|
|
|
method: 'wallet_switchEthereumChain',
|
2021-10-06 11:30:17 +02:00
|
|
|
params: [{ chainId: newNetworkData.chainId }]
|
2021-07-15 17:03:03 +02:00
|
|
|
})
|
|
|
|
} catch (switchError) {
|
|
|
|
if (switchError.code === 4902) {
|
2021-09-16 19:44:34 +02:00
|
|
|
await web3Provider.request(
|
2021-07-15 17:03:03 +02:00
|
|
|
{
|
|
|
|
method: 'wallet_addEthereumChain',
|
2021-10-06 11:30:17 +02:00
|
|
|
params: [newNetworkData]
|
2021-07-15 17:03:03 +02:00
|
|
|
},
|
|
|
|
(err: string, added: any) => {
|
|
|
|
if (err || 'error' in added) {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.error(
|
2021-07-15 17:03:03 +02:00
|
|
|
`Couldn't add ${network.name} (0x${
|
|
|
|
network.chainId
|
2021-08-31 16:56:48 +02:00
|
|
|
}) network to MetaMask, error: ${err || added.error}`
|
2021-07-15 17:03:03 +02:00
|
|
|
)
|
|
|
|
} else {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.log(
|
2021-07-15 17:03:03 +02:00
|
|
|
`Added ${network.name} (0x${network.chainId}) network to MetaMask`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.error(
|
2021-08-31 16:56:48 +02:00
|
|
|
`Couldn't add ${network.name} (0x${network.chainId}) network to MetaMask, error: ${switchError}`
|
2021-07-15 17:03:03 +02:00
|
|
|
)
|
2021-03-30 15:37:30 +02:00
|
|
|
}
|
2021-07-15 17:03:03 +02:00
|
|
|
}
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.log(
|
|
|
|
`Added ${network.name} (0x${network.chainId}) network to MetaMask`
|
|
|
|
)
|
2021-03-30 15:37:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:12:22 +02:00
|
|
|
export async function addTokenToWallet(
|
|
|
|
web3Provider: any,
|
|
|
|
address: string,
|
|
|
|
symbol: string,
|
|
|
|
logo?: string
|
|
|
|
): Promise<void> {
|
|
|
|
const image =
|
|
|
|
logo ||
|
|
|
|
'https://raw.githubusercontent.com/oceanprotocol/art/main/logo/token.png'
|
|
|
|
|
2021-03-30 15:37:30 +02:00
|
|
|
const tokenMetadata = {
|
|
|
|
type: 'ERC20',
|
2021-05-17 16:12:22 +02:00
|
|
|
options: { address, symbol, image, decimals: 18 }
|
2021-03-30 15:37:30 +02:00
|
|
|
}
|
2021-05-17 16:12:22 +02:00
|
|
|
|
2021-03-30 15:37:30 +02:00
|
|
|
web3Provider.sendAsync(
|
|
|
|
{
|
|
|
|
method: 'wallet_watchAsset',
|
|
|
|
params: tokenMetadata,
|
|
|
|
id: Math.round(Math.random() * 100000)
|
|
|
|
},
|
2021-05-17 16:12:22 +02:00
|
|
|
(err: { code: number; message: string }, added: any) => {
|
2021-03-30 15:37:30 +02:00
|
|
|
if (err || 'error' in added) {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.error(
|
2021-03-30 15:37:30 +02:00
|
|
|
`Couldn't add ${tokenMetadata.options.symbol} (${
|
|
|
|
tokenMetadata.options.address
|
2021-05-17 16:12:22 +02:00
|
|
|
}) to MetaMask, error: ${err.message || added.error}`
|
2021-03-30 15:37:30 +02:00
|
|
|
)
|
|
|
|
} else {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.log(
|
2021-03-30 15:37:30 +02:00
|
|
|
`Added ${tokenMetadata.options.symbol} (${tokenMetadata.options.address}) to MetaMask`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|