百度超级链XuperChain部署Solidity合约

环境:Ubuntu20
xuperchain: 5.1.0
go 1.17

前言

这篇文章我们使用xuperchain来进行部署合约到链上,请先启动节点链子,其中一条链子的默认端口号为37101

root@192-168-19-133:~# netstat -tunlp | grep chain
tcp        0      0 127.0.0.1:47101         0.0.0.0:*               LISTEN      53492/xchain
tcp        0      0 127.0.0.1:34499         0.0.0.0:*               LISTEN      53492/xchain
tcp6       0      0 :::37101                :::*                    LISTEN      53492/xchain
tcp6       0      0 :::37200                :::*                    LISTEN      53492/xchain
tcp6       0      0 :::37301                :::*                    LISTEN      53492/xchain

这里为了方便,我使用一条链子来操作

准备配置环境和编辑器

我们这次部署一个HelloWorld.sol合约

pragma solidity ^0.6.10;
contract HelloWorld{
    
    
    
    
    string private name;
    
    constructor() public{
    
    
        name = "Hello XuperChain";
    }
    
    
    function getName() view public returns(string memory){
    
    
        return name;
    }
    
    function setName(string memory _name) public {
    
    
        name=  _name;
    }
    }
  • 查看配置文件contract.yaml 文件,确保evm合约功能开启
    文件在xupchrchain根目录的conf文件夹里面
# evm合约配置
evm:
  driver: "evm"
  enable: true  # 这里要设置为true
  • 下载编辑器,我们需要将sol文件转换成bin和abi文件,这时候就需要编辑器了
 apt install python3-pip

 pip3 install solc-select

solc-select install           // 查询可以安装的版本
solc-select install 0.6.10     // 安装需要的版本
solc-select versions          // 查看当前已有的版本及正在使用的版本
solc-select use 0.6.10      // 选择自己需要的版本
solc --version 
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain# solc-select use 0.6.10
Switched global version to 0.6.10
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain# solc --version
solc, the solidity compiler commandline interface
Version: 0.6.10+commit.00c0fcaf.Linux.g++

然后我们进入output文件夹,并且将HelloWorld.sol 放到该目录下,然后在此目录执行编译sol文件

root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# solc --bin --abi HelloWorld.sol -o .
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> HelloWorld.sol
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# ll
总用量 56
drwxrwxrwx  9 root root 4096 919 14:24 ./
drwxr-xr-x 13 root root 4096 918 10:28 ../
drwxrwxrwx  2 root root 4096 918 10:29 bin/
drwxrwxrwx  2 root root 4096 918 10:29 conf/
-rwxrwxrwx  1 root root 4316 918 10:29 control.sh*
drwxrwxrwx  6 root root 4096 918 10:29 data/
-rw-r--r--  1 root root  354 919 14:24 HelloWorld.abi
-rw-r--r--  1 root root 2124 919 14:24 HelloWorld.bin
-rw-r--r--  1 root root  339 919 14:21 HelloWorld.sol
drwxrwxrwx  2 root root 4096 919 14:00 logs/
drwxrwxrwx  2 root root 4096 918 10:29 tmp/

可以看到多了个bin和abi文件

部署合约

部署合约的操作需要由合约账号完成,部署操作同样需要支付手续费,操作前需要确保合约账号下有足够的余额。
我们创建一个合约账户 ( XC1111111111111112@xuper)
bin/xchain-cli account new --account 1111111111111112--fee 2000

root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# bin/xchain-cli account new --account 1111111111111112--fee 2000
contract response:
        {
    
    
            "pm": {
    
    
                "rule": 1,
                "acceptValue": 1.0
            },
            "aksWeight": {
    
    
                "TeyyPLpp9L7QAcxHangtcHTu7HUZ6iydY": 1.0
            }
        }

The gas you cousume is: 1000
The fee you pay is: 2000
Tx id: 7e42150b9b539480dbe70f4388aa2c2b1436009898f210a14ec0e3704cf043db
account name: XC1111111111111112@xuper

然后转入一笔前给这个合约账户

root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# bin/xchain-cli transfer --to XC1111111111111112@xuper --amount 100000 -H 127.0.0.1:37101
fd75ebe4bbdba30868c5b9cfd09cec987556f14d4d383797d54ef8900e5dbf19
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# bin/xchain-cli account balance XC1111111111111112@xuper -H 127.0.0.1:37101
100000

可以看到合约账户有100000的金额,足够我们部署合约了

  • 部署合约
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# bin/xchain-cli evm deploy --account XC1111111111111112@xuper --cname helloworld1  --fee 5200  HelloWorld.bin  --abi HelloWorld.abi
contract response:
The gas you cousume is: 1292
The fee you pay is: 5200
Tx id: d78c35815f0aa913750b9bbdb962038251f1f05d65998d8c0f7b867d7c0c48ea

参数作用 evm deploy :部署solidity
–acount : 使用什么合约账户部署
–cname : 部署在链子上的合约名
–fee : 这次部署交易的费用【这次交易需要1292,但我们一次性支付必须大于等于这个费用,这次我们提供5200费用来部署,绰绰有余】
后面就是提供bin和abi文件的位置

这样就是部署成功合约,然后我们尝试调用一下合约

调用合约

  • setName 方法
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# bin/xchain-cli evm invoke --method setName  -a '{"_name":"Hello solidity "}' helloworld1  --fee  1200
contract response: []
The gas you cousume is: 129
The fee you pay is: 1200
Tx id: 2f755a26469f7301053fa6642b2ce92196f5c2f1860717541da814a4585f8bb2

使用invoke 参数表明是交易类型的调用, --method是方法名,-a 是参数的设置,helloworld1是合约名,–fee是这次调用方法的花费

  • getName 方法
root@192-168-19-133:/media/rocfile/xuperfile/flight/xuperchain/output# bin/xchain-cli  evm query --method getName  helloworld1
contract response: [{
    
    "0":"Hello solidity "}]

使用query参数表明是查询类型的调用,–method是方法名,helloworld1是合约名。

结语

使用xchain-cli命令行部署合约的操作还是很简单的。可能和fisco有一点区别,fisco偏向使用console控制台进行交互式命令操作

猜你喜欢

转载自blog.csdn.net/weixin_52865146/article/details/133025867