From 46edd4be93b296e1fea88c2393347d002244b71f Mon Sep 17 00:00:00 2001 From: Jason Ridgway-Taylor Date: Tue, 14 Nov 2023 14:38:07 +0800 Subject: [PATCH] Fixed linting and type issues --- batcher-ui/src/actions/events.ts | 4 ++-- batcher-ui/src/commands/events.ts | 22 ++++++++++++------- batcher-ui/src/commands/exchange.ts | 9 ++++---- .../src/components/batcher/SelectPair.tsx | 9 ++------ batcher-ui/src/contexts/events.tsx | 9 ++++---- batcher-ui/src/pages/index.tsx | 5 ++--- batcher-ui/src/pages/volumes.tsx | 1 - batcher-ui/src/reducers/events.ts | 2 +- batcher-ui/src/reducers/exchange.ts | 2 ++ batcher-ui/src/reducers/index.ts | 1 - batcher-ui/src/types/contracts/batcher.ts | 10 ++------- .../src/types/contracts/token-manager.ts | 1 - batcher-ui/src/utils/utils.ts | 11 +++++----- 13 files changed, 40 insertions(+), 46 deletions(-) diff --git a/batcher-ui/src/actions/events.ts b/batcher-ui/src/actions/events.ts index b3dbbfd9..af0e0e08 100644 --- a/batcher-ui/src/actions/events.ts +++ b/batcher-ui/src/actions/events.ts @@ -1,9 +1,9 @@ import type { BigMapEvent } from '@/types'; -export const newEvent = (event: BigMapEvent) => +export const newEvent = (event: BigMapEvent, tokens:any) => ({ type: 'NEW_EVENT', - payload: { event }, + payload: { event, tokens }, } as const); export const closeToast = () => diff --git a/batcher-ui/src/commands/events.ts b/batcher-ui/src/commands/events.ts index 3136872f..159fa88f 100644 --- a/batcher-ui/src/commands/events.ts +++ b/batcher-ui/src/commands/events.ts @@ -12,6 +12,7 @@ import type { OrderBookBigmap, RatesCurrentBigmap, BigMapEvent, + Token, } from '@/types'; import { computeAllHoldings, @@ -20,7 +21,7 @@ import { toVolumes, } from '@/utils/utils'; -export const newEventCmd = (event: BigMapEvent) => { +export const newEventCmd = (event: BigMapEvent, tokens: Map) => { return Cmd.run( (dispatch, getState) => { return event.data.map(async eventData => { @@ -30,14 +31,17 @@ export const newEventCmd = (event: BigMapEvent) => { case 'batch_set.batches': { const data = eventData.content.value as BatchBigmap; const status = mapStatus(data); + //const toks = Object.values(tokens)[0]; + const buyToken = tokens.get(data.pair.string_0); + const sellToken = tokens.get(data.pair.string_1); //! new batch dispatch(updateBatchNumber(parseInt(data.batch_number))); dispatch(updateBatcherStatus(status)); dispatch( updateVolumes( toVolumes(data.volumes, { - buyDecimals: parseInt(data.pair.decimals_1, 10), - sellDecimals: parseInt(data.pair.decimals_0, 10), + buyDecimals: buyToken.decimals, + sellDecimals: sellToken.decimals, }) ) ); @@ -49,7 +53,7 @@ export const newEventCmd = (event: BigMapEvent) => { const userAddress = userAddressSelector(getState()); //! user addresses are keys of this bigmap so we need to ensure that the key is the user address if (userAddress === eventData.content.key) { - const holdings = await computeAllHoldings(data); + const holdings = await computeAllHoldings(data, tokens); dispatch(updateHoldings(holdings)); } return Promise.resolve(); @@ -64,7 +68,7 @@ export const newEventCmd = (event: BigMapEvent) => { case 'rates_current': { //! oracle price has changed const data = eventData.content.value as RatesCurrentBigmap; - console.info("Oracle change",data); + console.info('Oracle change', data); dispatch( updateOraclePrice( computeOraclePrice(data.rate, { @@ -79,12 +83,14 @@ export const newEventCmd = (event: BigMapEvent) => { //! batch status has changed const data = eventData.content.value as BatchBigmap; const status = mapStatus(data); + const buyToken = tokens.get(data.pair.string_0); + const sellToken = tokens.get(data.pair.string_1); dispatch(updateBatcherStatus(status)); dispatch( updateVolumes( toVolumes(data.volumes, { - buyDecimals: parseInt(data.pair.decimals_1, 10), - sellDecimals: parseInt(data.pair.decimals_0, 10), + buyDecimals: buyToken.decimals, + sellDecimals: sellToken.decimals, }) ) ); @@ -96,7 +102,7 @@ export const newEventCmd = (event: BigMapEvent) => { const userAddress = userAddressSelector(getState()); //! user addresses are keys of this bigmap so we need to ensure that the key is the user address if (userAddress === eventData.content.key) { - const holdings = await computeAllHoldings(data); + const holdings = await computeAllHoldings(data, tokens); dispatch(updateHoldings(holdings)); } return Promise.resolve(); diff --git a/batcher-ui/src/commands/exchange.ts b/batcher-ui/src/commands/exchange.ts index 2028ef8c..17c60121 100644 --- a/batcher-ui/src/commands/exchange.ts +++ b/batcher-ui/src/commands/exchange.ts @@ -95,10 +95,10 @@ const fetchOraclePriceCmd = (tokenPair: string, { swap }: CurrentSwap) => { ); }; -const fetchVolumesCmd = (batchNumber: number, tokens:Map) => { +const fetchVolumesCmd = (batchNumber: number, tokens: Map) => { return Cmd.run( () => { - return getVolumes(batchNumber,tokens); + return getVolumes(batchNumber, tokens); }, { successActionCreator: updateVolumes, @@ -111,8 +111,9 @@ const fetchTokensCmd = () => { return Cmd.run( async () => { const tokens = await getTokens(); - - return tokens; + const mapped: Map = ((tokens as unknown) as Map); + console.info('Mapped tokens', mapped); + return mapped; }, { successActionCreator: updateTokens, diff --git a/batcher-ui/src/components/batcher/SelectPair.tsx b/batcher-ui/src/components/batcher/SelectPair.tsx index 9f4a2ddd..c1778728 100644 --- a/batcher-ui/src/components/batcher/SelectPair.tsx +++ b/batcher-ui/src/components/batcher/SelectPair.tsx @@ -11,7 +11,7 @@ import { currentSwapSelector } from '@/reducers'; import { useDispatch } from 'react-redux'; import { changePair } from '@/actions'; import Image from 'next/image'; -import { getTokensMetadata, getSwapsMetadata } from '@/utils/token-manager'; +import { getTokensMetadata } from '@/utils/token-manager'; interface SelectPairProps { isFrom: boolean; @@ -21,8 +21,8 @@ const SelectPair = ({ isFrom }: SelectPairProps) => { const { swap, isReverse } = useSelector(currentSwapSelector); const dispatch = useDispatch(); + //const tokens = useSelector(tokensSelector); const [availableTokens, setAvailableTokens] = useState([]); - const [availableSwaps, setAvailableSwaps] = useState([]); const displayValue = useCallback(() => { if (isReverse && isFrom) return swap.to.name; @@ -40,11 +40,6 @@ const SelectPair = ({ isFrom }: SelectPairProps) => { setAvailableTokens(tokens); } ); - getSwapsMetadata().then( - (swaps: { name: string; to: string; from: string }[]) => { - setAvailableSwaps(swaps); - } - ); }, []); return ( diff --git a/batcher-ui/src/contexts/events.tsx b/batcher-ui/src/contexts/events.tsx index 7ff0a8da..3852ceff 100644 --- a/batcher-ui/src/contexts/events.tsx +++ b/batcher-ui/src/contexts/events.tsx @@ -1,9 +1,9 @@ import { /* HubConnection */ HubConnectionBuilder } from '@microsoft/signalr'; import React, { createContext, useEffect } from 'react'; -// import { useSelector } from 'react-redux'; +import { useSelector } from 'react-redux'; import { useDispatch } from 'react-redux'; import { newEvent } from '@/actions/events'; -// import { userAddressSelector } from 'src/reducers'; +import { tokensSelector } from 'src/reducers'; import { setup /* subscribeTokenBalances */ } from '@/utils/webSocketUtils'; export const EventsContext = createContext<{}>({}); @@ -12,6 +12,7 @@ export const EventsProvider = ({ children }: { children: React.ReactNode }) => { // const [socket, setSocket] = useState(undefined); const dispatch = useDispatch(); // const userAddress = useSelector(userAddressSelector); + const tokens = useSelector(tokensSelector); useEffect(() => { const socket = new HubConnectionBuilder() @@ -20,9 +21,9 @@ export const EventsProvider = ({ children }: { children: React.ReactNode }) => { setup(socket); // setSocket(socket); socket.on('bigmaps', e => { - if (e.data) dispatch(newEvent(e)); + if (e.data) dispatch(newEvent(e, tokens)); }); - }, [dispatch]); + }, [dispatch, tokens]); // useEffect(() => { // console.warn(socket?.state, userAddress); diff --git a/batcher-ui/src/pages/index.tsx b/batcher-ui/src/pages/index.tsx index c42444a9..fa593fbe 100644 --- a/batcher-ui/src/pages/index.tsx +++ b/batcher-ui/src/pages/index.tsx @@ -7,7 +7,6 @@ import { useSelector, useDispatch } from 'react-redux'; import { currentPairSelector, userAddressSelector, - tokensSelector, } from '@/reducers'; import { getTokens, @@ -19,7 +18,7 @@ import { const Swap = () => { const userAddress = useSelector(userAddressSelector); const tokenPair = useSelector(currentPairSelector); - const tokens = useSelector(tokensSelector); + //const tokens = useSelector(tokensSelector); const dispatch = useDispatch(); @@ -39,7 +38,7 @@ const Swap = () => { useEffect(() => { dispatch(getTokens()); - }, [ dispatch]); + }, [dispatch]); return (
diff --git a/batcher-ui/src/pages/volumes.tsx b/batcher-ui/src/pages/volumes.tsx index 57f27b26..3c46c33f 100644 --- a/batcher-ui/src/pages/volumes.tsx +++ b/batcher-ui/src/pages/volumes.tsx @@ -8,7 +8,6 @@ import { import { useSelector } from 'react-redux'; import { useDispatch } from 'react-redux'; import { getVolumes } from '@/actions'; -import { batch } from 'react-redux'; const Volume = () => { const { sell, buy } = useSelector(volumesSelector); diff --git a/batcher-ui/src/reducers/events.ts b/batcher-ui/src/reducers/events.ts index 0b0adf92..4750830e 100644 --- a/batcher-ui/src/reducers/events.ts +++ b/batcher-ui/src/reducers/events.ts @@ -17,7 +17,7 @@ export const eventReducer = ( ) => { switch (action.type) { case 'NEW_EVENT': - return loop(state, newEventCmd(action.payload.event)); + return loop(state, newEventCmd(action.payload.event, action.payload.tokens)); case 'NEW_INFO': return { ...state, diff --git a/batcher-ui/src/reducers/exchange.ts b/batcher-ui/src/reducers/exchange.ts index b05da888..28770aa7 100644 --- a/batcher-ui/src/reducers/exchange.ts +++ b/batcher-ui/src/reducers/exchange.ts @@ -200,6 +200,8 @@ const exchangeReducer = ( case 'UPDATE_VOLUMES': return { ...state, volumes: action.payload.volumes }; case 'UPDATE_TOKENS': + console.info('tokens', action.payload.tokens); + console.info('state', state); return { ...state, tokens: action.payload.tokens }; case 'GET_TOKENS': return loop(state, fetchTokensCmd()); diff --git a/batcher-ui/src/reducers/index.ts b/batcher-ui/src/reducers/index.ts index b7b75011..21c24e72 100644 --- a/batcher-ui/src/reducers/index.ts +++ b/batcher-ui/src/reducers/index.ts @@ -8,7 +8,6 @@ import { MarketHoldingsState, EventsState, HoldingsState, - TokensState, } from '../types'; import { marketHoldingsReducer } from '@/reducers/marketholdings'; import { eventReducer } from '@/reducers/events'; diff --git a/batcher-ui/src/types/contracts/batcher.ts b/batcher-ui/src/types/contracts/batcher.ts index 567004e0..6fd37bfc 100644 --- a/batcher-ui/src/types/contracts/batcher.ts +++ b/batcher-ui/src/types/contracts/batcher.ts @@ -43,14 +43,8 @@ export type VolumesStorage = { }; export type PairStorage = { - address_0: string; - address_1: string; - decimals_0: string; - decimals_1: string; - name_0: string; - name_1: string; - standard_0: string; - standard_1: string; + string_0: string; + string_1: string; }; type P = { diff --git a/batcher-ui/src/types/contracts/token-manager.ts b/batcher-ui/src/types/contracts/token-manager.ts index 91ef874b..c1fcbb58 100644 --- a/batcher-ui/src/types/contracts/token-manager.ts +++ b/batcher-ui/src/types/contracts/token-manager.ts @@ -1,4 +1,3 @@ -import { boolean } from "fp-ts"; export type TokenManagerStorage = { valid_swaps: { diff --git a/batcher-ui/src/utils/utils.ts b/batcher-ui/src/utils/utils.ts index 3e8037bf..2f1b2029 100644 --- a/batcher-ui/src/utils/utils.ts +++ b/batcher-ui/src/utils/utils.ts @@ -457,14 +457,13 @@ const convertHoldingToPayout = ( return [scaled_payout, scaled_remainder]; }; -const findTokensForBatch = (batch: BatchBigmap, tokens: any) => { +const findTokensForBatch = (batch: BatchBigmap, tokens: Map) => { const pair = batch.pair; - const toks = Object.values(tokens)[0]; - const buyToken = toks.get(pair.string_0); - const sellToken = toks.get(pair.string_1); + const buyToken = tokens.get(pair.string_0); + const sellToken = tokens.get(pair.string_1); const tkns = { - to: { name: buyToken.name, decimals: parseInt(buyToken.decimals, 10) }, - from: { name: sellToken.name, decimals: parseInt(sellToken.decimals, 10) }, + to: { name: buyToken.name, decimals: buyToken.decimals }, + from: { name: sellToken.name, decimals: sellToken.decimals }, }; return tkns; };