Skip to content

Commit

Permalink
hotfix: safe sdk balances fail when user does not have the token anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesfracari committed Jun 17, 2024
1 parent b3796b7 commit d78d0aa
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions apps/cow-amm-deployer/src/hooks/useRunningAmmInfo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useSafeAppsSDK } from "@gnosis.pm/safe-apps-react-sdk";
import { TokenType } from "@gnosis.pm/safe-apps-sdk";
import { gql } from "graphql-tag";
import { useEffect, useState } from "react";
import { Address, formatUnits } from "viem";
import { Address, erc20Abi, formatUnits } from "viem";

import { composableCowAbi } from "#/lib/abis/composableCow";
import { cowAmmModuleAbi } from "#/lib/abis/cowAmmModule";
Expand Down Expand Up @@ -166,22 +167,65 @@ export function useRunningAMM(): {
setIsAmmFromModule(newIsAmmFromModule);
}

async function loadTokenBalance({
tokenAddress,
tokenDecimals,
chainId,
tokenSymbol,
owner,
}: {
tokenAddress: Address;
tokenDecimals: number;
chainId: ChainId;
tokenSymbol: string;
owner: Address;
}) {
const publicClient = publicClientsFromIds[chainId];
const balance = await publicClient.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "balanceOf",
args: [owner],
});
const logoUri = assets.find(
(asset) => asset.tokenInfo.symbol === tokenSymbol,
)?.tokenInfo.logoUri;
return {
tokenInfo: {
address: tokenAddress,
decimals: tokenDecimals,
symbol: tokenSymbol,
type: "ERC20" as TokenType,
name: tokenSymbol,
logoUri: logoUri || "",
},
balance: String(balance),
fiatBalance: "0",
fiatConversion: "0",
};
}

async function loadCowAmm() {
const gqlInfo = await fetchLastAmmInfo({
chainId: chainId as ChainId,
safeAddress: safeAddress as Address,
});

const token0 = assets.find(
(asset) =>
asset.tokenInfo.address.toLowerCase() ===
gqlInfo?.token0?.address.toLowerCase(),
);
const token1 = assets.find(
(asset) =>
asset.tokenInfo.address.toLowerCase() ===
gqlInfo?.token1?.address.toLowerCase(),
);
const token0 = await loadTokenBalance({
tokenAddress: gqlInfo?.token0?.address as Address,
tokenDecimals: gqlInfo?.token0?.decimals as number,
chainId: chainId as ChainId,
tokenSymbol: gqlInfo?.token0?.symbol as string,
owner: safeAddress as Address,
});

const token1 = await loadTokenBalance({
tokenAddress: gqlInfo?.token1?.address as Address,
tokenDecimals: gqlInfo?.token1?.decimals as number,
chainId: chainId as ChainId,
tokenSymbol: gqlInfo?.token1?.symbol as string,
owner: safeAddress as Address,
});

if (!token0 || !token1) {
return;
Expand Down

0 comments on commit d78d0aa

Please sign in to comment.