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

anchorpy parser problem #138

Open
soundsonacid opened this issue Mar 13, 2024 · 0 comments
Open

anchorpy parser problem #138

soundsonacid opened this issue Mar 13, 2024 · 0 comments

Comments

@soundsonacid
Copy link

soundsonacid commented Mar 13, 2024

context: processing transaction logs into events for archival purposes with anchorpy

code:

import asyncio
import base64
import binascii

from solana.rpc.async_api import AsyncClient # type: ignore
from solders.signature import Signature # type: ignore

from anchorpy import Wallet, Program, Provider # type: ignore

DRIFT_PROGRAM_ID: str = "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
PROGRAM_LOG: str = "Program log: "
PROGRAM_DATA: str = "Program data: "
PROGRAM_LOG_START_INDEX: int = len(PROGRAM_LOG)
PROGRAM_DATA_START_INDEX: int = len(PROGRAM_DATA)

async def parse_logs(program: Program, logs: list[str]) -> None:
    for log in logs:
        if log.startswith("Log truncated"):
            break

        handle_log(log, program)

def handle_log(
    log: str, program: Program
):
    if log.startswith(PROGRAM_LOG) or log.startswith(PROGRAM_DATA):
        log_str = (
            log[PROGRAM_LOG_START_INDEX:]
            if log.startswith(PROGRAM_LOG)
            else log[PROGRAM_DATA_START_INDEX:]
        )
        try:
            decoded = base64.b64decode(log_str)
        except binascii.Error:
            return 
        # throws here
        event = program.coder.events.parse(decoded)
        print(event)
        return 
    else:
        return
        
async def main():
    connection = AsyncClient("https://api.mainnet-beta.solana.com")
    signature = Signature.from_string("gjVEiXqtWPCbfKtsypL3xvNk8ef2j6D1bo5pHjvL5Bms66TbDozjs7jqusfWj2GhPgDmTojPYgYXtr7AuTk5Y3c")
    wallet = Wallet.dummy()
    provider = Provider(connection, wallet)
    program = await Program.at(DRIFT_PROGRAM_ID, provider)
    logs = (await connection.get_transaction(signature, max_supported_transaction_version=0)).value.transaction.meta.log_messages

    await parse_logs(program, logs)

if __name__ == "__main__":
    asyncio.run(main())

error:

    raise StreamError("stream read less than specified amount, expected %d, found %d" % (length, len(data)), path=path)
construct.core.StreamError: Error in path (parsing) -> discriminator
stream read less than specified amount, expected 8, found 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant