Getting Token CA on Newly Minted Coins using WebSockets
As a developer, you’re likely interested in learning how to subscribe to WebSocket events for newly minted coins on Solana. In this article, we’ll explore the possibility of obtaining the token CA (Contract Address) 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 obtain the contract address for each coin in the response.
WebSocket API Event Structure:
When subscribing to the WebSocket event using the solana:subscribe
API, you’ll receive a message structure with several fields:
{
"eventSignature": string,
"slot": number,
"value": {
...
field names and values ...
}
}
Token CA Retrieval:
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 CA
field in its message structure.
To retrieve the token CA, you’ll need to parse the message structure and extract the CA
field value.
Example Solution:
Here’s 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 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 also includes the slot number and value object.
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.
Leave a Reply