Skip to content

Commit

Permalink
wrap entire fn in produce
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu committed Jan 14, 2025
1 parent 61d9204 commit a8f621b
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions src/abacus-ts/calculators/accountActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,57 +57,49 @@ function modifyUsdcAssetPosition(
): ParentSubaccountData {
const { subaccountNumber, changes } = payload;
if (!changes.size) return parentSubaccountData;
const sizeBN = MustBigNumber(changes.size);

let childSubaccount: ChildSubaccountData | undefined =
parentSubaccountData.childSubaccounts[subaccountNumber];
return produce(parentSubaccountData, (draftParentSubaccountData) => {
const sizeBN = MustBigNumber(changes.size);

if (childSubaccount != null) {
// Modify childSubaccount
if (childSubaccount.assetPositions.USDC != null) {
const size = MustBigNumber(childSubaccount.assetPositions.USDC.size).plus(sizeBN).toString();
let childSubaccount: ChildSubaccountData | undefined =
draftParentSubaccountData.childSubaccounts[subaccountNumber];

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

childSubaccount = updatedChildSubaccount;
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;
}
} else {
if (sizeBN.gt(0)) {
const updatedChildSubaccount = produce(childSubaccount, (draftChildSubaccount) => {
draftChildSubaccount.assetPositions.USDC = {
// Upsert ChildSubaccountData into parentSubaccountData.childSubaccounts
childSubaccount = {
address: parentSubaccountData.address,
subaccountNumber,
openPerpetualPositions: {},
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;
}
} satisfies ChildSubaccountData;
}

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

0 comments on commit a8f621b

Please sign in to comment.