Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Cluster time fix (#166)
Browse files Browse the repository at this point in the history
* provides alternative to clustertime

* provides system timestamp when blocktime null

* moves comment to relevant place

* removes redundant finally
  • Loading branch information
Ruven Salamon authored Jan 26, 2022
1 parent 28bc5bd commit fa13614
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
18 changes: 13 additions & 5 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,19 @@ export const getGatekeeperNetwork = multiAsync(

export const getClusterTime = multiAsync(async (connection: Connection) => {
const slot = await connection.getSlot();
return connection.getBlockTime(slot);
return (
connection
.getBlockTime(slot)
.then((blockTime) => {
if (!blockTime) {
throw Error("Could not fetch cluster time");
}

return blockTime;
})
// * 1000 because now() returns milliseconds
.catch(() => Date.now() * 1000)
);
});

const getWeightedAverageFinancingFee = multiAsync(
Expand All @@ -149,10 +161,6 @@ const getWeightedAverageFinancingFee = multiAsync(

const [deals, clusterTime] = await Promise.all([_deals, _clusterTime]);

if (!clusterTime) {
throw Error("Could not fetch cluster time");
}

const runningFinancingFee = deals.reduce((result, deal) => {
const status = mapDealToStatus(deal.account, clusterTime);
if (status === DealStatus.IN_PROGRESS) {
Expand Down
5 changes: 0 additions & 5 deletions src/react/components/DealOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ export const DealOverview = () => {

setDeal(deal);

if (!clusterTime) {
// TODO: DO SOMETHING
throw Error("Could not fetch cluster time");
}

const dealStatus = mapDealToStatus(deal, clusterTime);
setDealStatus(dealStatus);

Expand Down
9 changes: 4 additions & 5 deletions src/react/components/DealsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const DealsTable = (props: Props) => {
const wallet = useAnchorWallet();
const connection = useConnection();
const [deals, setDeals] = useState<any>([]);
const [clusterTime, setClusterTime] = useState<number | null>(null);
const [clusterTime, setClusterTime] = useState<number>(Date.now() * 1000);
const navigate = useNavigate();
const marketSeed = useMarketSeed();

Expand Down Expand Up @@ -62,9 +62,8 @@ export const DealsTable = (props: Props) => {
? new Date(deal.goLiveAt.mul(new BN(1000)).toNumber())
: undefined;

const dealStatus = clusterTime && mapDealToStatus(deal, clusterTime);
const daysRemaining =
dealStatus && clusterTime && getDaysRemaining(deal, clusterTime, dealStatus);
const dealStatus = mapDealToStatus(deal, clusterTime);
const daysRemaining = getDaysRemaining(deal, clusterTime, dealStatus);

const targetRoute = Path.DEALS_DETAIL.replace(":borrower", deal.borrower.toString()).replace(
":deal",
Expand Down Expand Up @@ -105,7 +104,7 @@ export const DealsTable = (props: Props) => {
)}
</TableCell>
<TableCell>{`${daysRemaining} / ${deal.timeToMaturityDays}`}</TableCell>
<TableCell>{`${dealStatus !== null && formatDealStatus(dealStatus)}`}</TableCell>
<TableCell>{`${formatDealStatus(dealStatus)}`}</TableCell>
<TableCell>{showRepayButton && <CredixButton text="repay" />}</TableCell>
</TableRow>
);
Expand Down

0 comments on commit fa13614

Please sign in to comment.