Skip to content

Commit

Permalink
Subgraph: Modified basin daily snapshot to trigger at 9 am PT/12pm EST (
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking authored May 21, 2024
2 parents 2e0c244 + 307917e commit 49101d6
Show file tree
Hide file tree
Showing 28 changed files with 544 additions and 370 deletions.
4 changes: 3 additions & 1 deletion projects/subgraph-basin/src/utils/Well.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ export function incrementWellWithdraw(wellAddress: Address): void {

export function checkForSnapshot(wellAddress: Address, timestamp: BigInt, blockNumber: BigInt): void {
// We check for the prior period snapshot and then take one if needed
let dayID = dayFromTimestamp(timestamp) - 1;
// Schedule the "day" to begin at 9am PT/12pm ET.
// Future work could include properly adjusting this when DST occurs.
let dayID = dayFromTimestamp(timestamp, 8 * 60 * 60) - 1;
let hourID = hourFromTimestamp(timestamp) - 1;

let well = loadWell(wellAddress);
Expand Down
11 changes: 9 additions & 2 deletions projects/subgraph-core/utils/Dates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { BigInt } from "@graphprotocol/graph-ts";

export function dayFromTimestamp(timestamp: BigInt): i32 {
let day_ts = timestamp.toI32() - (timestamp.toI32() % 86400);
/**
* Optionally accepts an offset, which adjusts the start of the day from UTC 00:00.
* @param timestamp - the timestamp to extract the day from
* @param offset - how much sooner the day should roll over (relative to UTC)
* for example, for PST (UTC-7), an appropriate offset would be -7 * 60 * 60.
* This would make the day roll over 7 hours later.
*/
export function dayFromTimestamp(timestamp: BigInt, offset: i32 = 0): i32 {
let day_ts = timestamp.toI32() + offset - ((timestamp.toI32() + offset) % 86400);
return day_ts / 86400;
}

Expand Down
10 changes: 6 additions & 4 deletions projects/ui/src/components/Balances/SiloBalances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import {
UNRIPE_BEAN_WETH,
} from '~/constants/tokens';
import useWhitelist from '~/hooks/beanstalk/useWhitelist';
import { BeanstalkPalette, IconSize } from '../App/muiTheme';
import Fiat from '~/components/Common/Fiat';

import Row from '../Common/Row';
import { displayFullBN, displayTokenAmount } from '~/util';
import TokenIcon from '../Common/TokenIcon';
import { AppState } from '~/state';
import { ONE_BN, ZERO_BN } from '~/constants';
import useFarmerStalkByToken from '~/hooks/farmer/useFarmerStalkByToken';
import useGetChainToken from '~/hooks/chain/useGetChainToken';
import useUnripeUnderlyingMap from '~/hooks/beanstalk/useUnripeUnderlying';
import TokenIcon from '../Common/TokenIcon';
import Row from '../Common/Row';
import { BeanstalkPalette, IconSize } from '../App/muiTheme';
import Stat from '../Common/Stat';

const ARROW_CONTAINER_WIDTH = 20;
Expand Down Expand Up @@ -243,7 +243,9 @@ const SiloBalances: React.FC<{}> = () => {
</Typography>
)}
<Box display={{ md: 'inline', xs: 'none' }}>
<Typography component="span" color="text.primary">&nbsp;{token.symbol}</Typography>
<Typography component="span" color="text.primary">
&nbsp;{token.symbol}
</Typography>
</Box>
</Grid>
{/**
Expand Down
138 changes: 74 additions & 64 deletions projects/ui/src/components/Barn/Actions/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import { useFetchFarmerBarn } from '~/state/farmer/barn/updater';
import { FertilizerBalance } from '~/state/farmer/barn';

export type TransferFormValues = {
fertilizerIds: number[];
amounts: number[];
to: string | null;
totalSelected: number;
fertilizerIds: number[];
amounts: number[];
to: string | null;
totalSelected: number;
};

export type FullFertilizerBalance = FertilizerBalance & {
pctRepaid: BigNumber,
status: string,
humidity: BigNumber,
debt: BigNumber,
sprouts: BigNumber,
rinsableSprouts: BigNumber
pctRepaid: BigNumber;
status: string;
humidity: BigNumber;
debt: BigNumber;
sprouts: BigNumber;
rinsableSprouts: BigNumber;
};

const TransferForm: FC<FormikProps<TransferFormValues>> = ({
Expand All @@ -48,7 +48,7 @@ const TransferForm: FC<FormikProps<TransferFormValues>> = ({
isSubmitting,
}) => {
const sdk = useSdk();

/// Data
const beanstalkBarn = useSelector<AppState, AppState['_beanstalk']['barn']>(
(state) => state._beanstalk.barn
Expand All @@ -70,18 +70,20 @@ const TransferForm: FC<FormikProps<TransferFormValues>> = ({
);

/// Derived
const isReady = values.fertilizerIds && values.to && values.amounts && isValid;
const totalFertAmount = values.amounts.reduce((total, current) => (current || 0) + total, 0);
const isReady =
values.fertilizerIds && values.to && values.amounts && isValid;
const totalFertAmount = values.amounts.reduce(
(total, current) => (current || 0) + total,
0
);

const fertilizers = useMemo(() => {
const output: FullFertilizerBalance[] = [];
farmerBarn.balances.forEach((balance) => {
const pct = pctRepaid(balance);
const status = pct.eq(1) ? 'used' : 'active';
const humidity = balance.token.humidity;
const debt = balance.amount.multipliedBy(
humidity.div(100).plus(1)
);
const debt = balance.amount.multipliedBy(humidity.div(100).plus(1));
const sprouts = debt.multipliedBy(ONE_BN.minus(pct));
const rinsableSprouts = debt.multipliedBy(pct);

Expand All @@ -92,7 +94,7 @@ const TransferForm: FC<FormikProps<TransferFormValues>> = ({
humidity: humidity,
debt: debt,
sprouts: sprouts,
rinsableSprouts: rinsableSprouts
rinsableSprouts: rinsableSprouts,
};

output.push(fullBalance);
Expand All @@ -102,11 +104,16 @@ const TransferForm: FC<FormikProps<TransferFormValues>> = ({

const sproutAmounts = [];
for (let i = 0; i < fertilizers.length; i += 1) {
const pctRatio = BigNumber(values.amounts[i] || 0).div(fertilizers[i].amount);
const pctRatio = BigNumber(values.amounts[i] || 0).div(
fertilizers[i].amount
);
const sprouts = fertilizers[i].sprouts.multipliedBy(pctRatio);
sproutAmounts.push(sprouts);
};
const totalSprouts = sproutAmounts.reduce((total: BigNumber, current: BigNumber) => total.plus(current), BigNumber(0));
}
const totalSprouts = sproutAmounts.reduce(
(total: BigNumber, current: BigNumber) => total.plus(current),
BigNumber(0)
);

return (
<Form autoComplete="off">
Expand All @@ -118,44 +125,46 @@ const TransferForm: FC<FormikProps<TransferFormValues>> = ({
</FieldWrapper>
)}
{/* Txn info */}
{values.to && values.amounts.length > 0 && values.fertilizerIds.length > 0 && (
<>
<TxnSeparator />
<TokenOutput>
<TokenOutput.Row
amount={totalSprouts.negated()}
token={sdk.tokens.SPROUTS}
/>
</TokenOutput>
<Box>
<Accordion variant="outlined">
<StyledAccordionSummary title="Transaction Details" />
<AccordionDetails>
<TxnPreview
actions={
values.fertilizerIds !== undefined &&
values.fertilizerIds.length > 0 &&
values.to ?
[
{
type: ActionType.TRANSFER_FERTILIZER,
fertAmount: BigNumber(totalFertAmount),
sproutAmount: totalSprouts,
to: values.to,
},
{
type: ActionType.END_TOKEN,
token: SPROUTS,
},
]
{values.to &&
values.amounts.length > 0 &&
values.fertilizerIds.length > 0 && (
<>
<TxnSeparator />
<TokenOutput>
<TokenOutput.Row
amount={totalSprouts.negated()}
token={sdk.tokens.SPROUTS}
/>
</TokenOutput>
<Box>
<Accordion variant="outlined">
<StyledAccordionSummary title="Transaction Details" />
<AccordionDetails>
<TxnPreview
actions={
values.fertilizerIds !== undefined &&
values.fertilizerIds.length > 0 &&
values.to
? [
{
type: ActionType.TRANSFER_FERTILIZER,
fertAmount: BigNumber(totalFertAmount),
sproutAmount: totalSprouts,
to: values.to,
},
{
type: ActionType.END_TOKEN,
token: SPROUTS,
},
]
: []
}
/>
</AccordionDetails>
</Accordion>
</Box>
</>
)}
}
/>
</AccordionDetails>
</Accordion>
</Box>
</>
)}
<SmartSubmitButton
loading={isSubmitting}
disabled={!isReady || isSubmitting}
Expand Down Expand Up @@ -212,35 +221,36 @@ const Transfer: FC<{}> = () => {
if (values.fertilizerIds[i]) {
fertilizers.push(values.fertilizerIds[i]);
amounts.push(values.amounts[i]);
};
};
}
}

if (!account) throw new Error('Connect a wallet first.');
if (!to || !fertilizers || !amounts || fertilizers.length === 0) throw new Error('Missing data.');
if (!to || !fertilizers || !amounts || fertilizers.length === 0)
throw new Error('Missing data.');

txToast = new TransactionToast({
loading: `Transferring Fertilizers...`,
success: 'Fertilizer Transfer successful.',
});

let call
let call;
if (fertilizers.length === 1) {
call = fertilizer.safeTransferFrom(
account,
to,
fertilizers[0],
amounts[0],
"0x00"
'0x00'
);
} else {
call = fertilizer.safeBatchTransferFrom(
account,
to,
fertilizers,
amounts,
"0x00"
'0x00'
);
};
}

