Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mango Perpetuals v3 #13

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mango/src/main/java/com/mmorrell/mango/model/MangoOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mmorrell.mango.model;

import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.p2p.solanaj.core.PublicKey;

/**
* Class that represents a Serum order.
*/
@Data
@Builder
public class MangoOrder {

private long price;
private long quantity;
private long clientOrderId;
private float floatPrice;
private float floatQuantity;
private PublicKey owner;

// used in newOrderv3. no constructor, only setters/getters
private long maxQuoteQuantity;
private long clientId;
private boolean buy;

}
119 changes: 119 additions & 0 deletions mango/src/main/java/com/mmorrell/mango/model/MangoOrderBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.mmorrell.mango.model;

import java.util.ArrayList;
import java.util.Comparator;

/**
* This class represents a Serum MangoOrderbook, that get deserialized from bytes.
*
* Note:
*
* buffer_layout_1.blob(5),
* layout_1.accountFlagsLayout('accountFlags'),
* slab_1.SLAB_LAYOUT.replicate('slab'),
* buffer_layout_1.blob(7),
*
*/
public class MangoOrderBook {

private MangoSlab mangoSlab;
private byte baseDecimals;
private byte quoteDecimals;
private long baseLotSize;
private long quoteLotSize;

public static MangoOrderBook readMangoOrderBook(byte[] data) {
final MangoOrderBook mangoMangoOrderBook = new MangoOrderBook();

final MangoSlab mangoSlab = MangoSlab.readOrderBookSlab(data);
mangoMangoOrderBook.setSlab(mangoSlab);

return mangoMangoOrderBook;

}

public ArrayList<MangoOrder> getMangoOrders() {
if (mangoSlab == null) {
return null;
}

final ArrayList<MangoOrder> mangoOrders = new ArrayList<>();

mangoSlab.getMangoSlabNodes().forEach(slabNode -> {
if (slabNode instanceof MangoSlabLeafNode) {
MangoSlabLeafNode slabLeafNode = (MangoSlabLeafNode) slabNode;
mangoOrders.add(MangoOrder.builder()
.price(slabLeafNode.getPrice())
.quantity(slabLeafNode.getQuantity())
.clientOrderId(slabLeafNode.getClientOrderId())
.floatPrice(MangoUtils.priceLotsToNumber(slabLeafNode.getPrice(), baseDecimals, quoteDecimals, baseLotSize, quoteLotSize))
.floatQuantity((float) ((slabLeafNode.getQuantity() * baseLotSize) / MangoUtils.getBaseSplTokenMultiplier(baseDecimals)))
.owner(slabLeafNode.getOwner())
.build()
);
}
});

return mangoOrders;


}

/**
* Retrieves the top {@link MangoOrder} for bids (sorted by price descending).
* @return
*/
public MangoOrder getBestBid() {
final ArrayList<MangoOrder> MangoOrders = getMangoOrders();
MangoOrders.sort(Comparator.comparingLong(MangoOrder::getPrice).reversed());
return MangoOrders.get(0);
}

public MangoOrder getBestAsk() {
final ArrayList<MangoOrder> MangoOrders = getMangoOrders();
MangoOrders.sort(Comparator.comparingLong(MangoOrder::getPrice));
return MangoOrders.get(0);
}

public MangoSlab getSlab() {
return mangoSlab;
}

public void setSlab(MangoSlab mangoSlab) {
this.mangoSlab = mangoSlab;
}

public void setBaseDecimals(byte baseDecimals) {
this.baseDecimals = baseDecimals;
}

public byte getBaseDecimals() {
return baseDecimals;
}

public void setQuoteDecimals(byte quoteDecimals) {
this.quoteDecimals = quoteDecimals;
}

public byte getQuoteDecimals() {
return quoteDecimals;
}


public void setBaseLotSize(long baseLotSize) {
this.baseLotSize = baseLotSize;
}

public long getBaseLotSize() {
return baseLotSize;
}

public void setQuoteLotSize(long quoteLotSize) {
this.quoteLotSize = quoteLotSize;
}

public long getQuoteLotSize() {
return quoteLotSize;
}

}
40 changes: 40 additions & 0 deletions mango/src/main/java/com/mmorrell/mango/model/MangoPerpMarket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mmorrell.mango.model;

import lombok.Builder;
import lombok.Data;
import org.bitcoinj.core.Utils;
import org.p2p.solanaj.core.PublicKey;

@Data
@Builder
// 4nfmQP3KmUqEJ6qJLsS3offKgE96YUB4Rp7UQvm2Fbi9 mngo-perp
public class MangoPerpMarket {

// metadata 0-7 ignored
private static final int MANGO_GROUP_OFFSET = 8;
private static final int BIDS_OFFSET = MANGO_GROUP_OFFSET + 32;
private static final int ASKS_OFFSET = BIDS_OFFSET + 32;
private static final int EVENT_QUEUE_OFFSET = ASKS_OFFSET + 32;
private static final int QUOTE_LOT_SIZE_OFFSET = EVENT_QUEUE_OFFSET + 32;
private static final int BASE_LOT_SIZE_OFFSET = QUOTE_LOT_SIZE_OFFSET + 8;

private PublicKey mangoGroup;
private PublicKey bids;
private PublicKey asks;
private PublicKey eventQueue;
private long quoteLotSize;
private long baseLotSize;

public static MangoPerpMarket readMangoPerpMarket(byte[] data) {
final MangoPerpMarket mangoPerpMarket = MangoPerpMarket.builder()
.mangoGroup(PublicKey.readPubkey(data, MANGO_GROUP_OFFSET))
.bids(PublicKey.readPubkey(data, BIDS_OFFSET))
.asks(PublicKey.readPubkey(data, ASKS_OFFSET))
.eventQueue(PublicKey.readPubkey(data, EVENT_QUEUE_OFFSET))
.quoteLotSize(Utils.readInt64(data, QUOTE_LOT_SIZE_OFFSET))
.baseLotSize(Utils.readInt64(data, BASE_LOT_SIZE_OFFSET))
.build();

return mangoPerpMarket;
}
}
Loading