Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu committed Jan 13, 2025
1 parent 0c8228d commit c389a28
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 220 deletions.
136 changes: 136 additions & 0 deletions src/abacus-ts/calculators/accountActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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'>>;
}
): ParentSubaccountData {
const { subaccountNumber, changes } = payload;
if (!changes.size) return parentSubaccountData;
const sizeBN = MustBigNumber(changes.size);

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

if (childSubaccount != null) {
// Modify childSubaccount
if (childSubaccount.assetPositions.USDC != null) {
const size = MustBigNumber(childSubaccount.assetPositions.USDC.size).plus(sizeBN).toString();

const updatedChildSubaccount = produce(childSubaccount, (draftChildSubaccount) => {
if (draftChildSubaccount.assetPositions.USDC) {
draftChildSubaccount.assetPositions.USDC.size = size;
}
});

childSubaccount = updatedChildSubaccount;
} else {
if (sizeBN.gt(0)) {
const updatedChildSubaccount = produce(childSubaccount, (draftChildSubaccount) => {
draftChildSubaccount.assetPositions.USDC = {
assetId: '0',
symbol: 'USDC',
size: sizeBN.toString(),
side: IndexerPositionSide.LONG,
subaccountNumber,
};
});

childSubaccount = updatedChildSubaccount;
}
}
} else {
// Upsert ChildSubaccountData into parentSubaccountData.childSubaccounts
childSubaccount = {
address: parentSubaccountData.address,
subaccountNumber,
openPerpetualPositions: {},
assetPositions: {
USDC: {
assetId: '0',
symbol: 'USDC',
size: sizeBN.toString(),
side: IndexerPositionSide.LONG,
subaccountNumber,
},
},
} satisfies ChildSubaccountData;
}

return produce(parentSubaccountData, (draftParentSubaccountData) => {
draftParentSubaccountData.childSubaccounts[subaccountNumber] = childSubaccount;
});
}

export function applyOperationsToSubaccount(
parentSubaccount: ParentSubaccountData,
batchedOperations: SubaccountBatchedOperations
): ParentSubaccountData {
let parentSubaccountData: ParentSubaccountData = parentSubaccount;

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

return parentSubaccountData;
}
136 changes: 0 additions & 136 deletions src/abacus-ts/calculators/subaccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mapValues, orderBy } from 'lodash';

import { NUM_PARENT_SUBACCOUNTS } from '@/constants/account';
import {
IndexerAssetPositionResponseObject,
IndexerPerpetualPositionResponseObject,
IndexerPerpetualPositionStatus,
IndexerPositionSide,
Expand All @@ -15,7 +14,6 @@ import { calc } from '@/lib/do';
import { BIG_NUMBERS, MaybeBigNumber, MustBigNumber, ToBigNumber } from '@/lib/numbers';
import { isPresent } from '@/lib/typeUtils';

import { SubaccountBatchedOperations, SubaccountOperations } from '../types/operationTypes';
import { ChildSubaccountData, MarketsData, ParentSubaccountData } from '../types/rawTypes';
import {
GroupedSubaccountSummary,
Expand Down Expand Up @@ -290,137 +288,3 @@ function calculatePositionDerivedExtra(
updatedUnrealizedPnlPercent,
};
}

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'>>;
}
): void {
const { subaccountNumber, changes } = payload;

if (!changes.size) return;
const sizeBN = MustBigNumber(changes.size);

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

if (childSubaccount != null) {
// Modify childSubaccount
if (childSubaccount.assetPositions.USDC != null) {
const size = MustBigNumber(childSubaccount.assetPositions.USDC.size).plus(sizeBN).toString();
const assetPositions = {
...childSubaccount.assetPositions,
USDC: {
...childSubaccount.assetPositions.USDC,
size,
},
};

childSubaccount = {
...childSubaccount,
assetPositions,
};
} else {
if (sizeBN.gt(0)) {
childSubaccount.assetPositions.USDC = {
assetId: '0',
symbol: 'USDC',
size: sizeBN.toString(),
side: IndexerPositionSide.LONG,
subaccountNumber,
};
}
}
} else {
// Upsert ChildSubaccountData into parentSubaccountData.childSubaccounts
childSubaccount = {
address: parentSubaccountData.address,
subaccountNumber,
openPerpetualPositions: {},
assetPositions: {
USDC: {
assetId: '0',
symbol: 'USDC',
size: sizeBN.toString(),
side: IndexerPositionSide.LONG,
subaccountNumber,
},
},
};
}

parentSubaccountData.childSubaccounts = {
...parentSubaccountData.childSubaccounts,
[subaccountNumber]: childSubaccount,
};
}

export function applyOperationsToSubaccount(
parentSubaccount: ParentSubaccountData,
batchedOperations: SubaccountBatchedOperations
): ParentSubaccountData {
const parentSubaccountData: ParentSubaccountData = { ...parentSubaccount };

batchedOperations.operations.forEach((op) => {
const { payload, operation } = op;

switch (operation) {
case 'AddPerpetualPosition': {
// TODO: Implement addPerpetualPosition
break;
}
case 'ModifyPerpetualPosition': {
// TODO: Implement modifyPerpetualPosition
break;
}
case 'ModifyUsdcAssetPosition': {
modifyUsdcAssetPosition(parentSubaccountData, payload);
break;
}
default:
throw new Error(`Error processing invalid operation type`);
}
});

return parentSubaccountData;
}
6 changes: 1 addition & 5 deletions src/abacus-ts/ontology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
selectParentSubaccountSummaryLoading,
} from './selectors/account';
import {
createSelectParentSubaccountPositionsDeposit,
createSelectParentSubaccountPositionsWithdrawal,
createSelectParentSubaccountSummaryDeposit,
createSelectParentSubaccountSummaryWithdrawal,
} from './selectors/accountActions';
Expand Down Expand Up @@ -103,14 +101,12 @@ export const BonsaiHelpers = {
fills: getCurrentMarketAccountFills,
},
},
account: {
forms: {
deposit: {
parentSubaccountSummary: createSelectParentSubaccountSummaryDeposit,
parentSubaccountPositions: createSelectParentSubaccountPositionsDeposit,
},
withdraw: {
parentSubaccountSummary: createSelectParentSubaccountSummaryWithdrawal,
parentSubaccountPositions: createSelectParentSubaccountPositionsWithdrawal,
},
},
} as const satisfies NestedSelectors;
Expand Down
Loading

0 comments on commit c389a28

Please sign in to comment.