您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:在 Azure 区块链服务中创建、生成和部署智能合同Tutorial: Create, build, and deploy smart contracts on Azure Blockchain Service

此教程介绍如何在 Visual Studio Code 中使用适用于 Ethereum 的 Azure 区块链开发工具包扩展,在 Azure 区块链服务上创建、生成和部署智能合同。In this tutorial, use the Azure Blockchain Development Kit for Ethereum extension in Visual Studio Code to create, build, and deploy a smart contract on Azure Blockchain Service. 还可以使用开发工具包通过事务执行智能合同函数。You also use the development kit to execute a smart contract function via a transaction.

你将使用适用于 Ethereum 的 Azure 区块链开发工具包执行以下操作:You use Azure Blockchain Development Kit for Ethereum to:

  • 创建智能合同Create a smart contract
  • 部署智能合同Deploy a smart contract
  • 通过事务执行智能合同函数Execute a smart contract function via a transaction

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

在 Windows 上,node-gyp 模块需要使用已安装的 C++ 编译器。On Windows, an installed C++ compiler is required for the node-gyp module. 可以使用 MSBuild 工具:You can use the MSBuild tools:

  • 如果安装了 Visual Studio 2017,则将 npm 配置为通过命令 npm config set msvs_version 2017 -g 使用 MSBuild 工具If Visual Studio 2017 is installed, configure npm to use the MSBuild tools with the command npm config set msvs_version 2017 -g
  • 如果安装了 Visual Studio 2019,则为 npm 设置 MSBuild 工具路径。If Visual Studio 2019 is installed, set the MS build tools path for npm. 例如: npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"For example, npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
  • 如果都不是,则在提升的以管理员身份运行命令外壳中,使用 npm install --global windows-build-tools 安装独立的 VS Build 工具。Otherwise, install the stand-alone VS Build tools using npm install --global windows-build-tools in an elevated Run as administrator command shell.

有关 node-gyp 的详细信息,请参阅 GitHub 上的 node-gyp 存储库For more information about node-gyp, see the node-gyp repository on GitHub.

创建智能合同Create a smart contract

适用于 Ethereum 的 Azure 区块链开发工具包使用项目模板和 Truffle 工具来帮助搭建、生成和部署合同。The Azure Blockchain Development Kit for Ethereum uses project templates and Truffle tools to help scaffold, build, and deploy contracts. 在开始之前,请完成先决条件快速入门:使用 Visual Studio Code 连接到 Azure 区块链服务联盟网络Before you begin, complete the prerequisite Quickstart: Use Visual Studio Code to connect to a Azure Blockchain Service consortium network. 此快速入门介绍了如何安装和配置适用于 Ethereum 的 Azure 区块链开发工具包。The quickstart guides you through the installation and configuration of the Azure Blockchain Development Kit for Ethereum.

  1. 在 VS Code 命令面板中选择“Azure 区块链: 新建 Solidity 项目”。From the VS Code command palette, choose Azure Blockchain: New Solidity Project.
  2. 选择“创建基本项目”。 Choose Create basic project.
  3. 创建名为 HelloBlockchain 的新文件夹,然后选择“创建新的项目路径”。 Create a new folder named HelloBlockchain and Select new project path.

Azure 区块链开发工具包将为你创建并初始化一个新的 Solidity 项目。The Azure Blockchain Development Kit creates and initializes a new Solidity project for you. 基本项目包含一个示例 HelloBlockchain 智能合同,以及要生成并部署到 Azure 区块链服务中联盟成员的所有必要文件。The basic project includes a sample HelloBlockchain smart contract and all the necessary files to build and deploy to your consortium member in Azure Blockchain Service. 创建该项目可能需要几分钟时间。It may take several minutes for the project to be created. 可以选择 Azure 区块链的输出来监视 VS Code 终端面板中的进度。You can monitor the progress in VS Code's terminal panel by selecting the output for Azure Blockchain.

项目结构类似于以下示例:The project structure looks like the following example:

Solidity 项目

生成智能合同Build a smart contract

智能合同位于项目的 contracts 目录中。Smart contracts are located in the project's contracts directory. 需要先编译智能合同,然后才能将其部署到区块链。You compile smart contracts before you deploy them to a blockchain. 使用“生成合同”命令编译项目中的所有智能合同。 Use the Build Contracts command to compile all the smart contracts in your project.

  1. 在 VS Code 资源管理器侧栏中,展开项目中的“contracts”文件夹。 In the VS Code explorer sidebar, expand the contracts folder in your project.

  2. 右键单击“HelloBlockchain.sol”并从菜单中选择“生成合同”。 Right-click HelloBlockchain.sol and choose Build Contracts from the menu.

    选择“生成合同”菜单Choose Build contracts menu

