From dfa8ab40bd434d662aa152a9e65ac08cdd165098 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Wed, 6 Sep 2023 15:31:55 +0900 Subject: [PATCH] refactor is_accumulator_update (#41) * refactor is_accumulator_update * bump * add return type --- pythclient/price_feeds.py | 18 +++++++++++++++++- setup.py | 2 +- tests/test_price_feeds.py | 8 ++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pythclient/price_feeds.py b/pythclient/price_feeds.py index 0fd4a74..498e80d 100644 --- a/pythclient/price_feeds.py +++ b/pythclient/price_feeds.py @@ -390,9 +390,25 @@ def parse_price_attestation(bytes_): } +def is_accumulator_update(vaa, encoding=DEFAULT_VAA_ENCODING) -> bool: + """ + This function checks if a given VAA is an accumulator update. + + Parameters: + vaa (str): The VAA to check. + encoding (str, optional): The encoding of the VAA. Defaults to hex. + + Returns: + bool: True if the VAA is an accumulator update, False otherwise. + """ + if encode_vaa_for_chain(vaa, encoding, buffer=True)[:4].hex() == ACCUMULATOR_MAGIC: + return True + return False + + # Referenced from https://github.com/pyth-network/pyth-crosschain/blob/110caed6be3be7885773d2f6070b143cc13fb0ee/price_service/server/src/rest.ts#L139 def vaa_to_price_infos(vaa, encoding=DEFAULT_VAA_ENCODING) -> List[PriceInfo]: - if encode_vaa_for_chain(vaa, encoding, buffer=True)[:4].hex() == ACCUMULATOR_MAGIC: + if is_accumulator_update(vaa, encoding): return extract_price_info_from_accumulator_update(vaa, encoding) parsed_vaa = parse_vaa(vaa, encoding) batch_attestation = parse_batch_price_attestation(parsed_vaa["payload"]) diff --git a/setup.py b/setup.py index 651aae5..7a33f0e 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='pythclient', - version='0.1.13', + version='0.1.14', packages=['pythclient'], author='Pyth Developers', author_email='contact@pyth.network', diff --git a/tests/test_price_feeds.py b/tests/test_price_feeds.py index 5b989f4..dd9be33 100644 --- a/tests/test_price_feeds.py +++ b/tests/test_price_feeds.py @@ -6,6 +6,7 @@ MerkleUpdate, compress_accumulator_update, encode_vaa_for_chain, + is_accumulator_update, parse_accumulator_update, serialize_accumulator_update, vaa_to_price_info, @@ -36,6 +37,13 @@ ACCUMULATOR_UPDATE_DATA_SOL_DIFFERENT_VAA = "UE5BVQEAAAADuAEAAAADDQBKFAm5gZxQZ89PDktXvCHe7kD0oYCU0YfEEeMsHJ4IQW8A5g0qe0wESIGKcEcA6OPic12DE1tdp6YARgviIpFGAQH+1Lm+9Kd/9rKx0ExQ8Attr2+clx4Inv5fCUSAmLSoeX85Ozs8YsACeenITjLX5luKSNWgm21Wu9AajvgUSKyDAAJ76AwXmW2pJ2ga2ZpiQERAr6v66VM5Mrvjp5UiewmSbVv/7kf3yM2ARr6WppbBvnwVdpnN/JdQFRomAl9mZS/SAANPCkWipzos+ioWE5Zzk+xO10kmR7sLPWvalGOjqPMjJld8dxfN66QVqYvVIKNdQgyfcKKLEnfw1PKY1lyjPuYjAQSK25cVWNC8akycRKhjawoP0baF9xtbLWnYSAFBgiJjnlHdlXBEHYrJW7mB1b6ALjglMkEXcrCZsRULpWgQtMbyAQs13nvw502cMVFYXB6L82R5NcYyH4Xq4wW1Ra+x86B+ZCRgyvuH+ALP3j1/iA8ObB43eki55h83JINUyPiySTsbAQw1r4gtKmnAmKtK4Ge8e+iJyExww5huK48nqlWQZBBMcxIu0aQZDZO37Ysaa6jbHy6YgvMn1iHzh8reH2vfS/lyAQ1HVCPSzrTtOjF3ZF8BJqGWedNhdnffyvRI0g9EWqQRvXoPwy7w/41jnfKUrxcrhGpRzcNutlbPNN9kPtmUEq5gAA4QwFhqNPqzWy/hY2qp3RGHjV8i9zx4I0aJyOi7NB+1ewmm3yOZk4nvBqizGBtxdFKITNag0OHh4IbO8zON/6vnAA/qBsZEltyzDy/OJnqsX8gyOUm53/pFieXa23lSODPi9mYbwSCTjb03X3bLVjKqrHi71F5+qlbCqer7wfXNzSrgARDIhOeEsgcWUmfKfes/ln9OV4i8XAKQzlDaC4BRUnAO0yIPJZATE3rOq/UcXdHfQUJiI/BBzh+KNZTi7QsvDP1mABFZLX4GkPz92o6eLQro8y1RFNiP1w1yDf0Td7PBtE86ujHwQY3QqCHp2Q9hiVox7go1wDWEG6H0rzOKQfKTC9oyABLd6Fb+2ThyhiFtreyc/0lLmSfi3ZClZiC7eVOvg32ybmNtZWunfYuxojVdM6e3ETeHoZ8Xzsetab3iBKzOInmEAWTxKYEAAAAAABrhAfrtrFhR4yubI7X5QRqMK6xKrj7U3XuBHdGnLqSqcQAAAAAAijllAUFVV1YAAAAAAAWKvXUAACcQ3HvATwZJhOMXUOpsxl3js3cJ2UoBAFUA7w2Lb9os66QdoV1AldHaOSoNL47Qxse8D0z6yMKAtW0AAAAAdZyz4QAAAAAAGzQd////+AAAAABk8SmBAAAAAGTxKYAAAAAAdajBegAAAAAAGDQdCX7POyhhbRE0fZPLbyONUAJ+6eS8CUz89ZmOaX9iCON0AzYTqrcpKZe189sKyhdvV8wZPkdHj8K7sNCWVP77y4K8q0eIHSbEbiAZv3GQxptsAP/yBqn4QmvmKA8tqiNp2ZwyMvTreSVyWhDVPo+acVFaI5WQHw45KwMm3/L7zcEKr8+pzQl8FKfnH1wy3Tjw2hj8sPfq1NHeVttsBEK5SIPSXMgExPoP1vmy7SymxxR8JIM3ug==" +def test_is_accumulator_update(): + # Test that a price service VAA is not an accumulator update + assert is_accumulator_update(HEX_VAA, "hex") == False + # Test that an accumulator update VAA is an accumulator update + assert is_accumulator_update(ACCUMULATOR_UPDATE_DATA_BTC_SAME_VAA, "base64") == True + + def test_valid_hex_vaa_to_price_info(): price_info = vaa_to_price_info(BTC_ID, HEX_VAA) assert price_info.seq_num == 558149954