建立
make 指令執行三個任務:
初始化托管(Escrow)記錄並儲存所有交易條款。
建立保管庫(Vault)(一個由
mint_a擁有的escrow的 ATA)。使用 CPI 調用 SPL-Token 程式,將創建者的 Token A 移入該保管庫。
所需帳戶
以下是上下文所需的帳戶:
maker:托管的創建者。必須是簽署者且可變
escrow:我們正在初始化的托管帳戶。必須可變
mint_a:我們存入托管的代幣
mint_b:我們希望接收的代幣
maker_ata_a:由創建者擁有的關聯代幣帳戶。必須可變
vault:由托管擁有的關聯代幣帳戶。必須可變
system_program:系統程式。必須可執行
token_program:代幣程式。必須可執行
注意:我們將使用 Pinocchio 簡介 中介紹的類型。
在程式碼中,這看起來像這樣:
rust
pub struct MakeAccounts<'a> {
pub maker: &'a AccountInfo,
pub escrow: &'a AccountInfo,
pub mint_a: &'a AccountInfo,
pub mint_b: &'a AccountInfo,
pub maker_ata_a: &'a AccountInfo,
pub vault: &'a AccountInfo,
pub system_program: &'a AccountInfo,
pub token_program: &'a AccountInfo,
}
impl<'a> TryFrom<&'a [AccountInfo]> for MakeAccounts<'a> {
type Error = ProgramError;
fn try_from(accounts: &'a [AccountInfo]) -> Result<Self, Self::Error> {
let [maker, escrow, mint_a, mint_b, maker_ata_a, vault, system_program, token_program, _] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
// Basic Accounts Checks
SignerAccount::check(maker)?;
MintInterface::check(mint_a)?;
MintInterface::check(mint_b)?;
AssociatedTokenAccount::check(maker_ata_a, maker, mint_a, token_program)?;
// Return the accounts
Ok(Self {
maker,
escrow,
mint_a,
mint_b,
maker_ata_a,
vault,
system_program,
token_program,
})
}
}指令數據
以下是我們需要傳入的指令數據:
seed:在種子推導過程中使用的隨機數。必須是 u64
receive:創建者希望接收的數量。必須是 u64
amount:創建者希望存入的數量。必須是 u64
我們將檢查 amount 是否為零,因為這對於托管來說是沒有意義的。
在程式碼中,它看起來像這樣:
rust
pub struct MakeInstructionData {
pub seed: u64,
pub receive: u64,
pub amount: u64,
}
impl<'a> TryFrom<&'a [u8]> for MakeInstructionData {
type Error = ProgramError;
fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
if data.len() != size_of::<u64>() * 3 {
return Err(ProgramError::InvalidInstructionData);
}
let seed = u64::from_le_bytes(data[0..8].try_into().unwrap());
let receive = u64::from_le_bytes(data[8..16].try_into().unwrap());
let amount = u64::from_le_bytes(data[16..24].try_into().unwrap());
// Instruction Checks
if amount == 0 {
return Err(ProgramError::InvalidInstructionData);
}
Ok(Self {
seed,
receive,
amount,
})
}
}指令邏輯
我們首先在 TryFrom 實現中初始化所需的帳戶,這是在我們反序列化 instruction_data 和 accounts 之後進行的。
在這一步,我們使用從Pinocchio 簡介中介紹的輔助函數的ProgramAccount::init::<Escrow>特性來建立Escrow帳戶。同樣地,我們初始化 Vault 帳戶,因為它需要全新建立:
rust
pub struct Make<'a> {
pub accounts: MakeAccounts<'a>,
pub instruction_data: MakeInstructionData,
pub bump: u8,
}
impl<'a> TryFrom<(&'a [u8], &'a [AccountInfo])> for Make<'a> {
type Error = ProgramError;
fn try_from((data, accounts): (&'a [u8], &'a [AccountInfo])) -> Result<Self, Self::Error> {
let accounts = MakeAccounts::try_from(accounts)?;
let instruction_data = MakeInstructionData::try_from(data)?;
// Initialize the Accounts needed
let (_, bump) = find_program_address(&[b"escrow", accounts.maker.key(), &instruction_data.seed.to_le_bytes()], &crate::ID);
let seed_binding = instruction_data.seed.to_le_bytes();
let bump_binding = [bump];
let escrow_seeds = [
Seed::from(b"escrow"),
Seed::from(accounts.maker.key().as_ref()),
Seed::from(&seed_binding),
Seed::from(&bump_binding),
];
ProgramAccount::init::<Escrow>(
accounts.maker,
accounts.escrow,
&escrow_seeds,
Escrow::LEN
)?;
// Initialize the vault
AssociatedTokenAccount::init(
accounts.vault,
accounts.mint_a,
accounts.maker,
accounts.escrow,
accounts.system_program,
accounts.token_program,
)?;
Ok(Self {
accounts,
instruction_data,
bump,
})
}
}現在我們可以專注於邏輯本身,這將只是填充托管帳戶,然後將代幣轉移到 Vault。
rust
impl<'a> Make<'a> {
pub const DISCRIMINATOR: &'a u8 = &0;
pub fn process(&mut self) -> ProgramResult {
// Populate the escrow account
let mut data = self.accounts.escrow.try_borrow_mut_data()?;
let escrow = Escrow::load_mut(data.as_mut())?;
escrow.set_inner(
self.instruction_data.seed,
*self.accounts.maker.key(),
*self.accounts.mint_a.key(),
*self.accounts.mint_b.key(),
self.instruction_data.receive,
[self.bump],
);
// Transfer tokens to vault
Transfer {
from: self.accounts.maker_ata_a,
to: self.accounts.vault,
authority: self.accounts.maker,
amount: self.instruction_data.amount
}.invoke()?;
Ok(())
}
}