Solana'nın Token Programları delegasyonu destekler—başka bir hesaba, token hesabınızdan belirli bir limite kadar token transfer etme izni vermek. Bu, fonlarınızın kontrolünden vazgeçmeden otomatik ödemeler, emanet hizmetleri ve üçüncü taraf ödeme işleme gibi kullanım senaryolarını mümkün kılar.
Delegasyon nasıl çalışır
Bir delegeyi onayladığınızda, belirli bir hesaba sizin adınıza token transfer etme yetkisi veriyorsunuz:
- Sahip kontrolü elinde tutar: Tokenlar hala size aittir ve istediğiniz zaman transfer edebilir veya iptal edebilirsiniz
- Sınırlı harcama: Delege yalnızca onaylanan miktara kadar transfer yapabilir
- Hesap başına tek delege: Her token hesabının yalnızca bir aktif delegesi olabilir
- Yeni onay eskisinin yerini alır: Yeni bir delegeyi onaylamak, öncekini otomatik olarak iptal eder
Delegasyon emanetsizdir. Delege, limite kadar token harcayabilir, ancak onaylanan miktarın ötesinde hesaba erişemez veya hesabı boşaltamaz. Sahip istediği zaman iptal edebilir.
İş kullanım senaryoları
| Kullanım senaryosu | Delegasyon nasıl yardımcı olur |
|---|---|
| Ödeme işlemcileri | Satıcı, işlemciye işlemleri sonuçlandırma izni verir |
| Otomatik bordro | Hazine, bordro hizmetine maaşları dağıtma yetkisi verir |
| Emanet hizmetleri | Alıcı, koşullu serbest bırakma için emanet acentesine delege eder |
| Ticaret platformları | Kullanıcı, borsaya kendi adına işlem yapma yetkisi verir |
| Kart ihracı | Kullanıcı, kart düzenleyicisine token hesabından alışveriş yapma yetkisi verir |
Bir delegeyi onaylama
Başka bir hesaba, hesabınızdan token harcama izni verin:
import { getApproveCheckedInstruction } from "@solana-program/token";// Approve delegate to spend up to 1,000 USDC (6 decimals)const approveInstruction = getApproveCheckedInstruction({source: tokenAccountAddress, // Your token accountmint: usdcMintAddress, // USDC mintdelegate: delegateAddress, // Account receiving permissionowner: ownerKeypair, // You (must sign)amount: 1_000_000_000n, // 1,000 USDC in base unitsdecimals: 6});
Parametreler:
source: İzin veren token hesabıdelegate: Harcama iznine sahip olacak hesapowner: Token hesabının mevcut sahibi (işlemi imzalamalıdır)amount: Temsilcinin transfer edebileceği maksimum token miktarıdecimals: Doğrulama için token ondalık basamak sayısı (ondalık hataları önler)
Demo
// Generate keypairs for sender and delegateconst sender = (await generateKeypair()).signer;const delegate = (await generateKeypair()).signer;console.log("Sender Address:", sender.address);console.log("Delegate Address:", delegate.address);// Demo Setup: Create client, mint account, token account, and fund with initial tokensconst { client, mint, senderAta } = await demoSetup(sender);console.log("\nMint Address:", mint.address);console.log("Sender ATA:", senderAta);// =============================================================================// Approve Delegate// =============================================================================// Create instruction to approve delegateconst approveInstruction = getApproveCheckedInstruction({source: senderAta,mint: mint.address,delegate: delegate.address,owner: sender,amount: 1_000_000n, // 1.0 tokens with 6 decimalsdecimals: 6});// Send approve transactionconst signature = await client.transaction.prepareAndSend({authority: sender,instructions: [approveInstruction]});console.log("\n=== Approve Delegate ===");console.log("Transaction Signature:", signature);// Fetch token account data to show delegate is setconst tokenData = await fetchToken(client.runtime.rpc, senderAta);console.log("\nSender Token Account Data:", tokenData.data);// =============================================================================// Demo Setup Helper Function// =============================================================================
Temsilciyi iptal etme
Mevcut temsilciden tüm harcama izinlerini kaldırın:
import { getRevokeInstruction } from "@solana-program/token";const revokeInstruction = getRevokeInstruction({source: tokenAccountAddress, // Your token accountowner: ownerKeypair // You (must sign)});
İptal işlemi tüm temsilci izinlerini kaldırır—kısmi iptal yoktur. Limiti azaltmanız gerekiyorsa, aynı temsilciyi daha düşük bir miktarla onaylayın.
Demo
// Generate keypairs for sender and delegateconst sender = (await generateKeypair()).signer;const delegate = (await generateKeypair()).signer;console.log("Sender Address:", sender.address);console.log("Delegate Address:", delegate.address);// Demo Setup: Create client, mint account, token account, and fund with initial tokensconst { client, mint, senderAta } = await demoSetup(sender);console.log("\nMint Address:", mint.address);console.log("Sender ATA:", senderAta);// =============================================================================// Transaction 1: Approve Delegate// =============================================================================// Create instruction to approve delegateconst approveInstruction = getApproveCheckedInstruction({source: senderAta,mint: mint.address,delegate: delegate.address,owner: sender,amount: 1_000_000n, // 1.0 tokens with 6 decimalsdecimals: 6});// Send approve transactionconst approveSignature = await client.transaction.prepareAndSend({authority: sender,instructions: [approveInstruction]});console.log("\n=== Transaction 1: Approve Delegate ===");console.log("Transaction Signature:", approveSignature);// Fetch token account data to show delegate is setconst tokenDataAfterApprove = await fetchToken(client.runtime.rpc, senderAta);console.log("\nSender Token Account Data:", tokenDataAfterApprove.data);// =============================================================================// Transaction 2: Revoke Delegate// =============================================================================// Create instruction to revoke delegateconst revokeInstruction = getRevokeInstruction({source: senderAta,owner: sender});// Send revoke transactionconst revokeSignature = await client.transaction.prepareAndSend({authority: sender,instructions: [revokeInstruction]});console.log("\n=== Transaction 2: Revoke Delegate ===");console.log("Transaction Signature:", revokeSignature);// Fetch token account data to show delegate is revokedconst tokenDataAfterRevoke = await fetchToken(client.runtime.rpc, senderAta);console.log("\nSender Token Account Data:", tokenDataAfterRevoke.data);// =============================================================================// Demo Setup Helper Function// =============================================================================
Temsilci olarak transfer yapma
Temsilci olarak hareket ederken, standart bir transfer kullanın ancak sahip yerine temsilci keypair ile imzalayın:
import { getTransferCheckedInstruction } from "@solana-program/token";const transferInstruction = getTransferCheckedInstruction({source: ownerTokenAccount, // The account you have permission to spend frommint: usdcMintAddress,destination: recipientTokenAccount,authority: delegateKeypair, // You (the delegate) sign, not the owneramount: 100_000_000n, // 100 USDCdecimals: 6});
Transfer şu durumlarda başarılı olur:
- Kaynak hesapta yeterli bakiye varsa
- Temsilci işlemi imzalarsa
Her transfer kalan ödeneği azaltır. Ödenek sıfıra ulaştığında, temsilci artık token transfer edemez.
Demo
// Generate keypairs for sender, delegate, and recipientconst sender = (await generateKeypair()).signer;const delegate = (await generateKeypair()).signer;const recipient = (await generateKeypair()).signer;console.log("Sender Address:", sender.address);console.log("Delegate Address:", delegate.address);console.log("Recipient Address:", recipient.address);// Demo Setup: Create client, mint account, token accounts, and fund with initial tokensconst { client, mint, senderAta, recipientAta } = await demoSetup(sender,delegate,recipient);console.log("\nMint Address:", mint.address);console.log("Sender ATA:", senderAta);console.log("Recipient ATA:", recipientAta);// =============================================================================// Transaction 1: Approve Delegate// =============================================================================// Create instruction to approve delegateconst approveInstruction = getApproveCheckedInstruction({source: senderAta,mint: mint.address,delegate: delegate.address,owner: sender,amount: 1_000_000n, // 1.0 tokens with 6 decimalsdecimals: 6});// Send approve transactionconst approveSignature = await client.transaction.prepareAndSend({authority: sender,instructions: [approveInstruction]});console.log("\n=== Transaction 1: Approve Delegate ===");console.log("Delegate Address:", delegate.address);console.log("Transaction Signature:", approveSignature);// =============================================================================// Fetch Token Account Data to Demonstrate Delegate is Set// =============================================================================const tokenAccountData = await fetchToken(client.runtime.rpc, senderAta);console.log("\nSender Token Account Data:", tokenAccountData.data);// =============================================================================// Transaction 2: Transfer Using Delegate// =============================================================================// Create instruction to transfer tokens using delegate// Note: delegate is the authority here, not the ownerconst transferInstruction = getTransferCheckedInstruction({source: senderAta,mint: mint.address,destination: recipientAta,authority: delegate, // Delegate signs this transactionamount: 500_000n, // 0.5 tokens with 6 decimalsdecimals: 6});// Send transfer transaction// Delegate pays for the transaction and authorizes the transfer (sender not needed)const transferSignature = await client.transaction.prepareAndSend({authority: delegate, // Delegate pays fee and signsinstructions: [transferInstruction]});// =============================================================================// Fetch Final Token Account Balances// =============================================================================const finalSenderToken = await fetchToken(client.runtime.rpc, senderAta);const finalRecipientToken = await fetchToken(client.runtime.rpc, recipientAta);console.log("\n=== Transaction 2: Transfer Using Delegate ===");console.log("Transaction Signature:", transferSignature);console.log("\nSender Token Account Data:", finalSenderToken.data);console.log("\nRecipient Token Account Data:", finalRecipientToken.data);// =============================================================================// Demo Setup Helper Function// =============================================================================
Temsilci durumunu kontrol etme
Bir token hesabını sorgulayarak mevcut temsilcisini ve kalan limitini görün:
import { fetchToken } from "@solana-program/token";const tokenAccount = await fetchToken(rpc, tokenAccountAddress);if (tokenAccount.data.delegate) {console.log("Delegate:", tokenAccount.data.delegate);console.log("Remaining allowance:", tokenAccount.data.delegatedAmount);} else {console.log("No delegate set");}
Demo
// Demo Setup: Create client, mint, two token accounts (one with delegate, one without)const { client, ataWithDelegate, ataWithoutDelegate } = await demoSetup();// =============================================================================// Fetch Token Accounts// =============================================================================// Fetch token account with delegateconst tokenWithDelegate = await fetchToken(client.runtime.rpc, ataWithDelegate);console.log("Token Account with Delegate:", tokenWithDelegate);// Fetch token account without delegateconst tokenWithoutDelegate = await fetchToken(client.runtime.rpc,ataWithoutDelegate);console.log("\nToken Account without Delegate:", tokenWithoutDelegate);// =============================================================================// Demo Setup Helper Function// =============================================================================
Güvenlik hususları
Hesap sahipleri için:
- Yalnızca güvenilir temsilcileri onaylayın
- Gerekli minimum harcama limitini belirleyin
- Artık ihtiyaç duyulmadığında temsilci yetkilerini iptal edin
- Beklenmeyen transferler için hesaplarınızı izleyin
Hizmet sağlayıcılar (temsilciler) için:
- Talep edilen harcama limitini kullanıcılara açıkça iletin
- Temsilci hesabınız için uygun anahtar yönetimi uygulayın
- Limitler tükenmeden önce yeniden onay talep etmek için ödenek tüketimini takip edin
Temsilci yetkisi ile saklama karşılaştırması
| Özellik | Temsilci yetkisi | Tam saklama |
|---|---|---|
| Token sahipliği | Kullanıcıda kalır | Kullanıcı saklayıcıya transfer eder |
| Harcama kontrolü | Onaylanan tutarla sınırlı | Transfer edilen fonlara tam erişim |
| İptal | Anında, sahip tarafından | Saklayıcının işbirliği gerektirir |
| Risk maruziyeti | Onaylanan tutarla sınırlı | Tüm bakiye |
| Gereken güven | Sınırlı | Yüksek |
Temsilci yetkisi bir orta yol sağlar—otomatik ödemeleri mümkün kılarken risk maruziyetini onaylanan tutarla sınırlar.
İlgili kaynaklar
| Kaynak | Açıklama |
|---|---|
| Temsilci onaylama | Başka bir hesaba token hesabınızdan harcama izni verme. |
| Temsilci yetkisini iptal etme | Mevcut bir temsilciyi kaldırma ve harcama izinlerini iptal etme. |
| Token transferi | Token hesapları arasında token transferi yapma. |
Is this page helpful?