开发流程:
1,eos环境搭建,及adpp开发流程说明
2,创建前端源码框架,编写前端源码
3,创建智能合约目录,编写智能合约源码
4,前端调用eosjs,与智能合约交互
5,eosjs查询智能合约的table信息
6,详解eosjs调用智能合约接口
1,前端
eos使用react.js编写前端的用户界面
eosjs来与智能合约交互
eosjs实现了大多数rpc功能,常用的是合约接口调用和表数据查询
2,创建智能合约目录,编写智能合约源码
智能合约简介
使用c++进行开发,依赖boost 1.66.0版本的开源库
3,使用eosiocpp来创建一个test的临时测试合约
cd contract
root@VM-0-7-ubuntu:/home/ubuntu/eos/contracts/mytest# eosiocpp -n mytest
root@VM-0-7-ubuntu:/home/ubuntu/eos/contracts/mytest/mytest# ls
mytest.abi mytest.cpp mytest.hpp
mytest.hpp 智能合约的头文件
mytest.cpp 智能合约的源文件
mytest.abi 智能合约的ABI文件,让开发者通过json来和智能合约进行交互
4,编译智能合约,生成wast,abi文件
编译,生成wast文件
eosiocpp -0 luckpoint.wast luckpoint.cpp
生成abi文件
eosiocpp -g luckpoint.abi luckpoint.cpp
5,前段调用eosjs与智能合约进行交互
index.jsx中,调用eosjs的流程
前段界面实现eosjs的调用,与合约进行交互
eosjs实现了大多数的rpc功能,主要使用eosjs来进行智能合约的接口调用,以及查询智能合约的表数据。
步骤:
1,导入eosjs组件;
import EOSJS from 'eosjs'
let contract_name = 'luckpoint.co'
let account_name = 'banker'
2,实现eosjs配置信息;
const EOSJS_CONFIG = {
contractName: contract_name, // 合约名字
contractSender: account_name, // 执行合约的账户 (需要与私钥对应)
clientConfig: {
keyProvider: ['5JcziTgwUhQgKyvmvc4ygEPGonQPVrBYNTwezAg5UuJ7djyVDWQ'], // 账号对应的私钥
httpEndpoint: 'http://127.0.0.1:8888' // EOS节点程序的http终端
}
}
3,使用指定网络来加载配置文件,实例化eosjs对象;
// 读取游戏数据: eosjs.contract(contract_name)
let eosjs = EOSJS.Localnet(EOSJS_CONFIG.clientConfig)
eosjs.contract(contract_name)
.then((contract) => {
console.log('_updateGameData::' + contract_name + '合约加载成功!')
eosjs.getTableRows({"scope":contract_name, "code":contract_name, "table":"game", "json": true})
.then(result => {
console.log('_updateGameData::读取游戏列表成功!')
let rows = result.rows
let len = rows.length
for(let i = 0; i < len; i ++){
var id = result.rows[i].id
var winner = result.rows[i].winner
var player1 = result.rows[i].player1
var player2 = result.rows[i].player2
var createtime = result.rows[i].createtime
var owner = result.rows[i].owner
this.gameprops.gameDataList.push({
id: id,
winner: winner,
player1: player1,
player2: player2,
createtime: createtime,
owner: owner
})
}
if(len > 0){
this.gameprops.gameIndex = len - 1
}else{
this.gameprops.gameIndex = -1
}
this._refreshUIView()
})
.catch((err) => {
console.log('_updateGameData::读取游戏列表失败!')
this.gameprops.gameIndex = -1
this._refreshUIView()
})
})
}
4,指定的智能合约加载表数据:eosjs.contract(EOSJS_CONFIG.contractName)
_createGame() {
console.log('_createGame::创建游戏 ...')
this._showLoading(true, '正在创建游戏... ...')
let _sender = account_name
let eosjs = EOSJS.Localnet(EOSJS_CONFIG.clientConfig)
eosjs.contract(EOSJS_CONFIG.contractName)
.then((contract) => {
console.log('_createGame::加载合约成功!')
contract.creategame(_sender, { authorization: [_sender] })
.then((res) => {
console.log('_createGame::游戏创建成功!')
this._updateGameData()
})
.catch((err) => {
console.log('_createGame::游戏创建失败!')
})
})
}
5,启动智能合约,或者查询智能合约表数据:
启动eos节点:
> cd /你的eos所在父级目录/eos/build/programs/nodeos
>
> ./nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin
另起一个控制台,用户初始化钱包、合约等: cd /你的eos所在父级目录/eos
// 创建钱包、导入私钥、创建及部署合约
> cleos wallet create
>
> cleos wallet unlock
>
> cleos wallet import 5JcziTgwUhQgKyvmvc4ygEPGonQPVrBYNTwezAg5UuJ7djyVDWQ
>
> cleos create account eosio luckpoint.co EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ
>
> cleos set contract luckpoint.co ./contracts/luckpoint -p luckpoint.co
// 创建banker、player1、player2这三个账户
> cleos create account eosio banker EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ
>
> cleos create account eosio player1 EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ
>
> cleos create account eosio player2 EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ EOS6zzuh8wUHAmEftGNzHLRDCaxtVmTdBKWNCMDb9rF3DhQMB1XuQ
再启动一个控制台,用于启动web服务:
> cd /你的eos所在父级目录/eos/www_luckpoint
>
> npm run start
7、用浏览器打开(建议用Chrome浏览器,便于调试):[http://localhost:8080/](http://localhost:8080/)
8、开始游戏。
6,命令行执行合约接口及查询表数据:
// 创建一个新游戏
cleos push action luckpoint.co creategame '["banker"]' -p banker
// 玩家开牌命令(第一个参数为游戏id,需要与实际的游戏id对应)
cleos push action luckpoint.co opencard '[1,1]' -p player1
cleos push action luckpoint.co opencard '[1,2]' -p player2
// 查询表数据
cleos get table luckpoint.co luckpoint.co game
参考资料:
eosjs api文档:
https://github.com/EOSIO/eosjs
网友评论