163 lines
4.3 KiB
TypeScript
163 lines
4.3 KiB
TypeScript
import { getNetworkDisplayName } from '@hooks/useNetworkMetadata'
|
|
import { LoggerInstance } from '@oceanprotocol/lib'
|
|
import Web3 from 'web3'
|
|
import { getOceanConfig } from './ocean'
|
|
import { AbiItem } from 'web3-utils/types'
|
|
|
|
export function accountTruncate(account: string): string {
|
|
if (!account || account === '') return
|
|
const middle = account.substring(6, 38)
|
|
const truncated = account.replace(middle, '…')
|
|
return truncated
|
|
}
|
|
/**
|
|
* returns a dummy web3 instance, only usable to get info from the chain
|
|
* @param chainId
|
|
* @returns Web3 instance
|
|
*/
|
|
export async function getDummyWeb3(chainId: number): Promise<Web3> {
|
|
const config = getOceanConfig(chainId)
|
|
return new Web3(config.nodeUri)
|
|
}
|
|
|
|
export async function addCustomNetwork(
|
|
web3Provider: any,
|
|
network: EthereumListsChain
|
|
): Promise<void> {
|
|
// 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 = {
|
|
chainId: `0x${network.chainId.toString(16)}`,
|
|
chainName: getNetworkDisplayName(network),
|
|
nativeCurrency: network.nativeCurrency,
|
|
rpcUrls: network.rpc,
|
|
blockExplorerUrls
|
|
}
|
|
try {
|
|
await web3Provider.request({
|
|
method: 'wallet_switchEthereumChain',
|
|
params: [{ chainId: newNetworkData.chainId }]
|
|
})
|
|
} catch (switchError) {
|
|
if (switchError.code === 4902) {
|
|
await web3Provider.request(
|
|
{
|
|
method: 'wallet_addEthereumChain',
|
|
params: [newNetworkData]
|
|
},
|
|
(err: string, added: any) => {
|
|
if (err || 'error' in added) {
|
|
LoggerInstance.error(
|
|
`Couldn't add ${network.name} (0x${
|
|
network.chainId
|
|
}) network to MetaMask, error: ${err || added.error}`
|
|
)
|
|
} else {
|
|
LoggerInstance.log(
|
|
`Added ${network.name} (0x${network.chainId}) network to MetaMask`
|
|
)
|
|
}
|
|
}
|
|
)
|
|
} else {
|
|
LoggerInstance.error(
|
|
`Couldn't add ${network.name} (0x${network.chainId}) network to MetaMask, error: ${switchError}`
|
|
)
|
|
}
|
|
}
|
|
LoggerInstance.log(
|
|
`Added ${network.name} (0x${network.chainId}) network to MetaMask`
|
|
)
|
|
}
|
|
|
|
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'
|
|
|
|
const tokenMetadata = {
|
|
type: 'ERC20',
|
|
options: { address, symbol, image, decimals: 18 }
|
|
}
|
|
|
|
web3Provider.sendAsync(
|
|
{
|
|
method: 'wallet_watchAsset',
|
|
params: tokenMetadata,
|
|
id: Math.round(Math.random() * 100000)
|
|
},
|
|
(err: { code: number; message: string }, added: any) => {
|
|
if (err || 'error' in added) {
|
|
LoggerInstance.error(
|
|
`Couldn't add ${tokenMetadata.options.symbol} (${
|
|
tokenMetadata.options.address
|
|
}) to MetaMask, error: ${err.message || added.error}`
|
|
)
|
|
} else {
|
|
LoggerInstance.log(
|
|
`Added ${tokenMetadata.options.symbol} (${tokenMetadata.options.address}) to MetaMask`
|
|
)
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
export async function getTokenBalance(
|
|
accountId: string,
|
|
decimals: number,
|
|
tokenAddress: string,
|
|
web3: Web3
|
|
): Promise<string> {
|
|
const minABI = [
|
|
{
|
|
constant: true,
|
|
inputs: [
|
|
{
|
|
name: '_owner',
|
|
type: 'address'
|
|
}
|
|
],
|
|
name: 'balanceOf',
|
|
outputs: [
|
|
{
|
|
name: 'balance',
|
|
type: 'uint256'
|
|
}
|
|
],
|
|
payable: false,
|
|
stateMutability: 'view',
|
|
type: 'function'
|
|
}
|
|
] as AbiItem[]
|
|
|
|
try {
|
|
const token = new web3.eth.Contract(minABI, tokenAddress, {
|
|
from: accountId
|
|
})
|
|
const balance = await token.methods.balanceOf(accountId).call()
|
|
const adjustedDecimalsBalance = `${balance}${'0'.repeat(18 - decimals)}`
|
|
return web3.utils.fromWei(adjustedDecimalsBalance)
|
|
} catch (e) {
|
|
LoggerInstance.error(`ERROR: Failed to get the balance: ${e.message}`)
|
|
}
|
|
}
|
|
|
|
export function getTokenBalanceFromSymbol(
|
|
balance: UserBalance,
|
|
symbol: string
|
|
): string {
|
|
if (!symbol) return
|
|
const baseTokenBalance = balance?.[symbol.toLocaleLowerCase()]
|
|
return baseTokenBalance || '0'
|
|
}
|