Gửi Token

Cách nạp token vào số dư đang chờ xử lý bảo mật

Trước khi token có thể được chuyển một cách bảo mật, số dư token công khai phải được chuyển đổi thành số dư bảo mật. Quá trình chuyển đổi này diễn ra qua hai giai đoạn:

  1. Số dư đang chờ xử lý bảo mật: Ban đầu, token được "nạp" từ số dư công khai sang số dư bảo mật "đang chờ xử lý".
  2. Số dư khả dụng bảo mật: Số dư đang chờ xử lý sau đó được "áp dụng" vào số dư khả dụng, giúp token sẵn sàng cho các giao dịch chuyển bảo mật.

Phần này giải thích giai đoạn đầu tiên: nạp số dư token công khai vào số dư đang chờ xử lý bảo mật.

Sơ đồ sau đây mô tả các bước liên quan đến việc nạp token từ số dư công khai vào số dư đang chờ xử lý bảo mật:

Deposit Tokens

Lệnh yêu cầu

Để chuyển đổi số dư công khai thành số dư đang chờ xử lý bảo mật, hãy gọi lệnh ConfidentialTransferInstruction::Deposit. Số lượng tối đa mỗi lệnh nạp bị giới hạn ở mức 2^48.

Crate spl_token_client cung cấp phương thức confidential_transfer_deposit giúp xây dựng và gửi một giao dịch với lệnh Deposit, như minh họa trong ví dụ bên dưới.

Code mẫu

Ví dụ sau đây minh họa cách nạp số dư token công khai vào số dư đang chờ xử lý bảo mật.

Chuyển giao dịch bảo mật phụ thuộc vào chương trình ZK ElGamal Proof, được kích hoạt trên mainnet và devnet. Một solana-test-validator thông thường không kích hoạt nó, nhưng một validator cục bộ fork mainnet như Surfpool thì có. Chạy ví dụ trên một trong các môi trường đó (code sử dụng devnet) với tài khoản payer có đủ số dư, và thay thế địa chỉ mint và tài khoản giả bằng địa chỉ của bạn.

Rust

const ZK_PROOF_PROGRAM_ID: Pubkey =
solana_pubkey::pubkey!("ZkE1Gama1Proof11111111111111111111111111111");
fn main() -> Result<()> {
let rpc_client = RpcClient::new_with_commitment(
String::from("https://api.devnet.solana.com"),
CommitmentConfig::confirmed(),
);
// Owner = fee payer = token account owner. The setup below configures the
// account for confidential transfers (see "Create a Token Account").
let owner = load_keypair()?;
let amount: u64 = 100;
let decimals: u8 = 2;
// Setup: create a confidential token account with public tokens.
let (mint, token_account) = setup_deposit_account(&rpc_client, &owner, amount, decimals)?;
// Deposit moves tokens from the public balance into the pending confidential
// balance. No proof is required. The tokens land in the pending balance and
// must be applied (see "Apply Pending Balance") before they can be spent.
let deposit_ix = deposit(
&spl_token_2022::id(),
&token_account,
&mint,
amount,
decimals,
&owner.pubkey(),
&[&owner.pubkey()],
)?;
let blockhash = rpc_client.get_latest_blockhash()?;
let transaction =
Transaction::new_signed_with_payer(&[deposit_ix], Some(&owner.pubkey()), &[&owner], blockhash);
let signature = rpc_client.send_and_confirm_transaction(&transaction)?;
println!("Deposited {amount} tokens to the pending confidential balance: {signature}");
Ok(())
}

Typescript

const client = await createClient()
.use(signerFromFile(join(homedir(), ".config/solana/id.json")))
.use(
solanaRpc({
rpcUrl: "https://api.devnet.solana.com"
})
);
// The Solana CLI default keypair, used as fee payer, mint authority, and
// token account owner.
const owner = client.payer;
const amount = 100n;
const decimals = 2;
// Setup: create a confidential mint and token account with public tokens.
const mint = await createConfidentialMint(client, owner, decimals);
const token = await createConfidentialTokenAccount(client, owner, mint);
await mintPublicTokens(client, owner, mint, token, amount);
// Deposit moves public tokens into the pending confidential balance. No proof
// is required; the tokens must then be applied before they can be spent.
const depositInstruction = getConfidentialDepositInstruction({
token,
mint,
authority: owner,
amount,
decimals
});
const result = await client.sendTransaction([depositInstruction]);
console.log(
`Deposited ${amount} tokens to the pending confidential balance: ${result.context.signature}`
);

Is this page helpful?

Mục lục

Chỉnh sửa trang