결제구독

고정 위임

고정 위임을 사용하면 사용자가 다른 지갑이나 서비스가 최대 고정된 토큰 금액까지 인출할 수 있도록 승인할 수 있습니다. 각 성공적인 전송은 남은 허용량을 감소시킵니다. 만료 없음의 경우 expiryTs = 0를 사용하세요.

이 가이드는 흐름을 명확하게 유지합니다. 각 스니펫은 SDK 함수를 직접 사용하므로 어떤 계정이 파생되고, 어떤 명령어가 전송되며, 어떤 서명자가 각 단계를 지불하거나 승인하는지 확인할 수 있습니다.

설치

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

위임 생성

설정은 네 부분으로 구성됩니다:

  1. 사용자 서명자와 구독 플러그인으로 클라이언트를 생성합니다.
  2. 사용자의 토큰 계정과 Subscription Authority PDA를 파생합니다.
  3. Subscription Authority가 아직 존재하지 않으면 초기화합니다.
  4. 고정 위임을 생성하고 나중의 전송을 위해 해당 PDA를 파생합니다.
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,
});

위임으로부터 전송

피위임자가 전송에 서명합니다. SDK는 동일한 위임 PDA, 사용자의 토큰 계정, 수신자 토큰 계정이 필요합니다.

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();

위임 취소

위임자는 언제든지 위임을 취소할 수 있습니다. 취소하면 위임 PDA가 닫히고 rent가 서명자에게 반환됩니다.

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

참고사항

  • 초기화 전에 사용자의 token account가 존재해야 합니다.
  • 금액은 기본 단위입니다. 소수점 6자리 토큰의 경우 1_000_0001 토큰을 의미합니다.
  • Subscription Authority 계정이 아직 존재하지 않는 경우에만 initSubscriptionAuthority를 실행하세요.
  • 사용자는 설정 및 취소 트랜잭션에 서명합니다. 위임받은 사람은 전송에 서명합니다.

Is this page helpful?

목차

페이지 편집