在区块链L1层,除了以太系为主的用Solidity编写的智能合约之外,还有一类是Move语言编写的智能合约,本文以Aptos链为目标,使用Aptos CLI工具进行Move合约的编译、部署以及合约交互。
安装Aptos CLI
下载aptos-core源码
安装Aptos CLI:aptos.dev/tools/aptos…
首先,创建一个工作目录,命令行执行
mkdir first-move-module
cd first-move-module
然后,初始化Aptos:
aptos init
在命令行交互时,会出现提示:
Configuring for profile default Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet],
暂且不考虑Move合约的编写问题,先拷贝Aptos的示例合约:hello_blockchain.move和hello_blockchain_test.move到我们刚才创建的目录下(aptos-core/aptos-move/move-examples)
注意要在此目录下创建Move.toml,写法参考示例合约中的Move.toml,但需要注意的是,要把dependencies的路径指向aptos-core下的aptos-framework。有两种写法:
配置本地aptos-framework路径
[dependencies]
AptosFramework={local= "/Users/xxx/xxx/aptos-core/aptos-move/framework/aptos-framework" }
配置远程aptos-framework路径
[dependencies.AptosFramework]
git = "<https://github.com/aptos-labs/aptos-core.git>"
rev = "mainnet"
subdir = "aptos-move/framework/aptos-framework"
然后进行编译:
aptos move compile --named-addresses hello_blockchain=default
Compiling, may take a little while to download git dependencies...
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Examples
{
"Result":
[ "4c56adc7c6757b9947f459f7b676c7c8bd07ce690f082d5bb07ee3e15db34245::message" ]
}
可以运行测试合约的命令:
>> aptos move test --named-addresses hello_blockchain=default
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Examples
Running Move unit tests
[ PASS ] 0x4c56adc7c6757b9947f459f7b676c7c8bd07ce690f082d5bb07ee3e15db34245::message::sender_can_set_message
[ PASS ] 0x4c56adc7c6757b9947f459f7b676c7c8bd07ce690f082d5bb07ee3e15db34245::message_tests::sender_can_set_message
Test result: OK. Total tests: 2; passed: 2; failed: 0
{
"Result": "Success"
}
hahhah | hhhh | sss |
1233 | ||