Skip to content

Commit

Permalink
implement getBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkNerdi committed Apr 30, 2024
1 parent ab588b7 commit ce322b0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import { EvmNetworkType, NetworkHealth, NetworkNamespace, ChainId } from '../enu
import { IBlock, IEvmNetwork, IBaseEvmNetworkConfiguration } from '../interfaces'
import { CoinType } from '@iota/sdk/out/types'
import { EvmNetworkId, Web3Provider } from '../types'
import { IBaseToken } from '@core/token'
import { NETWORK_STATUS_POLL_INTERVAL } from '@core/network/constants'
import { BASE_TOKEN_ID, IBaseToken, ITokenBalance, TokenTrackingStatus } from '@core/token'
import features from '@features/features'
import { updateErc721NftsOwnership } from '@core/nfts/actions'
import { getOrRequestTokenFromPersistedTokens } from '@core/token/actions'
import { IAccountState } from '@core/account'
import { Converter } from '@core/utils'
import { getActiveProfile } from '@core/profile/stores'
import { IError } from '@core/error/interfaces'

export class BaseEvmNetwork implements IEvmNetwork {
public readonly provider: Web3Provider
Expand Down Expand Up @@ -94,4 +101,49 @@ export class BaseEvmNetwork implements IEvmNetwork {
const number = await this.provider.eth.getBlockNumber()
return this.provider.eth.getBlock(number)
}

async getBalance(account: IAccountState): Promise<ITokenBalance | undefined> {
const evmAddress = account.evmAddresses?.[this.coinType]
if (!evmAddress) {
return
}

if (features.collectibles.erc721.enabled) {
void updateErc721NftsOwnership(account, this.id)
}

const rawBalance = await this.provider.eth.getBalance(evmAddress)
const tokenBalance = { [BASE_TOKEN_ID]: Converter.bigIntLikeToBigInt(rawBalance) }

const erc20Balances = await this.getErc20BalancesForAddress(evmAddress)
for (const [tokenId, balance] of Object.entries(erc20Balances)) {
await getOrRequestTokenFromPersistedTokens(tokenId, this.id)
tokenBalance[tokenId] = Number.isNaN(Number(balance)) ? BigInt(0) : balance
}

return tokenBalance
}

async getErc20BalancesForAddress(evmAddress: string): Promise<ITokenBalance> {
const trackedTokens = getActiveProfile()?.trackedTokens?.[this.id] ?? {}
const erc20TokenBalances: ITokenBalance = {}
for (const [erc20Address, trackingStatus] of Object.entries(trackedTokens)) {
try {
if (trackingStatus === TokenTrackingStatus.Untracked) {
continue
}

const contract = this.getContract(ContractType.Erc20, erc20Address)
if (!contract || !this.coinType) {
continue
}
const rawBalance = await contract.methods.balanceOf(evmAddress).call()
erc20TokenBalances[erc20Address] = Converter.bigIntLikeToBigInt(rawBalance)
} catch (err) {
const error = (err as IError)?.message ?? err
console.error(error)
}
}
return erc20TokenBalances
}
}
19 changes: 15 additions & 4 deletions packages/shared/src/lib/core/network/classes/isc-chain.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { IBlock, IIscChainConfiguration, IIscChainMetadata } from '../interfaces'
import { IIscChainConfiguration, IIscChainMetadata } from '../interfaces'
import { Converter } from '@core/utils'
import { BaseEvmNetwork } from './base-evm-network.class'
import { IAccountState } from '@core/account/interfaces'
import { ITokenBalance } from '@core/token/interfaces'
import { fetchIscAssetsForAccount } from '@core/layer-2/utils'
import { getActiveProfileId } from '@core/profile/stores'

export class IscChain extends BaseEvmNetwork {
private readonly _chainApi: string
Expand Down Expand Up @@ -47,9 +51,16 @@ export class IscChain extends BaseEvmNetwork {
return (await response.json()) as IIscChainMetadata
}

async getLatestBlock(): Promise<IBlock> {
const number = await this.provider.eth.getBlockNumber()
return this.provider.eth.getBlock(number)
async getBalance(account: IAccountState): Promise<ITokenBalance | undefined> {
const evmAddress = account.evmAddresses?.[this.coinType]
if (!evmAddress) {
return undefined
}

const tokenBalance = (await super.getBalance(account)) ?? {}
const iscBalance = (await fetchIscAssetsForAccount(getActiveProfileId(), evmAddress, this, account)) ?? {}

return { ...tokenBalance, ...iscBalance }
}

async getGasEstimate(hex: string): Promise<bigint> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { EvmNetworkId, Web3Provider } from '../types'
import { IBlock } from './block.interface'
import { IBaseNetwork, IBaseNetworkMetadata } from './base-network.interface'
import { IIscChainMetadata } from './isc-chain-metadata.interface'
import { ITokenBalance } from '@core/token/interfaces'
import { IAccountState } from '@core/account/interfaces'

export interface IIscChain extends IEvmNetwork {
apiEndpoint: string
Expand All @@ -26,6 +28,7 @@ export interface IEvmNetwork extends IBaseNetwork, IBaseNetworkMetadata {
provider: Web3Provider

getGasPrice(): Promise<bigint | undefined>
getBalance(account: IAccountState): Promise<ITokenBalance | undefined>

getContract(type: ContractType, address: string): Contract
getLatestBlock(): Promise<IBlock>
Expand Down

0 comments on commit ce322b0

Please sign in to comment.