Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into ens-implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-plusplus committed Feb 12, 2024
2 parents ec2baa0 + b16d8bf commit 750e446
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wigwam/app",
"version": "2.0.1",
"version": "2.0.2",
"private": true,
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/lifi-widget/hooks/useRouteExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const useRouteExecution = ({
const process = getUpdatedProcess(routeExecution.route, clonedUpdatedRoute);

if (process) {
if (onBeforeTransaction && clonedUpdatedRoute && process.type === "SWAP") {
if (onBeforeTransaction && clonedUpdatedRoute && process.type === "SWAP" && clonedUpdatedRoute.steps[1]) {
onBeforeTransaction({...clonedUpdatedRoute, fromChainId: clonedUpdatedRoute.toChainId, fromToken: clonedUpdatedRoute.steps[1].action.fromToken, toToken: clonedUpdatedRoute.steps[1].action.toToken})
}
emitter.emit(WidgetEvent.RouteExecutionUpdated, {
Expand Down
2 changes: 1 addition & 1 deletion src/core/back/sync/tokens/account/nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const syncAccountNFTs = memoize(
const standard = nftData.erc_type?.includes("erc1155")
? TokenStandard.ERC1155
: TokenStandard.ERC721;
const tokenId = new BigNumber(nftData.token_id).toString();
const tokenId = nftData.token_id;
const tokenSlug = createTokenSlug({
standard,
address: contractAddress,
Expand Down
26 changes: 24 additions & 2 deletions src/core/back/sync/tokens/account/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import { IndexableTypeArray } from "dexie";

import * as repo from "core/repo";
import { AccountToken, TokenType } from "core/types";
import { AccountNFT, AccountToken, TokenType } from "core/types";
import { NATIVE_TOKEN_SLUG, createAccountTokenKey } from "core/common/tokens";

export async function prepareAccountTokensSync<T extends AccountToken>(
chainId: number,
accountAddress: string,
tokenType: TokenType,
) {
const existingAccTokens = (await repo.accountTokens
const existingAccTokensPure = (await repo.accountTokens
.where("[chainId+tokenType+accountAddress]")
.equals([chainId, tokenType, accountAddress])
.toArray()) as T[];

let existingAccTokens: T[];

// Remove existing nfts with broken token ids such as "1.23e+23" (float)
if (tokenType === TokenType.NFT) {
existingAccTokens = [];
const dbKeysToDelete: string[] = [];

for (const token of existingAccTokensPure as AccountNFT[]) {
if (token.tokenId.includes("e+")) {
dbKeysToDelete.push(createAccountTokenKey(token));
} else {
existingAccTokens.push(token as T);
}
}

if (dbKeysToDelete.length > 0) {
await repo.accountTokens.bulkDelete(dbKeysToDelete).catch(console.error);
}
} else {
existingAccTokens = existingAccTokensPure;
}

const existingTokensMap = new Map(
existingAccTokens
.filter((t) => t.tokenSlug !== NATIVE_TOKEN_SLUG)
Expand Down
41 changes: 26 additions & 15 deletions src/core/back/sync/tokens/total.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BigNumber from "bignumber.js";
import memoize from "mem";
import { getAddress } from "ethers";
import { withOfflineCache } from "lib/ext/offlineCache";

import { AccountToken, TokenStatus, TokenType } from "core/types";
Expand All @@ -8,9 +9,12 @@ import {
updateTotalBalance,
createAccountTokenKey,
NATIVE_TOKEN_SLUG,
getNetwork,
} from "core/common";

import { getDxChain, indexerApi } from "../indexer";
import { fetchCxAccountTokens } from "../indexer";

const DEAD_ADDRESS = "0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000";

export const refreshTotalBalances = memoize(
async (chainId: number, accountAddress: string) => {
Expand Down Expand Up @@ -74,23 +78,30 @@ export const refreshTotalBalances = memoize(

export const fetchTotalChainBalance = withOfflineCache(
async (chainId: number, accountAddress: string) => {
const dxChain = await getDxChain(chainId);
if (!dxChain) return null;

const res = await indexerApi.get<{ usd_value: number | string }>(
`/d/v1/user/chain_balance`,
{
params: {
id: accountAddress,
chain_id: dxChain.id,
_authAddress: accountAddress,
},
},
const accTokens = await fetchCxAccountTokens(
chainId,
accountAddress,
TokenType.Asset,
);
const network = await getNetwork(chainId).catch(() => null);

let totalValue = new BigNumber(0);

for (const token of accTokens) {
// Skip if mainnet token without metadata
// Skip if dead address
if (
(network?.type === "mainnet" &&
(!token.contract_ticker_symbol || !token.contract_decimals)) ||
getAddress(token.contract_address) === DEAD_ADDRESS
) {
continue;
}

const value = res.data.usd_value;
if (token.quote) totalValue = totalValue.plus(token.quote);
}

return value ? new BigNumber(res.data.usd_value).toString() : null;
return totalValue.toString();
},
{
key: ([chainId, accountAddress]) =>
Expand Down

0 comments on commit 750e446

Please sign in to comment.