diff --git a/batcher-ui/src/actions/events.ts b/batcher-ui/src/actions/events.ts index af0e0e08..8f04e575 100644 --- a/batcher-ui/src/actions/events.ts +++ b/batcher-ui/src/actions/events.ts @@ -1,27 +1,27 @@ -import type { BigMapEvent } from '@/types'; +import type { BigMapEvent, Token } from '@/types'; -export const newEvent = (event: BigMapEvent, tokens:any) => +export const newEvent = (event: BigMapEvent, tokens: Map) => ({ type: 'NEW_EVENT', payload: { event, tokens }, - } as const); + }) as const; export const closeToast = () => ({ type: 'CLOSE_TOAST', - } as const); + }) as const; export const newError = (errorContent: string) => ({ type: 'NEW_ERROR', payload: { errorContent }, - } as const); + }) as const; export const newInfo = (infoContent: string) => ({ type: 'NEW_INFO', payload: { infoContent }, - } as const); + }) as const; export type EventActions = | ReturnType diff --git a/batcher-ui/src/actions/holdings.ts b/batcher-ui/src/actions/holdings.ts index 81a0aab2..b0241200 100644 --- a/batcher-ui/src/actions/holdings.ts +++ b/batcher-ui/src/actions/holdings.ts @@ -1,21 +1,24 @@ -import { HoldingsState } from '@/types'; +import { HoldingsState, Token } from '@/types'; export const redeem = () => ({ type: 'REDEEM', - } as const); + }) as const; export const updateHoldings = (holdings: HoldingsState) => ({ type: 'UPDATE_HOLDINGS', payload: { holdings }, - } as const); + }) as const; -export const getHoldings = (userAddress: string | undefined, tokens:any) => +export const getHoldings = ( + userAddress: string | undefined, + tokens: Map +) => ({ type: 'GET_HOLDINGS', payload: { userAddress, tokens }, - } as const); + }) as const; export type HoldingsActions = | ReturnType diff --git a/batcher-ui/src/commands/events.ts b/batcher-ui/src/commands/events.ts index 159fa88f..2aa7cec8 100644 --- a/batcher-ui/src/commands/events.ts +++ b/batcher-ui/src/commands/events.ts @@ -19,9 +19,11 @@ import { computeOraclePrice, mapStatus, toVolumes, + ensureMapTypeOnTokens, } from '@/utils/utils'; -export const newEventCmd = (event: BigMapEvent, tokens: Map) => { +export const newEventCmd = (event: BigMapEvent, toks: Map) => { + const tokens = ensureMapTypeOnTokens(toks); return Cmd.run( (dispatch, getState) => { return event.data.map(async eventData => { @@ -40,8 +42,8 @@ export const newEventCmd = (event: BigMapEvent, tokens: Map) => { dispatch( updateVolumes( toVolumes(data.volumes, { - buyDecimals: buyToken.decimals, - sellDecimals: sellToken.decimals, + buyDecimals: buyToken?.decimals || 0, + sellDecimals: sellToken?.decimals || 0, }) ) ); @@ -89,8 +91,8 @@ export const newEventCmd = (event: BigMapEvent, tokens: Map) => { dispatch( updateVolumes( toVolumes(data.volumes, { - buyDecimals: buyToken.decimals, - sellDecimals: sellToken.decimals, + buyDecimals: buyToken?.decimals || 0, + sellDecimals: sellToken?.decimals || 0, }) ) ); diff --git a/batcher-ui/src/commands/holdings.ts b/batcher-ui/src/commands/holdings.ts index 47667610..a375749a 100644 --- a/batcher-ui/src/commands/holdings.ts +++ b/batcher-ui/src/commands/holdings.ts @@ -2,8 +2,9 @@ import { Cmd } from 'redux-loop'; import { getOrdersBook } from '@/utils/utils'; import { updateHoldings } from '@/actions/holdings'; import { newError } from '@/actions'; +import { Token } from '@/types'; -const fetchHoldingsCmd = (tokens: any, userAddress?: string) => { +const fetchHoldingsCmd = (tokens: Map, userAddress?: string) => { return Cmd.run( async () => { if (!userAddress) return Promise.reject('Not connected !'); diff --git a/batcher-ui/src/components/market-maker/Vault.tsx b/batcher-ui/src/components/market-maker/Vault.tsx index 90c17551..0e3a638f 100644 --- a/batcher-ui/src/components/market-maker/Vault.tsx +++ b/batcher-ui/src/components/market-maker/Vault.tsx @@ -4,7 +4,7 @@ import { getMarketHoldings, fetchUserBalances } from '@/actions'; import { selectCurrentVaultName } from '@/reducers'; import { ValidTokenAmount } from '@/types/contracts/token-manager'; import * as Form from '@radix-ui/react-form'; -import { scaleAmountUp } from '@/utils/utils'; +import { scaleAmountUp, emptyValidTokenAmount } from '@/utils/utils'; import { tzip12 } from '@taquito/tzip12'; import { tzip16 } from '@taquito/tzip16'; import { compose, OpKind } from '@taquito/taquito'; @@ -25,13 +25,13 @@ const Vault = () => { const marketHoldings = useSelector(selectHoldings); const [amountInput, setAmount] = useState('0'); const tokenName = useSelector(selectCurrentVaultName); - // const scaleTokenAmount = (ta: ValidTokenAmount): ValidTokenAmount => { - // const scaledAmount = ta.amount / 10 ** (ta?.token.decimals || 0); - // return { - // ...ta, - // amount: scaledAmount, - // }; - // }; + // const scaleTokenAmount = (ta: ValidTokenAmount): ValidTokenAmount => { + // const scaledAmount = ta.amount / 10 ** (ta?.token.decimals || 0); + // return { + // ...ta, + // amount: scaledAmount, + // }; + // }; useEffect(() => { dispatch(getMarketHoldings(tokenName || '', userAddress)); }, [dispatch, userAddress, tokenName]); @@ -419,14 +419,15 @@ const Vault = () => {

{`Shares: ${marketHoldings?.userVault?.shares}`}

{`Unclaimed Rewards: ${marketHoldings?.userVault?.unclaimed} TEZ`} {showAddLiquidity({ - vaultToken: marketHoldings?.nativeToken, + vaultToken: + marketHoldings?.nativeToken || emptyValidTokenAmount(), userBalances: userBalances, })} {showRemoveLiquidity({ - vaultToken: marketHoldings?.nativeToken, + vaultToken: marketHoldings?.nativeToken || emptyValidTokenAmount(), })} {showClaimRewards({ - vaultToken: marketHoldings?.nativeToken, + vaultToken: marketHoldings?.nativeToken || emptyValidTokenAmount(), })} diff --git a/batcher-ui/src/utils/utils.ts b/batcher-ui/src/utils/utils.ts index 2f1b2029..88896c75 100644 --- a/batcher-ui/src/utils/utils.ts +++ b/batcher-ui/src/utils/utils.ts @@ -17,6 +17,8 @@ import { SwapNames, RatesCurrentBigmap, Token, + ValidToken, + ValidTokenAmount, } from '@/types'; import { getTokenManagerStorage, @@ -364,6 +366,23 @@ export const getTimeDifference = ( return 0; }; +export const ensureMapTypeOnTokens = ( + tokens: Map +): Map => { + const typeOfTokens = typeof tokens; + console.info('tokens type', typeOfTokens); + if (tokens instanceof Map) { + return tokens; + } else { + let toks: Map = new Map(); + Object.values(tokens).forEach(v => { + console.info('v', v); + toks = v as Map; + }); + return toks; + } +}; + export const getTimeDifferenceInMs = ( status: BatcherStatus, startTime: string | null @@ -457,13 +476,16 @@ const convertHoldingToPayout = ( return [scaled_payout, scaled_remainder]; }; -const findTokensForBatch = (batch: BatchBigmap, tokens: Map) => { +const findTokensForBatch = (batch: BatchBigmap, toks: Map) => { const pair = batch.pair; + console.info('TOKS', toks); + const tokens = ensureMapTypeOnTokens(toks); + console.info('TOKENS', tokens); const buyToken = tokens.get(pair.string_0); const sellToken = tokens.get(pair.string_1); const tkns = { - to: { name: buyToken.name, decimals: buyToken.decimals }, - from: { name: sellToken.name, decimals: sellToken.decimals }, + to: { name: buyToken?.name || "", decimals: buyToken?.decimals || 0 }, + from: { name: sellToken?.name || "", decimals: sellToken?.decimals || 0 }, }; return tkns; }; @@ -510,7 +532,7 @@ const computeHoldingsByBatchAndDeposit = ( deposit: UserOrder, batch: BatchBigmap, currentHoldings: HoldingsState, - tokenMap: any + tokenMap: Map ) => { const side = getSideFromDeposit(deposit); const tokens = findTokensForBatch(batch, tokenMap); @@ -647,7 +669,7 @@ const addObj = (o1: any, o2: any) => { }; const computeHoldingsByBatch = ( - tokens: any, + tokens: Map, deposits: UserOrder[], //! depots dans un batch batch: BatchBigmap, currentHoldings: HoldingsState @@ -676,7 +698,7 @@ const computeHoldingsByBatch = ( export const computeAllHoldings = async ( orderbook: OrderBookBigmap, - tokens: any + tokens: Map ) => { return Promise.all( Object.entries(orderbook).map(async ([batchNumber, deposits]) => { @@ -704,7 +726,10 @@ export const computeAllHoldings = async ( ); }; -export const getOrdersBook = async (userAddress: string, tokens: any) => { +export const getOrdersBook = async ( + userAddress: string, + tokens: Map +) => { const orderBookByBatch: { [key: number]: UserOrder[] } = await getBigMapByIdAndUserAddress(userAddress); return computeAllHoldings(orderBookByBatch, tokens); @@ -712,3 +737,33 @@ export const getOrdersBook = async (userAddress: string, tokens: any) => { const getDepositAmount = (depositAmount: number, decimals: number) => Math.floor(depositAmount) / 10 ** decimals; + +export const emptyToken = () => { + const t: Token = { + address: '', + name: '', + decimals: 0, + standard: 'FA2 token', + tokenId: 0, + }; + return t; +}; + +export const emptyValidToken = () => { + const t: ValidToken = { + name: '', + address: '', + token_id: '0', + decimals: '0', + standard: '', + }; + return t; +}; + +export const emptyValidTokenAmount = () => { + const ta: ValidTokenAmount = { + token: emptyValidToken(), + amount: 0, + }; + return ta; +}; diff --git a/batcher-ui/tsconfig.json b/batcher-ui/tsconfig.json index e915b3d3..0ed5f538 100644 --- a/batcher-ui/tsconfig.json +++ b/batcher-ui/tsconfig.json @@ -19,7 +19,7 @@ "allowJs": false, "skipLibCheck": true, "experimentalDecorators": true, - "strict": false, + "strict": true, "noEmit": true, "incremental": true, "esModuleInterop": true,