Skip to content

Commit

Permalink
[Dashboard] Fixed get transactions (#2511)
Browse files Browse the repository at this point in the history
* Updated get transactions

* Updated query

* Added negative symbol for outgoing transactions

* Improved get_transactions_query for python SDK
  • Loading branch information
eugenvoronov authored Sep 19, 2024
1 parent 2bcba19 commit 525eb5b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ export class DetailsService {
const transactions = await TransactionUtils.getTransactions({
chainId,
fromAddress: address,
toAddress: address,
first,
skip,
});
const result = transactions.map((transaction) => {
const transcationPaginationObject: TransactionPaginationDto =
plainToInstance(TransactionPaginationDto, transaction, {
excludeExtraneousValues: true,
});
plainToInstance(
TransactionPaginationDto,
{ ...transaction, currentAddress: address },
{ excludeExtraneousValues: true },
);
return transcationPaginationObject;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export class TransactionPaginationDto {
public to: string;

@ApiProperty({ example: '0.123' })
@Transform(({ value }) => value.toString())
@Transform(({ value, obj }) => {
return obj.currentAddress.toLowerCase() === obj.from.toLowerCase()
? `-${value.toString()}`
: value.toString();
})
@IsString()
@Expose()
public value: string;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/python/human-protocol-sdk/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from human_protocol_sdk.operator import OperatorUtils, LeaderFilter
from human_protocol_sdk.agreement import agreement
from human_protocol_sdk.transaction import TransactionUtils, TransactionFilter


def get_escrow_statistics(statistics_client: StatisticsClient):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@


def get_transactions_query(filter: TransactionFilter) -> str:
return """
from_address = filter.from_address
to_address = filter.to_address

if from_address == to_address:
address_condition = f"""
{f'{{ from: $fromAddress }}' if from_address else ''}
{f'{{ to: $toAddress }}' if to_address else ''}
"""
else:
address_condition = f"""
{f'from: $fromAddress' if from_address else ''}
{f'to: $toAddress' if to_address else ''}
"""

where_clause = f"""
where: {{
{"or: [" + address_condition + "]," if from_address == to_address else address_condition}
{f'timestamp_gte: $startDate,' if filter.start_date else ''}
{f'timestamp_lte: $endDate,' if filter.end_date else ''}
{f'block_gte: $startBlock,' if filter.start_block else ''}
{f'block_lte: $endBlock,' if filter.end_block else ''}
}}
"""

return f"""
query GetTransactions(
$fromAddress: String
$toAddress: String
Expand All @@ -27,32 +51,17 @@ def get_transactions_query(filter: TransactionFilter) -> str:
$skip: Int
) {{
transactions(
where: {{
{from_address_clause}
{to_address_clause}
{start_date_clause}
{end_date_clause}
{start_block_clause}
{end_block_clause}
}}
orderBy: timestamp
orderDirection: $orderDirection
first: $first
{where_clause}
orderBy: timestamp,
orderDirection: $orderDirection,
first: $first,
skip: $skip
) {{
...TransactionFields
}}
}}
{transaction_fragment}
""".format(
transaction_fragment=transaction_fragment,
from_address_clause="from: $fromAddress" if filter.from_address else "",
to_address_clause="to: $toAddress" if filter.to_address else "",
start_date_clause="timestamp_gte: $startDate" if filter.start_date else "",
end_date_clause="timestamp_lte: $endDate" if filter.end_date else "",
start_block_clause="block_gte: $startBlock" if filter.start_block else "",
end_block_clause="block_lte: $endBlock" if filter.end_block else "",
)
"""


def get_transaction_query() -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ export const GET_TRANSACTIONS_QUERY = (filter: ITransactionsFilter) => {
const { startDate, endDate, startBlock, endBlock, fromAddress, toAddress } =
filter;

const addressCondition =
fromAddress === toAddress
? `
${fromAddress ? `{ from: $fromAddress }` : ''}
${toAddress ? `{ to: $toAddress }` : ''}
`
: `
${fromAddress ? `from: $fromAddress` : ''}
${toAddress ? `to: $toAddress` : ''}
`;

const WHERE_CLAUSE = `
where: {
${fromAddress ? `from: $fromAddress,` : ''}
${toAddress ? `to: $toAddress,` : ''}
${startDate ? `timestamp_gte: $startDate,` : ''}
${endDate ? `timestamp_lte: $endDate,` : ''}
${startBlock ? `block_gte: $startBlock,` : ''}
${endBlock ? `block_lte: $endBlock,` : ''}
}
`;
where: {
${fromAddress === toAddress ? `or: [ ${addressCondition} ],` : addressCondition}
${startDate ? `timestamp_gte: $startDate,` : ''}
${endDate ? `timestamp_lte: $endDate,` : ''}
${startBlock ? `block_gte: $startBlock,` : ''}
${endBlock ? `block_lte: $endBlock,` : ''}
}
`;

return gql`
query getTransactions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class TransactionUtils {
if (!networkData) {
throw ErrorUnsupportedChainID;
}

const { transactions } = await gqlFetch<{
transactions: ITransaction[];
}>(getSubgraphUrl(networkData), GET_TRANSACTIONS_QUERY(filter), {
Expand Down

0 comments on commit 525eb5b

Please sign in to comment.