gagliardetto / solana-go

Go SDK library and RPC client for the Solana Blockchain
Apache License 2.0
930 stars 264 forks source link

how to know it is "Token Program: Sync Native" #153

Open yangyile1990 opened 1 year ago

yangyile1990 commented 1 year ago

I use

out, err := s.client3.GetTransaction(context.TODO(), sign, &rpc3.GetTransactionOpts{
        Encoding: solana.EncodingBase64,
    })
txn, err := out.Transaction.GetTransaction()

I print the txn:

{
    "signatures": [
        "3wMPKe86EK4zZEuD6GycPoi3CvWrPBvvwzdLcrLv6JW2FNSq89Re1jhPbSvnw9H54aAMkkeU5F72k21mgAjYwqgB"
    ],
    "message": {
        "accountKeys": [
            "5wp1KskystsYK8iU7V2W9eAC6XKwuvggMB8pVsJM33CX",
            "3g7AbGhgZr2Xb43MQuJnrQLoH8HefrUMrM6LcQa93dEG",
            "11111111111111111111111111111111",
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
        ],
        "header": {
            "numRequiredSignatures": 1,
            "numReadonlySignedAccounts": 0,
            "numReadonlyUnsignedAccounts": 2
        },
        "recentBlockhash": "ZprqouuSmzbU3cGqsayT3NftzxgW3H91q2W8XW4iQ38",
        "instructions": [
            {
                "programIdIndex": 2,
                "accounts": [
                    0,
                    1
                ],
                "data": "3Bxs4gyJ5DCmb9Xd"
            },
            {
                "programIdIndex": 3,
                "accounts": [
                    1
                ],
                "data": "J"
            }
        ]
    }
}

where why I decode the instructions[1]. I got an error, because I decode the "J" with:

type TokenInstructParam struct {
    Instruction token.Instruction
    Amount      uint64
    Decimals    uint8
}

But the program is "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA".

So my question is how to find the instructions[1] as "Token Program: Sync Native"

https://explorer.solana.com/tx/3wMPKe86EK4zZEuD6GycPoi3CvWrPBvvwzdLcrLv6JW2FNSq89Re1jhPbSvnw9H54aAMkkeU5F72k21mgAjYwqgB

yangyile1990 commented 1 year ago

how to get the "type": "syncNative" like this:

{
  "jsonrpc": "2.0",
  "result": {
    "blockTime": 1675358593,
    "meta": {
      "err": null,
      "fee": 5000,
      "innerInstructions": [],
      "logMessages": [
        "Program 11111111111111111111111111111111 invoke [1]",
        "Program 11111111111111111111111111111111 success",
        "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]",
        "Program log: Instruction: SyncNative",
        "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3045 of 400000 compute units",
        "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success"
      ],
      "postBalances": [
        16312840,
        1444044870,
        1,
        934087680
      ],
      "postTokenBalances": [
        {
          "accountIndex": 1,
          "mint": "So11111111111111111111111111111111111111112",
          "owner": "5wp1KskystsYK8iU7V2W9eAC6XKwuvggMB8pVsJM33CX",
          "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
          "uiTokenAmount": {
            "amount": "1442005590",
            "decimals": 9,
            "uiAmount": 1.44200559,
            "uiAmountString": "1.44200559"
          }
        }
      ],
      "preBalances": [
        537186943,
        923175767,
        1,
        934087680
      ],
      "preTokenBalances": [
        {
          "accountIndex": 1,
          "mint": "So11111111111111111111111111111111111111112",
          "owner": "5wp1KskystsYK8iU7V2W9eAC6XKwuvggMB8pVsJM33CX",
          "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
          "uiTokenAmount": {
            "amount": "921136487",
            "decimals": 9,
            "uiAmount": 0.921136487,
            "uiAmountString": "0.921136487"
          }
        }
      ],
      "rewards": [],
      "status": {
        "Ok": null
      }
    },
    "slot": 175687414,
    "transaction": {
      "message": {
        "accountKeys": [
          {
            "pubkey": "5wp1KskystsYK8iU7V2W9eAC6XKwuvggMB8pVsJM33CX",
            "signer": true,
            "source": "transaction",
            "writable": true
          },
          {
            "pubkey": "3g7AbGhgZr2Xb43MQuJnrQLoH8HefrUMrM6LcQa93dEG",
            "signer": false,
            "source": "transaction",
            "writable": true
          },
          {
            "pubkey": "11111111111111111111111111111111",
            "signer": false,
            "source": "transaction",
            "writable": false
          },
          {
            "pubkey": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
            "signer": false,
            "source": "transaction",
            "writable": false
          }
        ],
        "addressTableLookups": null,
        "instructions": [
          {
            "parsed": {
              "info": {
                "destination": "3g7AbGhgZr2Xb43MQuJnrQLoH8HefrUMrM6LcQa93dEG",
                "lamports": 520869103,
                "source": "5wp1KskystsYK8iU7V2W9eAC6XKwuvggMB8pVsJM33CX"
              },
              "type": "transfer"
            },
            "program": "system",
            "programId": "11111111111111111111111111111111"
          },
          {
            "parsed": {
              "info": {
                "account": "3g7AbGhgZr2Xb43MQuJnrQLoH8HefrUMrM6LcQa93dEG"
              },
              "type": "syncNative"
            },
            "program": "spl-token",
            "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
          }
        ],
        "recentBlockhash": "ZprqouuSmzbU3cGqsayT3NftzxgW3H91q2W8XW4iQ38"
      },
      "signatures": [
        "3wMPKe86EK4zZEuD6GycPoi3CvWrPBvvwzdLcrLv6JW2FNSq89Re1jhPbSvnw9H54aAMkkeU5F72k21mgAjYwqgB"
      ]
    },
    "version": "legacy"
  },
  "id": "a6202aa5-44e7-4d51-9a0d-acf89f06d3c6"
}
gagliardetto commented 1 year ago

that

                "data": "J"

is base64 (base58?) encoded by array.

If you go read the source code of the token program, you will see that syncNative instruction is number 17, and accepts no parameters:

https://github.com/solana-labs/solana-program-library/blob/ce2f7e2ccf4a192648145cb71871c2b7716e3734/token/program/src/instruction.rs#L377

That means that the parameters to the token program is just the number of the instruction to call, and no other params.