首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Rust SDK在Aptos上执行移动脚本?

如何使用Rust SDK在Aptos上执行移动脚本?
EN

Stack Overflow用户
提问于 2022-11-15 22:03:15
回答 1查看 86关注 0票数 1

我想执行这个移动脚本,例如在sources/top_up.move

代码语言:javascript
复制
script {
    use std::signer;
    use aptos_framework::aptos_account;
    use aptos_framework::aptos_coin;
    use aptos_framework::coin;

    fun main(src: &signer, dest: address, desired_balance: u64) {
        let src_addr = signer::address_of(src);

        let balance = coin::balance<aptos_coin::AptosCoin>(src_addr);
        if (balance < desired_balance) {
            aptos_account::transfer(src, dest, desired_balance - balance);
        };
    }
}

这是调用部署在链上的coin.move模块上的函数。对于这个问题,它所做的并不是那么重要,但简而言之,它检查目标帐户的余额是否小于desired_balance,如果是的话,它将达到desired_balance

我可以通过CLI轻松地执行这个移动脚本,如下所示:

代码语言:javascript
复制
aptos move compile
aptos move run-script --compiled-script-path build/MyModule/bytecode_scripts/main.mv

甚至只是这个:

代码语言:javascript
复制
aptos move run-script --script-path sources/top_up.move

我想知道的是,我是否可以使用Rust SDK来完成这个任务?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-15 22:03:15

首先,您需要像上面所做的那样编译脚本。假设您有这样的项目布局:

代码语言:javascript
复制
src/
    main.rs
move/
    Move.toml
    sources/
        top_up.mv

您可能想进入move/并运行aptos move compile,就像前面所说的。从这里开始,您可以依赖于代码中的编译脚本(参见下面)。

完成后,下面是一个演示如何使用Rust SDK执行移动脚本的最小代码示例。

Cargo.toml

代码语言:javascript
复制
[package]
name = "my-example"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
aptos-sdk = { git = "https://github.com/aptos-labs/aptos-core", branch = "mainnet" }

src/main.rs

代码语言:javascript
复制
use aptos_sdk::crypto::ed25519::Ed25519PublicKey;
use aptos_sdk::types::transaction::authenticator::AuthenticationKey;
use aptos_sdk::{
    rest_client::Client,
    transaction_builder::TransactionFactory,
    types::{
        account_address::AccountAddress,
        chain_id::ChainId,
        transaction::{Script, SignedTransaction, TransactionArgument},
        LocalAccount,
    },
};

static SCRIPT: &[u8] =
    include_bytes!("../../move/build/MyModule/bytecode_scripts/main.mv");

fn main() -> anyhow::Result<()> {
    // Prior to the follow code we assume you've already acquired the necessary
    // information such as chain_id, the private key of the account submitting
    // the transaction, arguments for the Move script, etc.

    // Build a transaction factory.
    let txn_factory = TransactionFactory::new(chain_id);

    // Build a local representation of an account.
    let account = LocalAccount::new(
        AuthenticationKey::ed25519(&Ed25519PublicKey::from(&private_key)).derived_address()
        private_key,
        0,
    );

    // Build an API client.
    let client = Client::new("https://fullnode.mainnet.aptoslabs.com");

    // Create a builder where the payload is the script.
    let txn_builder = transaction_factory.script(Script::new(
        SCRIPT.to_vec(),
        // type args
        vec![],
        // args
        vec![
            TransactionArgument::Address(dest_address),
            TransactionArgument::U64(desired_balance),
        ],
    )));

    // Build the transaction request and sign it.
    let signed_txn = account.sign_with_transaction_builder(
        txn_builder
    );

    // Submit the transaction.
    client.submit_and_wait_bcs(&signed_transaction).await?;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74452702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档