Thanh toánĐăng ký

Ủy Quyền Định Kỳ

Ủy quyền định kỳ cho phép người dùng phê duyệt cho ví hoặc dịch vụ khác rút tiền trong giới hạn được đặt lại sau mỗi chu kỳ.

Hướng dẫn này giữ các thành phần chuyển động trong tầm nhìn. Bạn sẽ tạo các tài khoản trước, khởi tạo Subscription Authority nếu cần, tạo ủy quyền định kỳ, sau đó sử dụng PDA ủy quyền đó cho các giao dịch chuyển tiền.

Cài Đặt

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

Tạo Ủy Quyền

Quá trình thiết lập gồm bốn phần:

  1. Tạo client với người ký người dùng và plugin subscriptions.
  2. Tạo token account của người dùng, PDA Subscription Authority và PDA ủy quyền định kỳ.
  3. Khởi tạo Subscription Authority nếu chưa tồn tại.
  4. Tạo ủy quyền định kỳ với số tiền chu kỳ, độ dài chu kỳ, thời gian bắt đầu và thời gian hết hạn.
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,
findRecurringDelegationPda,
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 now = BigInt(Math.floor(Date.now() / 1000));
const nonce = 0n;
const amountPerPeriod = 1_000_000n;
const periodLengthS = 86_400n;
const startTs = now;
const expiryTs = now + periodLengthS * 30n;
const [userAta] = await findAssociatedTokenPda({
mint: tokenMint,
owner: userSigner.address,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
const [subscriptionAuthorityPda] = await findSubscriptionAuthorityPda({
user: userSigner.address,
tokenMint,
});
const [delegationPda] = await findRecurringDelegationPda({
subscriptionAuthority: subscriptionAuthorityPda,
delegator: userSigner.address,
delegatee,
nonce,
});
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
.createRecurringDelegation({
tokenMint,
delegatee,
nonce,
amountPerPeriod,
periodLengthS,
startTs,
expiryTs,
})
.sendTransaction();

Chuyển Tiền Từ Ủy Quyền

Người được ủy quyền ký mỗi giao dịch chuyển tiền. Chương trình kiểm tra chu kỳ hiện tại và từ chối các giao dịch chuyển tiền vượt quá hạn mức còn lại của chu kỳ.

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

Thu hồi ủy quyền

Người ủy quyền có thể thu hồi ủy quyền định kỳ bất cứ lúc nào. Việc thu hồi sẽ đóng PDA ủy quyền và hoàn trả tiền thuê cho người ký.

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

Ghi chú

  • amountPerPeriod tính theo đơn vị cơ sở. Với token có 6 chữ số thập phân, 1_000_000 nghĩa là 1 token.
  • Chương trình từ chối các giao dịch chuyển vượt quá hạn mức còn lại trong chu kỳ hiện tại.
  • Khi chu kỳ tiếp theo bắt đầu, lượng đã rút sẽ được đặt lại.
  • expiryTs là giới hạn cứng: một khi đã qua mốc này, các giao dịch chuyển sẽ thất bại và nhà tài trợ có thể thu hồi rent. Không có khoảng thời gian ân hạn nào.
  • Đặt startTs thành 0 để bắt đầu ủy quyền khi giao dịch được xác nhận trên chuỗi thay vì tại một thời điểm cố định. Điều này mở rộng khoảng thời gian để người dùng ký và tham gia. Khi thực hiện điều này, expiryTs phải khác không — một ủy quyền bắt đầu khi xác nhận không thể đồng thời được đặt là không bao giờ hết hạn.
  • Người dùng ký các giao dịch thiết lập và thu hồi. Người được ủy quyền ký các giao dịch chuyển.

Is this page helpful?

Mục lục

Chỉnh sửa trang