EOS测试链智能合约部署调用

ETH与EOS两者智能合约进行简单的对比。

1、编译智能合约(合约编译成.wasm与.abi格式后即可部署到区块链)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[root@C03-12U-26 testcontract]# cat testcontract.cpp
#include <eosiolib/eosio.hpp> 
   
class [[eosio::contract]] testcontract : public eosio::contract 
public: 
    testcontract( eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds ) 
        : eosio::contract(receiver, code, ds),  _students(receiver, code.value) 
    {} 
   
    //添加学生 
    [[eosio::action]] 
    void add(eosio::name user, std::string studentName,std::string studentGender,uint32_t studentAge,uint32_t studentHeight) 
    
        require_auth(user); 
        eosio::print("Add student ", studentName); 
        _students.emplace(get_self(), [&](auto& p) { 
            p.id = _students.available_primary_key(); 
            p.studentName = studentName; 
            p.studentGender = studentGender; 
            p.studentAge = studentAge; 
            p.studentHeight = studentHeight; 
        }); 
    
   
    //根据主键删除学生 
    [[eosio::action]] 
    void del(eosio::name user, uint64_t id) 
    
        require_auth(user); 
        eosio::print("Del student", id); 
        auto itr = _students.find(id); 
        if (itr != _students.end()) { 
            _students.erase(itr); 
        
    
   
    //根据主键修改学生 
    [[eosio::action]] 
    void update(eosio::name user, uint64_t id,std::string studentName,std::string studentGender,uint32_t studentAge,uint32_t studentHeight) 
    
        require_auth(user); 
        eosio::print("update student", studentName); 
        auto itr = _students.find(id); 
        if (itr != _students.end()) 
        
            _students.modify(itr, user, [&]( auto& p ) { 
                p.studentName = studentName; 
                p.studentGender = studentGender; 
                p.studentAge = studentAge; 
                p.studentHeight = studentHeight; 
            }); 
        
    
   
    //清空表,待定 
    [[eosio::action]] 
    void clear(eosio::name user) 
    
        require_auth(user); 
        std::vector<uint64_t> keysForDeletion; 
        for(auto& item : _students) { 
            keysForDeletion.push_back(item.id); 
        
   
        for (uint64_t key : keysForDeletion) { 
            eosio::print("remove from _students ", key); 
            auto itr = _students.find(key); 
            if (itr != _students.end()) { 
                _students.erase(itr); 
            
        
    
   
    //private: -- not private so the cleos get table call can see the table data. 
    // create the multi index tables to store the data 
    struct [[eosio::table]] students 
    
        uint64_t      id; // primary key 
        std::string   studentName; 
        std::string   studentGender; 
        uint32_t    studentAge; 
        uint32_t    studentHeight; 
   
        uint64_t primary_key() const { return id; } 
        uint64_t by_age() const {return studentAge; } 
    }; 
   
    //数据表根据age排序 
    typedef eosio::multi_index<"students"_n, students, eosio::indexed_by<"studentage"_n, eosio::const_mem_fun<students, uint64_t, &students::by_age>>> studentstable; 
   
    //students数据库表 
    studentstable _students; 
}; 
   
EOSIO_DISPATCH( testcontract, (add)(del)(update)(clear))

 2、部署智能合约

   解锁账号钱包

1
cleos -u https://jungle2.cryptolions.io:443  wallet unlock -n zmcheng-wallet

   部署合约

1
cleos -u https://jungle2.cryptolions.io:443 set contract zmcheng12345 /root/github.com/testcontract -p zmcheng12345@active

Reading WASM from /root/github.com/testcontract/testcontract.wasm...
Publishing contract...
executed transaction: 785f421fe911a142b7cc4b15131cf3f4947847531ed8c52150aa148fe405ea0d 7536 bytes 1560 us
# eosio <= eosio::setcode {"account":"zmcheng12345","vmtype":0,"vmversion":0,"code":"0061736d0100000001d0012060000060037f7e7e0...
# eosio <= eosio::setabi {"account":"zmcheng12345","abi":"0e656f73696f3a3a6162692f312e3100050361646400050475736572046e616d650...
warning: transaction executed locally, but may not be confirmed by the network yet

 调用合约(增删改查)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 push action zmcheng12345 add '["zmcheng12345","zmcheng","male",25,170]' -p zmcheng12345@active
executed transaction: a64271659aea1ce07fd05a4ff1bb0188c441de04263f3a18b819cbee205d2bbd  128 bytes  273 us
#  zmcheng12345 <= zmcheng12345::add            {"user":"zmcheng12345","studentName":"zmcheng","studentGender":"male","studentAge":25,"studentHeight...
>> Add student zmcheng
warning: transaction executed locally, but may not be confirmed by the network yet         ]
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 get table zmcheng12345 zmcheng12345 students
{
  "rows": [{
      "id": 0,
      "studentName": "zmcheng",
      "studentGender": "male",
      "studentAge": 25,
      "studentHeight": 170
    },{
      "id": 1,
      "studentName": "www",
      "studentGender": "male",
      "studentAge": 26,
      "studentHeight": 170
    }
  ],
  "more": false
}
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 push action zmcheng12345 update '["zmcheng12345",1,"www","male",12,123]' -p zmcheng12345@active
executed transaction: 765c35452964de54771c3fe505daad272a8a53ec61954a2cc43350f7f84e86e3  128 bytes  213 us
#  zmcheng12345 <= zmcheng12345::update         {"user":"zmcheng12345","id":1,"studentName":"www","studentGender":"male","studentAge":12,"studentHei...
>> update studentwww
warning: transaction executed locally, but may not be confirmed by the network yet         ]
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 get table zmcheng12345 zmcheng12345 students
{
  "rows": [{
      "id": 0,
      "studentName": "zmcheng",
      "studentGender": "male",
      "studentAge": 25,
      "studentHeight": 170
    },{
      "id": 1,
      "studentName": "www",
      "studentGender": "male",
      "studentAge": 12,
      "studentHeight": 123
    }
  ],
  "more": false
}
[root@C03-12U-26 testcontract]#
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 push action zmcheng12345 del '["zmcheng12345",1]' -p zmcheng12345@active
executed transaction: 1a64a42ebccac0f9b7061c42a0fa8daaebe7b1a18db1cb058fe479608ee9170a  112 bytes  255 us
#  zmcheng12345 <= zmcheng12345::del            {"user":"zmcheng12345","id":1}
>> Del student1
warning: transaction executed locally, but may not be confirmed by the network yet         ]
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 get table zmcheng12345 zmcheng12345 students
{
  "rows": [{
      "id": 0,
      "studentName": "zmcheng",
      "studentGender": "male",
      "studentAge": 25,
      "studentHeight": 170
    }
  ],
  "more": false
}

  

posted @   人艰不拆_zmc  阅读(719)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2018-09-17 书单
2014-09-17 SDUT1157:小鼠迷宫问题(bfs+dfs)
点击右上角即可分享
微信分享提示