Azure 区块链开发工具包使用 Truffle 来编译智能合同。Azure Blockchain Development Kit uses Truffle to compile the smart contracts.

Truffle 编译器输出

部署智能合同Deploy a smart contract

Truffle 使用迁移脚本将合同部署到 Ethereum 网络。Truffle uses migration scripts to deploy your contracts to an Ethereum network. 迁移是项目的 migrations 目录中的 JavaScript 文件。Migrations are JavaScript files located in the project's migrations directory.

  1. 若要部署智能合同,请右键单击“HelloBlockchain.sol”并从菜单中选择“部署合同”。 To deploy your smart contract, right-click HelloBlockchain.sol and choose Deploy Contracts from the menu.
  2. 在命令面板中选择 Azure 区块链联盟网络。Choose your Azure Blockchain consortium network in the command palette. 在创建项目时,该联盟区块链网络已添加到项目的 Truffle 配置文件中。The consortium blockchain network was added to the project's Truffle configuration file when you created the project.
  3. 选择“生成助记键”。 Choose Generate mnemonic. 选择文件名,并将助记键文件保存到项目文件夹中。Choose a filename and save the mnemonic file in the project folder. 例如,myblockchainmember.envFor example, myblockchainmember.env. 助记键文件用于为区块链成员生成 Ethereum 私钥。The mnemonic file is used to generate an Ethereum private key for your blockchain member.

Azure 区块链开发工具包使用 Truffle 执行迁移脚本,以将合同部署到区块链。Azure Blockchain Development Kit uses Truffle to execute the migration script to deploy the contracts to the blockchain.

已成功部署合同

调用合同函数Call a contract function

HelloBlockchain 合同的 SendRequest 函数将更改 RequestMessage 状态变量。The HelloBlockchain contract's SendRequest function changes the RequestMessage state variable. 可通过事务更改区块链网络的状态。Changing the state of a blockchain network is done via a transaction. 可以使用 Azure 区块链开发工具包智能合同交互页通过事务调用 SendRequest 函数。You can use the Azure Blockchain Development Kit smart contract interaction page to call the SendRequest function via a transaction.

  1. 若要与智能合同交互,请右键单击“HelloBlockchain.sol”,并从菜单中选择“显示智能合同交互页”。 To interact with your smart contract, right-click HelloBlockchain.sol and choose Show Smart Contract Interaction Page from the menu.

    从菜单中选择“显示智能合同交互页”

  2. 在交互页中可以选择已部署的合同版本、调用函数、查看当前状态和查看元数据。The interaction page allows you to choose a deployed contract version, call functions, view current state, and view metadata.

    示例智能合同交互页

  3. 若要调用智能合同函数,请选择合同操作并传递参数。To call smart contract function, select the contract action and pass your arguments. 选择 SendRequest 合同操作,并为 requestMessage 参数Choose SendRequest contract action and enter Hello, Blockchain! 并输入 Hello, Blockchain!for the requestMessage parameter. 选择“执行”以通过事务调用 SendRequest 函数。 Select Execute to call the SendRequest function via a transaction.

    执行 SendRequest 操作

处理该事务后,interaction 节会反映状态更改。Once the transaction is processed, the interaction section reflects the state changes.

合同状态更改

SendRequest 函数设置 RequestMessageState 字段。The SendRequest function sets the RequestMessage and State fields. RequestMessage 的当前状态是传递的参数 Hello, BlockchainThe current state for RequestMessage is the argument you passed Hello, Blockchain. State 字段值保留为 RequestThe State field value remains Request.

清理资源Clean up resources

如果不再需要,可以通过删除创建区块链成员先决条件快速入门中创建的 myResourceGroup 资源组来删除资源。When no longer needed, you can delete the resources by deleting the myResourceGroup resource group you created in the Create a blockchain member prerequisite quickstart.

若要删除资源组,请执行以下操作:To delete the resource group:

  1. 在 Azure 门户中,导航至左侧导航窗格中的“资源组” ,然后选择要删除的资源组。In the Azure portal, navigate to Resource group in the left navigation pane and select the resource group you want to delete.
  2. 选择“删除资源组” 。Select Delete resource group. 输入资源组名称确认删除并选择“删除” 。Verify deletion by entering the resource group name and select Delete.

后续步骤Next steps

在本教程中,你已使用 Azure 区块链开发工具包创建了一个示例 Solidity 项目。In this tutorial, you created a sample Solidity project using Azure Blockchain Development Kit. 你生成并部署了一个智能合同,然后通过托管在 Azure 区块链服务中的区块链联盟网络上的事务调用了一个函数。You built and deployed a smart contract then called a function via a transaction on a blockchain consortium network hosted on Azure Blockchain Service.