-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
89 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
packages/shared/src/lib/core/account/actions/setSelectedAccount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 4 additions & 62 deletions
66
packages/shared/src/lib/core/layer-2/actions/fetchEvmBalancesForAccount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,17 @@ | ||
import { IAccountState } from '@core/account/interfaces' | ||
import { ContractType, fetchIscAssetsForAccount } from '@core/layer-2' | ||
import { IEvmNetwork } from '@core/network/interfaces' | ||
import { getEvmNetworks } from '@core/network/stores' | ||
import { updateErc721NftsOwnership } from '@core/nfts/actions' | ||
import { getActiveProfile } from '@core/profile/stores' | ||
import { getOrRequestTokenFromPersistedTokens } from '@core/token/actions' | ||
import { Converter } from '@core/utils/convert' | ||
import { BASE_TOKEN_ID, ITokenBalance, TokenTrackingStatus } from '@core/token' | ||
import features from '@features/features' | ||
import { IError } from '@core/error' | ||
import { isIscChain } from '@core/network/utils/isIscChain' | ||
import { ITokenBalance } from '@core/token' | ||
import { setLayer2AccountBalanceForChain } from '@core/layer-2/stores' | ||
|
||
export function fetchEvmBalancesForAccount(profileId: string, account: IAccountState): void { | ||
const { evmAddresses, index } = account | ||
export function fetchEvmBalancesForAccount(account: IAccountState): void { | ||
const evmNetworks = getEvmNetworks() | ||
evmNetworks.forEach(async (evmNetwork) => { | ||
const { coinType, id: networkId } = evmNetwork | ||
const evmAddress = evmAddresses?.[coinType] | ||
if (!evmAddress) { | ||
return | ||
} | ||
|
||
if (features.collectibles.erc721.enabled) { | ||
void updateErc721NftsOwnership(account, evmNetwork.id) | ||
} | ||
|
||
try { | ||
const tokenBalance = isIscChain(evmNetwork) | ||
? await fetchIscAssetsForAccount(profileId, evmAddress, evmNetwork, account) | ||
: await getEvmBaseCoinBalanceForAddress(evmAddress, evmNetwork) | ||
const tokenBalance: ITokenBalance = (await evmNetwork.getBalance(account)) ?? {} | ||
|
||
const erc20Balances = await getErc20BalancesForAddress(evmAddress, evmNetwork) | ||
for (const [tokenId, balance] of Object.entries(erc20Balances)) { | ||
await getOrRequestTokenFromPersistedTokens(tokenId, networkId) | ||
tokenBalance[tokenId] = Number.isNaN(Number(balance)) ? BigInt(0) : balance | ||
} | ||
setLayer2AccountBalanceForChain(index, networkId, tokenBalance) | ||
setLayer2AccountBalanceForChain(account.index, evmNetwork.id, tokenBalance) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}) | ||
} | ||
|
||
async function getEvmBaseCoinBalanceForAddress(address: string, network: IEvmNetwork): Promise<ITokenBalance> { | ||
const rawBalance = await network.provider.eth.getBalance(address) | ||
return { [BASE_TOKEN_ID]: Converter.bigIntLikeToBigInt(rawBalance) } | ||
} | ||
|
||
async function getErc20BalancesForAddress(evmAddress: string, evmNetwork: IEvmNetwork): Promise<ITokenBalance> { | ||
const networkId = evmNetwork.id | ||
const coinType = evmNetwork.coinType | ||
|
||
const trackedTokens = getActiveProfile()?.trackedTokens?.[networkId] ?? {} | ||
const erc20TokenBalances: ITokenBalance = {} | ||
for (const [erc20Address, trackingStatus] of Object.entries(trackedTokens)) { | ||
try { | ||
if (trackingStatus === TokenTrackingStatus.Untracked) { | ||
continue | ||
} | ||
|
||
const contract = evmNetwork?.getContract(ContractType.Erc20, erc20Address) | ||
if (!contract || !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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters