基于EOS的Dapp就是html页面通过调用eosjs与EOS链进行RPC通信,进行合约执行与数据交换。
下面演示Hello合约的Dapp调用
合约源代码
//hello.cpp
#include <eosiolib/eosio.hpp>
using namespace eosio;
class hello : public eosio::contract {
public:
using contract::contract;
/// @abi action
void hi( account_name user ) {
print( "Hello, ", name{user} );
}
};
EOSIO_ABI( hello, (hi) )
合约部署参考(零基础部署测试合约(三)---合约部署)
html 调用js关键代码
import EOS from 'eosjs'
const EOS_CONFIG = {
contractName: "dapp.token", // Contract name
contractReceiver: "dapp.exec", // User executing the contract (should be paired with private key)
clientConfig: {
keyProvider: '5JC9FdRjX3c3fsB62S5Tz22LgTuRHegb1XEV16uft8u3njmm9E5', // Your private key
httpEndpoint: 'http://192.168.1.112:8888', // EOS http endpoint
chainId: '706a7ddd808de9fc2b8879904f3b392256c83104c1d544b38302cc07d9fca477'
}
}
//执行合约
DoContract(){
let eosClient = EOS(EOS_CONFIG.clientConfig)
eosClient.contract(EOS_CONFIG.contractName)
.then((contract) => {
contract.hi(EOS_CONFIG.contractReceiver, { authorization: [EOS_CONFIG.contractReceiver] })
.then((res) => { console.log("Success") })
.catch((err) => {console.log("Fail"); console.log(err) })
})
}