vnt 合约
vnt 简称维特,采用聚合链架构,打造分布式智能价值网络。为多种资产授权、交易、流转提供低廉的资产服务,实现数字资产的高效流转。
创建并运行 debian10 镜像
docker run --name vnt --restart always -itd debian:10
进入 debian 终端
docker exec -it vnt /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 npm nano
安装 go1.14
cd /usr/local/ && curl -O https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
tar -xvf go1.14.2.linux-amd64.tar.gz && rm go1.14.2.linux-amd64.tar.gz
ln go/bin/go bin/go && ln go/bin/gofmt bin/gofmt && go env -w GOPROXY=https://goproxy.cn
安装 vnt
git clone https://github.com/vntchain/go-vnt ~/go/src/github.com/vntchain/go-vnt --depth 1
cd ~/go/src/github.com/vntchain/go-vnt/cmd/gvnt && go install .
echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc
配置和启动一个测试节点
1、生成一个见证人账户,记住账户地址和密码 vnt
gvnt account new --datadir ~/vntnode
2、生成初始见证人的 p2p 地址,会打印在终端上,如:/ip4/127.0.0.1/tcp/30303/ipfs/1kHQwhd4kSVunUVPSbUH8av9FKLcrDYJDopjrwBYz9a9SDv,按 ctrl+ c 结束
gvnt --datadir ~/vntnode
3、移除临时数据 gvnt 目录,否则使用配置文件初始化节点时会失败
rm -rf ~/vntnode/gvnt
4、使用前 2 步获得的账号和 p2p 地址创建创世块配置文件 dpos.json
nano ~/vntnode/dpos.json
修改三个字段:
- witnessesUrl 初始见证人 p2p 地址列表
- alloc 初始见证人余额分配
witnesses 初始见证人列表
模板:{
"config": { "chainId": 1012, "dpos": { "period": 2, "witnessesNum": 1, "witnessesUrl": [ "/ip4/127.0.0.1/tcp/30303/ipfs/1kHQwhd4kSVunUVPSbUH8av9FKLcrDYJDopjrwBYz9a9SDv" ] } }, "timestamp": "0x5c2a3d00", "extraData": "0x", "gasLimit": "0x47b760", "difficulty": "0x1", "coinbase": "0x0000000000000000000000000000000000000000", "alloc": { "0a85b5e48a789edaa73a7c46e3396210c255df8d": { "balance": "0x200000000000000000000000000000000000000000000000000000000000000" } }, "witnesses": [ "0x0a85b5e48a789edaa73a7c46e3396210c255df8d" ], "number": "0x0", "gasUsed": "0x0", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
4、使用 dpos.json 初始化和启动节点,注意开启 rpc 支持
gvnt init ~/vntnode/dpos.json --datadir ~/vntnode
gvnt --datadir ~/vntnode --rpc --rpcport 8546 --rpcapi admin,personal,rpc,core,dpos,net,vnt console
5、手动开启关闭挖矿
personal.unlockAccount(core.coinbase, "", 3153600000) //中间参数是账户密码,最后一个参数指解锁的秒数
bp.start()
bp.stop()
启动过程中注意见证人账户地址和密码以及开启的 rpc 端口,部署调用合约需要用到,在创世区块给见证人账户地址发钱用于部署合约花费
wasm 编译器安装与编译
直接使用维特提供的 docker 版编译器,不能在容器 shell 里操作,新建一个本地 shell,快捷键 ctrl+shift+t
docker pull vntchain/bottle:0.6.1
docker run --rm -v <contract directory>:/tmp vntchain/bottle:0.6.1 compile -code /tmp/<contract file name>
下载 erc20 代码
git clone https://github.com/ooozws/bottle-contract-template ~/erc20 --depth 1
编译 wasm 字节码,合约目录要使用绝对路径, 编译结果在目录~/erc20/contracts/output 下, 如果要查看需要更改权限(docker 容器权限问题)
docker run --rm -v ~/erc20/contracts:/tmp vntchain/bottle:0.6.1 compile -code /tmp/Erc20.c
sudo chmod +rx -R ~/erc20/contracts/output
复制编译的 wasm 到容器 vnt 容器
docker cp ~/erc20/contracts/output vnt:/root
部署调用合约 ,切换到 vnt 容器 shell,使用类似 web3js 的 vnt.js, 部署合约需要用到 compress 文件和 abi 文件
安装 vnt.js 和进入 node 控制台
cd ~/output && npm install --save https://github.com/vntchain/vnt.js.git && node
输入执行以下示例命令
var Vnt = require("vnt")
var fs = require("fs")
var vnt = new Vnt()
var sleep = () => new Promise((res) => setTimeout(res, 2000))
vnt.setProvider(new vnt.providers.HttpProvider("http://127.0.0.1:8546"))
var acct = vnt.core.accounts[0]
var abi = JSON.parse(fs.readFileSync('TokenERC20.abi', 'utf-8'))
var code = '0x' + fs.readFileSync('TokenERC20.compress').toString('hex');
async function deploy() {
// 使用abi初始化一个合约对象
var contract = vnt.core.contract(abi)
var deployContract = contract.new(
1000000000,
"bitcoin",
"BTC",
{ from: acct, data: code, gas: 4000000 }
)
await sleep()
return deployContract
}
(async () => {
vnt.personal.unlockAccount(acct, '')
var contract = await deploy()
console.log("contract addr:",contract.address)
r = contract.GetTokenName.call({ from: acct })
console.log("TokenName", r.toString())
r = contract.GetTokenName.sendTransaction({ from: acct })
console.log("tx hash", r.toString())
})()