1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-28 00:27:49 +02:00
market/src/@utils/web3.ts
mihaisc 8d1782a800
Restore order (#1068)
* minor refactors

* minor refactors

* fixes

* buy dt

* consumePrice + estimation

* various fixes

* cleanup

* fix build

* fix ssh issue

* feedback

* build fix

* ssh fix

* remove console.log

* suggested fixes

* other fixes

* switch to decimal

* more fixes

* more fixes

* fix

* some fee refactors

* more fee refactoring

* lib update, fre rename

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* minor refactors

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* build fixes

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* update + more refactoring

* calc price

* fix build

* restore accountId in effect

* fix order

* fix build and update lib

* fix order index

* fix comments

* pool fix

* remove console.log

* fix order fixed rate exchange

* fixed free order and messaging

* add comment

* minor type fix

* more type fixes
2022-02-14 08:27:36 -08:00

113 lines
3.2 KiB
TypeScript

import { getNetworkDisplayName } from '@hooks/useNetworkMetadata'
import { LoggerInstance } from '@oceanprotocol/lib'
import Web3 from 'web3'
import { getOceanConfig } from './ocean'
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, network.chainId),
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`
)
}
}
)
}