Skip to main content
async def openBulkOrder(
    marketIds: list[int],
    initialMargin: list[float],
    leverage: list[int],
    isLong: list[bool],
    priceThreshold: list[float],
):
    contract = retrieveContract()
    w3 = retrieveWeb3Instance()
    greatestOrderIdByMarket = []
    for i, market in enumerate(marketIds, 0):
        greatestOrderId = 0
        orders = await retrieveOrders()
        for i, order in enumerate(orders, 0):
            if order.market_id == market:
                if order.order_id > greatestOrderId:
                    greatestOrderId = order.order_id
        greatestOrderIdByMarket.append(greatestOrderId)
    print("current ids", greatestOrderIdByMarket)
    openOrders = []
    for i, market in enumerate(marketIds, 0):
        individual = (
            market,
            convertFloatToUINT(initialMargin[i], USDC_DECIMALS),
            convertFloatToUINT(leverage[i], LEVERAGE_SCALAR),
            isLong[i],
            convertFloatToUINT(priceThreshold[i], DEFAULT_TICK_UNITS),
        )
        openOrders.append(individual)
    transaction = contract.functions.openOrdersBatch(openOrders).build_transaction(
        {
            "from": Web3.to_checksum_address(os.getenv("ACCOUNT")),
            "nonce": w3.eth.get_transaction_count(
                Web3.to_checksum_address(os.getenv("ACCOUNT"))
            ),
            "gas": GAS_VALUE,
            "gasPrice": math.ceil(w3.eth.gas_price + (w3.eth.gas_price * 0.25)),
        }
    )

    # TODO: determine how to handle multiple market ids
    await submitTransactionWithOracle(w3, transaction, marketIds[0])
    retVals = []
    for j, market in enumerate(marketIds, 0):
        found = False
        count = 0
        while found == False:
            orders = await retrieveOrders()
            for i, order in enumerate(orders, 0):
                if order.market_id == market:
                    if order.order_id > greatestOrderIdByMarket[j]:
                        greatestOrderId = order.order_id
                        found = True
                        retVal = order
            if found == False:
                count += 1
                if count == 9:
                    break
                time.sleep(5)
        if found == True:
            retVals.append(retVal)
        else:
            print("Max retry attempts reached")
            return
    print(retVals)
    return retVals