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 46edd4b commit 5bda817
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 36 deletions.
12 changes: 6 additions & 6 deletions batcher-ui/src/actions/events.ts
Original file line number Diff line number Diff line change
@@ -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<string, Token>) =>
({
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<typeof newEvent>
Expand Down
13 changes: 8 additions & 5 deletions batcher-ui/src/actions/holdings.ts
Original file line number Diff line number Diff line change
@@ -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<string, Token>
) =>
({
type: 'GET_HOLDINGS',
payload: { userAddress, tokens },
} as const);
}) as const;

export type HoldingsActions =
| ReturnType<typeof redeem>
Expand Down
12 changes: 7 additions & 5 deletions batcher-ui/src/commands/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import {
computeOraclePrice,
mapStatus,
toVolumes,
ensureMapTypeOnTokens,
} from '@/utils/utils';

export const newEventCmd = (event: BigMapEvent, tokens: Map<string, Token>) => {
export const newEventCmd = (event: BigMapEvent, toks: Map<string, Token>) => {
const tokens = ensureMapTypeOnTokens(toks);
return Cmd.run(
(dispatch, getState) => {
return event.data.map(async eventData => {
Expand All @@ -40,8 +42,8 @@ export const newEventCmd = (event: BigMapEvent, tokens: Map<string, Token>) => {
dispatch(
updateVolumes(
toVolumes(data.volumes, {
buyDecimals: buyToken.decimals,
sellDecimals: sellToken.decimals,
buyDecimals: buyToken?.decimals || 0,
sellDecimals: sellToken?.decimals || 0,
})
)
);
Expand Down Expand Up @@ -89,8 +91,8 @@ export const newEventCmd = (event: BigMapEvent, tokens: Map<string, Token>) => {
dispatch(
updateVolumes(
toVolumes(data.volumes, {
buyDecimals: buyToken.decimals,
sellDecimals: sellToken.decimals,
buyDecimals: buyToken?.decimals || 0,
sellDecimals: sellToken?.decimals || 0,
})
)
);
Expand Down
3 changes: 2 additions & 1 deletion batcher-ui/src/commands/holdings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Token>, userAddress?: string) => {
return Cmd.run(
async () => {
if (!userAddress) return Promise.reject('Not connected !');
Expand Down
23 changes: 12 additions & 11 deletions batcher-ui/src/components/market-maker/Vault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,13 +25,13 @@ const Vault = () => {
const marketHoldings = useSelector(selectHoldings);
const [amountInput, setAmount] = useState<string>('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]);
Expand Down Expand Up @@ -419,14 +419,15 @@ const Vault = () => {
<p>{`Shares: ${marketHoldings?.userVault?.shares}`}</p>
{`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(),
})}
</div>
</div>
Expand Down
69 changes: 62 additions & 7 deletions batcher-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
SwapNames,
RatesCurrentBigmap,
Token,
ValidToken,
ValidTokenAmount,
} from '@/types';
import {
getTokenManagerStorage,
Expand Down Expand Up @@ -364,6 +366,23 @@ export const getTimeDifference = (
return 0;
};

export const ensureMapTypeOnTokens = (
tokens: Map<string, Token>
): Map<string, Token> => {
const typeOfTokens = typeof tokens;
console.info('tokens type', typeOfTokens);
if (tokens instanceof Map) {
return tokens;
} else {
let toks: Map<string, Token> = new Map<string, Token>();
Object.values(tokens).forEach(v => {
console.info('v', v);
toks = v as Map<string, Token>;
});
return toks;
}
};

export const getTimeDifferenceInMs = (
status: BatcherStatus,
startTime: string | null
Expand Down Expand Up @@ -457,13 +476,16 @@ const convertHoldingToPayout = (
return [scaled_payout, scaled_remainder];
};

const findTokensForBatch = (batch: BatchBigmap, tokens: Map<string,Token>) => {
const findTokensForBatch = (batch: BatchBigmap, toks: Map<string, Token>) => {
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;
};
Expand Down Expand Up @@ -510,7 +532,7 @@ const computeHoldingsByBatchAndDeposit = (
deposit: UserOrder,
batch: BatchBigmap,
currentHoldings: HoldingsState,
tokenMap: any
tokenMap: Map<string, Token>
) => {
const side = getSideFromDeposit(deposit);
const tokens = findTokensForBatch(batch, tokenMap);
Expand Down Expand Up @@ -647,7 +669,7 @@ const addObj = (o1: any, o2: any) => {
};

const computeHoldingsByBatch = (
tokens: any,
tokens: Map<string, Token>,
deposits: UserOrder[], //! depots dans un batch
batch: BatchBigmap,
currentHoldings: HoldingsState
Expand Down Expand Up @@ -676,7 +698,7 @@ const computeHoldingsByBatch = (

export const computeAllHoldings = async (
orderbook: OrderBookBigmap,
tokens: any
tokens: Map<string, Token>
) => {
return Promise.all(
Object.entries(orderbook).map(async ([batchNumber, deposits]) => {
Expand Down Expand Up @@ -704,11 +726,44 @@ export const computeAllHoldings = async (
);
};

export const getOrdersBook = async (userAddress: string, tokens: any) => {
export const getOrdersBook = async (
userAddress: string,
tokens: Map<string, Token>
) => {
const orderBookByBatch: { [key: number]: UserOrder[] } =
await getBigMapByIdAndUserAddress(userAddress);
return computeAllHoldings(orderBookByBatch, tokens);
};

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;
};
2 changes: 1 addition & 1 deletion batcher-ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"allowJs": false,
"skipLibCheck": true,
"experimentalDecorators": true,
"strict": false,
"strict": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
Expand Down

0 comments on commit 5bda817

Please sign in to comment.