0x take Limit Order of protocol analysis

Posted by aaaaCHoooo on Sun, 02 Jan 2022 05:31:27 +0100

0x take Limit Order of protocol analysis

As for the question of what 0x is, I won't be verbose here. There are many documents explaining the basic concepts. Here we mainly show the listing and eating process of limit order.
0xprotocol Process Overview
Explanation of some problems in 0x

0x limit order hanging order eating order process

Interaction of market price list of 0x Official documents The writing is relatively clear and the operation is relatively simple. Here we mainly record the hanging order and eating order of the limit order.

Hanging order

  1. Prepare an order in format
    Data structure of limited price orders in OX
 struct LimitOrder {
        IERC20TokenV06 makerToken;
        IERC20TokenV06 takerToken;
        uint128 makerAmount;
        uint128 takerAmount;
        uint128 takerTokenFeeAmount;
        address maker;
        address taker;
        address sender;
        address feeRecipient;
        bytes32 pool;
        uint64 expiry;
        uint256 salt;
    }
const order = new utils.LimitOrder({
    "makerToken": "0xc778417e063141139fce010982780140aa0cd5ab",
    "takerToken": "0xf8db576976096d5f19c21ab12115d7a2aa7c77cc",
    "makerAmount": "1",
    "takerAmount": "1000000000000000",
    "maker": "0xD993d1049bd38eDBe12f8F6c9cdB716970B778D2",
    "taker": "0x0000000000000000000000000000000000000000",
    "pool": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "expiry": "1627240945",
    "salt": "1626940945557",
    "chainId": 3,
    "verifyingContract": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
    "takerTokenFeeAmount": "0",
    "sender": "0x0000000000000000000000000000000000000000",  
    "feeRecipient": "0x0000000000000000000000000000000000000000",
    "signature":{
      "v": 28,
      "r": "0x921d18a7b4eaf9ce5227031f872dd54a73237e5eb90d81da3da05465f16f9b4f",
      "s": "0x524904cac050c9ae8f5999cc0f96415d1422dc5b36bb7fbe11d8522cc6b28365",
      "signatureType": 2
    }
  });

The signature field is the order signature. There are many ways to create a signature
One is the tool signature provided by @ 0x / protocol utils Document address
But the signature I tried this way can't be listed.

In another way, the signature made by the provided tool can complete the hanging list
sign address

  1. Pass the generated order as the body parameter api Publish, and the request is a post request

  2. After publishing, it can be queried by api according to some parameters, Query api

Eating list

  1. maker should approve the makettoken to [0x related contract] 0xFbB2a5B444aA89a4054883A4D23FBA45BFc6840A
  2. The taker also wants the takerToken approve to the contract
  3. Note that the protocol fee of 70*gasPrice shall be paid for the take order
  4. Analysis of some methods in 0x contract
orderParam Order parameters
["0xf8db576976096d5f19c21ab12115d7a2aa7c77cc","0xc778417e063141139fce010982780140aa0cd5ab",1000,1,0,"0xcc2847ab347a4752a233e20b7e4410e138f096f6","0x0000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000",2014956256,0]  
signParam  Signature parameters  
[2,28,"0xd447dc3ef38cf18e82e34e2bffb78d2c8e7175881ae6b881a005a956b0f964ba","0x2a2b6dfd811a90d1dd72b830f09f9dcd6db2f9224b343dd4d4d8b33543c24cf8"]  
  1. getLimitOrderHash(orderParam) gets the order hash of the limit order
    return orderHash
  2. getLimitOrderInfo(orderParam) gets the information of the price limit
    Returns a structure, including order hash, order status, and quantity consumed in the order
    return struct OrderInfo {
        bytes32 orderHash;
        OrderStatus status;
        uint128 takerTokenFilledAmount;
    }
    
    The order statuses are as follows:
    enum OrderStatus {
        INVALID,
        FILLABLE,
        FILLED,
        CANCELLED,
        EXPIRED
    }
    

The status of orders that can be purchased is 1 filled, and the status of orders that have been purchased is 2 FILLED

  1. getLimitOrderRelevantState(orderParam,signParam) is used to obtain some status of the limit order, which will show how many quantities of the current order can be purchased
    The three parameters returned are
    The structure of OrderInfo, which has been mentioned above
    Actual fillabletakertokenamount,
    Issignaturevalue whether the signer's signature is correct
    If the actual actualfillabletakertokenamount is > 0 and issignaturevalue = true, the purchase can be successful

  2. Fillllimitorder (orderparam, signparam, takearamount)
    The above queries can be said to be prepared for filling in the correct parameters of this method.
    After checking the above status, there is no problem. Fill in the order parameter and signature parameter, and then fill in an appropriate takeamount (< = actualfillabletakertokenamount)
    In addition, pay 70K*gasPrice protocol fee when sending transactions.
    Under normal circumstances, you can complete the order eating operation by sending the transaction.

OK, the above is the process of hanging and eating orders using 0x protocol.

If you want to analyze the token flow in detail, you can analyze the following
A transaction hash for eating orders

If you have any questions, you can leave a message.

Topics: Blockchain Ethereum