Skip to content

Commit

Permalink
fix transfers parent SA API for external addresses in transfers
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb committed Dec 6, 2024
1 parent 8c7d85f commit 1431ce3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
15 changes: 15 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@ export const defaultTransfer3: TransferCreateObject = {
assetId: defaultAsset2.id,
};

export const defaultTransferWithAlternateAddress: TransferCreateObject = {
...defaultTransfer,
senderSubaccountId: defaultSubaccountIdWithAlternateAddress,
recipientSubaccountId: defaultSubaccountId,
};

export const defaultTransferId: string = TransferTable.uuid(
defaultTransfer.eventId,
defaultTransfer.assetId,
Expand All @@ -654,6 +660,15 @@ export const defaultTransferId: string = TransferTable.uuid(
defaultTransfer.recipientWalletAddress,
);

export const defaultTransferWithAlternateAddressId: string = TransferTable.uuid(
defaultTransferWithAlternateAddress.eventId,
defaultTransferWithAlternateAddress.assetId,
defaultTransferWithAlternateAddress.senderSubaccountId,
defaultTransferWithAlternateAddress.recipientSubaccountId,
defaultTransferWithAlternateAddress.senderWalletAddress,
defaultTransferWithAlternateAddress.recipientWalletAddress,
);

export const defaultWithdrawal: TransferCreateObject = {
senderSubaccountId: defaultSubaccountId,
recipientWalletAddress: defaultWalletAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import request from 'supertest';
import { getQueryString, sendRequest } from '../../../helpers/helpers';
import {
createdDateTime, createdHeight,
defaultAsset,
defaultAsset, defaultSubaccount2Num0,
defaultTendermintEventId4,
defaultWalletAddress,
isolatedSubaccountId,
defaultWalletAddress, isolatedSubaccountId,
} from '@dydxprotocol-indexer/postgres/build/__tests__/helpers/constants';
import Big from 'big.js';

Expand Down Expand Up @@ -460,11 +459,15 @@ describe('transfers-controller#V4', () => {
createdAtHeight: testConstants.createdHeight,
};
await WalletTable.create(defaultWallet);
await Promise.all([
SubaccountTable.create(defaultSubaccount2Num0),
]);
await Promise.all([
TransferTable.create(testConstants.defaultTransfer),
TransferTable.create(transfer2),
TransferTable.create(testConstants.defaultWithdrawal),
TransferTable.create(testConstants.defaultDeposit),
TransferTable.create(testConstants.defaultTransferWithAlternateAddress),
]);

const response: request.Response = await sendRequest({
Expand Down Expand Up @@ -550,6 +553,24 @@ describe('transfers-controller#V4', () => {
transactionHash: testConstants.defaultWithdrawal.transactionHash,
};

const expectedTransferWithAlternateAddressResponse: ParentSubaccountTransferResponseObject = {
id: testConstants.defaultTransferWithAlternateAddressId,
sender: {
address: testConstants.defaultAddress2,
parentSubaccountNumber: testConstants.defaultSubaccount2Num0.subaccountNumber,
},
recipient: {
address: testConstants.defaultAddress,
parentSubaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
},
size: testConstants.defaultTransferWithAlternateAddress.size,
createdAt: testConstants.defaultTransferWithAlternateAddress.createdAt,
createdAtHeight: testConstants.defaultTransferWithAlternateAddress.createdAtHeight,
symbol: testConstants.defaultAsset.symbol,
type: TransferType.TRANSFER_IN,
transactionHash: testConstants.defaultTransferWithAlternateAddress.transactionHash,
};

expect(response.body.transfers).toEqual(
expect.arrayContaining([
expect.objectContaining({
Expand All @@ -564,6 +585,9 @@ describe('transfers-controller#V4', () => {
expect.objectContaining({
...expectedDepositResponse,
}),
expect.objectContaining({
...expectedTransferWithAlternateAddressResponse,
}),
]),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,16 @@ class TransfersController extends Controller {
transfer,
idToAsset,
idToSubaccount,
address,
parentSubaccountNumber);
});

// Filter out transfers where the sender and recipient parent subaccount numbers are the same
const transfersFiltered:
ParentSubaccountTransferResponseObject[] = transfersWithParentSubaccount.filter(
(transfer) => {
return transfer.sender.parentSubaccountNumber !== transfer.recipient.parentSubaccountNumber;
return transfer.sender.address !== transfer.recipient.address ||
transfer.sender.parentSubaccountNumber !== transfer.recipient.parentSubaccountNumber;
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export function transferToParentSubaccountResponseObject(
transfer: TransferFromDatabase,
assetMap: AssetById,
subaccountMap: SubaccountById,
address: string,
parentSubaccountNumber: number,
): ParentSubaccountTransferResponseObject {

Expand All @@ -251,6 +252,9 @@ export function transferToParentSubaccountResponseObject(
: parentSubaccountHelpers.getParentSubaccountNum(
subaccountMap[transfer.senderSubaccountId!].subaccountNumber,
);
const senderAddress = transfer.senderWalletAddress
? transfer.senderWalletAddress
: subaccountMap[transfer.senderSubaccountId!].address;

const recipientParentSubaccountNum = transfer.recipientWalletAddress
? undefined
Expand All @@ -260,13 +264,13 @@ export function transferToParentSubaccountResponseObject(

// Determine transfer type based on parent subaccount number.
let transferType: TransferType = TransferType.TRANSFER_IN;
if (senderParentSubaccountNum === parentSubaccountNumber) {
if (senderAddress === address && senderParentSubaccountNum === parentSubaccountNumber) {
if (transfer.recipientSubaccountId) {
transferType = TransferType.TRANSFER_OUT;
} else {
transferType = TransferType.WITHDRAWAL;
}
} else if (recipientParentSubaccountNum === parentSubaccountNumber) {
} else { // if (recipientParentSubaccountNum === parentSubaccountNumber) {
if (transfer.senderSubaccountId) {
transferType = TransferType.TRANSFER_IN;
} else {
Expand Down

0 comments on commit 1431ce3

Please sign in to comment.