У меня есть кошелек платформы с некоторым Sol в нем. Я создал токен SPL и изгонял.private async Task CreateAssociatedTokenAccount(Wallet wallet, string vtcMintAddress)
{
try
{
// Initialize the RPC client to interact with the blockchain
IRpcClient rpcClient = ClientFactory.GetClient(Cluster.MainNet); // Use Mainnet
var userPublicKey = wallet.Account.PublicKey;
var mintAddress = new PublicKey(vtcMintAddress); // VTC Mint Address
// Derive the associated token account address using the associated token account program
var associatedTokenAddress = AssociatedTokenAccountProgram.DeriveAssociatedTokenAccount(
userPublicKey, mintAddress);
// Check if the associated token account already exists
var accountInfo = await rpcClient.GetAccountInfoAsync(associatedTokenAddress);
if (accountInfo.Result?.Value != null)
{
Console.WriteLine($"Associated token account already exists for {userPublicKey}");
return;
}
// Get the recent blockhash from the Solana network
var blockHashResult = await rpcClient.GetLatestBlockHashAsync();
// Extract the blockhash from the result
string blockhash = blockHashResult.Result.Value.Blockhash;
// Use the pre-defined platform wallet address (the one you already have as a constant)
var platformWalletAddress = new PublicKey(Constants.PlatformWalletPubKey); // Use your platform wallet's public key
// Optionally, retrieve the private key for the platform wallet if needed (if you need to sign transactions)
// You can securely store or access the private key for this wallet.
var platformWallet = new Wallet(Constants.PlatformWalletPrivateKey);
// Create the associated token account transaction
var tx = new TransactionBuilder()
.SetRecentBlockHash(blockhash) // Use the blockhash string directly
.SetFeePayer(platformWallet.Account) // Platform wallet is the fee payer
.AddInstruction(AssociatedTokenAccountProgram.CreateAssociatedTokenAccount(
platformWallet.Account.PublicKey, // payer (platform wallet)
userPublicKey, // owner of the new token account
mintAddress)); // mint of the token (VTC)
byte[] txBytes = tx.Build(new List { platformWallet.Account });
// Send the transaction to the network
var result = await rpcClient.SendTransactionAsync(Convert.ToBase64String(txBytes));
if (result.WasSuccessful)
{
Console.WriteLine($"Successfully created associated token account: {associatedTokenAddress}");
}
else
{
Console.WriteLine($"Failed to create associated token account: {result.Reason}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error creating associated token account: {ex.Message}");
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... en-account
Solana - создана ассоциированная учетная запись токена ⇐ C#
Место общения программистов C#
-
Anonymous
1756435989
Anonymous
У меня есть кошелек платформы с некоторым Sol в нем. Я создал токен SPL и изгонял.private async Task CreateAssociatedTokenAccount(Wallet wallet, string vtcMintAddress)
{
try
{
// Initialize the RPC client to interact with the blockchain
IRpcClient rpcClient = ClientFactory.GetClient(Cluster.MainNet); // Use Mainnet
var userPublicKey = wallet.Account.PublicKey;
var mintAddress = new PublicKey(vtcMintAddress); // VTC Mint Address
// Derive the associated token account address using the associated token account program
var associatedTokenAddress = AssociatedTokenAccountProgram.DeriveAssociatedTokenAccount(
userPublicKey, mintAddress);
// Check if the associated token account already exists
var accountInfo = await rpcClient.GetAccountInfoAsync(associatedTokenAddress);
if (accountInfo.Result?.Value != null)
{
Console.WriteLine($"Associated token account already exists for {userPublicKey}");
return;
}
// Get the recent blockhash from the Solana network
var blockHashResult = await rpcClient.GetLatestBlockHashAsync();
// Extract the blockhash from the result
string blockhash = blockHashResult.Result.Value.Blockhash;
// Use the pre-defined platform wallet address (the one you already have as a constant)
var platformWalletAddress = new PublicKey(Constants.PlatformWalletPubKey); // Use your platform wallet's public key
// Optionally, retrieve the private key for the platform wallet if needed (if you need to sign transactions)
// You can securely store or access the private key for this wallet.
var platformWallet = new Wallet(Constants.PlatformWalletPrivateKey);
// Create the associated token account transaction
var tx = new TransactionBuilder()
.SetRecentBlockHash(blockhash) // Use the blockhash string directly
.SetFeePayer(platformWallet.Account) // Platform wallet is the fee payer
.AddInstruction(AssociatedTokenAccountProgram.CreateAssociatedTokenAccount(
platformWallet.Account.PublicKey, // payer (platform wallet)
userPublicKey, // owner of the new token account
mintAddress)); // mint of the token (VTC)
byte[] txBytes = tx.Build(new List { platformWallet.Account });
// Send the transaction to the network
var result = await rpcClient.SendTransactionAsync(Convert.ToBase64String(txBytes));
if (result.WasSuccessful)
{
Console.WriteLine($"Successfully created associated token account: {associatedTokenAddress}");
}
else
{
Console.WriteLine($"Failed to create associated token account: {result.Reason}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error creating associated token account: {ex.Message}");
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79555751/solana-created-associated-token-account[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия