mirror of
https://github.com/ZigZagExchange/zksync-lite-market-maker.git
synced 2025-12-17 15:14:30 +01:00
roll back
This commit is contained in:
@@ -274,14 +274,12 @@ function isOrderFillable(order) {
|
|||||||
return { fillable: true, reason: null, walletId: goodWalletIds[0]};
|
return { fillable: true, reason: null, walletId: goodWalletIds[0]};
|
||||||
}
|
}
|
||||||
|
|
||||||
function genQuote(chainId, marketId, side, baseQuantity, quoteQuantity = 0) {
|
function genQuote(chainId, marketId, side, baseQuantity) {
|
||||||
const market = MARKETS[marketId];
|
const market = MARKETS[marketId];
|
||||||
if (CHAIN_ID !== chainId) throw new Error("badchain");
|
if (CHAIN_ID !== chainId) throw new Error("badchain");
|
||||||
if (!market) throw new Error("badmarket");
|
if (!market) throw new Error("badmarket");
|
||||||
if (!(['b','s']).includes(side)) throw new Error("badside");
|
if (!(['b','s']).includes(side)) throw new Error("badside");
|
||||||
if (baseQuantity < 0) throw new Error("badbasequantity");
|
if (baseQuantity <= 0) throw new Error("badquantity");
|
||||||
if (quoteQuantity < 0) throw new Error("badquotequantity");
|
|
||||||
if (baseQuantity === 0 & quoteQuantity === 0) throw new Error("badquantity");
|
|
||||||
|
|
||||||
validatePriceFeed(marketId);
|
validatePriceFeed(marketId);
|
||||||
|
|
||||||
@@ -293,25 +291,17 @@ function genQuote(chainId, marketId, side, baseQuantity, quoteQuantity = 0) {
|
|||||||
const primaryPrice = PRICE_FEEDS[mmConfig.priceFeedPrimary];
|
const primaryPrice = PRICE_FEEDS[mmConfig.priceFeedPrimary];
|
||||||
if (!primaryPrice) throw new Error("badprice");
|
if (!primaryPrice) throw new Error("badprice");
|
||||||
const SPREAD = mmConfig.minSpread + (baseQuantity * mmConfig.slippageRate);
|
const SPREAD = mmConfig.minSpread + (baseQuantity * mmConfig.slippageRate);
|
||||||
if (baseQuantity && !quoteQuantity) {
|
let quoteQuantity;
|
||||||
if (side === 'b') {
|
if (side === 'b') {
|
||||||
quoteQuantity = (baseQuantity * primaryPrice * (1 + SPREAD)) + market.quoteFee;
|
quoteQuantity = (baseQuantity * primaryPrice * (1 + SPREAD)) + market.quoteFee;
|
||||||
} else if (side === 's') {
|
}
|
||||||
|
else if (side === 's') {
|
||||||
quoteQuantity = (baseQuantity - market.baseFee) * primaryPrice * (1 - SPREAD);
|
quoteQuantity = (baseQuantity - market.baseFee) * primaryPrice * (1 - SPREAD);
|
||||||
}
|
}
|
||||||
} else if (!baseQuantity && quoteQuantity){
|
|
||||||
if (side === 'b') {
|
|
||||||
baseQuantity = (quoteQuantity / (primaryPrice * (1 + SPREAD))) + market.baseFee;
|
|
||||||
} else if (side === 's') {
|
|
||||||
baseQuantity = (quoteQuantity - market.quoteFee) / (primaryPrice * (1 - SPREAD));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Error("badbase/quotequantity");
|
|
||||||
}
|
|
||||||
const quotePrice = (quoteQuantity / baseQuantity).toPrecision(6);
|
const quotePrice = (quoteQuantity / baseQuantity).toPrecision(6);
|
||||||
if (quotePrice < 0) throw new Error("Amount is inadequate to pay fee");
|
if (quotePrice < 0) throw new Error("Amount is inadequate to pay fee");
|
||||||
if (isNaN(quotePrice)) throw new Error("Internal Error. No price generated.");
|
if (isNaN(quotePrice)) throw new Error("Internal Error. No price generated.");
|
||||||
return { quotePrice, baseQuantity, quoteQuantity };
|
return { quotePrice, quoteQuantity };
|
||||||
}
|
}
|
||||||
|
|
||||||
function validatePriceFeed(marketId) {
|
function validatePriceFeed(marketId) {
|
||||||
@@ -358,25 +348,24 @@ async function sendFillRequest(orderreceipt, accountId) {
|
|||||||
const side = orderreceipt[3];
|
const side = orderreceipt[3];
|
||||||
const baseQuantity = orderreceipt[5];
|
const baseQuantity = orderreceipt[5];
|
||||||
const quoteQuantity = orderreceipt[6];
|
const quoteQuantity = orderreceipt[6];
|
||||||
let quote, tokenSell, tokenBuy, sellQuantity, buyQuantity, buySymbol, sellSymbol;
|
const quote = genQuote(chainId, marketId, side, baseQuantity);
|
||||||
|
let tokenSell, tokenBuy, sellQuantity, buyQuantity, buySymbol, sellSymbol;
|
||||||
if (side === "b") {
|
if (side === "b") {
|
||||||
quote = genQuote(chainId, marketId, side, baseQuantity);
|
|
||||||
tokenSell = market.baseAssetId;
|
tokenSell = market.baseAssetId;
|
||||||
tokenBuy = market.quoteAssetId;
|
tokenBuy = market.quoteAssetId;
|
||||||
sellSymbol = market.baseAsset.symbol;
|
sellSymbol = market.baseAsset.symbol;
|
||||||
buySymbol = market.quoteAsset.symbol;
|
buySymbol = market.quoteAsset.symbol;
|
||||||
// Add 1 bip to to protect against rounding errors
|
// Add 1 bip to to protect against rounding errors
|
||||||
sellQuantity = baseQuantity.toFixed(market.baseAsset.decimals); // set by user
|
sellQuantity = (baseQuantity * 1.0001).toFixed(market.baseAsset.decimals);
|
||||||
buyQuantity = quote.quoteQuantity.toFixed(market.quoteAsset.decimals);
|
buyQuantity = (quote.quoteQuantity * 0.9999).toFixed(market.quoteAsset.decimals);
|
||||||
} else if (side === "s") {
|
} else if (side === "s") {
|
||||||
quote = genQuote(chainId, marketId, side, 0, quoteQuantity);
|
|
||||||
tokenSell = market.quoteAssetId;
|
tokenSell = market.quoteAssetId;
|
||||||
tokenBuy = market.baseAssetId;
|
tokenBuy = market.baseAssetId;
|
||||||
sellSymbol = market.quoteAsset.symbol;
|
sellSymbol = market.quoteAsset.symbol;
|
||||||
buySymbol = market.baseAsset.symbol;
|
buySymbol = market.baseAsset.symbol;
|
||||||
// Add 1 bip to to protect against rounding errors
|
// Add 1 bip to to protect against rounding errors
|
||||||
sellQuantity = quoteQuantity.toFixed(market.quoteAsset.decimals); // set by user
|
sellQuantity = (quote.quoteQuantity * 1.0001).toFixed(market.quoteAsset.decimals);
|
||||||
buyQuantity = quote.baseQuantity.toFixed(market.baseAsset.decimals);
|
buyQuantity = (baseQuantity * 0.9999).toFixed(market.baseAsset.decimals);
|
||||||
}
|
}
|
||||||
const sellQuantityParsed = syncProvider.tokenSet.parseToken(
|
const sellQuantityParsed = syncProvider.tokenSet.parseToken(
|
||||||
tokenSell,
|
tokenSell,
|
||||||
|
|||||||
Reference in New Issue
Block a user