ecaf2054fc
* feat: add light node upgrade * refactor: improve upgrade page * feat: pretty print xdai and add xbzz faucets * feat: display xBZZ balance (#312) * refactor: change rpc provider * fix: remove version alert * fix: load really xBZZ balance instead of xDAI (#314) * feat: add bee desktop api key support * chore: remove dead code * chore: revert useless change * refactor: extract desktop utils module (#339) * refactor: extract desktop utils module * fix: add 0x prefix if it missing from address * refactor: extract BalanceProvider * fix: remove double finally * fix: remove token fallbacks * fix: reuse address and handle balance errors * chore: disable eslint for any * refactor: remove upgrade page * refactor: cleanup, debounce and axios * refactor: change fetch to axios * chore: remove dead code * chore: revert import ordering * refactor: use axios instead of fetch * refactor: use token instead of string Co-authored-by: Cafe137 <aron@aronsoos.com> Co-authored-by: Vojtech Simetka <vojtech@simetka.cz>
67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import { debounce } from '@material-ui/core'
|
|
import axios from 'axios'
|
|
import { Contract, providers } from 'ethers'
|
|
|
|
const PROVIDER = 'https://gno.getblock.io/mainnet/?api_key=d7b92d96-9784-49a8-a800-b3edd1647fc7'
|
|
|
|
async function eth_getBalance(address: string): Promise<string> {
|
|
if (!address.startsWith('0x')) {
|
|
address = `0x${address}`
|
|
}
|
|
const response = await axios(PROVIDER, {
|
|
method: 'POST',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
},
|
|
data: {
|
|
jsonrpc: '2.0',
|
|
method: 'eth_getBalance',
|
|
params: [address, 'latest'],
|
|
id: 1,
|
|
},
|
|
})
|
|
|
|
return response.data.result
|
|
}
|
|
|
|
const partialERC20tokenABI = [
|
|
{
|
|
constant: true,
|
|
inputs: [
|
|
{
|
|
name: '_owner',
|
|
type: 'address',
|
|
},
|
|
],
|
|
name: 'balanceOf',
|
|
outputs: [
|
|
{
|
|
name: 'balance',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
payable: false,
|
|
type: 'function',
|
|
},
|
|
]
|
|
|
|
const provider = new providers.JsonRpcProvider(PROVIDER)
|
|
|
|
async function eth_getBalanceERC20(
|
|
address: string,
|
|
tokenAddress = '0xdbf3ea6f5bee45c02255b2c26a16f300502f68da',
|
|
): Promise<string> {
|
|
if (!address.startsWith('0x')) {
|
|
address = `0x${address}`
|
|
}
|
|
const contract = new Contract(tokenAddress, partialERC20tokenABI, provider)
|
|
const balance = await contract.balanceOf(address)
|
|
|
|
return balance.toString()
|
|
}
|
|
|
|
export const Rpc = {
|
|
eth_getBalance: debounce(eth_getBalance, 1_000),
|
|
eth_getBalanceERC20: debounce(eth_getBalanceERC20, 1_000),
|
|
}
|