Getting Token CA on Newly Minted Coins Using WebSockets
As a developer, you probably want to learn how to subscribe to WebSocket events for newly minted coins on Solana. In this article, we will explore the possibility of obtaining the CA (Contract Address) token when a new token is called from the websocket API.
The 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 name and value ...}
}
Getting the Token CA:
The token CA 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 its message structure.
To get the token CA, you need to parse the message structure and extract the value of the “CA” field.
Sample Solution:
Here is an example solution using TypeScript:
import { WebsocketClient } from: 'solana-websocket-api';
const client = new WebsocketClient({
url: '
});
async function getNewlyMintedCoins() {
const subscription = await client.subscribe('new-minted-coins', { slot: 305696650 });
while (true) {
const message = await subscription.waitForMessage({
timeout: 1000,
});
if (!message) break;
// Parse the message structure to extract the CA field value
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 name of the event (in this case, “new-minted-coins”). The message structure includes both the slot number and the value object.
In summary, by parsing the WebSocket API event message structure, you can retrieve the token CA associated with Solana’s newly minted coins. This solution provides an example code snippet to get you started.