Skip to content

Commit

Permalink
refactor(frontend): cast ft addresses (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov authored Dec 5, 2024
1 parent 834b8ab commit 7809040
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 45 deletions.
10 changes: 4 additions & 6 deletions frontend/src/features/history/utils/filters.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { HexString } from '@gear-js/api';
import { ActorId, H160 } from 'sails-js';

import { FTAddressPair } from '@/types';

const getLastDaysISOTimestamp = (daysCount: number) =>
new Date(Date.now() - daysCount * 24 * 60 * 60 * 1000).toISOString();

const getAssetOptions = (addresses: [ActorId, H160, 'ethereum' | 'gear'][], symbols: Record<HexString, string>) => {
const getAssetOptions = (addresses: FTAddressPair[], symbols: Record<HexString, string>) => {
const options = [] as { label: string; value: string }[];

for (const pair of addresses) {
const varaAddress = pair[0].toString() as HexString;
const ethAddress = pair[1].toString() as HexString;

for (const [varaAddress, ethAddress] of addresses) {
const varaSymbol = symbols[varaAddress];
const ethSymbol = symbols[ethAddress];

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/features/swap/hooks/use-bridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HexString } from '@gear-js/api';
import { useMemo, useState } from 'react';

import { useTokens } from '@/hooks';
Expand All @@ -16,7 +15,7 @@ function useBridge(networkIndex: number) {

const [pair, setPair] = useState('0');
const pairIndex = Number(pair);
const address = addresses?.[pairIndex][networkIndex].toString() as HexString | undefined;
const address = addresses?.[pairIndex][networkIndex];
const symbol = address ? symbols?.[address] : undefined;
const decimals = address ? tokenDecimals?.[address] : undefined;

Expand Down
12 changes: 3 additions & 9 deletions frontend/src/features/swap/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HexString } from '@gear-js/api';
import { ActorId, H160 } from 'sails-js';
import { formatUnits, parseUnits } from 'viem';
import { z } from 'zod';

import { FTAddressPair } from '@/types';
import { isUndefined } from '@/utils';

import { ERROR_MESSAGE } from './consts';
Expand Down Expand Up @@ -42,21 +42,15 @@ const getAmountSchema = (
);
};

const getOptions = (
addresses: [ActorId, H160, 'ethereum' | 'gear'][] | undefined,
symbols: Record<HexString, string> | undefined,
) => {
const getOptions = (addresses: FTAddressPair[] | undefined, symbols: Record<HexString, string> | undefined) => {
const varaOptions: { label: string; value: string }[] = [];
const ethOptions: { label: string; value: string }[] = [];

if (!addresses || !symbols) return { varaOptions, ethOptions };

addresses.forEach((pair, index) => {
addresses.forEach(([varaAddress, ethAddress], index) => {
const value = index.toString();

const varaAddress = pair[0].toString() as HexString;
const ethAddress = pair[1].toString() as HexString;

const varaSymbol = symbols[varaAddress];
const ethSymbol = symbols[ethAddress];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HexString } from '@gear-js/api';
import { getTypedEntries, useAccount } from '@gear-js/react-hooks';
import { Modal } from '@gear-js/vara-ui';
import { formatUnits } from 'viem';
Expand All @@ -24,9 +23,7 @@ function TokenTrackerModal({ lockedBalance, close }: Props) {
const { addresses, decimals, symbols } = useTokens();

const networkIndex = account ? 0 : 1;
const nonNativeAddresses = addresses?.filter(
(pair) => (pair[networkIndex].toString() as HexString) !== WRAPPED_VARA_CONTRACT_ADDRESS,
);
const nonNativeAddresses = addresses?.filter((pair) => pair[networkIndex] !== WRAPPED_VARA_CONTRACT_ADDRESS);

const { data: varaFtBalances } = useVaraFTBalances(nonNativeAddresses);
const { data: ethFtBalances } = useEthFTBalances(nonNativeAddresses);
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/features/token-tracker/hooks/use-eth-ft-balances.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { HexString } from '@gear-js/api';
import { useMemo } from 'react';
import { ActorId, H160 } from 'sails-js';
import { useReadContracts } from 'wagmi';

import { FUNGIBLE_TOKEN_ABI } from '@/consts';
import { TokenSupply } from '@/consts/sails/vft-manager';
import { useEthAccount } from '@/hooks';
import { FTAddressPair } from '@/types';
import { isUndefined } from '@/utils';

function useEthFTBalances(addresses: [ActorId, H160, TokenSupply][] | undefined) {
function useEthFTBalances(addresses: FTAddressPair[] | undefined) {
const ethAccount = useEthAccount();

const contracts = useMemo(
() =>
addresses?.map(([, address]) => ({
address: address.toString() as HexString,
address,
abi: FUNGIBLE_TOKEN_ABI,
functionName: 'balanceOf',
args: [ethAccount.address],
Expand All @@ -26,7 +24,7 @@ function useEthFTBalances(addresses: [ActorId, H160, TokenSupply][] | undefined)
if (!addresses) return;

const entries = data.map(({ result }, index) => {
const address = addresses[index][1].toString() as HexString;
const address = addresses[index][1];
const balance = isUndefined(result) ? 0n : BigInt(result);

return [address, balance] as const;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { HexString } from '@gear-js/api';
import { useAccount, useApi } from '@gear-js/react-hooks';
import { useQuery } from '@tanstack/react-query';
import { ActorId, H160 } from 'sails-js';

import { VftProgram } from '@/consts';
import { TokenSupply } from '@/consts/sails/vft-manager';
import { FTAddressPair } from '@/types';

function useVaraFTBalances(addresses: [ActorId, H160, TokenSupply][] | undefined) {
function useVaraFTBalances(addresses: FTAddressPair[] | undefined) {
const { api, isApiReady } = useApi();
const { account } = useAccount();

Expand All @@ -17,8 +16,7 @@ function useVaraFTBalances(addresses: [ActorId, H160, TokenSupply][] | undefined

const result: Record<HexString, bigint> = {};

for (const pair of addresses) {
const address = pair[0].toString() as HexString;
for (const [address] of addresses) {
const balance = await new VftProgram(api, address).vft.balanceOf(account.decodedAddress);

result[address] = balance;
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/hooks/tokens/use-ft-addresses.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HexString } from '@gear-js/api';
import { useProgram, useProgramQuery } from '@gear-js/react-hooks';

import { VftManagerProgram } from '@/consts';
Expand All @@ -16,6 +17,9 @@ function useFTAddresses() {
serviceName: 'vftManager',
functionName: 'varaToEthAddresses',
args: [],
query: {
select: (data) => data.map((pair) => [pair[0].toString(), pair[1].toString()] as [HexString, HexString]),
},
});
}

Expand Down
9 changes: 3 additions & 6 deletions frontend/src/hooks/tokens/use-ft-decimals.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HexString } from '@gear-js/api';
import { useApi } from '@gear-js/react-hooks';
import { useQuery } from '@tanstack/react-query';
import { ActorId, H160 } from 'sails-js';
import { useConfig } from 'wagmi';
import { readContract } from 'wagmi/actions';

import { VftProgram, FUNGIBLE_TOKEN_ABI } from '@/consts';
import { FTAddressPair } from '@/types';

function useFTDecimals(addresses: [ActorId, H160, 'ethereum' | 'gear'][] | undefined) {
function useFTDecimals(addresses: FTAddressPair[] | undefined) {
const { api, isApiReady } = useApi();
const wagmiConfig = useConfig();

Expand All @@ -25,10 +25,7 @@ function useFTDecimals(addresses: [ActorId, H160, 'ethereum' | 'gear'][] | undef

const result: Record<HexString, number> = {};

for (const pair of addresses) {
const varaAddress = pair[0].toString() as HexString;
const ethAddress = pair[1].toString() as HexString;

for (const [varaAddress, ethAddress] of addresses) {
const [varaDecimals, ethDecimals] = await Promise.all([
readVaraDecimals(varaAddress),
readEthDecimals(ethAddress),
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/hooks/tokens/use-ft-symbols.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HexString } from '@gear-js/api';
import { useApi } from '@gear-js/react-hooks';
import { useQuery } from '@tanstack/react-query';
import { ActorId, H160 } from 'sails-js';
import { useConfig } from 'wagmi';
import { readContract } from 'wagmi/actions';

import { VftProgram, FUNGIBLE_TOKEN_ABI } from '@/consts';
import { FTAddressPair } from '@/types';

function useFTSymbols(addresses: [ActorId, H160, 'ethereum' | 'gear'][] | undefined) {
function useFTSymbols(addresses: FTAddressPair[] | undefined) {
const { api, isApiReady } = useApi();
const wagmiConfig = useConfig();

Expand All @@ -25,10 +25,7 @@ function useFTSymbols(addresses: [ActorId, H160, 'ethereum' | 'gear'][] | undefi

const result: Record<HexString, string> = {};

for (const pair of addresses) {
const varaAddress = pair[0].toString() as HexString;
const ethAddress = pair[1].toString() as HexString;

for (const [varaAddress, ethAddress] of addresses) {
const [varaSymbol, ethSymbol] = await Promise.all([readVaraSymbol(varaAddress), readEthSymbol(ethAddress)]);

result[varaAddress] = varaSymbol;
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HexString } from '@gear-js/api';
import { FunctionComponent, SVGProps } from 'react';

type SVGComponent = FunctionComponent<
Expand All @@ -6,4 +7,8 @@ type SVGComponent = FunctionComponent<
}
>;

export type { SVGComponent };
type VaraAddress = HexString;
type EthAddress = HexString;
type FTAddressPair = [VaraAddress, EthAddress];

export type { SVGComponent, FTAddressPair };

0 comments on commit 7809040

Please sign in to comment.