1. 引言
在Solana中,智能合約稱為program,
相關代碼見:
- https://github.com/ironaddicteddog/anchor-escrow(Escrow program implemented in Anchor)
- https://github.com/paul-schaaf/solana-escrow
- https://github.com/mvines/solana-bpf-program-template
通過界面創建Solana SPL token有:
- Solana SPL TOKEN CREATOR UI
2. 何為托管合約?
假設Alice有資產X,Bob有資產Y,二者想進行資產交換,
傳統的方案為:引入Alice和Bob都信任的第三方Cathy,Cathy只有等收到 Alice發來的X資產 和 Bob發來的Y資產 之后,才會 將Y資產發給Alice 并 將X資產發給Bob,
可通過智能合約來替代可信第三方Cathy的角色,
Solana中的合約為無狀態(Stateless)的,若想存盤state,需借助accounts引數:
entrypoint!(process_instruction);
fn process_instruction(
program_id: &Pubkey, //當前執行的合約
accounts: &[AccountInfo], // The accounts referenced by an instruction represent on-chain state and serve as both the inputs and outputs of a program
instruction_data: &[u8], // 傳給合約的引數
) -> ProgramResult {
Processor::process(program_id, accounts, instruction_data)
}
Solana合約本身存盤在標記為executable的account中,每個account可hold data and SOL,每個account有一個owner,僅owner可debit the account and adjust its data,Crediting may be done by anyone,
理論上,合約對其擁有的賬戶擁有完全的自主權,由合約的創建者來限制這種自主權,由合約的用戶來驗證程式的創建者是否真的這樣做了,
所有需要read或write的accounts都必須傳入entrypoint function,因為runtime知道所有要讀寫的account,因此可支持交易并行執行,
參考資料
[1] Programming on Solana - An Introduction
[2] Solana transactions
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/375978.html
標籤:區塊鏈
上一篇:虎符接入元宇宙
