Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor is_accumulator_update #41

Merged
merged 3 commits into from
Sep 6, 2023
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
18 changes: 17 additions & 1 deletion pythclient/price_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='pythclient',
version='0.1.13',
version='0.1.14',
packages=['pythclient'],
author='Pyth Developers',
author_email='[email protected]',
Expand Down
8 changes: 8 additions & 0 deletions tests/test_price_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down