diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index 1de37ba7ac..aeaedeae3c 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -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, @@ -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, diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/transfers-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/transfers-controller.test.ts index 89646c3d1e..432e050cc9 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/transfers-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/transfers-controller.test.ts @@ -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'; @@ -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({ @@ -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({ @@ -564,6 +585,9 @@ describe('transfers-controller#V4', () => { expect.objectContaining({ ...expectedDepositResponse, }), + expect.objectContaining({ + ...expectedTransferWithAlternateAddressResponse, + }), ]), ); }); diff --git a/indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts b/indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts index 258f2e76ff..d332617fcc 100644 --- a/indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts @@ -218,6 +218,7 @@ class TransfersController extends Controller { transfer, idToAsset, idToSubaccount, + address, parentSubaccountNumber); }); @@ -225,7 +226,8 @@ class TransfersController extends Controller { 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 { diff --git a/indexer/services/comlink/src/request-helpers/request-transformer.ts b/indexer/services/comlink/src/request-helpers/request-transformer.ts index 20718c971b..91c557c030 100644 --- a/indexer/services/comlink/src/request-helpers/request-transformer.ts +++ b/indexer/services/comlink/src/request-helpers/request-transformer.ts @@ -243,6 +243,7 @@ export function transferToParentSubaccountResponseObject( transfer: TransferFromDatabase, assetMap: AssetById, subaccountMap: SubaccountById, + address: string, parentSubaccountNumber: number, ): ParentSubaccountTransferResponseObject { @@ -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 @@ -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 {