跳转到内容

快速入门

如果尚未安装 Aptos .NET SDK,请遵循以下任一指南开始使用。

  1. 设置 AptosClient

    添加 Aptos 命名空间并实例化 AptosClient,即可设置 Aptos 客户端。可以使用 Networks 中的预定义配置,或自行配置。

    using Aptos;
    class Program
    {
    static void Main(string[] args)
    {
    var config = new AptosConfig(Aptos.Networks.Mainnet);
    var client = new AptosClient(config);
    }
    }
  2. 查询区块链

    设置客户端后,就可以查询区块链。

    using Aptos;
    class Program
    {
    static void Main(string[] args)
    {
    var config = new AptosConfig(Aptos.Networks.Mainnet);
    var client = new AptosClient(config);
    var ledgerInfo = client.Block.GetLedgerInfo();
    Console.WriteLine(ledgerInfo.BlockHeight);
    }
    }
  3. 签名并提交交易

    要与区块链交互,需要创建签名人并构建交易。

    using Aptos;
    class Program
    {
    static void Main(string[] args)
    {
    var config = new AptosConfig(Aptos.Networks.Mainnet);
    var client = new AptosClient(config);
    // 1. Create a signer
    var signer = Account.Generate();
    // 2. Build the transaction
    var transaction = await client.Transaction.Build(
    sender: account,
    data: new GenerateEntryFunctionPayloadData(
    function: "0x1::aptos_account::transfer_coins",
    typeArguments: ["0x1::aptos_coin::AptosCoin"],
    functionArguments: [account.Address, "100000"]
    )
    );
    // 3. Sign and submit the transaction
    var pendingTransaction = client.Transaction.SignAndSubmitTransaction(account, transaction);
    // 4. (Optional) Wait for the transaction to be committed
    var committedTransaction = await client.Transaction.WaitForTransaction(pendingTransaction);
    }
    }
  4. 智能合约 view 函数

    调用 view 函数查询智能合约。

    using Aptos;
    class Program
    {
    static void Main(string[] args)
    {
    var config = new AptosConfig(Aptos.Networks.Mainnet);
    var client = new AptosClient(config);
    // Call the view function by specifying the function name, arguments, and type arguments
    var values = await client.Contract.View(
    new GenerateViewFunctionPayloadData(
    function: "0x1::coin::name",
    functionArguments: [],
    typeArguments: ["0x1::aptos_coin::AptosCoin"]
    )
    );
    // Returns a list of return values: ["Aptos Coin"]
    Console.WriteLine("APT Name: " + values[0]);
    }
    }