ontology 合约

星期五, 6月 12, 2020 | 2分钟阅读 | 更新于 星期五, 9月 18, 2020

@

ontology简称本体,是新一代公有基础链项目与分布式信任协作平台,提供智能合约支持。

创建并运行debian10镜像

docker run --name onto --restart always -p 20334-20336:20334-20336 -itd debian:10

进入debian终端

docker exec -it onto /bin/bash

换源(加速下载包)和更新

/bin/bash -c 'echo -e "\
deb http://mirrors.163.com/debian/ buster main non-free contrib\n\
deb http://mirrors.163.com/debian/ buster-updates main non-free contrib\n\
deb http://mirrors.163.com/debian/ buster-backports main non-free contrib\n\
deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib" > /etc/apt/sources.list;\
apt update;\
apt upgrade -y;'

安装所需的包

apt install -y curl git gcc xxd

安装go1.14,设置go代理,默认的无法访问

cd /usr/local/ && curl -O https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz
tar -xvf go1.14.4.linux-amd64.tar.gz && rm go1.14.4.linux-amd64.tar.gz
ln go/bin/go bin/go && ln go/bin/gofmt bin/gofmt && go env -w GOPROXY=https://goproxy.cn

安装ontology

git clone https://github.com/ontio/ontology ~/go/src/github.com/ontio/ontology -b v1.10.0 --depth 1
cd ~/go/src/github.com/ontio/ontology && go install .
echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc

安装rust

curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc

rustup换源,加速下载

/bin/bash -c 'echo -e "\
[source.crates-io]\n\
replace-with = "tuna"\n\
\n\
[source.tuna]\n\
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"" > $HOME/.cargo/config;'

安装wasm编译器

rustup target add wasm32-unknown-unknown
cargo install --git=https://github.com/ontio/ontio-wasm-build

拉取rust版wasm工具和示例代码

cd && git clone https://github.com/ontio/ontology-wasm-cdt-rust --depth 1

编译示例代码,编译出来的wasm文件在target/wasm32-unknown-unknown/release

cd ontology-wasm-cdt-rust && bash build.sh

生成测试钱包,需要输入密码,记住这个密码,启动节点和签名交易需要它,以123456密码为例

cd && ontology account add -d

启动测试节点,使用-p参数指定密码或者按提示输入

ontology --testmode -p 123456

在网站smartx.ont.io上部署wasm合约 smartx.ont.io是本体在线开发合约工具,支持编辑、编译、部署等一站式服务,但是目前不支持wasm合约的编辑与编译,可以部署wasm合约,还需要安装本体的钱包来签名和发送交易。

通过合约子命令部署与调用合约 生成16进制的合约代码,wasm合约是二进制形式的,本地部署不支持这种形式的,使用xxd命令转换,以前面的示例合约hello为例

xxd -p ~/ontology-wasm-cdt-rust/target/wasm32-unknown-unknown/release/helloworld.wasm | tr -d $'\n' > ~/hello

部署合约,如果成功将会打印合约地址

ontology contract deploy --gasprice 0 --gaslimit 100000000 --vmtype 3 --code hello --name hello

调用合约,下面的地址填写实际部署成功返回的合约地址

ontology contract invoke --gasprice 0 --gaslimit 100000000 --address 9fa035fbf7b10dffa1d98003c97136fa88c0323a --vmtype 3 --params string:hello

注:可以使用-p参数来预执行交易,即本地交易,不会将此交易打包到区块中去

© 2026 我的博客

🌱 Powered by Hugo with theme Dream.

关于这个博客

关于博客

记录软件开发过程的一些知识和技巧,提升自我

关于博主

除了才华一无所有~~ 薪尽自然凉~~ 强迫症患者~~ 热爱古风~~