mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
grab and output ETH & OCEAN balance
This commit is contained in:
parent
2f5975ad49
commit
f3617458ff
@ -11,6 +11,11 @@ import { Ocean, Logger, Account, Config } from '@oceanprotocol/lib'
|
|||||||
import Web3Modal, { ICoreOptions } from 'web3modal'
|
import Web3Modal, { ICoreOptions } from 'web3modal'
|
||||||
import { getDefaultProviders } from './getDefaultProviders'
|
import { getDefaultProviders } from './getDefaultProviders'
|
||||||
|
|
||||||
|
interface Balance {
|
||||||
|
eth: string
|
||||||
|
ocean: string
|
||||||
|
}
|
||||||
|
|
||||||
interface OceanProviderValue {
|
interface OceanProviderValue {
|
||||||
web3: Web3 | undefined
|
web3: Web3 | undefined
|
||||||
web3Provider: any
|
web3Provider: any
|
||||||
@ -19,7 +24,7 @@ interface OceanProviderValue {
|
|||||||
config: Config
|
config: Config
|
||||||
account: Account
|
account: Account
|
||||||
accountId: string
|
accountId: string
|
||||||
balance: string
|
balance: Balance
|
||||||
chainId: number | undefined
|
chainId: number | undefined
|
||||||
status: ProviderStatus
|
status: ProviderStatus
|
||||||
connect: (opts?: Partial<ICoreOptions>) => void
|
connect: (opts?: Partial<ICoreOptions>) => void
|
||||||
@ -42,7 +47,7 @@ function OceanProvider({
|
|||||||
const [chainId, setChainId] = useState<number | undefined>()
|
const [chainId, setChainId] = useState<number | undefined>()
|
||||||
const [account, setAccount] = useState<Account | undefined>()
|
const [account, setAccount] = useState<Account | undefined>()
|
||||||
const [accountId, setAccountId] = useState<string | undefined>()
|
const [accountId, setAccountId] = useState<string | undefined>()
|
||||||
const [balance, setBalance] = useState<string | undefined>()
|
const [balance, setBalance] = useState<Balance | undefined>()
|
||||||
const [status, setStatus] = useState<ProviderStatus>(
|
const [status, setStatus] = useState<ProviderStatus>(
|
||||||
ProviderStatus.NOT_AVAILABLE
|
ProviderStatus.NOT_AVAILABLE
|
||||||
)
|
)
|
||||||
@ -65,9 +70,11 @@ function OceanProvider({
|
|||||||
if (opts === undefined) {
|
if (opts === undefined) {
|
||||||
opts = await getDefaultProviders()
|
opts = await getDefaultProviders()
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = new Web3Modal(opts)
|
const instance = new Web3Modal(opts)
|
||||||
setWeb3Modal(instance)
|
setWeb3Modal(instance)
|
||||||
Logger.log('Web3Modal instance created', instance)
|
Logger.log('Web3Modal instance created.', instance)
|
||||||
|
|
||||||
const provider = await instance.connect()
|
const provider = await instance.connect()
|
||||||
setWeb3Provider(provider)
|
setWeb3Provider(provider)
|
||||||
|
|
||||||
@ -78,19 +85,22 @@ function OceanProvider({
|
|||||||
const ocean = await Ocean.getInstance(config)
|
const ocean = await Ocean.getInstance(config)
|
||||||
|
|
||||||
setOcean(ocean)
|
setOcean(ocean)
|
||||||
Logger.log('Ocean instance created ', ocean)
|
Logger.log('Ocean instance created.', ocean)
|
||||||
Logger.log('Web3 created ', web3)
|
Logger.log('Web3 created.', web3)
|
||||||
setStatus(ProviderStatus.CONNECTED)
|
setStatus(ProviderStatus.CONNECTED)
|
||||||
|
|
||||||
const account = (await ocean.accounts.list())[0]
|
const account = (await ocean.accounts.list())[0]
|
||||||
setAccount(account)
|
setAccount(account)
|
||||||
Logger.log('Account ', account)
|
Logger.log('Account ', account)
|
||||||
|
|
||||||
const accountId = await getAccount(web3)
|
const accountId = await getAccountId(web3)
|
||||||
setAccountId(accountId)
|
setAccountId(accountId)
|
||||||
Logger.log('account id', accountId)
|
Logger.log('account id', accountId)
|
||||||
const balance = await getBalance(web3, accountId)
|
|
||||||
|
const balance = await getBalance(web3, account)
|
||||||
setBalance(balance)
|
setBalance(balance)
|
||||||
Logger.log('balance', accountId)
|
Logger.log('balance', JSON.stringify(balance))
|
||||||
|
|
||||||
const chainId = web3 && (await web3.eth.getChainId())
|
const chainId = web3 && (await web3.eth.getChainId())
|
||||||
setChainId(chainId)
|
setChainId(chainId)
|
||||||
Logger.log('chain id ', chainId)
|
Logger.log('chain id ', chainId)
|
||||||
@ -101,14 +111,19 @@ function OceanProvider({
|
|||||||
web3Modal.clearCachedProvider()
|
web3Modal.clearCachedProvider()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAccount(web3: Web3) {
|
async function getAccountId(web3: Web3) {
|
||||||
const accounts = await web3.eth.getAccounts()
|
const accounts = await web3.eth.getAccounts()
|
||||||
return accounts[0]
|
return accounts[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getBalance(web3: Web3, address: string) {
|
async function getBalance(web3: Web3, account: Account) {
|
||||||
const balance = await web3.eth.getBalance(address)
|
const balanceEth = await web3.eth.getBalance(account.getId())
|
||||||
return Web3.utils.fromWei(balance)
|
const balanceOcean = await account.getOceanBalance()
|
||||||
|
|
||||||
|
return {
|
||||||
|
eth: Web3.utils.fromWei(balanceEth),
|
||||||
|
ocean: Web3.utils.fromWei(balanceOcean)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -187,5 +202,5 @@ function OceanProvider({
|
|||||||
// Helper hook to access the provider values
|
// Helper hook to access the provider values
|
||||||
const useOcean = (): OceanProviderValue => useContext(OceanContext)
|
const useOcean = (): OceanProviderValue => useContext(OceanContext)
|
||||||
|
|
||||||
export { OceanProvider, useOcean, OceanProviderValue }
|
export { OceanProvider, useOcean, OceanProviderValue, Balance }
|
||||||
export default OceanProvider
|
export default OceanProvider
|
||||||
|
Loading…
x
Reference in New Issue
Block a user