Search code examples
javascriptethereumweb3jssolanasolana-web3js

How to get the wallet ID of sender and receiver from solana transaction signature?


I have the signature of a Solana transaction and I want to find the wallet ID of the sender and the receiver. This is what I did:

const connection = new Web3.Connection(Web3.clusterApiUrl("devnet"), "confirmed");
const transaction = await connection.getTransaction(signature);

When I do console.log(transaction), it has fields like blockTime and all, but not to and from address. Can someone help?


Solution

  • When calling getTransaction, the node will return the transaction in the format specified. By default, connection.getTransaction will return the transaction with base58 encoding, so try using connection.getParsedTransaction instead. Here's an example for a simple transfer:

    curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d '
                                      {
                                        "jsonrpc": "2.0",
                                        "id": 1,
                                        "method": "getTransaction",
                                        "params": [
                                          "2YHDUWRRh4jwaAqSPJqCiu97FTy6Pu2C6XGbAzsaBbyjQeXW11z
    hhF3DJHt4vDFHVND1ybdSHf6E5FxbjFXZP4gQ",
                                          "jsonParsed"
                                        ]
                                      }
                                    ' | python3 -m json.tool
    {
        "jsonrpc": "2.0",
        "result": {
            "blockTime": 1647001173,
            "meta": {
                "err": null,
                "fee": 5000,
                "innerInstructions": [],
                "logMessages": [
                    "Program 11111111111111111111111111111111 invoke [1]",
                    "Program 11111111111111111111111111111111 success"
                ],
                "postBalances": [
                    23932341357,
                    110000000,
                    1
                ],
                "postTokenBalances": [],
                "preBalances": [
                    23942346357,
                    100000000,
                    1
                ],
                "preTokenBalances": [],
                "rewards": [],
                "status": {
                    "Ok": null
                }
            },
            "slot": 120237987,
            "transaction": {
                "message": {
                    "accountKeys": [
                        {
                            "pubkey": "4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn",
                            "signer": true,
                            "writable": true
                        },
                        {
                            "pubkey": "4AUt2JyjzJYVhWkjKugXmzhWizpb4SpLHBtL2fuqPskU",
                            "signer": false,
                            "writable": true
                        },
                        {
                            "pubkey": "11111111111111111111111111111111",
                            "signer": false,
                            "writable": false
                        }
                    ],
                    "instructions": [
                        {
                            "parsed": {
                                "info": {
                                    "destination": "4AUt2JyjzJYVhWkjKugXmzhWizpb4SpLHBtL2fuqPskU",
                                    "lamports": 10000000,
                                    "source": "4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn"
                                },
                                "type": "transfer"
                            },
                            "program": "system",
                            "programId": "11111111111111111111111111111111"
                        }
                    ],
                    "recentBlockhash": "3zny2xt5wimev9Jry3TiAyK8yA2pMMcGvsPWpFN5HiL6"
                },
                "signatures": [
                    "2YHDUWRRh4jwaAqSPJqCiu97FTy6Pu2C6XGbAzsaBbyjQeXW11zhhF3DJHt4vDFHVND1ybdSHf6E5FxbjFXZP4gQ"
                ]
            }
        },
        "id": 1
    }
    

    In result.transaction.message.instructions[0].parsed you'll get what you're looking for.

    More information about the getTransaction call at https://docs.solana.com/developing/clients/jsonrpc-api#gettransaction