Skip to content

Commit

Permalink
Merge branch 'main' into fix-dust-after-settling-positions
Browse files Browse the repository at this point in the history
  • Loading branch information
leomassazza authored Jan 15, 2025
2 parents da6e5e6 + 219728b commit 38095ed
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 63 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"dev:docs": "pnpm --filter docs run dev",
"dev:website": "pnpm --filter website run dev",
"dev:api": "concurrently \"NODE_ENV=development pnpm --filter api run dev:service\" \"NODE_ENV=development pnpm --filter api run dev:worker\"",
"start:reindex-market": "pnpm --filter data run start:reindex-market",
"start:reindex-missing": "pnpm --filter data run start:reindex-missing"
"start:reindex-market": "pnpm --filter api run start:reindex-market",
"start:reindex-missing": "pnpm --filter api run start:reindex-missing"
},
"keywords": [],
"devDependencies": {
Expand Down
41 changes: 21 additions & 20 deletions packages/app/src/app/resources/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { formatUnits } from 'viem';

import { Card, CardContent } from '@/components/ui/card';
import CandlestickChart from '~/lib/components/chart';
import { EpochTiming } from '~/lib/components/foil/EpochTiming';
import NumberDisplay from '~/lib/components/foil/numberDisplay';
import { MarketLayout } from '~/lib/components/market/MarketLayout';
import { ResourceNav } from '~/lib/components/market/ResourceNav';
Expand Down Expand Up @@ -82,16 +83,10 @@ const EpochsTable = ({ data }: { data: Epoch[] }) => {
className={`flex items-center justify-between cursor-pointer px-4 py-1.5 ${hoveredIndex === index ? 'bg-secondary' : 'hover:bg-secondary/50'}`}
>
<div className="flex items-baseline">
<span>
Ends {format(new Date(epoch.endTimestamp * 1000), 'M/d')}
</span>
<span className="text-xs text-muted-foreground ml-2">
{Math.round(
(epoch.endTimestamp - epoch.startTimestamp) /
(7 * 24 * 3600)
)}{' '}
week period
</span>
<EpochTiming
startTimestamp={epoch.startTimestamp}
endTimestamp={epoch.endTimestamp}
/>
</div>
<ChevronRight className="h-6 w-6 text-muted-foreground" />
</div>
Expand Down Expand Up @@ -174,7 +169,11 @@ const renderPriceDisplay = (

return (
<span className="text-2xl font-bold">
<NumberDisplay value={formatUnits(BigInt(price.value), 9)} /> {unit}
<NumberDisplay
value={formatUnits(BigInt(price.value), 9)}
precision={resourceId === 'celestia-blobspace' ? 6 : 4}
/>{' '}
{unit}
</span>
);
};
Expand Down Expand Up @@ -236,15 +235,17 @@ const MarketContent = ({ params }: { params: { id: string } }) => {
// Get the current resource and its markets
const resource = resources?.find((r) => r.slug === params.id);
const epochs =
resource?.markets.flatMap((market) =>
(market.epochs || []).map((epoch) => ({
...epoch,
market: {
address: market.address,
chainId: market.chainId,
},
}))
) || [];
resource?.markets
.flatMap((market) =>
(market.epochs || []).map((epoch) => ({
...epoch,
market: {
address: market.address,
chainId: market.chainId,
},
}))
)
.sort((a, b) => b.startTimestamp - a.startTimestamp) || [];

const formattedResourcePrices: ResourcePricePoint[] =
resourcePrices?.map((price) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/lib/components/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const CandlestickChart: React.FC<Props> = ({
borderColor: theme === 'dark' ? '#363537' : '#cccccc',
timeVisible: true,
secondsVisible: false,
minBarSpacing: 0.001,
},
});

Expand Down
31 changes: 31 additions & 0 deletions packages/app/src/lib/components/foil/EpochTiming.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { format } from 'date-fns';

interface EpochTimingProps {
startTimestamp: number;
endTimestamp: number;
showDuration?: boolean;
}

export const EpochTiming = ({
startTimestamp,
endTimestamp,
showDuration = true,
}: EpochTimingProps) => {
const now = Math.floor(Date.now() / 1000);
const isStartInFuture = startTimestamp > now;
const date = isStartInFuture ? startTimestamp : endTimestamp;
const prefix = isStartInFuture ? 'Starts' : 'Ends';

const weeks = Math.round((endTimestamp - startTimestamp) / (7 * 24 * 3600));

return (
<>
{prefix} {format(new Date(date * 1000), 'M/d')}
{showDuration && (
<span className="text-xs text-muted-foreground ml-2">
{weeks} week period
</span>
)}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ const AddEditLiquidity: React.FC = () => {

const isDecrease =
liquidityAction === 'remove' && Number(modifyLiquidity) > 0;
const isAmountUnchanged = Number(modifyLiquidity) === 0;
const isAmountUnchanged = isEdit
? Number(modifyLiquidity) === 0
: Number(depositAmount) === 0;

/// //// READ CONTRACT HOOKS ///////
const { data: positionData, refetch: refetchPosition } = useReadContract({
Expand Down Expand Up @@ -640,17 +642,17 @@ const AddEditLiquidity: React.FC = () => {
}
}, [tokenAmountsError, toast]);

// hanlde uniswap error
// handle uniswap error
useEffect(() => {
if (uniswapPositionError) {
if (uniswapPositionError && isEdit) {
console.error('uniswapPositionError: ', uniswapPositionError);
toast({
title: 'Failed to get position from uniswap',
description: uniswapPositionError?.message,
duration: 5000,
});
}
}, [uniswapPositionError, toast]);
}, [uniswapPositionError, isEdit, toast]);

useEffect(() => {
if (isEdit) return;
Expand Down Expand Up @@ -920,6 +922,7 @@ const AddEditLiquidity: React.FC = () => {

return (
<Button className="w-full" size="lg" type="submit" disabled={isDisabled}>
{pendingTxn && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{getButtonText()}
</Button>
);
Expand Down
29 changes: 19 additions & 10 deletions packages/app/src/lib/components/foil/numberDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import {

type NumberDisplayProps = {
value: number | string | bigint;
precision?: number;
};

const NumberDisplay: React.FC<NumberDisplayProps> = ({ value }) => {
const NumberDisplay: React.FC<NumberDisplayProps> = ({
value,
precision = 4,
}) => {
const formatNumber = (val: bigint | number | string): string => {
let numValue: number;
let stringValue: string;
Expand All @@ -34,30 +38,35 @@ const NumberDisplay: React.FC<NumberDisplayProps> = ({ value }) => {
return 'Invalid number';
}

if (Math.abs(numValue) < 0.0001 && numValue !== 0) {
return '<0.0001';
if (Math.abs(numValue) < 1 / 10 ** precision && numValue !== 0) {
return `<${1 / 10 ** precision}`;
}

const roundedValue = Number(numValue.toFixed(4));
const roundedValue = Number(numValue.toFixed(precision));

return roundedValue.toString();
};

const displayValue = formatNumber(value || 0);
const originalValue = value.toString();

return displayValue.length ? (
if (!displayValue.length) {
return <Minus className="opacity-20" />;
}

if (displayValue === originalValue) {
return <span className="cursor-default">{displayValue}</span>;
}

return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger type="button" className="cursor-default">
{displayValue}
</TooltipTrigger>
<TooltipContent className="font-normal">
{value.toString()}
</TooltipContent>
<TooltipContent className="font-normal">{originalValue}</TooltipContent>
</Tooltip>
</TooltipProvider>
) : (
<Minus className="opacity-20" />
);
};

Expand Down
24 changes: 3 additions & 21 deletions packages/app/src/lib/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { usePathname } from 'next/navigation';
import React, { useState, useEffect } from 'react';

import ConnectButton from '../components/ConnectButton';
import { EpochTiming } from '../components/foil/EpochTiming';
import { ModeToggle } from '../components/ModeToggle';
import {
Accordion,
Expand Down Expand Up @@ -50,16 +51,7 @@ const ResourcePopover = ({ label, path }: { label: string; path: string }) => {
}, [hoveredResource, resources]);

const formatDuration = (start: number, end: number) => {
const endDate = new Date(end * 1000);
const weeks = Math.round((end - start) / (7 * 24 * 3600));
return (
<>
Ends {format(endDate, 'M/d')}
<span className="text-xs text-muted-foreground ml-2">
{weeks} week period
</span>
</>
);
return <EpochTiming startTimestamp={start} endTimestamp={end} />;
};

const handleLinkClick = () => {
Expand Down Expand Up @@ -205,17 +197,7 @@ const NavLinks = ({
};

const formatDuration = (start: number, end: number) => {
const startDate = new Date(start * 1000);
const endDate = new Date(end * 1000);
const weeks = Math.round((end - start) / (7 * 24 * 3600));
return (
<>
{format(endDate, 'M/d')}
<span className="text-sm text-muted-foreground ml-2">
{weeks} week period
</span>
</>
);
return <EpochTiming startTimestamp={start} endTimestamp={end} />;
};

const renderMobileMarketLinks = (path: string) => {
Expand Down
4 changes: 0 additions & 4 deletions packages/protocol/src/market/modules/TradeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ contract TradeModule is ITradeModule, ReentrancyGuardUpgradeable {

Epoch.Data storage epoch = Epoch.load(epochId);

if (block.timestamp < epoch.startTime) {
revert Errors.EpochNotStarted(epochId, epoch.startTime);
}

// check if epoch is not settled
epoch.validateNotSettled();

Expand Down
6 changes: 5 additions & 1 deletion packages/protocol/src/market/modules/UMASettlementModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ contract UMASettlementModule is
epoch.marketParams.bondAmount
);

uint256 decimalPrice = DecimalPrice.sqrtRatioX96ToPrice(
settlementSqrtPriceX96
);

bytes memory claim = abi.encodePacked(
string(epoch.marketParams.claimStatement),
" between timestamps ",
Strings.toString(epoch.startTime),
" and ",
Strings.toString(epoch.endTime),
" (inclusive) is ",
Strings.toString(settlementSqrtPriceX96),
Strings.toString(decimalPrice),
"."
);

Expand Down
1 change: 0 additions & 1 deletion packages/protocol/src/market/storage/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ library Errors {
error EpochNotSettled(uint256 epochId);
error ExpiredEpochNotSettled(uint256 epochEndTime);
error EpochAlreadyStarted();
error EpochNotStarted(uint256 epochId, uint256 epochStartTime);
error EpochSettled();
error ExpiredEpoch();
error TokensAlreadyCreated();
Expand Down

0 comments on commit 38095ed

Please sign in to comment.