区块链 xuperchain 命令行 部署使用EVM 合约 教程
一、创建合约账户
# 16位数字组成的字符串
./xchain-cli account new --account 1111111111111111
# 增加fee参数,重新执行
./xchain-cli account new --account 1111111111111111 --fee 1001
命令运行后就会调用xchain的系统合约功能 NewAccount
创建一个名为 XC1111111111111111@xuper
(如果链名字为xuper)的账号
给该账户充钱,以便对后面的合约进行部署
./xchain-cli transfer --to XC1111111111111111@xuper --amount 100000000000 --keys data/keys/
./xchain-cli account balance XC1111111111111111@xuper -H 127.0.0.1:37101
二、开启evm合约功能
1. 修改每个节点的conf/xchain.yaml
确保evm合约功能开启
vim conf/xchain.yaml
2. 重启每个节点
暂时没有停止命令,所以使用
ps -ef | grep xchain
查找到进程号,然后kill。
本文直接采用重启机器。
然后,启动每个节点
nohup ./xchain &
等一会确认每个节点是否连上
./xchain-cli status -H 192.168.92.129:37101
三、编译
1. 安装solc编译器
请参见https://solidity-cn.readthedocs.io/zh/latest/installing-solidity.html。
sudo snap install solc
solc --version
// solc, the solidity compiler commandline interface
// Version: 0.5.9+commit.c68bc34e.Darwin.appleclang
" class="reference-link">
2. 编写智能合约
我们以contractsdk/evm/example中的counter合约为例
pragma solidity >=0.0.0;
contract Counter {
address owner;
mapping (string => uint256) values;
constructor() public{
owner = msg.sender;
}
function increase(string memory key) public payable{
values[key] = values[key] + 1;
}
function get(string memory key) view public returns (uint) {
return values[key];
}
function getOwner() view public returns (address) {
return owner;
}
}
3. 编译合约
cd ../core/contractsdk/evm/example/counter
// 通过solc编译合约源码
solc --bin --abi Counter.sol -o .
// 合约二进制文件和abi文件分别存放在当前目录下,Counter.bin和Counter.abi
四、部署合约
cd ~/xuper/xuperchain/output
./xchain-cli evm deploy --account XC1111111111111111@xuper --cname counterevm --fee 5200000 ../core/contractsdk/evm/example/counter/Counter.bin --abi ../core/contractsdk/evm/example/counter/Counter.abi
--abi Counter.abi
:表示部署需要使用的abi文件,用于合约方法参数编解码- ``-a ``:如果合约需要构造函数,通过-a进行指定。与c++、golang等合约的部署和调用方式相同。
counterevm为自定义的合约的名字,后面查询时也是使用这个名字
五、合约调用
调用solidity合约。通过合约名直接发起合约调用和查询。
# 调用solidity合约,increase方法,counterevm为合约名
./xchain-cli evm invoke --method increase -a '{"key":"test"}' counterevm --fee 22787517
# 调用solidity合约,get方法,counterevm为合约名
./xchain-cli evm query --method get -a '{"key":"test"}' counterevm
# 调用结果,其中0表示返回值的次序,1为返回值
# key,value: 0 1
参考
https://xuper.baidu.com/n/xuperdoc/advanced_usage/contract_accounts.html
还没有评论,来说两句吧...