Skip to content

Commit

Permalink
update the Query for token fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
sepehr2github committed Feb 1, 2024
1 parent 1d92fd9 commit 46a1622
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/services/token/domain/covalent-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type TokenItemType = {
contractName: string;
contractTickerSymbol: string;
logoUrl: string;
native_token: boolean;
nativeToken: boolean;
};

export type TokenBalanceResponse = {
Expand Down
32 changes: 14 additions & 18 deletions src/services/token/token-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ class TokenService {
tokenBalanceQueryDocument = gql`
query TokensBalances(
$currency: String!
$networkId: String!
$network: Network!
$address: String!
) {
tokensBalances(
currency: $currency
networkId: $networkId
network: $network
address: $address
) {
updatedAt
Expand All @@ -159,7 +159,7 @@ class TokenService {
contractTickerSymbol
contractDecimals
logoUrl
native_token
nativeToken
balance
}
}
Expand All @@ -175,9 +175,7 @@ class TokenService {
ignoreZeroBalances = true,
nativeTokenBalance,
}: IFetchTokenBalancesParams): Promise<AssetBalance[] | null> => {
const {networkId} = CHAIN_METADATA[network].covalent ?? {};

if (!networkId) {
if (!network) {
console.info(
`fetchTokenBalances - network ${network} not supported by Covalent`
);
Expand All @@ -189,7 +187,7 @@ class TokenService {
const {tokensBalances: data} = await request(
aragonGateway.backendUrl,
this.tokenBalanceQueryDocument,
{networkId, address, currency: this.defaultCurrency}
{network, address, currency: this.defaultCurrency}
);

if (data.error || data == null) {
Expand All @@ -200,29 +198,27 @@ class TokenService {
}

return (data as TokenBalanceResponse).items.flatMap(
({native_token, ...item}) => {
({nativeToken, ...item}) => {
if (
ignoreZeroBalances &&
((native_token && nativeTokenBalance === BigInt(0)) ||
((nativeToken && nativeTokenBalance === BigInt(0)) ||
BigNumber.from(item.balance).isZero())
)
// ignore zero balances if indicated
return [];

return {
id: native_token ? AddressZero : item.contractAddress,
address: native_token ? AddressZero : item.contractAddress,
name: native_token ? nativeCurrency.name : item.contractName,
symbol: native_token
id: nativeToken ? AddressZero : item.contractAddress,
address: nativeToken ? AddressZero : item.contractAddress,
name: nativeToken ? nativeCurrency.name : item.contractName,
symbol: nativeToken
? nativeCurrency.symbol
: item.contractTickerSymbol?.toUpperCase(),
decimals: native_token
decimals: nativeToken
? nativeCurrency.decimals
: item.contractDecimals,
type: (native_token
? TokenType.NATIVE
: TokenType.ERC20) as tokenType,
balance: native_token
type: (nativeToken ? TokenType.NATIVE : TokenType.ERC20) as tokenType,
balance: nativeToken
? (nativeTokenBalance as bigint)
: BigInt(item.balance),
updateDate: new Date(data.updated_at),
Expand Down

0 comments on commit 46a1622

Please sign in to comment.