Getting a CA Token for Newly Minted Coins Using WebSockets
As a developer, you are probably interested in learning how to subscribe to WebSocket events for newly minted Solana coins. In this article, we will look at the ability to get a CA token (contract address) when calling a new token from a web socket API.
Context:
We have a specific context where we want to create a subscription to the “solana:subscribe” API, which returns a list of newly minted coins. We also need to get the contract address for each coin in the response.
WebSocket API Event Structure:
When you subscribe to a WebSocket event using the solana:subscribe
API, you will receive a message structure with several fields:
{
"eventSignature": string,
"slot": number,
"value": {
...
field names and values ...}
}
Retrieving the CA token:
The CA of a token is usually the address of the contract that minted the new coin. When a new token is called from the websocket API, it includes the “CA” field in the message structure.
To retrieve the CA token, you will need to parse the message structure and extract the value of the “CA” field.
Sample solution:
Here is a sample solution using TypeScript:
import { WebsocketClient } from 'solana-websocket-api';
const client = new WebsocketClient({
url: '
});
asynchronous function getNewlyMintedCoins() {
const subscription = await client.subscribe('new-minted-coins', {slot: 305696650 });
while (true) {
const message = await subscription.waitForMessage({
wait: 1000,
});
if (!message) break;
// Parse the message structure to extract the value of the CA field
const caField = message.value;
const caAddress = caField.ca.toString();
console.log(
CA address for newly minted coin ${caAddress}
);}
}
Note: This solution assumes that the solana:subscribe
API returns a list of events with a single event signature, which is the event name (in this case new-minted-coin
). The message structure also includes a slot number and a value object.
Finally, you can get the Solana New Minted Coin CA token by parsing the structure of the WebSocket API event message. This solution provides a sample code snippet to get you started.