Skip to content

Commit

Permalink
Fixed linting and type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist committed Nov 14, 2023
1 parent 8f78c0d commit 46edd4b
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 46 deletions.
4 changes: 2 additions & 2 deletions batcher-ui/src/actions/events.ts
Original file line number Diff line number Diff line change
@@ -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 = () =>
Expand Down
22 changes: 14 additions & 8 deletions batcher-ui/src/commands/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
OrderBookBigmap,
RatesCurrentBigmap,
BigMapEvent,
Token,
} from '@/types';
import {
computeAllHoldings,
Expand All @@ -20,7 +21,7 @@ import {
toVolumes,
} from '@/utils/utils';

export const newEventCmd = (event: BigMapEvent) => {
export const newEventCmd = (event: BigMapEvent, tokens: Map<string, Token>) => {
return Cmd.run(
(dispatch, getState) => {
return event.data.map(async eventData => {
Expand All @@ -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,
})
)
);
Expand All @@ -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();
Expand All @@ -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, {
Expand All @@ -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,
})
)
);
Expand All @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions batcher-ui/src/commands/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ const fetchOraclePriceCmd = (tokenPair: string, { swap }: CurrentSwap) => {
);
};

const fetchVolumesCmd = (batchNumber: number, tokens:Map<string,Token>) => {
const fetchVolumesCmd = (batchNumber: number, tokens: Map<string, Token>) => {
return Cmd.run(
() => {
return getVolumes(batchNumber,tokens);
return getVolumes(batchNumber, tokens);
},
{
successActionCreator: updateVolumes,
Expand All @@ -111,8 +111,9 @@ const fetchTokensCmd = () => {
return Cmd.run(
async () => {
const tokens = await getTokens();

return tokens;
const mapped: Map<string, Token> = ((tokens as unknown) as Map<string, Token>);
console.info('Mapped tokens', mapped);
return mapped;
},
{
successActionCreator: updateTokens,
Expand Down
9 changes: 2 additions & 7 deletions batcher-ui/src/components/batcher/SelectPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,8 +21,8 @@ const SelectPair = ({ isFrom }: SelectPairProps) => {
const { swap, isReverse } = useSelector(currentSwapSelector);
const dispatch = useDispatch();

//const tokens = useSelector(tokensSelector);
const [availableTokens, setAvailableTokens] = useState<any[]>([]);
const [availableSwaps, setAvailableSwaps] = useState<any[]>([]);

const displayValue = useCallback(() => {
if (isReverse && isFrom) return swap.to.name;
Expand All @@ -40,11 +40,6 @@ const SelectPair = ({ isFrom }: SelectPairProps) => {
setAvailableTokens(tokens);
}
);
getSwapsMetadata().then(
(swaps: { name: string; to: string; from: string }[]) => {
setAvailableSwaps(swaps);
}
);
}, []);

return (
Expand Down
9 changes: 5 additions & 4 deletions batcher-ui/src/contexts/events.tsx
Original file line number Diff line number Diff line change
@@ -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<{}>({});
Expand All @@ -12,6 +12,7 @@ export const EventsProvider = ({ children }: { children: React.ReactNode }) => {
// const [socket, setSocket] = useState<HubConnection | undefined>(undefined);
const dispatch = useDispatch();
// const userAddress = useSelector(userAddressSelector);
const tokens = useSelector(tokensSelector);

useEffect(() => {
const socket = new HubConnectionBuilder()
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions batcher-ui/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useSelector, useDispatch } from 'react-redux';
import {
currentPairSelector,
userAddressSelector,
tokensSelector,
} from '@/reducers';
import {
getTokens,
Expand All @@ -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();

Expand All @@ -39,7 +38,7 @@ const Swap = () => {

useEffect(() => {
dispatch(getTokens());
}, [ dispatch]);
}, [dispatch]);

return (
<div className="flex flex-col md:mx-[15%] mx-4">
Expand Down
1 change: 0 additions & 1 deletion batcher-ui/src/pages/volumes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion batcher-ui/src/reducers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions batcher-ui/src/reducers/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion batcher-ui/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
MarketHoldingsState,
EventsState,
HoldingsState,
TokensState,
} from '../types';
import { marketHoldingsReducer } from '@/reducers/marketholdings';
import { eventReducer } from '@/reducers/events';
Expand Down
10 changes: 2 additions & 8 deletions batcher-ui/src/types/contracts/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K extends string> = {
Expand Down
1 change: 0 additions & 1 deletion batcher-ui/src/types/contracts/token-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { boolean } from "fp-ts";

export type TokenManagerStorage = {
valid_swaps: {
Expand Down
11 changes: 5 additions & 6 deletions batcher-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,13 @@ const convertHoldingToPayout = (
return [scaled_payout, scaled_remainder];
};

const findTokensForBatch = (batch: BatchBigmap, tokens: any) => {
const findTokensForBatch = (batch: BatchBigmap, tokens: Map<string,Token>) => {
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;
};
Expand Down

0 comments on commit 46edd4b

Please sign in to comment.