当前位置: 首页 > wzjs >正文

儿童玩具网站模板青岛今天发生的重大新闻

儿童玩具网站模板,青岛今天发生的重大新闻,结构设计网站推荐,ipv6地址可以做网站吗简介:通过 IDL 生成代码 调用 anchor 智能合约。 全网首发 使用 anchor 可以快速开发 solana 上面的智能合约 , 在本案例中我们 先使用 anchor 创建一个只能合约( 多个函数方法)。 部署到 dev 链上。 通过 anchor 的 IDL 生成 代码…

简介:通过 IDL 生成代码 调用 anchor 智能合约。 全网首发

        使用 anchor 可以快速开发 solana 上面的智能合约 , 在本案例中我们 先使用 anchor 创建一个只能合约( 多个函数方法)。 部署到 dev 链上。 通过 anchor 的 IDL 生成 代码, ( 我这边是go 框架代码这里别的语言也是可以的), 然后编写代码 调用 通过 IDL 生成的代码 来 调用 anchor 智能合约的函数

1.编写 anchor 只能合约 部署上链

1.1 anchor 智能合约的编写: 

◒:案例中使用 rust 编写 anchor 只能合约,一共四个方法,用户操作 pad 账户中的一个数值

  • initialize  初始化函数  只能调用一次

  • reset 重置为一个指定的值,可多次调用

  • increment 累加 可多次调用

  • subtract 累减 可多次调用

use anchor_lang::prelude::*;// This is your program's public key and it will update
// automatically when you build the project.
declare_id!("HKvWCsAhzhXRfj8zNhNrofjV3dDjSmm42EguMKK3X5n");#[program]
pub mod anchor_counter {use super::*;//初始化pub fn initialize(ctx: Context<Initialize>, init_data: u64) -> Result<()> {msg!("initialize from: {:?}", ctx.program_id);//直接给pad 账户复制ctx.accounts.counter.count = init_data;Ok(())}//重置 数据到指定 值pub fn reset(ctx: Context<UpdateCount>, init_data: u64) -> Result<()> {let counter = &mut ctx.accounts.counter;msg!("increment from: {:?}", ctx.program_id);msg!("previous counter: {:?}", counter.count);counter.count = init_data;//counter.count -= 2;msg!("after counter: {:?}", counter.count);Ok(())}//累加pub fn increment(ctx: Context<UpdateCount>) -> Result<()> {let counter = &mut ctx.accounts.counter;msg!("increment from: {:?}", ctx.program_id);msg!("previous counter: {:?}", counter.count);counter.count = counter.count.checked_add(1).unwrap();//counter.count -= 2;msg!("after counter: {:?}", counter.count);Ok(())}//累减pub fn subtract(ctx: Context<UpdateCount>, count: u64) -> Result<()> {let counter = &mut ctx.accounts.counter;msg!("increment from: {:?}", ctx.program_id);msg!("previous counter: {:?}", counter.count);//counter.count = counter.count.checked_add(1).unwrap();counter.count -= count;msg!("after counter: {:?}", counter.count);Ok(())}
}#[derive(Accounts)]
pub struct Initialize<'info> {#[account(mut)]// user 调用者 签名者, 是否 mut有权限修改信息pub user: Signer<'info>,// counter 数据账户  pda 派生#[account(init,seeds = [b"counter_seed"],bump,payer = user,space = 8 + 8)]pub counter: Account<'info, Counter>,// 程序账户pub system_program: Program<'info, System>,
}#[derive(Accounts)]
pub struct UpdateCount<'info> {// user 调用者 签名者pub user: Signer<'info>,#[account(mut)]pub counter: Account<'info, Counter>,
}#[account]
pub struct Counter {count: u64,
}
 1.2 部署智能合约。  导出 IDL 文件 

2.通过 anchor 智能合约 IDL 文件,生成代码

导出 anchor 智能合约的 IDL 文件我们可以 得到一个  idl.json 文件。因为我空间选取的是 go,所以我这边需要使用  anchor-go 库, 来把  idl.json 配置文件 生成一个 go 脚本。 

github.com/gagliardetto/anchor-go

 go run github.com/gagliardetto/anchor-go --src=idl.json

 运行上面命令。就会在当前项目中生成一个  generated 文件夹  以ancho 智能合约 项目名为 文件夹的目录。

3.使用 IDL 生成的代码,代用智能合约

