• 四川玖德网络科技有限公司
栏目列表
新闻动态
当前位置: 主页 > 区块链技术 >

EOS交易所开发

时间:2019-09-29 15:08来源:未知 字体大小:【
请注意,本文是在EOS DAWN 2.0代币的。

exchange 智能合约模拟的是交易所的功能,支持充值提现eos和currency,支持eos和currency之间的买卖,别有一番玩味。
值得注意的是,将currency  token 转到佳一所,是使用了currency智能合约发行的,由它管理currency token的转账是必然的。
 
充值eos:往交易所充值eos。
$ eosc push message eos transfer '{"from":"inita","to":"exchange","amount":50, "memo":"imToken"}' --scope inita,
exchange --permission inita@active
tips:充值只需要个人账户授权
 
提现eos:从交易所提现eos

$ eosc push message eos transfer '{"from":"exchange","to":"inita","amount":50, "memo":"binance"}' --scope exchange,
inita 
--permission exchange@active --permission inita@active
tips: 提现需要交易所账户和个人账户授权
至于为什么使用的是这个结构体(exchange.abi没有这个结构体),可以使用eosc查询abi文件:

$ eosc get code eos -a eos.abi
code hash: 0000000000000000000000000000000000000000000000000000000000000000
saving abi to eos.abi
$ cat eos.abi
  ...
  "structs": [{
      "name": "transfer",
      "base": "",
      "fields": {
        "from": "account_name",
        "to": "account_name",
        "amount": "uint64",
        "memo": "string"
      }
      ...
由于打印太多,我只摘取了transfer部分。
从上面可以看出,我们转账eos,其实使用了的是智能合约,也就是说eos代币本身也是用智能合约生成的。

充值currency:往交易所充值currency

$ eosc push message currency transfer '{"from":"inita","to":"exchange","quantity":50}' --scope inita,exchange --permis
sion inita@active

提现currency:往交易所提现currency

$ eosc push message currency transfer '{"from":"exchange","to":"inita","quantity":50}' --scope inita,exchange --permiss
ion exchange@active inita@active

值得注意的是,以上命令的格式取决于已经部署的currency.abi。因为exchange智能合约在转账currency时使用了curren
cy.hpp中的currency名称空间。

如果currency.abi的transfer的格式是这样的:
{
      "name": "transfer",
      "base": "",
      "fields": {
        "from": "account_name",
        "to": "account_name",
        "quantity": "currency_tokens"
      }
}
那么以上的充值命令需要改为:
$ eosc push message currency transfer '{"from":"inita","to":"exchange","quantity":{"quantity":50}}' --scope inita,
exchange --permission inita@active
提现命令改为:
$ eosc push message currency transfer '{"from":"exchange","to":"inita","quantity":{"quantity":50}}' --scope inita,
exchange --permission exchange@active inita@active
如果按照EOS官方提供的代码,下买单或者卖单是不能成功的,需要修改一下几个地方:
1、修改exchange.hpp中bids、asks这两个table的定义,第4个参数原本是bids/asks,需要改成bid/ask,否则会报错
   TABLE2(bids,exchange,exchange,bid,bid,bids_by_id,order_id,bids_by_price,price);
   TABLE2(asks,exchange,exchange,ask,ask,asks_by_id,order_id,asks_by_price,price);
2、修改/usr/local/include/eoslib/token.hpp中struct price的operator *,
    friend BaseToken operator * ( const QuoteToken& b, const price& q ) {
      eosio::print( "b: ", b, " 
" );
      eosio::print( "operator* ", uint128(b.quantity), " * ", uint128( q.base_per_quote ), " / ", precision, "
" );
      //return QuoteToken( uint64_t( mult_div_i128( b.quantity, q.base_per_quote, precision ) ) );
      //return BaseToken( uint64_t((b.quantity * q.base_per_quote) / precision) );
      return BaseToken( uint64_t(b.quantity * q.base_per_quote) );
    }
值得注意的是,price结构体有2个构造函数,exchange智能合约使用了第一个构造函数,如果使用第2个构造函数,那上面的*操作符就不
需要修改了。

    /**
    * Default constructor.
    * Initialize base per quote to be 1.
    * @brief Default constructor.
    */
    price():base_per_quote(1ul){}

    /**
    * Construction for price given the base token and quote token.
    * @brief Construction for price given the base token and quote token.
    * @param base - base token
    * @param quote - quote token
    */
    price( BaseToken base, QuoteToken quote ) {
      assert( base  >= BaseToken(1ul), "invalid price" );
      assert( quote >= QuoteToken(1ul), "invalid price" );

      base_per_quote = base.quantity;
      base_per_quote *= precision;
      base_per_quote /= quote.quantity;
    }
这样就可以下买单或者卖单了。

下买单:

$ eosc push message exchange buy '{"buyer":{"name":"inita","number":5},"at_price":"1","quantity":5,"expiration":
"2018-04-14T08:53:41","fill_or_kill":"0"}' -S exchange,inita -p inita@active

下卖单:

$ eosc push message exchange sell '{"seller":{"name":"initb","number":5},"at_price":"1","quantity":5,"expiration":
"2018-04-14T08:53:41","fill_or_kill":"0"}' -S exchange,initb -p initb@active

取消买单:

$ eosc push message exchange cancelbuy '{"name":"inita","number":5}' -S inita,exchange -p inita@active

取消卖单:

$ eosc push message exchange cancelsell '{"name":"initb","number":5}' -S initb,exchange -p initb@active


------分隔线----------------------------