EOS合约解析---EOS Exchange 合约

1、功能:

Exchange 合约提供了创建和交易货币的能力。
使用户能够在任何一对之间创建交换标准货币类型。通过为订单簿的双方提供相同的价值并为发行人提供该订单簿中的初始份额来创建新的交易所。
为防止过多的舍入错误,初始存款应包括相当数量的基础货币和报价货币,交换股份的数量应为最大初始存款数量的100倍。
用户必须先将资金存入交易所,然后才能在交易所进行交易。
每次创建交易所时,也会创建该交易所做市商的新货币。 此货币供应和符号必须是唯一的,并使用货币合约的表来管理它。

2、exchange/exchange.hpp

2.1 头文件

//类型
\#include <eosiolib/types.hpp>
//货币头文件
\#include <eosiolib/currency.hpp>
\#include <boost/container/flat_map.hpp>
\#include \<\cmath>
//销售状态
\#include <exchange/market_state.hpp>

2.2 构造函数

   class exchange {
      private:
         account_name      _this_contract;
         currency          _excurrencies;
         exchange_accounts _accounts;
      public:
         exchange( account_name self ):_this_contract(self),

2.2 功能函数

piblic:
         //创建交易
         void createx( account_name    creator,
                       asset           initial_supply,
                       uint32_t        fee,
                       extended_asset  base_deposit,
                       extended_asset  quote_deposit
                     );

         //处理存款
         void deposit( account_name from, extended_asset quantity );
         //取款
         void withdraw( account_name  from, extended_asset quantity );
         //借贷
         void lend( account_name lender, symbol_type market, extended_asset quantity );

3、exchange/exchange.cpp

3.1 apply方法:每次action 运行时,在合约实现中调用apply()响应次操作。

extern "C" {
   //noreturn:执行完成后不会返回调用者
   [[noreturn]] void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
      //创建exchange对象
      eosio::exchange  ex( receiver );
      //调用apply方法
      ex.apply( code, action );
      //退出程序
      eosio_exit(0);
   }

apply

   void exchange::apply( account_name contract, account_name act ) {

      //操作是transfer类型的处理
      if( act == N(transfer) ) {
         on( unpack_action_data<currency::transfer>(), contract );
         return;
      }
      //合约不是当前合约返回
      if( contract != _this_contract )
         return;

      //执行exchange合约的各种action
      auto& thiscontract = *this;
      switch( act ) {
         EOSIO_API( exchange, (createx)(deposit)(withdraw)(lend)(unlend) )
      };

      //对exchange处理后不同的action进行操作
      switch( act ) {
         case N(trade):
            on( unpack_action_data<trade>() );
            return;
         case N(upmargin):
            on( unpack_action_data<upmargin>() );
            return;
         case N(covermargin):
            on( unpack_action_data<covermargin>() );
            return;
         default:
            _excurrencies.apply( contract, act ); 
            return;
      }
   }

3.2 创建交易

   void exchange::createx( account_name    creator,
                 asset           initial_supply,
                 uint32_t        /* fee */,
                 extended_asset  base_deposit,
                 extended_asset  quote_deposit
               ) {
      //需要creator的授权
      require_auth( creator );
      //检验初始发行量是否有效、是否大于0
      eosio_assert( initial_supply.is_valid(), "invalid initial supply" );
      eosio_assert( initial_supply.amount > 0, "initial supply must be positive" );
      //检验基础存款
      eosio_assert( base_deposit.is_valid(), "invalid base deposit" );
      eosio_assert( base_deposit.amount > 0, "base deposit must be positive" );
      //检验引用存款
      eosio_assert( quote_deposit.is_valid(), "invalid quote deposit" );
      eosio_assert( quote_deposit.amount > 0, "quote deposit must be positive" );
      //检验两种资产是否是同种货币
      eosio_assert( base_deposit.get_extended_symbol() != quote_deposit.get_extended_symbol(),
                    "must exchange between two different currencies" );

      print( "base: ", base_deposit.get_extended_symbol() );
      print( "quote: ",quote_deposit.get_extended_symbol() );

      auto exchange_symbol = initial_supply.symbol.name();
      print( "marketid: ", exchange_symbol, " \n " );

      //这样并不是直接建立了一个新表,而是让 C++ 程序与数据库对应的表之间建立了数据传输的通道
      markets exstates( _this_contract, exchange_symbol );
      //在数据表中查找交易符号
      auto existing = exstates.find( exchange_symbol );

      eosio_assert( existing == exstates.end(), "market already exists" );
      exstates.emplace( creator, [&]( auto& s ) {
          s.manager = creator;
          s.supply  = extended_asset(initial_supply, _this_contract);
          s.base.balance = base_deposit;
          s.quote.balance = quote_deposit;

          s.base.peer_margin.total_lent.symbol          = base_deposit.symbol;
          s.base.peer_margin.total_lent.contract        = base_deposit.contract;
          s.base.peer_margin.total_lendable.symbol      = base_deposit.symbol;
          s.base.peer_margin.total_lendable.contract    = base_deposit.contract;

          s.quote.peer_margin.total_lent.symbol         = quote_deposit.symbol;
          s.quote.peer_margin.total_lent.contract       = quote_deposit.contract;
          s.quote.peer_margin.total_lendable.symbol     = quote_deposit.symbol;
          s.quote.peer_margin.total_lendable.contract   = quote_deposit.contract;
      });

      _excurrencies.create_currency( { .issuer = _this_contract,
                                 // TODO: After currency contract respects maximum supply limits, the maximum supply here needs to be set appropriately.
                                .maximum_supply = asset( 0, initial_supply.symbol ),
                                .issuer_can_freeze = false,
                                .issuer_can_whitelist = false,
                                .issuer_can_recall = false } );

      _excurrencies.issue_currency( { .to = _this_contract,
                                     .quantity = initial_supply,
                                     .memo = string("initial exchange tokens") } );

      _accounts.adjust_balance( creator, extended_asset( initial_supply, _this_contract ), "new exchange issue" );
      _accounts.adjust_balance( creator, -base_deposit, "new exchange deposit" );
      _accounts.adjust_balance( creator, -quote_deposit, "new exchange deposit" );
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、玩过EOS的都知道,EOS本身更新迭代非常之快,所以有些知识点可能与最新版有所出入,希望小伙伴理解!此文档适用于EOS-v1.0.5以上版本和v1.1.x版本,目前的v1.2.x可能会有极少部分出入,比如eosiocpp工具看更新说明再不用安装的状态下就能使用,目前还未测试。有兴趣的小伙伴可以留言交流。 2、当前文档目录结构介绍: #思考研究问题 1、如何保证EOS中发布的智能合约不被随意篡改? #玩转EOS智能合约代码 #玩转客户端cleos 1、先玩转与智能合约相关的操作 #使用eosiocpp工具编译智能合约生成abi文件和wast文件 #编译合约(无法通过) #安装build/programs下工具 #重新编译合约 #部署合约到账户 #购买RAM #测试调用部署的合约 #更新\升级已经部署过的智能合约(相对空的合约) #更新添加新的函数接口(action)合约 #有关require_auth的合约测试 2、玩转智能合约与数据库相关操作 #参考资料 #持久化API (Multi-Index) 1、一般来讲,对数据库的操作无外乎增删改查 2、表结构示例详解 3、Multi_index定义,建立数据表 4、实例化multi_index 5、操作数据,增删改查 #玩转table表 1、Table表producers 2、Table表global 3、Table表voters 4、Table表rammarket (获取RAM实时价格) 5、Table表refunds (查看账户退款信息) 6、Table表namebids (罗列参与竞拍的账户信息) 3、启动nodeos节点出现脏数据 4、查看账户抵押资产,抵押token,赎回token #查看账户余额(可用余额) #查看SYS货币信息,eosio.token是经营货币的合约 #查看公钥对应账户 #查看子账户(控制账户) #查看账户信息 #查看账户抵押信息 #钱不够那就转账,随便耍 #get transaction无结果了解一下 #卖出RAM(卖给系统账户eosio.ram),字节bytes #抵押token获取CPU和net资源 #赎回抵押token,默认三天后到账,执行后可查看账户状态

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值