Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bonsai-core): modifyUsdcAssetPosition #1423

Merged
merged 22 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions src/abacus-ts/calculators/accountActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { produce } from 'immer';

import {
IndexerAssetPositionResponseObject,
IndexerPositionSide,
} from '@/types/indexer/indexerApiGen';

import { MustBigNumber } from '@/lib/numbers';

import { SubaccountBatchedOperations, SubaccountOperations } from '../types/operationTypes';
import { ChildSubaccountData, ParentSubaccountData } from '../types/rawTypes';

export function createUsdcDepositOperations({
subaccountNumber,
depositAmount,
}: {
subaccountNumber: number;
depositAmount: string;
}): SubaccountBatchedOperations {
return {
operations: [
SubaccountOperations.ModifyUsdcAssetPosition({
subaccountNumber,
changes: {
size: depositAmount,
},
}),
],
};
}

export function createUsdcWithdrawalOperations({
subaccountNumber,
withdrawAmount,
}: {
subaccountNumber: number;
withdrawAmount: string;
}): SubaccountBatchedOperations {
return {
operations: [
SubaccountOperations.ModifyUsdcAssetPosition({
subaccountNumber,
changes: {
size: MustBigNumber(withdrawAmount).negated().toString(),
},
}),
],
};
}

function modifyUsdcAssetPosition(
parentSubaccountData: ParentSubaccountData,
payload: {
subaccountNumber: number;
changes: Partial<Pick<IndexerAssetPositionResponseObject, 'size'>>;
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
}
): ParentSubaccountData {
const { subaccountNumber, changes } = payload;
if (!changes.size) return parentSubaccountData;

return produce(parentSubaccountData, (draftParentSubaccountData) => {
const sizeBN = MustBigNumber(changes.size);

let childSubaccount: ChildSubaccountData | undefined =
draftParentSubaccountData.childSubaccounts[subaccountNumber];

if (childSubaccount != null) {
// Modify childSubaccount
if (childSubaccount.assetPositions.USDC != null) {
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
const size = MustBigNumber(childSubaccount.assetPositions.USDC.size)
.plus(sizeBN)
.toString();

childSubaccount.assetPositions.USDC.size = size;
} else if (sizeBN.gt(0)) {
// Upsert USDC Asset Position
childSubaccount.assetPositions.USDC = {
assetId: '0',
symbol: 'USDC',
size: sizeBN.toString(),
side: IndexerPositionSide.LONG,
subaccountNumber,
} satisfies IndexerAssetPositionResponseObject;
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
// Upsert ChildSubaccountData into parentSubaccountData.childSubaccounts
childSubaccount = {
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
address: parentSubaccountData.address,
subaccountNumber,
openPerpetualPositions: {},
assetPositions: {
USDC: {
assetId: '0',
symbol: 'USDC',
size: sizeBN.toString(),
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
side: IndexerPositionSide.LONG,
subaccountNumber,
},
},
} satisfies ChildSubaccountData;
}

draftParentSubaccountData.childSubaccounts[subaccountNumber] = childSubaccount;
});
}

export function applyOperationsToSubaccount(
parentSubaccount: ParentSubaccountData,
batchedOperations: SubaccountBatchedOperations
): ParentSubaccountData {
let parentSubaccountData: ParentSubaccountData = parentSubaccount;
jaredvu marked this conversation as resolved.
Show resolved Hide resolved

batchedOperations.operations.forEach((op) => {
SubaccountOperations.match(op, {
AddPerpetualPosition: () => {
// TODO: Implement addPerpetualPosition
},
ModifyPerpetualPosition: () => {
// TODO: Implement modifyPerpetualPosition
},
ModifyUsdcAssetPosition: (args) => {
parentSubaccountData = modifyUsdcAssetPosition(parentSubaccountData, args);
},
});
});

return parentSubaccountData;
}
12 changes: 12 additions & 0 deletions src/abacus-ts/ontology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
selectParentSubaccountSummary,
selectParentSubaccountSummaryLoading,
} from './selectors/account';
import {
createSelectParentSubaccountSummaryDeposit,
createSelectParentSubaccountSummaryWithdrawal,
} from './selectors/accountActions';
import {
selectAllAssetsInfo,
selectAllAssetsInfoLoading,
Expand Down Expand Up @@ -97,6 +101,14 @@ export const BonsaiHelpers = {
fills: getCurrentMarketAccountFills,
},
},
forms: {
deposit: {
parentSubaccountSummary: createSelectParentSubaccountSummaryDeposit,
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
},
withdraw: {
parentSubaccountSummary: createSelectParentSubaccountSummaryWithdrawal,
},
},
} as const satisfies NestedSelectors;

export const BonsaiHooks = {
Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/selectors/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const selectRelevantMarketsList = createAppSelector(
}
);

