diff --git a/.changeset/new-suits-wink.md b/.changeset/new-suits-wink.md new file mode 100644 index 0000000000..1e38801504 --- /dev/null +++ b/.changeset/new-suits-wink.md @@ -0,0 +1,5 @@ +--- +"@human-protocol/sdk": patch +--- + +enhance transaction handling to support bigint for block filters diff --git a/packages/sdk/python/human-protocol-sdk/example.py b/packages/sdk/python/human-protocol-sdk/example.py index 43a874447c..d695ff64a7 100644 --- a/packages/sdk/python/human-protocol-sdk/example.py +++ b/packages/sdk/python/human-protocol-sdk/example.py @@ -10,86 +10,94 @@ StatisticsFilter, WorkerFilter, StakersFilter, + TransactionFilter, ) from human_protocol_sdk.statistics import ( - StatisticsClient, + StatisticsUtils, HMTHoldersParam, ) from human_protocol_sdk.operator import OperatorUtils, OperatorFilter -from human_protocol_sdk.agreement import agreement from human_protocol_sdk.staking.staking_utils import StakingUtils +from human_protocol_sdk.transaction import TransactionUtils from human_protocol_sdk.utils import SubgraphOptions -def get_escrow_statistics(statistics_client: StatisticsClient): - print(statistics_client.get_escrow_statistics()) +def get_escrow_statistics(): + print(StatisticsUtils.get_escrow_statistics(ChainId.POLYGON_AMOY)) print( - statistics_client.get_escrow_statistics( + StatisticsUtils.get_escrow_statistics( + ChainId.POLYGON_AMOY, StatisticsFilter( date_from=datetime.datetime(2023, 5, 8), date_to=datetime.datetime(2023, 6, 8), - ) + ), ) ) -def get_worker_statistics(statistics_client: StatisticsClient): - print(statistics_client.get_worker_statistics()) +def get_worker_statistics(): + print(StatisticsUtils.get_worker_statistics(ChainId.POLYGON_AMOY)) print( - statistics_client.get_worker_statistics( + StatisticsUtils.get_worker_statistics( + ChainId.POLYGON_AMOY, StatisticsFilter( date_from=datetime.datetime(2023, 5, 8), date_to=datetime.datetime(2023, 6, 8), - ) + ), ) ) -def get_payment_statistics(statistics_client: StatisticsClient): - print(statistics_client.get_payment_statistics()) +def get_payment_statistics(): + print(StatisticsUtils.get_payment_statistics(ChainId.POLYGON_AMOY)) print( - statistics_client.get_payment_statistics( + StatisticsUtils.get_payment_statistics( + ChainId.POLYGON_AMOY, StatisticsFilter( date_from=datetime.datetime(2023, 5, 8), date_to=datetime.datetime(2023, 6, 8), - ) + ), ) ) -def get_hmt_statistics(statistics_client: StatisticsClient): - print(statistics_client.get_hmt_statistics()) +def get_hmt_statistics(): + print(StatisticsUtils.get_hmt_statistics(ChainId.POLYGON_AMOY)) -def get_hmt_holders(statistics_client: StatisticsClient): +def get_hmt_holders(): print( - statistics_client.get_hmt_holders( + StatisticsUtils.get_hmt_holders( + ChainId.POLYGON_AMOY, HMTHoldersParam( order_direction="desc", - ) + ), ) ) print( - statistics_client.get_hmt_holders( + StatisticsUtils.get_hmt_holders( + ChainId.POLYGON_AMOY, HMTHoldersParam( order_direction="asc", - ) + ), ) ) print( - statistics_client.get_hmt_holders( - HMTHoldersParam(address="0xf183b3b34e70dd17859455389a3ab54d49d41e6f") + StatisticsUtils.get_hmt_holders( + ChainId.POLYGON_AMOY, + HMTHoldersParam(address="0xf183b3b34e70dd17859455389a3ab54d49d41e6f"), ) ) -def get_hmt_daily_data(statistics_client: StatisticsClient): +def get_hmt_daily_data(): print( - statistics_client.get_hmt_daily_data( + StatisticsUtils.get_hmt_daily_data( + ChainId.POLYGON_AMOY, StatisticsFilter( date_from=datetime.datetime(2024, 5, 8), date_to=datetime.datetime(2024, 6, 8), - ) + ), ) ) @@ -216,18 +224,6 @@ def get_workers(): print(len(workers)) -def agreement_example(): - annotations = [ - ["cat", "not", "cat"], - ["cat", "cat", "cat"], - ["not", "not", "not"], - ["cat", "nan", "not"], - ] - - agreement_report = agreement(annotations, measure="fleiss_kappa") - print(agreement_report) - - def get_stakers_example(): stakers = StakingUtils.get_stakers( StakersFilter( @@ -246,8 +242,18 @@ def get_stakers_example(): print("No stakers found.") +def get_transactions_example(): + first_page = TransactionUtils.get_transactions( + TransactionFilter( + chain_id=ChainId.POLYGON_AMOY, + from_address="0xF3D9a0ba9FA14273C515e519DFD0826Ff87d5164", + start_block=6282708, + ) + ) + print("Fetched", len(first_page), "transactions with start block") + + if __name__ == "__main__": - statistics_client = StatisticsClient() # Run single example while testing, and remove comments before commit @@ -255,15 +261,13 @@ def get_stakers_example(): get_operators() get_payouts() get_cancellation_refunds() + get_hmt_holders() + get_escrow_statistics() + get_hmt_statistics() + get_payment_statistics() + get_worker_statistics() + get_hmt_daily_data() - statistics_client = StatisticsClient(ChainId.POLYGON_AMOY) - get_hmt_holders(statistics_client) - get_escrow_statistics(statistics_client) - get_hmt_statistics(statistics_client) - get_payment_statistics(statistics_client) - get_worker_statistics(statistics_client) - get_hmt_daily_data(statistics_client) - - agreement_example() get_workers() get_stakers_example() + get_transactions_example() diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/cancel.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/cancel.py index 223b88aafb..6d5ddf36db 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/cancel.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/cancel.py @@ -70,4 +70,6 @@ def get_cancellation_refund_by_escrow_query(): }} }} {cancellation_refund_fragment} -""".format(cancellation_refund_fragment=cancellation_refund_fragment) +""".format( + cancellation_refund_fragment=cancellation_refund_fragment + ) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py index 5d1b01f6e6..38a0ca8919 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py @@ -97,7 +97,9 @@ def get_escrow_query(): }} }} {escrow_fragment} -""".format(escrow_fragment=escrow_fragment) +""".format( + escrow_fragment=escrow_fragment + ) def get_status_query( diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py index 09c06278ae..3f43bbff9e 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py @@ -14,4 +14,6 @@ }} }} {reward_added_event_fragment} -""".format(reward_added_event_fragment=reward_added_event_fragment) +""".format( + reward_added_event_fragment=reward_added_event_fragment +) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/transaction.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/transaction.py index f17cc61876..ef637424b2 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/transaction.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/transaction.py @@ -103,4 +103,6 @@ def get_transaction_query() -> str: }} }} {transaction_fragment} -""".format(transaction_fragment=transaction_fragment) +""".format( + transaction_fragment=transaction_fragment + ) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/worker.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/worker.py index ee21af1ef7..9788272612 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/worker.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/worker.py @@ -18,7 +18,9 @@ def get_worker_query() -> str: }} }} {worker_fragment} -""".format(worker_fragment=worker_fragment) +""".format( + worker_fragment=worker_fragment + ) def get_workers_query(filter: WorkerFilter) -> str: diff --git a/packages/sdk/typescript/human-protocol-sdk/example/transactions.ts b/packages/sdk/typescript/human-protocol-sdk/example/transactions.ts index 3e6c0aa6c7..62a374b259 100644 --- a/packages/sdk/typescript/human-protocol-sdk/example/transactions.ts +++ b/packages/sdk/typescript/human-protocol-sdk/example/transactions.ts @@ -15,6 +15,14 @@ export const getTransactions = async () => { }); console.log(response); + + const response2 = await TransactionUtils.getTransactions({ + chainId: ChainId.POLYGON_AMOY, + fromAddress: '0xF3D9a0ba9FA14273C515e519DFD0826Ff87d5164', + startBlock: response[0].block, + }); + + console.log(response2); }; (async () => { diff --git a/packages/sdk/typescript/human-protocol-sdk/src/interfaces.ts b/packages/sdk/typescript/human-protocol-sdk/src/interfaces.ts index 7e97635c8c..e9bca41378 100644 --- a/packages/sdk/typescript/human-protocol-sdk/src/interfaces.ts +++ b/packages/sdk/typescript/human-protocol-sdk/src/interfaces.ts @@ -149,8 +149,8 @@ export interface ITransaction { export interface ITransactionsFilter extends IPagination { chainId: ChainId; - startBlock?: number; - endBlock?: number; + startBlock?: number | bigint; + endBlock?: number | bigint; startDate?: Date; endDate?: Date; fromAddress?: string; diff --git a/packages/sdk/typescript/human-protocol-sdk/src/transaction.ts b/packages/sdk/typescript/human-protocol-sdk/src/transaction.ts index 6359018653..d2802feff7 100644 --- a/packages/sdk/typescript/human-protocol-sdk/src/transaction.ts +++ b/packages/sdk/typescript/human-protocol-sdk/src/transaction.ts @@ -129,8 +129,8 @@ export class TransactionUtils { * token?: string; // (Optional) The token address to filter transactions. * startDate?: Date; // (Optional) The start date to filter transactions (inclusive). * endDate?: Date; // (Optional) The end date to filter transactions (inclusive). - * startBlock?: number; // (Optional) The start block number to filter transactions (inclusive). - * endBlock?: number; // (Optional) The end block number to filter transactions (inclusive). + * startBlock?: bigint | number; // (Optional) The start block to filter transactions (inclusive). + * endBlock?: bigint | number; // (Optional) The end block to filter transactions (inclusive). * first?: number; // (Optional) Number of transactions per page. Default is 10. * skip?: number; // (Optional) Number of transactions to skip. Default is 0. * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is DESC. @@ -220,8 +220,8 @@ export class TransactionUtils { ? getUnixTimestamp(filter?.startDate) : undefined, endDate: filter.endDate ? getUnixTimestamp(filter.endDate) : undefined, - startBlock: filter.startBlock ? filter.startBlock : undefined, - endBlock: filter.endBlock ? filter.endBlock : undefined, + startBlock: filter.startBlock ? Number(filter.startBlock) : undefined, + endBlock: filter.endBlock ? Number(filter.endBlock) : undefined, method: filter.method ? filter.method : undefined, escrow: filter.escrow ? filter.escrow : undefined, token: filter.token ? filter.token : undefined, diff --git a/packages/sdk/typescript/human-protocol-sdk/test/transaction.test.ts b/packages/sdk/typescript/human-protocol-sdk/test/transaction.test.ts index ff3ce6016d..1e5649bcdc 100644 --- a/packages/sdk/typescript/human-protocol-sdk/test/transaction.test.ts +++ b/packages/sdk/typescript/human-protocol-sdk/test/transaction.test.ts @@ -367,6 +367,31 @@ describe('TransactionUtils', () => { expect(result).toEqual([expected]); }); + test('should serialize bigint block filters before querying', async () => { + const gqlFetchSpy = vi.spyOn(gqlFetch, 'default').mockResolvedValueOnce({ + transactions: [mockTransaction], + }); + const filter: ITransactionsFilter = { + chainId: ChainId.LOCALHOST, + startBlock: 12345n, + endBlock: 12350n, + first: 1, + }; + + const result = await TransactionUtils.getTransactions(filter); + + expect(gqlFetchSpy).toHaveBeenCalledWith( + NETWORKS[ChainId.LOCALHOST]?.subgraphUrl, + expect.anything(), + expect.objectContaining({ + startBlock: 12345, + endBlock: 12350, + }), + undefined + ); + expect(result).toHaveLength(1); + }); + test('should return an array of transactions filtered by escrow', async () => { const gqlFetchSpy = vi.spyOn(gqlFetch, 'default').mockResolvedValueOnce({ transactions: [mockTransaction],