聚焦资讯
服务于区块链创业者

rust开发solana合约

chatGPT账号

本地使用rust开发solana合约,并且部署上线,本地使用TypeScript与合约交互

  1. 创建cargo lib项目
  2. 编译项目为动态库(.so)
  3. 部署合约上链

创建cargo lib项目

cargo new --lib program-solana

安装扩展

cd program-solana
cargo add borsh  # 安装开源包,二进制编码包
cargo add solana-program # solana开发包

编写合约

use solana_program::{
    account_info::AccountInfo,
    entrypoint,
    entrypoint::ProgramResult,
    pubkey::Pubkey,
    msg,
};

// declare and export the program's entrypoint
entrypoint!(process_instruction);

// program entrypoint's implementation
pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8]
) -> ProgramResult {
    // log a message to the blockchain
    msg!("Hello, world!");

    // gracefully exit the program
    Ok(())
}

编译rust合约为动态库(.so文件)

在cargo.toml添加

[lib]
name = "program_solana"
crate-type = ["cdylib", "lib"]

编译合约

cargo build-bpf  # 会在target/deploy/program_solana.so文件

部署合约上链

solana program deploy target/deploy/program_solana.so
Program Id: 5HHcKZifkrLLXLasmJCrV738GkXWTbw7UWbka3xH8Gff

import solanaWeb3 from '@solana/web3.js';
import fs from 'fs';

const TESTNET_URL = "https://api.devnet.solana.com"
const TOKEN_PROGRAM_ID = new solanaWeb3.PublicKey('5HHcKZifkrLLXLasmJCrV738GkXWTbw7UWbka3xH8Gff');

const keyPairJson = JSON.parse(fs.readFileSync('/Users/admin/.config/solana/id.json', 'utf-8'));
const payer = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(keyPairJson));
// 创建连接和相关的密钥对
const connection = new solanaWeb3.Connection(TESTNET_URL);

class HelloProgram {
    public ProgramId:solanaWeb3.PublicKey
    constructor() {
        this.ProgramId = TOKEN_PROGRAM_ID
    }
    public createHelloData() :Buffer {
        return Buffer.from([0]);
    }
}

async function main() {
    // 创建交易
    const transaction = new solanaWeb3.Transaction();
    console.log("address:", payer.publicKey.toBase58());
     // 创建指令
    const instructionData =  new HelloProgram().createHelloData(); // instructionId 是一个标识方法的数字,args 是方法参数的字节表示。需要根据实际的智能合约进行构造。
    const instruction = new solanaWeb3.TransactionInstruction({
        keys: [{ pubkey: payer.publicKey, isSigner: false, isWritable: true }],
        programId: TOKEN_PROGRAM_ID,
        data: instructionData, 
     });
    // 将指令添加到交易中
    transaction.add(instruction);

     // 为交易签名
     const { blockhash,lastValidBlockHeight } = await connection.getLatestBlockhash("finalized");
     transaction.recentBlockhash = blockhash
     transaction.sign(payer);

      // 发送交易
    const txid = await connection.sendRawTransaction(transaction.serialize());
    console.log('Transaction sent:', txid);
}

main();

pdf+视频Solana链SOL发币教程及多模式组合合约源代码下载:

Solana链SOL发币(合约部署、开源、锁仓、LP、参数配置、开发、故障处理、工具使用)教程下载:

多模式(燃烧、回流指定营销地址、分红本币及任意币种,邀请推广八代收益,LP加池分红、交易分红、复利分红、NFT分红、自动筑池、动态手续费、定时开盘、回购)组合合约源代码下载:

pdf+视频Solana链SOL发币教程及多模式组合合约源代码下载地址:

此内容仅供注册用户可见,请登录!

添加VX或者telegram获取全程线上免费指导

免责声明

发文时比特币价格:$64249

免责声明:

本文不代表行至网立场,且不构成投资建议,请谨慎对待。用户由此造成的损失由用户自行承担,与行至网没有任何关系;

行至网不对网站所发布内容的准确性,真实性等任何方面做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,行至网对项目的真实性,准确性等任何方面均不做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,行至网不对其构成任何投资建议,用户由此造成的损失由用户自行承担,与行至网没有任何关系;

行至区块链研究院声明:行至区块链研究院内容由行至网发布,部分来源于互联网和行业分析师投稿收录,内容为行至区块链研究院加盟专职分析师独立观点,不代表行至网立场。

chatGPT账号
赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权,未经允许不得转载。
文章名称:《rust开发solana合约》
文章链接:https://www.xingzhi.io/definance/30889.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

服务于区块链创业者

业务范围商务合作

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的区块链世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录后才能复制或下载网站内容