准备�
上一小节, 我们实现了最简单的hello 智能合约,那么依然我们来使用uesr 用户来操作这个合约来操作eos的数据库
编译执行合约shell脚本:start.sh
由于脚本比较粗糙,并未对执行结果进行判断
#!/bin/bash
echo "clean"
rm -rf jianshu.table.token.was* jianshu.table.token.abi
sleep 1
eosiocpp -o jianshu.table.token.wast jianshu.table.token.cpp
echo " BUILD *.wast success ! "
eosiocpp -g jianshu.table.token.abi jianshu.table.token.cpp
echo " BUILD *.abi success ! "
echo " contract * * * * ! "
sleep 1
cleos set contract user ../jianshu.table.token/
echo " RUN contract * * * * ! "
sleep 1
cleos push action user create '{"account":"9527","id":"511x2x199103051xxx", "name":"user", "address":"chengdu"}' -p user
sleep 1
cleos get table user user people
jianshu.table.token.hpp
/**
* @file
* @author Aries
*/
#include
#include
using namespace eosio;
namespace yykik {
using std::string;
class tablepeople : public contract {
public:
using contract::contract;
tablepeople(account_name self) : contract(self) {}
/// @abi action
void create(const account_name account, uint64_t id, string name, string address);
/// @abi action
void remove(const account_name account );
private:
/// @abi table people i64
struct people {
uint64_t id; //用户唯一识别Id
account_name account;//用户账户名
string name; //用户名
string address;//用户地址
/// Define the people primary key
/// 主键索引
uint64_t primary_key() const { return account; }
//用户id索引
uint64_t get_id() const { return id; }
/// Serialization people
EOSLIB_SERIALIZE(people, (account)(id)(name)(address))
};
/// 默认通过主键索引,使用indexed_by,可以通过自定义函数进行索引
typedef eosio::multi_index>
> people_index;
};
}
jianshu.table.token.cpp
#include "jianshu.table.token.hpp"
namespace yykik {
void tablepeople::create(const account_name account, uint64_t id, string name, string address)
{
// 获取授权,如果没有授权,Action调用会中止,事务会回滚
require_auth(_self);
//eosio_assert(account==N(user),"错误:其他账户没有权限调用该方法");
//校验参数
eosio_assert(address.size() < 36, "错误: 地址不允许超过36字节");
people_index peopmul (_self, _self);
peopmul.emplace(_self, [&](auto &obj){
obj.account = account;
obj.id = id;
obj.name = name;
obj.address = address;
});
}
void tablepeople::remove(const account_name account)
{
require_auth(account);
people_index peopmul (_self, _self);
auto peoperiter = peopmul.find(account);
eosio_assert(peoperiter != peopmul.end(), "people account not fount!");
peopmul.erase(peoperiter);
}
}
EOSIO_ABI( yykik::tablepeople, (create)(remove) )
操作如下:
查看数据库数据:
cleos get table user user people
{
"rows": [],
"more": false
}
执行合约:
cleos push action user create '{"account":"user","id":"51132419910305xxxx", "name":"user", "address":"chengdu"}' -p user
executed transaction: 330e618629480a5115802577e389da34dc81b37aa15da35defc2004f3dd8178a 128 bytes 14276 us
# user <= user::create {"account":"user","id":"51132419910305xxxx","name":"user","address":"chengdu"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
查看数据库数据:
cleos get table user user people
{
"rows": [{
"id": "15426359243929812992",
"account": ".wgdhdn4n4wcj",
"name": "user",
"address": "chengdu"
}
],
"more": false
}
执行智能合约:
cleos push action user create '{"account":"aries","id":"51132419910305xxxx", "name":"user", "address":"chengdu"}' -p user
executed transaction: 6964fb0d293400ff8593c9590d4126162b142c64a300fe05024e23c0bd2a2490 128 bytes 1341 us
# user <= user::create {"account":"aries","id":"51132419910305xxxx","name":"user","address":"chengdu"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
查看数据库数据:
cleos get table user user people
{
"rows": [{
"id": "3881166094886502400",
"account": ".wgdhdn4n4wcj",
"name": "user",
"address": "chengdu"
},{
"id": "15426359243929812992",
"account": ".wgdhdn4n4wcj",
"name": "user",
"address": "chengdu"
}
],
"more": false
}