const txn = await call;
txToast.confirming(txn);
Expand Down
16 changes: 8 additions & 8 deletions projects/ui/src/components/Barn/FertilizerImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const FertilizerImage: FC<FertilizerImageProps> = ({
progress,
id,
noOpenseaLink,
verySmallIdStyling
verySmallIdStyling,
}) => {
const inner = (
<Stack
Expand Down Expand Up @@ -72,13 +72,13 @@ const FertilizerImage: FC<FertilizerImageProps> = ({
zIndex: verySmallIdStyling ? 3 : 1,
}}
>
<Typography
color={verySmallIdStyling ? "text.primary" : "gray"}
sx={{
fontSize: 11,
display: 'flex',
<Typography
color={verySmallIdStyling ? 'text.primary' : 'gray'}
sx={{
fontSize: 11,
display: 'flex',
justifyContent: verySmallIdStyling ? 'center' : 'start',
paddingX: verySmallIdStyling ? 0 : 1
paddingX: verySmallIdStyling ? 0 : 1,
}}
>
#{id.toString()}
Expand Down Expand Up @@ -115,7 +115,7 @@ const FertilizerImage: FC<FertilizerImageProps> = ({
{inner}
</Button>
);
};
}
return (
<Button
variant="outlined"
Expand Down
6 changes: 1 addition & 5 deletions projects/ui/src/components/Barn/FertilizerSelectDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const FertilizerSelectDialog: FC<FertilizerSelectDialogProps & DialogProps> = ({
// Dialog
open,
}) => {

const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));

Expand All @@ -33,10 +32,7 @@ const FertilizerSelectDialog: FC<FertilizerSelectDialogProps & DialogProps> = ({
}}
>
{fertilizers.length > 0 ? (
<FertilizerSelect
isMobile={isMobile}
fertilizers={fertilizers}
/>
<FertilizerSelect isMobile={isMobile} fertilizers={fertilizers} />
) : (
<EmptyState message="You have no Fertilizer." />
)}
Expand Down
4 changes: 2 additions & 2 deletions projects/ui/src/components/Barn/MyFertilizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import { displayFullBN, MaxBN, MinBN } from '~/util/Tokens';
import { SPROUTS, RINSABLE_SPROUTS } from '~/constants/tokens';
import { ONE_BN, ZERO_BN } from '~/constants';
import { AppState } from '~/state';
import TokenIcon from '../Common/TokenIcon';
import { FontSize } from '../App/muiTheme';
import { FertilizerBalance } from '~/state/farmer/barn';
import Row from '~/components/Common/Row';

import { FC } from '~/types';
import { FontSize } from '../App/muiTheme';
import TokenIcon from '../Common/TokenIcon';

enum TabState {
ACTIVE = 0,
Expand Down
2 changes: 1 addition & 1 deletion projects/ui/src/components/Common/Charts/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import { useTooltip, useTooltipInPortal } from '@visx/tooltip';
import { CurveFactory } from 'd3-shape';
import { Group } from '@visx/group';
import ParentSize from '@visx/responsive/lib/components/ParentSize';
import { BeanstalkPalette } from '~/components/App/muiTheme';
import ChartPropProvider, {
BaseDataPoint,
ProviderChartProps,
} from './ChartPropProvider';
import { BeanstalkPalette } from '~/components/App/muiTheme';

// ------------------------
// Line Chart
Expand Down
Loading

0 comments on commit 49101d6

Please sign in to comment.