139-Solana入门(三)

看到一篇很不错的很详细的文章

Programming on Solana - An Introduction | paulx

我们来看一下lib.rs,
首先是use导包
然后用了entrypoint!宏去声明了process_instruction方法是程序入口。entrypoints是调用程序的唯一方法。

然后我们看3个参数
program_id是现在程序的id
instruction_data是传输的数据,可以是任何东西
accounts

我们来详细说说accounts
solana程序是无状态的,不保存状态的
如果要保存状态,要使用accounts
程序本身也被存储到accounts里面,标记为executable
每个account保存着数据和sol币
每个account有一个owner
只有owner才能控制这个account,修改数据
普通account的owner就是System Program
所有的account只能被program拥有
program也被program拥有
program被存储到account里面,这些executable account被BPF loader拥有

我的nba历史前五人
1.乔丹
2.奥尼尔
3.科比
4.詹姆斯
5.邓肯

所有的要被read和write的account要被传到entrypoint里面
程序流程:
1.用户调用entrypoint
2.entrypoint把参数传到processor
3.processor让instruction.rs解码instruction_data参数
4.使用解码数据,processor来决定使用哪个processing方法
5.processor可能使用state.rs来编码或解码一个account的状态
所以说,instruction就定义了一个程序的api


然后来看下escrow程序的流程
我们有2个人,Alice和Bob,这样的话就是2个system_program的account
Alice和Bob要转账token,所以我们要用到token program
在token program里面,持有token的话需要token account
所以2种token,2个人,就是4个token account
假设我们的token是tokenX和tokenY
所以Alice有一个Xaccount,一个Yaccount
Bob也有一个Xaccount,一个Yaccount
然后我们要有一个account来存储一些escrow数据


token program
如果Alice有很多的token account,那么最好是一个private key来掌控所有的token account,那么token account的owner就设置为Alice的account
注意,这里的这个owner属性和账户的owner是不一样的
账户owner始终是program
token owner属性是token program声明在user space里的
这个属性是被编码在token account的data里面的
还有其他的属性,比如余额等等
为了区分,账户owner就叫authority
token account被创建之后,那么其实这个token account的private key就不重要了,反正是和authority关联的,比如Alice和Bob的主账户地址

我们看下account的属性
1.key        账户公钥
2.is_signer    tx是否被这个公钥sign
3.is_writable    账户是否可写
4.lamports    余额
5.data        数据
6.owner        拥有者program
7.executable    账户有loaded program
8.rent_epoch    下一次租金的时间

如果在浏览器里面看,我们还可以看到一个mint字段
mint字段就是token account是属于哪个token的
每个token都有一个mint account保存着这个token的metadata,比如supply
之后会需要这个字段去确认Alice和Bob的token account


transferring ownership
escrow program也需要一个account去持有Alice的Xtoken
一种方法是Alice创建一个临时的Xtoken账户,然后把这个Xtoken账户的authority转移给escrow program.

developers should use the data field to save data inside accounts
the token program owns token accounts which - inside their data field - hold relevant information(opens new window)
the token program also owns token mint accounts with relevant data(opens new window)
each token account holds a reference to their token mint account, thereby stating which token mint they belong to
the token program allows the authority of a token account to transfer its ownership to another address
All internal Solana internal account information are saved into fields on the account (opens new window)but never into the data field which is solely meant for user space information

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值