• 四川玖德網絡科技有限公司
欄目列表
新聞動態
當前位置: 主頁 > 區塊鏈技術 >

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


------分隔線----------------------------
手机上看东西真能赚钱吗