0
def SOL_USDC():
    client = Client("https://api.mainnet-beta.solana.com")

    base = "6P4tvbzRY6Bh3MiWDHuLqyHywovsRwRpfskPvyeSoHsz"
    quote = "6mK4Pxs6GhwnessH7CvPivqDYauiHZmAdbEFDpXFk9zt"

    base_pubkey = Pubkey.from_string(base)
    quote_pubkey = Pubkey.from_string(quote)
    

    base_balance = client.get_token_account_balance(base_pubkey).value.ui_amount
    quote_balance = client.get_token_account_balance(quote_pubkey).value.ui_amount

    price = quote_balance / base_balance

    print(
        f"WSOL: {base_balance}\n"
        f"USDC: {quote_balance}\n"
        f"Price: {price}"
    )

I used the basevault and quotevault of the pair address of SOL / USDC (8sLbNZoA1cfnvMJLPfp98ZLAnFSYCFApfJKMbiXNLwxj), I am using the same way of calculation of price to all the tokens but the difference is not multiplying the native price of this to USD because this is the Solana itself.

1 Answer 1

1

I'm not sure if this is what you're asking, but the code is correct! The reported UI amount from the native SOL account will be in terms of SOL, and no additional special processing is required, because the account is actually wrapped SOL (wSOL). Here's the result of querying the account with jsonParsed encoding:

$ curl https://api.mainnet-beta.solana.com -X POST -H "Content-Type: application/json" -d '
                    {
                      "jsonrpc": "2.0",
                      "id": 1,
                      "method": "getAccountInfo",
                      "params": [
                        "6P4tvbzRY6Bh3MiWDHuLqyHywovsRwRpfskPvyeSoHsz",
                        {
                          "encoding": "jsonParsed"
                        }
                      ]
                    }
                  ' | jq
{
  "jsonrpc": "2.0",
  "result": {
    "context": {
      "apiVersion": "1.18.25",
      "slot": 296127177
    },
    "value": {
      "data": {
        "parsed": {
          "info": {
            "isNative": true,
            "mint": "So11111111111111111111111111111111111111112",
            "owner": "8sLbNZoA1cfnvMJLPfp98ZLAnFSYCFApfJKMbiXNLwxj",
            "rentExemptReserve": {
              "amount": "2039280",
              "decimals": 9,
              "uiAmount": 0.00203928,
              "uiAmountString": "0.00203928"
            },
            "state": "initialized",
            "tokenAmount": {
              "amount": "7111510909507",
              "decimals": 9,
              "uiAmount": 7111.510909507,
              "uiAmountString": "7111.510909507"
            }
          },
          "type": "account"
        },
        "program": "spl-token",
        "space": 165
      },
      "executable": false,
      "lamports": 7111512948987,
      "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
      "rentEpoch": 18446744073709551615,
      "space": 165
    }
  },
  "id": 1
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.