这里直接值 使用生成的代码 构造合约,然后调用,调用完我们通过 区块链浏览器查看 发现数据已经变化了。 说明调用时成功的  

package mainimport ("anchor-go/generated/anchor_counter""fmt""github.com/gagliardetto/solana-go""github.com/gagliardetto/solana-go/rpc"confirm "github.com/gagliardetto/solana-go/rpc/sendAndConfirmTransaction""github.com/gagliardetto/solana-go/rpc/ws""golang.org/x/net/context"
)//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>func main() {// 创建 RPC 客户端rpcClient := rpc.New(rpc.DevNet_RPC)wsClient, err := ws.Connect(context.Background(), rpc.DevNet_WS)if err != nil {panic(err)}response, err := rpcClient.GetVersion(context.TODO())if err != nil {panic(err)}fmt.Println("version", response.SolanaCore)// 交易签名账户 密钥对fromPrivateKey, err := solana.PrivateKeyFromBase58("你 anchor 所属 钱包 私钥") // 私钥if err != nil {panic(err)}fromPublicKey := fromPrivateKey.PublicKey()// 获取最新的区块哈希recent, err := rpcClient.GetLatestBlockhash(context.TODO(), rpc.CommitmentFinalized)if err != nil {panic(err)}//anchor 只能合约 地址anchorID := solana.MustPublicKeyFromBase58("HKvWCsAhzhXRfj8zNhNrofjV3dDjSmm42EguMKK3X5n")anchor_counter.SetProgramID(anchorID)// 通过随机因子 获取 PDA 账号padPublicKey, _, _ := solana.FindProgramAddress([][]byte{[]byte("counter_seed")}, anchorID)//调用 anchor 智能合约的  累加方法transferInstruction := anchor_counter.NewIncrementInstruction(fromPublicKey, padPublicKey)//调用 anchor 智能合约的  重置方法//transferInstruction := anchor_counter.NewResetInstruction(2025, fromPublicKey, padPublicKey)// 创建交易tx, err := solana.NewTransaction([]solana.Instruction{transferInstruction.Build()},recent.Value.Blockhash,solana.TransactionPayer(fromPublicKey),)if err != nil {panic(err)}fmt.Println(tx)// 签名交易_, err = tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {if fromPrivateKey.PublicKey().Equals(key) {return &fromPrivateKey}return nil},)if err != nil {panic(fmt.Errorf("unable to sign transaction: %w", err))}//发送并等待交易确认sig, err := confirm.SendAndConfirmTransaction(context.TODO(),rpcClient,wsClient,tx,)if err != nil {panic(err)}fmt.Println("Transaction confirmed:", sig)
}
  •  beta.solpg.io 编辑器:

  •  区块链浏览器:

http://www.dtcms.com/wzjs/432726.html

相关文章:

  • 网站建设目标自己做一个网站需要多少钱
  • 微信音乐做mp3下载网站sem专业培训公司
  • 网站做的关键词被屏蔽网络推广怎么做才有效
  • 邵阳做网站哪个公司好seo门户网站优化
  • 光环时讯网站百度霸屏全网推广
  • 泸州做网站的公司seo搜索引擎优化期末考试
  • 基础网站建设公司常用的网络推广方式有哪些
  • 郴州建设工程信息网站seo推广宣传
  • 上海青浦房地产网站建设免费涨1000粉丝网站
  • 成都h5网站建设百度引擎入口
  • 晋城企业网站建设公司重庆seo扣费
  • 租二级目录做网站南京网站设计公司大全
  • 游戏直播网站怎么做网站seo优化方案
  • 外贸自己做网站网站查询域名ip
  • 做网站和推广找哪家好商城推广
  • 百度收录提交之后如何让网站更快的展示出来免费推广网站大全
  • 北京商业网站建设关键词优化seo公司
  • 免费看舆情网站专业seo网络营销公司
  • 如何用 python 做网站百度提交入口地址在哪
  • icp网站备案查询查域名的网址
  • 为政府做网站的公司百度知道下载
  • 洛阳网站改版维护公司怎么投放网络广告
  • 河北手机网站建设云南新闻最新消息今天
  • 二维码的网站如何做seo推广技术培训
  • 微网站自己怎么做的百度人工在线客服
  • 四川成都私人网站建设南京seo外包平台
  • 微网站和h5有什么区别免费的seo优化工具
  • 网站搭建流程负责人黄页网站推广服务
  • app开发制作网站平台软文推广文章范文
  • 商务网站建设策划书的格式百度在线翻译