您的位置:首页 > 其它

基于EOS的去中心化交易所

2018-03-24 12:10 288 查看
本文由【区块链研习社】优质内容计划支持,更多关于区块链的深度好文,请点击《区块链研习社》

请注意,本文是在EOS DAWN 2.0代币的。

exchange智能合约模拟的是交易所的功能,支持充值提现eos和currency,支持eos和currency之间的买卖,别有一番玩味。

值得注意的是,将currency token转到交易所,是使用了我们上节课讲到的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: 提现需要交易所账户和个人账户授权

充值currency:往交易所充值currency

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


提现currency:往交易所提现currency

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


值得注意的是,以上命令的格式取决于已经部署的currency.abi。因为exchange智能合约在转账currency时使用了currency.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 currency智能合约的那些坑》

如果按照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, " \n" );
eosio::print( "operator* ", uint128(b.quantity), " * ", uint128( q.base_per_quote ), " / ", precision, "\n" );
//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":{"na
9b67
me":"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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息