BetalingenAbonnementen

Vaste Delegatie

Een vaste delegatie stelt een gebruiker in staat om een andere wallet of service goed te keuren om tot een vast tokenbedrag op te halen. Elke geslaagde overdracht vermindert de resterende toelage. Gebruik expiryTs = 0 voor geen vervaldatum.

Deze handleiding houdt de flow zichtbaar. Elk fragment gebruikt de SDK-functies rechtstreeks zodat u kunt zien welk account wordt afgeleid, welke instructie wordt verzonden, en welke ondertekenaar voor elke stap betaalt of autoriseert.

Installatie

pnpm add @solana/subscriptions @solana/kit @solana/kit-plugin-rpc @solana/kit-plugin-signer @solana-program/token

Maak De Delegatie Aan

De setup bestaat uit vier onderdelen:

  1. Maak een client aan met de ondertekenaar van de gebruiker en de subscripties-plugin.
  2. Leid het token account van de gebruiker en de Subscription Authority PDA af.
  3. Initialiseer de Subscription Authority als deze nog niet bestaat.
  4. Maak de vaste delegatie aan en leid de PDA ervan af voor latere overdrachten.
import { address, createClient } from '@solana/kit';
import { solanaLocalRpc } from '@solana/kit-plugin-rpc';
import { signer } from '@solana/kit-plugin-signer';
import { findAssociatedTokenPda, TOKEN_PROGRAM_ADDRESS } from '@solana-program/token';
import {
fetchMaybeSubscriptionAuthority,
findFixedDelegationPda,
findSubscriptionAuthorityPda,
subscriptionsProgram,
} from '@solana/subscriptions';
const client = createClient()
.use(signer(userSigner))
.use(solanaLocalRpc({ rpcUrl: 'http://127.0.0.1:8899' }))
.use(subscriptionsProgram());
const tokenMint = address('TOKEN_MINT_ADDRESS_HERE');
const delegatee = address('DELEGATEE_WALLET_ADDRESS_HERE');
const nonce = 0n;
const amount = 1_000_000n;
const expiryTs = BigInt(Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30);
const [userAta] = await findAssociatedTokenPda({
mint: tokenMint,
owner: userSigner.address,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
const [subscriptionAuthorityPda] = await findSubscriptionAuthorityPda({
user: userSigner.address,
tokenMint,
});
const subscriptionAuthority = await fetchMaybeSubscriptionAuthority(
client.rpc,
subscriptionAuthorityPda,
);
if (!subscriptionAuthority.exists) {
await client.subscriptions.instructions
.initSubscriptionAuthority({
tokenMint,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
userAta,
})
.sendTransaction();
}
await client.subscriptions.instructions
.createFixedDelegation({
tokenMint,
delegatee,
nonce,
amount,
expiryTs,
})
.sendTransaction();
const [delegationPda] = await findFixedDelegationPda({
subscriptionAuthority: subscriptionAuthorityPda,
delegator: userSigner.address,
delegatee,
nonce,
});

Overdracht Vanuit De Delegatie

De gedelegeerde ondertekent de overdracht. De SDK heeft dezelfde delegatie-PDA nodig, het token account van de gebruiker en het token account van de ontvanger.

const receiverAta = address('RECEIVER_TOKEN_ACCOUNT_ADDRESS_HERE');
await client.subscriptions.instructions
.transferFixed({
delegatee: delegateeSigner,
delegator: userSigner.address,
delegatorAta: userAta,
tokenMint,
delegationPda,
amount: 100_000n,
receiverAta,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
})
.sendTransaction();

Delegatie Intrekken

De delegant kan de delegatie op elk moment intrekken. Het intrekken sluit de delegatie-PDA en retourneert de rent aan de ondertekenaar.

await client.subscriptions.instructions
.revokeDelegation({
authority: userSigner,
delegationAccount: delegationPda,
})
.sendTransaction();

Opmerkingen

  • De token account van de gebruiker moet bestaan voor initialisatie.
  • Bedragen zijn in basiseenheden. Voor een token met 6 decimalen betekent 1_000_000 dat 1 token.
  • Voer initSubscriptionAuthority alleen uit wanneer het Subscription Authority-account nog niet bestaat.
  • De gebruiker ondertekent setup- en intrekkingstransacties. De gedelegeerde ondertekent overboekingen.

Is this page helpful?

Inhoudsopgave

Pagina Bewerken
© 2026 Solana Foundation. Alle rechten voorbehouden.