feat: add prerequisite checks before swap (#588)
* feat: add prerequisite checks before swap * fix: add missing authentication on desktop config call * refactor(wip): introduce swap error * refactor: use wrapWithSwapError * fix: log originalError instead of error * fix: show snackbar when error is unexpected
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
export class SwapError extends Error {
|
||||
snackbarMessage: string
|
||||
originalError?: Error
|
||||
|
||||
constructor(snackbarMessage: string, error?: Error) {
|
||||
super(error?.message || snackbarMessage)
|
||||
this.name = 'SwapError'
|
||||
this.originalError = error
|
||||
this.snackbarMessage = snackbarMessage
|
||||
}
|
||||
}
|
||||
|
||||
export function isSwapError(error: unknown): error is SwapError {
|
||||
return error instanceof Error && error.name === 'SwapError'
|
||||
}
|
||||
|
||||
export function wrapWithSwapError<T>(promise: Promise<T>, snackbarMessage: string): Promise<T> {
|
||||
return promise.catch((error: Error) => {
|
||||
throw new SwapError(snackbarMessage, error)
|
||||
})
|
||||
}
|
||||
+21
-1
@@ -2,7 +2,23 @@ import axios from 'axios'
|
||||
import { BEE_DESKTOP_LATEST_RELEASE_PAGE_API } from '../constants'
|
||||
import { DaiToken } from '../models/DaiToken'
|
||||
import { Token } from '../models/Token'
|
||||
import { postJson } from './net'
|
||||
import { getJson, postJson } from './net'
|
||||
|
||||
export interface BeeConfig {
|
||||
'api-addr': string
|
||||
'debug-api-addr': string
|
||||
'debug-api-enable': boolean
|
||||
password: string
|
||||
'swap-enable': boolean
|
||||
'swap-initial-deposit': bigint
|
||||
mainnet: boolean
|
||||
'full-node': boolean
|
||||
'cors-allowed-origins': string
|
||||
'resolver-options': string
|
||||
'use-postage-snapshot': boolean
|
||||
'data-dir': string
|
||||
'swap-endpoint'?: string
|
||||
}
|
||||
|
||||
export async function getBzzPriceAsDai(desktopUrl: string): Promise<Token> {
|
||||
const response = await axios.get(`${desktopUrl}/price`)
|
||||
@@ -23,6 +39,10 @@ export async function setJsonRpcInDesktop(desktopUrl: string, value: string): Pr
|
||||
})
|
||||
}
|
||||
|
||||
export function getDesktopConfiguration(desktopUrl: string): Promise<BeeConfig> {
|
||||
return getJson(`${desktopUrl}/config`)
|
||||
}
|
||||
|
||||
async function updateDesktopConfiguration(desktopUrl: string, values: Record<string, unknown>): Promise<void> {
|
||||
await postJson(`${desktopUrl}/config`, values)
|
||||
}
|
||||
|
||||
+12
-1
@@ -2,6 +2,16 @@ import { debounce } from '@material-ui/core'
|
||||
import { Contract, providers, Wallet, BigNumber as BN } from 'ethers'
|
||||
import { bzzABI, BZZ_TOKEN_ADDRESS } from './bzz-abi'
|
||||
|
||||
const NETWORK_ID = 100
|
||||
|
||||
async function getNetworkChainId(url: string): Promise<number> {
|
||||
const provider = new providers.JsonRpcProvider(url, NETWORK_ID)
|
||||
await provider.ready
|
||||
const network = await provider.getNetwork()
|
||||
|
||||
return network.chainId
|
||||
}
|
||||
|
||||
async function eth_getBalance(address: string, provider: providers.JsonRpcProvider): Promise<string> {
|
||||
if (!address.startsWith('0x')) {
|
||||
address = `0x${address}`
|
||||
@@ -78,7 +88,7 @@ export async function sendBzzTransaction(
|
||||
}
|
||||
|
||||
async function makeReadySigner(privateKey: string, jsonRpcProvider: string) {
|
||||
const provider = new providers.JsonRpcProvider(jsonRpcProvider, 100)
|
||||
const provider = new providers.JsonRpcProvider(jsonRpcProvider, NETWORK_ID)
|
||||
await provider.ready
|
||||
const signer = new Wallet(privateKey, provider)
|
||||
|
||||
@@ -86,6 +96,7 @@ async function makeReadySigner(privateKey: string, jsonRpcProvider: string) {
|
||||
}
|
||||
|
||||
export const Rpc = {
|
||||
getNetworkChainId,
|
||||
sendNativeTransaction,
|
||||
sendBzzTransaction,
|
||||
_eth_getBalance: eth_getBalance,
|
||||
|
||||
Reference in New Issue
Block a user