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
5 changes: 5 additions & 0 deletions .changeset/new-suits-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@human-protocol/sdk": patch
---

enhance transaction handling to support bigint for block filters
100 changes: 52 additions & 48 deletions packages/sdk/python/human-protocol-sdk/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
),
)
)

Expand Down Expand Up @@ -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(
Expand All @@ -246,24 +242,32 @@ 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

get_escrows()
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()
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def get_escrow_query():
}}
}}
{escrow_fragment}
""".format(escrow_fragment=escrow_fragment)
""".format(
escrow_fragment=escrow_fragment
)


def get_status_query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ def get_transaction_query() -> str:
}}
}}
{transaction_fragment}
""".format(transaction_fragment=transaction_fragment)
""".format(
transaction_fragment=transaction_fragment
)
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/typescript/human-protocol-sdk/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/typescript/human-protocol-sdk/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Loading