Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('List incoming transfers by Safe - Transactions Controller (Unit)', ()
});
});

it('Should get a ERC20 incoming transfer mapped to the expected format', async () => {
it('Should get a trusted ERC20 incoming transfer mapped to the expected format', async () => {
const chain = chainBuilder().build();
const safe = safeBuilder().build();
const erc20Transfer = erc20TransferBuilder()
Expand All @@ -194,6 +194,7 @@ describe('List incoming transfers by Safe - Transactions Controller (Unit)', ()
const token = tokenBuilder()
.with('type', TokenType.Erc20)
.with('address', erc20Transfer.tokenAddress)
.with('trusted', true)
.build();
networkService.get.mockImplementation((url) => {
const getChainUrl = `${safeConfigUrl}/api/v1/chains/${chain.chainId}`;
Expand Down Expand Up @@ -263,6 +264,146 @@ describe('List incoming transfers by Safe - Transactions Controller (Unit)', ()
});
});

it('Should get a non-trusted ERC20 incoming transfer mapped to the expected format', async () => {
const chain = chainBuilder().build();
const safe = safeBuilder().build();
const erc20Transfer = erc20TransferBuilder()
.with('executionDate', new Date('2022-11-07T09:03:48Z'))
.with('to', safe.address)
.with('from', safe.address)
.with('transferId', 'e1015fc6905')
.with('value', faker.number.int({ min: 1 }).toString())
.build();
const trusted = false;
const token = tokenBuilder()
.with('type', TokenType.Erc20)
.with('address', erc20Transfer.tokenAddress)
.with('trusted', trusted)
.build();
networkService.get.mockImplementation((url) => {
const getChainUrl = `${safeConfigUrl}/api/v1/chains/${chain.chainId}`;
const getIncomingTransfersUrl = `${chain.transactionService}/api/v1/safes/${safe.address}/incoming-transfers/`;
const getSafeUrl = `${chain.transactionService}/api/v1/safes/${safe.address}`;
const getContractUrlPattern = `${chain.transactionService}/api/v1/contracts/`;
const getTokenUrlPattern = `${chain.transactionService}/api/v1/tokens/${erc20Transfer.tokenAddress}`;
if (url === getChainUrl) {
return Promise.resolve({ data: chain, status: 200 });
}
if (url === getIncomingTransfersUrl) {
return Promise.resolve({
data: pageBuilder()
.with('results', [erc20TransferToJson(erc20Transfer)])
.build(),
status: 200,
});
}
if (url === getSafeUrl) {
return Promise.resolve({ data: safe, status: 200 });
}
if (url.includes(getContractUrlPattern)) {
return Promise.reject({ detail: 'Not found' });
}
if (url === getTokenUrlPattern) {
return Promise.resolve({ data: token, status: 200 });
}
return Promise.reject(new Error(`Could not match ${url}`));
});

await request(app.getHttpServer())
.get(
`/v1/chains/${chain.chainId}/safes/${safe.address}/incoming-transfers/?trusted=${trusted}`,
)
.expect(200)
.then(({ body }) => {
expect(body).toMatchObject({
results: [
{
type: 'TRANSACTION',
transaction: {
id: `transfer_${safe.address}_e1015fc6905`,
executionInfo: null,
safeAppInfo: null,
timestamp: erc20Transfer.executionDate.getTime(),
txStatus: 'SUCCESS',
txInfo: {
type: 'Transfer',
sender: { value: safe.address },
recipient: { value: safe.address },
direction: 'OUTGOING',
transferInfo: {
type: 'ERC20',
tokenAddress: erc20Transfer.tokenAddress,
tokenName: token.name,
tokenSymbol: token.symbol,
logoUri: token.logoUri,
decimals: token.decimals,
value: erc20Transfer.value,
},
},
},
conflictType: 'None',
},
],
});
});
});

it('Should filter out non-trusted ERC20 incoming transfers by default', async () => {
const chain = chainBuilder().build();
const safe = safeBuilder().build();
const erc20Transfer = erc20TransferBuilder()
.with('executionDate', new Date('2022-11-07T09:03:48Z'))
.with('to', safe.address)
.with('from', safe.address)
.with('transferId', 'e1015fc6905')
.with('value', faker.number.int({ min: 1 }).toString())
.build();
const token = tokenBuilder()
.with('type', TokenType.Erc20)
.with('address', erc20Transfer.tokenAddress)
.with('trusted', false)
.build();
networkService.get.mockImplementation((url) => {
const getChainUrl = `${safeConfigUrl}/api/v1/chains/${chain.chainId}`;
const getIncomingTransfersUrl = `${chain.transactionService}/api/v1/safes/${safe.address}/incoming-transfers/`;
const getSafeUrl = `${chain.transactionService}/api/v1/safes/${safe.address}`;
const getContractUrlPattern = `${chain.transactionService}/api/v1/contracts/`;
const getTokenUrlPattern = `${chain.transactionService}/api/v1/tokens/${erc20Transfer.tokenAddress}`;
if (url === getChainUrl) {
return Promise.resolve({ data: chain, status: 200 });
}
if (url === getIncomingTransfersUrl) {
return Promise.resolve({
data: pageBuilder()
.with('results', [erc20TransferToJson(erc20Transfer)])
.build(),
status: 200,
});
}
if (url === getSafeUrl) {
return Promise.resolve({ data: safe, status: 200 });
}
if (url.includes(getContractUrlPattern)) {
return Promise.reject({ detail: 'Not found' });
}
if (url === getTokenUrlPattern) {
return Promise.resolve({ data: token, status: 200 });
}
return Promise.reject(new Error(`Could not match ${url}`));
});

await request(app.getHttpServer())
.get(
`/v1/chains/${chain.chainId}/safes/${safe.address}/incoming-transfers/`,
)
.expect(200)
.then(({ body }) => {
expect(body).toMatchObject({
results: [],
});
});
});

it('Should get a ERC721 incoming transfer mapped to the expected format', async () => {
const chain = chainBuilder().build();
const safe = safeBuilder().build();
Expand Down
49 changes: 9 additions & 40 deletions src/routes/transactions/mappers/transactions-history.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import { ModuleTransactionMapper } from '@/routes/transactions/mappers/module-tr
import { MultisigTransactionMapper } from '@/routes/transactions/mappers/multisig-transactions/multisig-transaction.mapper';
import { TransferMapper } from '@/routes/transactions/mappers/transfers/transfer.mapper';
import { IConfigurationService } from '@/config/configuration.service.interface';
import { isTransferTransactionInfo } from '@/routes/transactions/entities/transfer-transaction-info.entity';
import { isErc20Transfer } from '@/routes/transactions/entities/transfers/erc20-transfer.entity';
import { Transaction } from '@/routes/transactions/entities/transaction.entity';

class TransactionDomainGroup {
timestamp!: number;
Expand Down Expand Up @@ -189,44 +186,16 @@ export class TransactionsHistoryMapper {
): Promise<TransactionItem[]> {
const limitedTransfers = transfers.slice(0, this.maxNestedTransfers);

const nestedTransactions = await Promise.all(
limitedTransfers.map((transfer) =>
this.transferMapper.mapTransfer(chainId, transfer, safe),
),
);

return nestedTransactions
.filter((nestedTransaction): boolean => {
// We are interested in transfers that:
// - Have value and:
// - If onlyTrusted is true then it should be a trusted transfer
// - If onlyTrusted is false then any transfer is valid
return (
this.isTransferWithValue(nestedTransaction) &&
(!onlyTrusted || this.isTrustedTransfer(nestedTransaction))
);
})
.map((nestedTransaction) => new TransactionItem(nestedTransaction));
}

/**
* Returns true if it is an ERC20 transfer with value.
* Returns false otherwise.
*
* @private
*/
private isTransferWithValue(transaction: Transaction): boolean {
if (!isTransferTransactionInfo(transaction.txInfo)) return true;
if (!isErc20Transfer(transaction.txInfo.transferInfo)) return true;

return Number(transaction.txInfo.transferInfo.value) > 0;
}

private isTrustedTransfer(transaction: Transaction): boolean {
if (!isTransferTransactionInfo(transaction.txInfo)) return true;
if (!isErc20Transfer(transaction.txInfo.transferInfo)) return true;
const nestedTransactions = await this.transferMapper.mapTransfers({
chainId,
transfers: limitedTransfers,
safe,
onlyTrusted,
});

return !!transaction.txInfo.transferInfo.trusted;
return nestedTransactions.map(
(nestedTransaction) => new TransactionItem(nestedTransaction),
);
}

private mapGroupTransactions(
Expand Down
Loading