【CyberMiles草莓糖】如何使用Lity调用智能合约教程“Hello Lity”

快速入门

准备工作

原文链接:https://homorix.github.io/2018/10/16/手把手教你使用Lity编写和运行智能合约/

本电脑系统:macOS Mojave 10.14 / 8G 1.8 GHz Intel Core i5

本文档是Cyber Miles的高级开发语言Lity版的"Hello Word"

操作过程中有任何问题需要帮助的,见文章底部

安装好:git ,homebrew , cmake (老鸟如果已安装,请跳过)

1.1 安装homebrew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

测试brew安装是否成功:

brew help

image

1.2 安装Git:
brew install git
1.3 安装cmake:
brew install cmake

image

准备工作完成后,开始安装Lity
2.1安装lity
git clone https://github.com/CyberMiles/lity.git

image

安装完成后,依次输入

cd lity
mkdir build
source ./scripts/install_

(这里如果有问题,请看备注)

图片5.图片6

image

显示“进程已完成”后,重新新建一个标签,输入以下代码。

image
依次输入

cd build
cmake ..

image

安装成功
继续输入

make

image

检查是否成功

./lityc/lityc --help

image

2. 运行私有链(启动CyberMiles节点)

2.1.安装docker: https://docs.docker.com/docker-for-mac/install/

使用brew下载并安装

brew cask install docker

image

2.2在lauchpad中找到小?点击运行

image

运行成功小图标
附手动下载地址: https://download.docker.com/mac/stable/Docker.dmg

2.3.检查Docker安装成功
docker run hello-world

image

2.4 运行travis: https://travis.readthedocs.io/en/latest/getting-started.html#use-docker

使用docker下载travis的image

docker run --rm -v ~/volumes/local:/travis cybermiles/travis node init --home /travis

image

2.5 使用docker运行
travis
docker run --name travis -v ~/volumes/local:/travis -t -p 26657:26657 -p 8545:8545 cybermiles/travis node start --home /travis

image

2.6 新开个终端,检测链上变化:
docker logs -f travis

image

2.7 再新建一个窗口,连接cybermiles节点:
docker inspect -f '{{ .NetworkSettings.IPAddress }}' travis

image

2.8 使用docker运行本地节点
docker run --rm -it cybermiles/travis attach http://172.17.0.2:8545

image

3.1 准备账户

准备账户,在上一个窗口继续输入

cmt.accounts

或者

personal.listAccounts

image

3.2查看账户信息:
cmt.getBalance(cmt.accounts[0])

image

3.3 创建账户 CMT100为新账户的密码
personal.newAccount('CMT100')

运行截图31

3.4 给新账户转账
查看用户列表

personal.listAccounts

image

3.4 解锁账户
personal.unlockAccount("0x7eff122b94897ea5b0e2a9abf47b86337fafebdc","1234");

image

3.5给新建的账户地址转账
cmt.sendTransaction({from: '0x7eff122b94897ea5b0e2a9abf47b86337fafebdc', to: '0xc1f9378552bd55b29733f0915d8311071ed4143e', value: web3.toWei(99, 'cmt')});
3.6查看账户信息
cmt.getBalance(cmt.accounts[2])

显示“9900000000000000”

3.7 解锁自己创建的账户
personal.unlockAccount(cmt.accounts[2],’CMT100')

image

3.8编写合约代码

新建终端 在lity/build 下创建

vi hello.lity

写入内容:

pragma solidity ^0.4.23;
contract ReverseContract {
function reverse(string input) public returns(string) {
string memory output = eni(“reverse”, input);
return output;
}
}

写入内容后,保存

image

3.9 用lityc将源代码编译为字节代码

分别输入

./lityc/lityc --bin hello.lity
./lityc/lityc --abi hello.lity

image

4. 部署合约

4.1解锁账户

回到docker run 的终端窗口
输入:

personal.unlockAccount(cmt.accounts[2],'CMT100');

image

4.2定义abi变量

ABI变量内容

image

 var helloContract=web3.cmt.contract([{"constant":false,"inputs":[{"name":"input","type":"string"}],"name":"reverse","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]);

显示“undefined”

image

5.部署合约

5.1继续在连接本地节点终端窗口输入:

var hello = helloContract.new(
...    {
......      from: web3.cmt.accounts[3], 
......      data: '0x606060405260408051908101604052600b81527f68656c6c6f206d696e69510000000000000000000000000000000000000000006020820152600090805161004b92916020019061005c565b50341561005757600080fd5b6100f7565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009d57805160ff19168380011785556100ca565b828001600101855582156100ca579182015b828111156100ca5782518255916020019190600101906100af565b506100d69291506100da565b5090565b6100f491905b808211156100d657600081556001016100e0565b90565b6101b5806101066000396000f3006060604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663954ab4b28114610045575b600080fd5b341561005057600080fd5b6100586100cf565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561009457808201518382015260200161007c565b50505050905090810190601f1680156100c15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d7610177565b60008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561016d5780601f106101425761010080835404028352916020019161016d565b820191906000526020600020905b81548152906001019060200180831161015057829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a72305820703363ca4b060b28e7655f46b6f6941b09da933cfc5839e543b819068d0572db0029', 
......      gas: '4700000'
......    }, function (e, contract){
......     console.log(e, contract);
......     if (typeof contract.address !== 'undefined') {
.........          console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
.........     }
......  });

如果时间超时,需要重新解锁。

personal.unlockAccount(cmt.accounts[2],'CMT100')
5.2调用合约
hello.reverse.call("hello,atao!",{from:cmt.accounts[2]});

image

笔记:

  1. if you see this:
    Error: An unexpected error occurred during the brew link step
    The formula built, but is not symlinked into /usr/local
    Permission denied @ dir_s_mkdir - /usr/local/Frameworks
    Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks
    then do this:
    sudo chown -R $(whoami) $(brew --prefix)/*
    note the $(brew --prefix)/* …High Sierra doesn’t allow you to change permissions on /user/local directly)
    sudo install -d -o $(whoami) -g admin /usr/local/Frameworks
    Last: source ./script/install_deps.sh

有任何问题可以加QQ解决:

QQ群1:832462204
QQ群2:875719854

CM官方文档:https://travis.readthedocs.io/en/latest/getting-started.html

Lity:https://www.litylang.org/gas/

lity入门: https://lity.readthedocs.io

libENI: https://libeni.readthedocs.io/en/latest/

Github:https://github.com/CyberMiles/lity

WEB3-CMT.JS: https://cybermiles.github.io/web3-cmt.js/api/

lity开发入门指南(有视频):https://dwz.cn/qDs8XQOt

如何利用Lity创新(有视频):https://dwz.cn/B63y9P1R

Lity如何防止合约错误:https://dwz.cn/4jMiQz5q

欢迎各位开发者到Github上提问,把有价值的问题留存下来,以便后面的开发者可以快速解决问题。

草莓100,别墅靠海.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值