@f5i23q999d/cow-sdk
Version:
<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>
36 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateUniqueOrderId = void 0;
const order_signing_1 = require("../order-signing/index.js");
const common_1 = require("../common/index.js");
async function calculateUniqueOrderId(chainId, order, checkEthFlowOrderExists, env) {
const { orderDigest, orderId } = await order_signing_1.OrderSigningUtils.generateOrderId(chainId, {
...order,
sellTokenBalance: order.sellTokenBalance,
buyTokenBalance: order.buyTokenBalance,
validTo: common_1.MAX_VALID_TO_EPOCH,
sellToken: common_1.WRAPPED_NATIVE_CURRENCIES[chainId].address,
}, {
owner: env === 'staging' ? common_1.BARN_ETH_FLOW_ADDRESS : common_1.ETH_FLOW_ADDRESS,
});
if (checkEthFlowOrderExists && (await checkEthFlowOrderExists(orderId, orderDigest))) {
console.error('ETH FLOW', '[calculateUniqueOrderId] ❌ Collision detected: ' + orderId, {
sellAmount: order.sellAmount,
fee: order.feeAmount,
});
// Recursive call, increment one fee until we get an unique order Id
return calculateUniqueOrderId(chainId, adjustAmounts(order), checkEthFlowOrderExists);
}
return orderId;
}
exports.calculateUniqueOrderId = calculateUniqueOrderId;
function adjustAmounts(order) {
const buyAmount = BigInt(order.buyAmount);
// On fee=0, fee is, well, 0. Thus, we cannot shift amounts around and remain with the exact same price.
// Also, we don't want to touch the sell amount.
// If we move it down, the price might become "too good", if we move it up, the user might not have enough funds!
// Thus, we make the buy amount a tad bit worse by 1 wei.
// We can only hope this doesn't happen for an order buying 0 a decimals token 🤞
return { ...order, buyAmount: (buyAmount - BigInt(1)).toString() };
}
//# sourceMappingURL=calculateUniqueOrderId.js.map