StarkNet 配置 protostar 部署Cairo合约
Posted 瘦身小蚂蚁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了StarkNet 配置 protostar 部署Cairo合约相关的知识,希望对你有一定的参考价值。
1、安装protostar
1.0、环境
操作系统 : Ubuntu 22.04
安装git:可参考链接中的2.4.4 VMware 安装Ubuntu22.04_瘦身小蚂蚁的博客-CSDN博客
1.1、更新软件包列表
sudo apt update
tracy@tracy-virtual-machine:~$ sudo apt update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:2 https://deb.nodesource.com/node_14.x jammy InRelease [4,563 B]
Get:3 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [720 kB]
Get:4 https://deb.nodesource.com/node_14.x jammy/main amd64 Packages [776 B]
Hit:5 http://cn.archive.ubuntu.com/ubuntu jammy InRelease
Get:6 http://cn.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:7 http://cn.archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]
Get:8 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main i386 Packages [464 kB]
Get:9 http://security.ubuntu.com/ubuntu jammy-security/main i386 Packages [277 kB]
Get:10 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [146 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/main amd64 DEP-11 Metadata [41.4 kB]
Get:12 http://security.ubuntu.com/ubuntu jammy-security/main DEP-11 48x48 Icons [13.8 kB]
Get:13 http://security.ubuntu.com/ubuntu jammy-security/main DEP-11 64x64 Icons [22.7 kB]
Get:14 http://security.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [9,016 B]
Get:15 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [693 kB]
Get:16 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [978 kB]
Get:17 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [209 kB]
Get:18 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 DEP-11 Metadata [101 kB]
Get:19 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main DEP-11 48x48 Icons [33.0 kB]
Get:20 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main DEP-11 64x64 Icons [51.3 kB]
Get:21 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [13.8 kB]
Get:22 http://cn.archive.ubuntu.com/ubuntu jammy-updates/restricted i386 Packages [27.3 kB]
Get:23 http://security.ubuntu.com/ubuntu jammy-security/restricted i386 Packages [26.9 kB]
Get:24 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [108 kB]
Get:25 http://cn.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [731 kB]
Get:26 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 c-n-f Metadata [588 B]
Get:27 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [715 kB]
Get:28 http://cn.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [114 kB]
Get:29 http://cn.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 c-n-f Metadata [588 B]
Get:30 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [897 kB]
Get:31 http://security.ubuntu.com/ubuntu jammy-security/universe i386 Packages [519 kB]
Get:32 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe i386 Packages [605 kB]
Get:33 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [179 kB]
Get:34 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [118 kB]
Get:35 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 DEP-11 Metadata [269 kB]
Get:36 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 DEP-11 Metadata [18.5 kB]
Get:37 http://security.ubuntu.com/ubuntu jammy-security/universe DEP-11 48x48 Icons [15.4 kB]
Get:38 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe DEP-11 48x48 Icons [173 kB]
Get:39 http://security.ubuntu.com/ubuntu jammy-security/universe DEP-11 64x64 Icons [24.5 kB]
Get:40 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [14.1 kB]
Get:41 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [19.4 kB]
Get:42 http://security.ubuntu.com/ubuntu jammy-security/multiverse Translation-en [4,068 B]
Get:43 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 c-n-f Metadata [228 B]
Get:44 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe DEP-11 64x64 Icons [260 kB]
Get:45 http://cn.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [18.4 kB]
Get:46 http://cn.archive.ubuntu.com/ubuntu jammy-updates/multiverse i386 Packages [3,500 B]
Get:47 http://cn.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [24.1 kB]
Get:48 http://cn.archive.ubuntu.com/ubuntu jammy-updates/multiverse Translation-en [6,312 B]
Get:49 http://cn.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:50 http://cn.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 c-n-f Metadata [444 B]
Get:51 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main i386 Packages [33.7 kB]
Get:52 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [40.7 kB]
Get:53 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main Translation-en [9,800 B]
Get:54 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main amd64 DEP-11 Metadata [8,000 B]
Get:55 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main DEP-11 48x48 Icons [6,986 B]
Get:56 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main DEP-11 64x64 Icons [10.5 kB]
Get:57 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main DEP-11 64x64@2 Icons [29 B]
Get:58 http://cn.archive.ubuntu.com/ubuntu jammy-backports/main amd64 c-n-f Metadata [392 B]
Get:59 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [19.5 kB]
Get:60 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe i386 Packages [10.7 kB]
Get:61 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [14.0 kB]
Get:62 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 DEP-11 Metadata [12.2 kB]
Get:63 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe DEP-11 48x48 Icons [4,851 B]
Get:64 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe DEP-11 64x64 Icons [10.5 kB]
Get:65 http://cn.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [392 B]
Fetched 9,192 kB in 23s (403 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
291 packages can be upgraded. Run 'apt list --upgradable' to see them.
1.2、安装software-properties-common
sudo apt install software-properties-common
安装过程中会提示 Do you want to continue? [Y/n] 输入y 回车即可
tracy@tracy-virtual-machine:~$ sudo apt install software-properties-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
systemd-hwe-hwdb
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
python3-software-properties software-properties-gtk ubuntu-advantage-tools
The following packages will be upgraded:
python3-software-properties software-properties-common software-properties-gtk
ubuntu-advantage-tools
4 upgraded, 0 newly installed, 0 to remove and 287 not upgraded.
Need to get 286 kB of archives.
After this operation, 1,635 kB disk space will be freed.
Do you want to continue? [Y/n] y
Get:1 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 ubuntu-advantage-tools amd64 27.13.6~22.04.1 [172 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 software-properties-common all 0.99.22.6 [14.1 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 software-properties-gtk all 0.99.22.6 [71.3 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 python3-software-properties all 0.99.22.6 [28.8 kB]
Fetched 286 kB in 3s (108 kB/s)
Preconfiguring packages ...
(Reading database ... 215819 files and directories currently installed.)
Preparing to unpack .../ubuntu-advantage-tools_27.13.6~22.04.1_amd64.deb ...
Unpacking ubuntu-advantage-tools (27.13.6~22.04.1) over (27.9~22.04.1) ...
Preparing to unpack .../software-properties-common_0.99.22.6_all.deb ...
Unpacking software-properties-common (0.99.22.6) over (0.99.22.2) ...
Preparing to unpack .../software-properties-gtk_0.99.22.6_all.deb ...
Unpacking software-properties-gtk (0.99.22.6) over (0.99.22.2) ...
Preparing to unpack .../python3-software-properties_0.99.22.6_all.deb ...
Unpacking python3-software-properties (0.99.22.6) over (0.99.22.2) ...
Setting up python3-software-properties (0.99.22.6) ...
Setting up ubuntu-advantage-tools (27.13.6~22.04.1) ...
Installing new version of config file /etc/apt/apt.conf.d/20apt-esm-hook.conf ...
Installing new version of config file /etc/ubuntu-advantage/help_data.yaml ...
Installing new version of config file /etc/ubuntu-advantage/uaclient.conf ...
Installing new version of config file /etc/update-motd.d/91-contract-ua-esm-status ...
Setting up software-properties-common (0.99.22.6) ...
Setting up software-properties-gtk (0.99.22.6) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for shared-mime-info (2.1-2) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for libglib2.0-0:amd64 (2.72.1-1) ...
Processing triggers for man-db (2.10.2-1) ...
1.3、将Deadsnakes PPA添加到Ubuntu系统来源列表中
sudo add-apt-repository ppa:deadsnakes/ppa
添加时会出现 Press [ENTER] to continue or Ctrl-c to cancel. 按下键盘上的回车键【Enter】即可继续。
tracy@tracy-virtual-machine:~$ sudo add-apt-repository ppa:deadsnakes/ppa
Repository: 'deb https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu/ jammy main'
Description:
This PPA contains more recent Python versions packaged for Ubuntu.
Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.
Update Note
===========
Please use this repository instead of ppa:fkrull/deadsnakes.
Reporting Issues
================
Issues can be reported in the master issue tracker at:
https://github.com/deadsnakes/issues/issues
Supported Ubuntu and Python Versions
====================================
- Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.11
- Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9 - Python3.11
- Ubuntu 22.04 (jammy) Python3.7 - Python3.9, Python3.11
- Note: Python2.7 (all), Python 3.6 (bionic), Python 3.8 (focal), Python 3.10 (jammy) are not provided by deadsnakes as upstream ubuntu provides those packages.
Why some packages aren't built:
- Note: for focal, older python versions require libssl<1.1 so they are not currently built
- Note: for jammy, older python versions requre libssl<3 so they are not currently built
- If you need these, reach out to asottile to set up a private ppa
The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported.
Packages
========
The packages provided here are loosely based on the debian upstream packages with some modifications to make them more usable as non-default pythons and on ubuntu. As such, the packages follow debian's patterns and often do not include a full python distribution with just `apt install python#.#`. Here is a list of packages that may be useful along with the default install:
- `python#.#-dev`: includes development headers for building C extensions
- `python#.#-venv`: provides the standard library `venv` module
- `python#.#-distutils`: provides the standard library `distutils` module
- `python#.#-lib2to3`: provides the `2to3-#.#` utility as well as the standard library `lib2to3` module
- `python#.#-gdbm`: provides the standard library `dbm.gnu` module
- `python#.#-tk`: provides the standard library `tkinter` module
Third-Party Python Modules
==========================
Python modules in the official Ubuntu repositories are packaged to work with the Python interpreters from the official repositories. Accordingly, they generally won't work with the Python interpreters from this PPA. As an exception, pure-Python modules for Python 3 will work, but any compiled extension modules won't.
To install 3rd-party Python modules, you should use the common Python packaging tools. For an introduction into the Python packaging ecosystem and its tools, refer to the Python Packaging User Guide:
https://packaging.python.org/installing/
Sources
=======
The package sources are available at:
https://github.com/deadsnakes/
Nightly Builds
==============
For nightly builds, see ppa:deadsnakes/nightly https://launchpad.net/~deadsnakes/+archive/ubuntu/nightly
More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/deadsnakes-ubuntu-ppa-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/deadsnakes-ubuntu-ppa-jammy.list
Adding key to /etc/apt/trusted.gpg.d/deadsnakes-ubuntu-ppa.gpg with fingerprint F23C5A6CF475977595C89F51BA6932366A755776
Hit:1 https://deb.nodesource.com/node_14.x jammy InRelease
Hit:2 http://cn.archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:4 http://cn.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://cn.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:6 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy InRelease [18.1 kB]
Get:7 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 Packages [18.5 kB]
Get:8 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main i386 Packages [6,476 B]
Get:9 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main Translation-en [4,192 B]
Fetched 47.2 kB in 6s (8,253 B/s)
Reading package lists... Done
1.4、安装python3.9
sudo apt install python3.9
安装过程中会提示 Do you want to continue? [Y/n] 输入y 回车即可
tracy@tracy-virtual-machine:~$ sudo apt install python3.9
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
systemd-hwe-hwdb
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
libpython3.9-minimal libpython3.9-stdlib python3.9-minimal
Suggested packages:
python3.9-venv binfmt-support
The following NEW packages will be installed:
libpython3.9-minimal libpython3.9-stdlib python3.9 python3.9-minimal
0 upgraded, 4 newly installed, 0 to remove and 287 not upgraded.
Need to get 5,243 kB of archives.
After this operation, 19.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 libpython3.9-minimal amd64 3.9.16-1+jammy1 [834 kB]
Get:2 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 python3.9-minimal amd64 3.9.16-1+jammy1 [2,073 kB]
Get:3 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 libpython3.9-stdlib amd64 3.9.16-1+jammy1 [1,841 kB]
Get:4 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy/main amd64 python3.9 amd64 3.9.16-1+jammy1 [495 kB]
Fetched 5,243 kB in 13s (403 kB/s)
Selecting previously unselected package libpython3.9-minimal:amd64.
(Reading database ... 215904 files and directories currently installed.)
Preparing to unpack .../libpython3.9-minimal_3.9.16-1+jammy1_amd64.deb ...
Unpacking libpython3.9-minimal:amd64 (3.9.16-1+jammy1) ...
Selecting previously unselected package python3.9-minimal.
Preparing to unpack .../python3.9-minimal_3.9.16-1+jammy1_amd64.deb ...
Unpacking python3.9-minimal (3.9.16-1+jammy1) ...
Selecting previously unselected package libpython3.9-stdlib:amd64.
Preparing to unpack .../libpython3.9-stdlib_3.9.16-1+jammy1_amd64.deb ...
Unpacking libpython3.9-stdlib:amd64 (3.9.16-1+jammy1) ...
Selecting previously unselected package python3.9.
Preparing to unpack .../python3.9_3.9.16-1+jammy1_amd64.deb ...
Unpacking python3.9 (3.9.16-1+jammy1) ...
Setting up libpython3.9-minimal:amd64 (3.9.16-1+jammy1) ...
Setting up python3.9-minimal (3.9.16-1+jammy1) ...
Setting up libpython3.9-stdlib:amd64 (3.9.16-1+jammy1) ...
Setting up python3.9 (3.9.16-1+jammy1) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
验证python3.9是否安装成功
python3.9 --version
tracy@tracy-virtual-machine:~$ python3.9 --version
Python 3.9.16
1.5、安装protostar
curl -L https://raw.githubusercontent.com/software-mansion/protostar/master/install.sh | bash
tracy@tracy-virtual-machine:~$ curl -L https://raw.githubusercontent.com/software-mansion/protostar/master/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3621 100 3621 0 0 5043 0 --:--:-- --:--:-- --:--:-- 5043
Installing protostar
Retrieving latest version from https://github.com/software-mansion/protostar...
Using version v0.9.2
Downloading protostar from https://github.com/software-mansion/protostar/releases/download/v0.9.2/protostar-Linux.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 137M 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0./dist/protostar/
./dist/protostar/libbz2.so.1.0
./dist/protostar/cairo_python_bindings/
./dist/protostar/cairo_python_bindings/cairo_python_bindings.cpython-39-x86_64-linux-gnu.so
50 137M 50 69.8M 0 0 903k 0 0:02:35 0:01:19 0:01:16 968k./dist/protostar/libgdbm.so.6
./dist/protostar/setuptools-65.5.1.dist-info/
./dist/protostar/setuptools-65.5.1.dist-info/INSTALLER
./dist/protostar/setuptools-65.5.1.dist-info/METADATA
./dist/protostar/setuptools-65.5.1.dist-info/LICENSE
./dist/protostar/setuptools-65.5.1.dist-info/WHEEL
./dist/protostar/setuptools-65.5.1.dist-info/top_level.txt
./dist/protostar/setuptools-65.5.1.dist-info/entry_points.txt
./dist/protostar/setuptools-65.5.1.dist-info/RECORD
./dist/protostar/setuptools-65.5.1.dist-info/REQUESTED
# 省略中间部分
./dist/protostar/libtinfo.so.6
./dist/protostar/eth_account-0.5.9.dist-info/
./dist/protostar/eth_account-0.5.9.dist-info/INSTALLER
./dist/protostar/eth_account-0.5.9.dist-info/METADATA
./dist/protostar/eth_account-0.5.9.dist-info/LICENSE
./dist/protostar/eth_account-0.5.9.dist-info/WHEEL
./dist/protostar/eth_account-0.5.9.dist-info/top_level.txt
./dist/protostar/eth_account-0.5.9.dist-info/RECORD
./dist/protostar/eth_account-0.5.9.dist-info/REQUESTED
Detected your preferred shell is bash and added Protostar to PATH. Run 'source /home/tracy/.bashrc' or start a new terminal session to use Protostar.
Then, run 'protostar --help'.
验证protostar是否安装成功
关闭终端并重新打开终端(我用的终端是XShell),输入下面查看protostar版本命令
protostar -v
tracy@tracy-virtual-machine:~$ protostar -v
Protostar version: 0.9.2
Cairo-lang version: 0.10.3
17:49:25 [INFO] Execution time: 1.81 s
2、protostar部署合约
2.1、创建并进入文件夹
mkdir starknet
cd starknet/
tracy@tracy-virtual-machine:~$ mkdir starknet
tracy@tracy-virtual-machine:~$ cd starknet/
tracy@tracy-virtual-machine:~/starknet$
2.2、创建protostar工程
protostar init myFirst
tracy@tracy-virtual-machine:~/starknet$ protostar init myFirst
19:32:48 [INFO] Execution time: 1.37 s
2.3、进入并查看创建的工程目录
cd myFirst
ls
tracy@tracy-virtual-machine:~/starknet$ ls
myFirst
tracy@tracy-virtual-machine:~/starknet$ cd myFirst
tracy@tracy-virtual-machine:~/starknet/myFirst$ ls
protostar.toml src tests
- protostar.toml 是一个配置文件,用于存储 protostar 命令的配置信息。它包含了每个命令的默认参数和值,使得在运行命令时可以避免每次都要手动输入参数的麻烦。
- src 目录:用于存放源代码文件
- tests 目录:用于存放测试代码文件
2.4、查看测试合约代码
在tests文件目录中默认有一个测试合约代码
cat tests/test_main.cairo
tracy@tracy-virtual-machine:~/starknet/myFirst$ ls tests
test_main.cairo
tracy@tracy-virtual-machine:~/starknet/myFirst$ cat tests/test_main.cairo
%lang starknet
from src.main import balance, increase_balance
from starkware.cairo.common.cairo_builtins import HashBuiltin
@external
func test_increase_balancesyscall_ptr: felt*, range_check_ptr, pedersen_ptr: HashBuiltin*()
let (result_before) = balance.read();
assert result_before = 0;
increase_balance(42);
let (result_after) = balance.read();
assert result_after = 42;
return ();
@external
func test_cannot_increase_balance_with_negative_value
syscall_ptr: felt*, range_check_ptr, pedersen_ptr: HashBuiltin*
()
let (result_before) = balance.read();
assert result_before = 0;
% expect_revert("TRANSACTION_FAILED", "Amount must be positive") %
increase_balance(-42);
return ();
2.5、build工程
protostar build
tracy@tracy-virtual-machine:~/starknet/myFirst$ protostar build
Building projects' contracts
Class hash for contract "main": 0x2a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc
20:05:24 [INFO] Execution time: 2.79 s
build成功后, 会生成一个build目录,查看build目录列表,会发现已为测试合约生成了2个json文件,如下所示:
tracy@tracy-virtual-machine:~/starknet/myFirst$ ls
build protostar.toml src tests
tracy@tracy-virtual-machine:~/starknet/myFirst$ ls build/
main_abi.json main.json
2.6、部署账户
如果是第一次使用protostar,需要使用protostar calculate-account-address命令部署账户,命令如下 :
protostar calculate-account-address --account-class-hash 0x025ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918 --account-address-salt 1
上面的 0x025ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918 是通过Argent X 中账户地址 -> View on StarkScan,打开浏览器查看账户地址看到的 class hash,如下图所示:
tracy@tracy-virtual-machine:~/starknet/myFirst$ protostar calculate-account-address --account-class-hash 0x025ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918 --account-address-salt 1
Address: 0x00b89a4302ea68352488fe1bcc9b0e01cd71dc4d630e5e69194c9e60084996fe
21:26:17 [INFO] Execution time: 1.53 s
2.7、配置账户私钥
export PROTOSTAR_ACCOUNT_PRIVATE_KEY=xxxxx
# xxxxx 为账户私钥
tracy@tracy-virtual-machine:~/starknet/myFirst$ export PROTOSTAR_ACCOUNT_PRIVATE_KEY=xxxxx
2.8、声明JSON合约
protostar declare ./build/main.json --account-address 0x0413Ce84f8a946D1aaf6776e485e5AD1817c441D0fe77f013f2C3e0F8b7cC95E --max-fee auto --network testnet
上面命令中的 0x0413Ce84f8a946D1aaf6776e485e5AD1817c441D0fe77f013f2C3e0F8b7cC95E 是我的Argent账户中的钱包地址,如下图所示:
tracy@tracy-virtual-machine:~/starknet/myFirst$ protostar declare ./build/main.json --account-address 0x0413Ce84f8a946D1aaf6776e485e5AD1817c441D0fe77f013f2C3e0F8b7cC95E --max-fee auto --network testnet
Declare transaction was sent.
Class hash: 0x02a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc
StarkScan https://testnet.starkscan.co/class/0x02a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc
Voyager https://goerli.voyager.online/class/0x02a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc
Transaction hash: 0x032858540b38507af71fb84ae5af7e26e4ffb67d50b99b3a0740d974fe36c907
StarkScan https://testnet.starkscan.co/tx/0x032858540b38507af71fb84ae5af7e26e4ffb67d50b99b3a0740d974fe36c907
Voyager https://goerli.voyager.online/tx/0x032858540b38507af71fb84ae5af7e26e4ffb67d50b99b3a0740d974fe36c907
21:35:31 [INFO] Execution time: 9.14 s
2.9、部署合约
protostar deploy 0x02a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc --network testnet --max-fee auto --account-address 0x0413Ce84f8a946D1aaf6776e485e5AD1817c441D0fe77f013f2C3e0F8b7cC95E
上面命令中 0x02a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc 为 步骤2.8 声明JSON合约后得到的Class hash, 而 0x0413Ce84f8a946D1aaf6776e485e5AD1817c441D0fe77f013f2C3e0F8b7cC95E 是钱包账户地址
tracy@tracy-virtual-machine:~/starknet/myFirst$ protostar deploy 0x02a5de1b145e18dfeb31c7cd7ff403714ededf5f3fdf75f8b0ac96f2017541bc --network testnet --max-fee auto --account-address 0x0413Ce84f8a946D1aaf6776e485e5AD1817c441D0fe77f013f2C3e0F8b7cC95E
Invoke transaction was sent to the Universal Deployer Contract.
Contract address: 0x004dc674d4314d4e5d64b997cc8fe5d5567b5f0f1f61705655e32da70bafd382
StarkScan https://testnet.starkscan.co/contract/0x004dc674d4314d4e5d64b997cc8fe5d5567b5f0f1f61705655e32da70bafd382
Voyager https://goerli.voyager.online/contract/0x004dc674d4314d4e5d64b997cc8fe5d5567b5f0f1f61705655e32da70bafd382
Transaction hash: 0x060c0d7449e0aa9ff5b7f94c7e7cee79a4e18f8d992a973dc59a63df59f1a830
StarkScan https://testnet.starkscan.co/tx/0x060c0d7449e0aa9ff5b7f94c7e7cee79a4e18f8d992a973dc59a63df59f1a830
Voyager https://goerli.voyager.online/tx/0x060c0d7449e0aa9ff5b7f94c7e7cee79a4e18f8d992a973dc59a63df59f1a830
21:46:41 [INFO] Execution time: 5.96 s
参考文档
以上是关于StarkNet 配置 protostar 部署Cairo合约的主要内容,如果未能解决你的问题,请参考以下文章