const selectRelevantMarketsData = createAppSelector(
export const selectRelevantMarketsData = createAppSelector(
[selectRelevantMarketsList, selectRawMarketsData],
(marketIds, markets) => {
if (markets == null || marketIds == null) {
Expand Down
60 changes: 60 additions & 0 deletions src/abacus-ts/selectors/accountActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createAppSelector } from '@/state/appTypes';

import {
applyOperationsToSubaccount,
createUsdcDepositOperations,
createUsdcWithdrawalOperations,
} from '../calculators/accountActions';
import { calculateParentSubaccountSummary } from '../calculators/subaccount';
import { selectRelevantMarketsData } from './account';
import { selectRawParentSubaccountData } from './base';

export const createSelectParentSubaccountSummaryDeposit = () =>
createAppSelector(
[
selectRawParentSubaccountData,
selectRelevantMarketsData,
(
_s,
input: {
subaccountNumber: number;
depositAmount: string;
}
) => input,
],
(parentSubaccount, markets, depositInputs) => {
if (parentSubaccount == null || markets == null) {
return undefined;
}

const operations = createUsdcDepositOperations(depositInputs);
const modifiedParentSubaccount = applyOperationsToSubaccount(parentSubaccount, operations);
const result = calculateParentSubaccountSummary(modifiedParentSubaccount, markets);
return result;
}
);

export const createSelectParentSubaccountSummaryWithdrawal = () =>
tyleroooo marked this conversation as resolved.
Show resolved Hide resolved
createAppSelector(
[
selectRawParentSubaccountData,
selectRelevantMarketsData,
(
_s,
input: {
subaccountNumber: number;
withdrawAmount: string;
}
) => input,
],
(parentSubaccount, markets, withdrawalInputs) => {
if (parentSubaccount == null || markets == null) {
return undefined;
}

const operations = createUsdcWithdrawalOperations(withdrawalInputs);
const modifiedParentSubaccount = applyOperationsToSubaccount(parentSubaccount, operations);
const result = calculateParentSubaccountSummary(modifiedParentSubaccount, markets);
return result;
}
);
8 changes: 4 additions & 4 deletions src/abacus-ts/types/operationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import {
export const SubaccountOperations = unionize(
{
AddPerpetualPosition: ofType<{
subaccountNumber: string;
subaccountNumber: number;
market: string;
position: Omit<IndexerPerpetualPositionResponseObject, 'market' | 'subaccountNumber'>;
}>(),
ModifyPerpetualPosition: ofType<{
subaccountNumber: string;
subaccountNumber: number;
market: string;
changes: Partial<Omit<IndexerPerpetualPositionResponseObject, 'market' | 'subaccountNumber'>>;
}>(),
ModifyUsdcAssetPosition: ofType<{
subaccountNumber: string;
changes: Partial<Pick<IndexerAssetPositionResponseObject, 'size' | 'side'>>;
subaccountNumber: number;
changes: Partial<Pick<IndexerAssetPositionResponseObject, 'size'>>;
}>(),
},
{ tag: 'operation' as const, value: 'payload' as const }
Expand Down
19 changes: 19 additions & 0 deletions src/views/dialogs/DepositDialog2/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';

import { BonsaiHelpers } from '@/abacus-ts/ontology';
import { BalanceRequest, RouteRequest, SkipClient } from '@skip-go/client';
import { useQuery } from '@tanstack/react-query';
import { Chain, parseUnits } from 'viem';
Expand All @@ -14,6 +15,7 @@ import { WalletNetworkType } from '@/constants/wallets';

import { useSkipClient } from '@/hooks/transfers/skipClient';
import { useAccounts } from '@/hooks/useAccounts';
import { useParameterizedSelector } from '@/hooks/useParameterizedSelector';

import { SourceAccount } from '@/state/wallet';

Expand Down Expand Up @@ -147,3 +149,20 @@ export function useDepositRoutes(token: TokenForTransfer, amount: string) {
placeholderData: (prev) => prev,
});
}

export function useDepositDeltas({ depositAmount }: { depositAmount: string }) {
const depositInput = useMemo(
() => ({
subaccountNumber: 0,
depositAmount,
}),
[depositAmount]
);

const modifiedParentSubaccount = useParameterizedSelector(
BonsaiHelpers.forms.deposit.parentSubaccountSummary,
depositInput
);

return modifiedParentSubaccount;
}
21 changes: 21 additions & 0 deletions src/views/dialogs/WithdrawDialog2/queries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { useMemo } from 'react';

import { BonsaiHelpers } from '@/abacus-ts/ontology';
import { RouteRequest, SkipClient } from '@skip-go/client';
import { useQuery } from '@tanstack/react-query';
import { parseUnits } from 'viem';
Expand All @@ -7,6 +10,7 @@ import { timeUnits } from '@/constants/time';
import { DYDX_CHAIN_USDC_DENOM, TokenForTransfer } from '@/constants/tokens';

import { useSkipClient } from '@/hooks/transfers/skipClient';
import { useParameterizedSelector } from '@/hooks/useParameterizedSelector';

async function getSkipWithdrawalRoutes(
skipClient: SkipClient,
Expand Down Expand Up @@ -53,3 +57,20 @@ export function useWithdrawalRoutes({
retry: false,
});
}

export function useWithdrawalDeltas({ withdrawAmount }: { withdrawAmount: string }) {
const withdrawInput = useMemo(
() => ({
subaccountNumber: 0,
withdrawAmount,
}),
[withdrawAmount]
);

const modifiedParentSubaccount = useParameterizedSelector(
BonsaiHelpers.forms.withdraw.parentSubaccountSummary,
withdrawInput
);

return modifiedParentSubaccount;
}
Loading