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

fix transfers parent SA API for external addresses in transfers (backport #2630) #2631

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading