roll back

This commit is contained in:
TrooperCrypto
2022-03-27 15:39:37 +02:00
parent 819299ad04
commit 4a96390c73

View File

@@ -274,44 +274,34 @@ 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);
const mmConfig = MM_CONFIG.pairs[marketId]; const mmConfig = MM_CONFIG.pairs[marketId];
const mmSide = mmConfig.side || 'd'; const mmSide = mmConfig.side || 'd';
if (mmSide !== 'd' && mmSide === side) { if (mmSide !== 'd' && mmSide === side) {
throw new Error("badside"); throw new Error("badside");
} }
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') { }
quoteQuantity = (baseQuantity - market.baseFee) * primaryPrice * (1 - SPREAD); else if (side === 's') {
} quoteQuantity = (baseQuantity - market.baseFee) * primaryPrice * (1 - SPREAD);
} else if (!baseQuantity && quoteQuantity){ }
if (side === 'b') { const quotePrice = (quoteQuantity / baseQuantity).toPrecision(6);
baseQuantity = (quoteQuantity / (primaryPrice * (1 + SPREAD))) + market.baseFee; if (quotePrice < 0) throw new Error("Amount is inadequate to pay fee");
} else if (side === 's') { if (isNaN(quotePrice)) throw new Error("Internal Error. No price generated.");
baseQuantity = (quoteQuantity - market.quoteFee) / (primaryPrice * (1 - SPREAD)); return { quotePrice, quoteQuantity };
}
} else {
throw new Error("badbase/quotequantity");
}
const quotePrice = (quoteQuantity / baseQuantity).toPrecision(6);
if (quotePrice < 0) throw new Error("Amount is inadequate to pay fee");
if (isNaN(quotePrice)) throw new Error("Internal Error. No price generated.");
return { quotePrice, baseQuantity, quoteQuantity };
} }
function validatePriceFeed(marketId) { function validatePriceFeed(marketId) {
@@ -349,72 +339,71 @@ function validatePriceFeed(marketId) {
} }
async function sendFillRequest(orderreceipt, accountId) { async function sendFillRequest(orderreceipt, accountId) {
const chainId = orderreceipt[0]; const chainId = orderreceipt[0];
const orderId = orderreceipt[1]; const orderId = orderreceipt[1];
const marketId = orderreceipt[2]; const marketId = orderreceipt[2];
const market = MARKETS[marketId]; const market = MARKETS[marketId];
const baseCurrency = market.baseAssetId; const baseCurrency = market.baseAssetId;
const quoteCurrency = market.quoteAssetId; const quoteCurrency = market.quoteAssetId;
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);
if (side === "b") { let tokenSell, tokenBuy, sellQuantity, buyQuantity, buySymbol, sellSymbol;
quote = genQuote(chainId, marketId, side, baseQuantity); if (side === "b") {
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,
sellQuantity sellQuantity
); );
const sellQuantityPacked = zksync.utils.closestPackableTransactionAmount(sellQuantityParsed); const sellQuantityPacked = zksync.utils.closestPackableTransactionAmount(sellQuantityParsed);
const tokenRatio = {}; const tokenRatio = {};
tokenRatio[tokenBuy] = buyQuantity; tokenRatio[tokenBuy] = buyQuantity;
tokenRatio[tokenSell] = sellQuantity; tokenRatio[tokenSell] = sellQuantity;
const oneMinExpiry = (Date.now() / 1000 | 0) + 60; const oneMinExpiry = (Date.now() / 1000 | 0) + 60;
const orderDetails = { const orderDetails = {
tokenSell, tokenSell,
tokenBuy, tokenBuy,
amount: sellQuantityPacked, amount: sellQuantityPacked,
ratio: zksync.utils.tokenRatio(tokenRatio), ratio: zksync.utils.tokenRatio(tokenRatio),
validUntil: oneMinExpiry validUntil: oneMinExpiry
} }
const fillOrder = await WALLETS[accountId].syncWallet.getOrder(orderDetails); const fillOrder = await WALLETS[accountId].syncWallet.getOrder(orderDetails);
// Set wallet flag // Set wallet flag
WALLETS[accountId]['ORDER_BROADCASTING'] = true; WALLETS[accountId]['ORDER_BROADCASTING'] = true;
// ORDER_BROADCASTING should not take longer as 5 sec // ORDER_BROADCASTING should not take longer as 5 sec
setTimeout(function() { setTimeout(function() {
WALLETS[accountId]['ORDER_BROADCASTING'] = false; WALLETS[accountId]['ORDER_BROADCASTING'] = false;
}, 5000); }, 5000);
const resp = { op: "fillrequest", args: [chainId, orderId, fillOrder] }; const resp = { op: "fillrequest", args: [chainId, orderId, fillOrder] };
zigzagws.send(JSON.stringify(resp)); zigzagws.send(JSON.stringify(resp));
rememberOrder(chainId, rememberOrder(chainId,
marketId, marketId,
orderId, orderId,
quote.quotePrice, quote.quotePrice,
sellSymbol, sellSymbol,
sellQuantity, sellQuantity,
buySymbol, buySymbol,
buyQuantity buyQuantity
); );
} }
async function broadcastFill(chainId, orderId, swapOffer, fillOrder, wallet) { async function broadcastFill(chainId, orderId, swapOffer, fillOrder, wallet) {