Skip to content

Commit

Permalink
🐛 fix issues with holdings and improve other things
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Burg committed Sep 11, 2023
1 parent 3b94cce commit 5b48165
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 32 deletions.
19 changes: 13 additions & 6 deletions batcher-ui/pages/holdings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import React, { useCallback, useContext, useEffect } from 'react';
import { TezosToolkitContext } from 'src/contexts/tezos-toolkit';
import { useDispatch, useSelector } from 'react-redux';
import { getHoldings, userAddressSelector } from 'src/reducers';
import { getHoldings as getHoldingsAction } from 'src/actions';
import {
getHoldings as getHoldingsAction,
newError,
newInfo,
} from 'src/actions';

const Holdings = () => {
const { tezos } = useContext(TezosToolkitContext);
Expand Down Expand Up @@ -35,17 +39,20 @@ const Holdings = () => {

if (redeemTransaction) {
//?useless
dispatch(newInfo('Attempting to redeem holdings...'));
// message.loading('Attempting to redeem holdings...', 0);
const confirm = await redeemTransaction.confirmation();
if (!confirm.completed) {
console.error('Failed to redeem holdings' + confirm);
dispatch(newError('Failed to redeem holdings.'));
} else {
console.info('Successfully redeemed holdings');
dispatch(newInfo('Successfully redeemed holdings.'));
}
} else {
dispatch(newError('Failed to redeem tokens.'));
throw new Error('Failed to redeem tokens');
}
} catch (error: any) {
} catch (error) {
dispatch(newError('Unable to redeem holdings.'));
console.error('Unable to redeem holdings' + error);
}
};
Expand All @@ -70,7 +77,7 @@ const Holdings = () => {
{Object.values(open).map((b, i) => {
return (
<td
className="border border-white p-2 text-center bg-lightgray"
className="border border-white p-2 text-center bg-lightgray w-[33%]"
key={i}>
{b}
</td>
Expand Down Expand Up @@ -99,7 +106,7 @@ const Holdings = () => {
{Object.values(cleared).map((b, i) => {
return (
<td
className="border border-white p-2 text-center bg-lightgray"
className="border border-white p-2 text-center bg-lightgray w-[33%]"
key={i}>
{b}
</td>
Expand Down
21 changes: 15 additions & 6 deletions batcher-ui/src/commands/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
updateBatcherStatus,
} from 'src/actions';
import { updateHoldings } from 'src/actions/holdings';
import { userAddressSelector } from 'src/reducers';
import { BatchBigmap, OrderBookBigmap, RatesCurrentBigmap } from 'src/types';
import { BigMapEvent } from 'src/types/events';
import {
Expand All @@ -17,7 +18,7 @@ import {

export const newEventCmd = (event: BigMapEvent) => {
return Cmd.run(
dispatch => {
(dispatch, getState) => {
return event.data.map(async eventData => {
switch (eventData.action) {
case 'add_key': {
Expand All @@ -41,8 +42,12 @@ export const newEventCmd = (event: BigMapEvent) => {
case 'user_batch_ordertypes': {
//! deposit from new address
const data = eventData.content.value as OrderBookBigmap;
const holdings = await computeAllHoldings(data);
dispatch(updateHoldings(holdings));
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);
dispatch(updateHoldings(holdings));
}
return Promise.resolve();
}
case 'rates_current':
Expand Down Expand Up @@ -83,8 +88,12 @@ export const newEventCmd = (event: BigMapEvent) => {
case 'user_batch_ordertypes': {
//! new deposit or redeem from an existing address
const data = eventData.content.value as OrderBookBigmap;
const holdings = await computeAllHoldings(data);
dispatch(updateHoldings(holdings));
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);
dispatch(updateHoldings(holdings));
}
return Promise.resolve();
}
default:
Expand All @@ -97,7 +106,7 @@ export const newEventCmd = (event: BigMapEvent) => {
});
},
{
args: [Cmd.dispatch],
args: [Cmd.dispatch, Cmd.getState],
}
);
};
38 changes: 25 additions & 13 deletions batcher-ui/src/components/Exchange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ const Exchange = () => {
const [amountInput, setAmount] = useState<string>('0');
const [animate, setAnimate] = useState(false);

//TODO: rewrite error management
if (!tezos)
return (
<div>
<p className="text-xxl">
{"There is an error with Tezos Tool Kit, can't swap !"}
</p>
</div>
if (!tezos) {
dispatch(
newError(
"There is an error with Tezos Tool Kit, can't swap ! Please contact Marigold if problem persists."
)
);
return null;
}

const toTolerance = (isReverse: boolean, priceStategy: PriceStrategy) => {
switch (priceStategy) {
Expand Down Expand Up @@ -154,7 +153,14 @@ const Exchange = () => {
};

if (selectedToken.standard === 'FA1.2 token') {
if (!swap.from.token.address) return; //TODO: improve this
if (!swap.from.token.address) {
dispatch(
newError(
`Can\t retrieve token contract address for ${swap.from.token.name}`
)
);
return;
}
const tokenfa12Contract = await tezos?.wallet.at(
swap.from.token.address,
compose(tzip12, tzip16)
Expand Down Expand Up @@ -236,10 +242,16 @@ const Exchange = () => {
dispatch(fetchUserBalances());
setAmount('0');
}
} catch (error) {
dispatch(
newError('Unknown deposit error, please retry or contact Marigold')
);
} catch (error: any) {
console.error(error);
if (error?.title === 'Aborted') {
// Action aborted by user
dispatch(newError(error.description));
} else {
dispatch(
newError('Unknown deposit error, please retry or contact Marigold')
);
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions batcher-ui/src/reducers/holdings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { fetchHoldingsCmd } from 'src/commands/holdings';
import { HoldingsState } from 'src/types';

const initialState: HoldingsState = {
open: { tzBTC: 0, USDT: 0 },
cleared: { tzBTC: 0, USDT: 0 },
open: { tzBTC: 0, USDT: 0, EURL: 0 },
cleared: { tzBTC: 0, USDT: 0, EURL: 0 },
};

export const holdingsReducer = (
Expand Down
16 changes: 11 additions & 5 deletions batcher-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ const addObj = (o1: any, o2: any) => {
[c]: o1[c] + o2[c],
};
},
{ tzBTC: 0, USDT: 0 }
{ tzBTC: 0, USDT: 0, EURL: 0 }
);
};

Expand All @@ -635,7 +635,10 @@ const computeHoldingsByBatch = (
),
};
},
{ open: { tzBTC: 0, USDT: 0 }, cleared: { tzBTC: 0, USDT: 0 } }
{
open: { tzBTC: 0, USDT: 0, EURL: 0 },
cleared: { tzBTC: 0, USDT: 0, EURL: 0 },
}
);
};

Expand All @@ -646,8 +649,8 @@ export const computeAllHoldings = (orderbook: OrderBookBigmap) => {
parseInt(batchNumber, 10)
);
return computeHoldingsByBatch(deposits, batch, {
open: { tzBTC: 0, USDT: 0 },
cleared: { tzBTC: 0, USDT: 0 },
open: { tzBTC: 0, USDT: 0, EURL: 0 },
cleared: { tzBTC: 0, USDT: 0, EURL: 0 },
});
})
).then(holdings =>
Expand All @@ -658,7 +661,10 @@ export const computeAllHoldings = (orderbook: OrderBookBigmap) => {
cleared: addObj(acc.cleared, currentHoldings.cleared),
};
},
{ open: { tzBTC: 0, USDT: 0 }, cleared: { tzBTC: 0, USDT: 0 } }
{
open: { tzBTC: 0, USDT: 0, EURL: 0 },
cleared: { tzBTC: 0, USDT: 0, EURL: 0 },
}
)
);
};
Expand Down

0 comments on commit 5b48165

Please sign in to comment.