Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
clean up
  • Loading branch information
mike-dydx committed May 2, 2024
1 parent be82d14 commit 5fc6a38
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 66 deletions.
4 changes: 4 additions & 0 deletions Utilities/Utilities.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
02F958052A182BC400828F9A /* SecureStoreProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02F958042A182BC400828F9A /* SecureStoreProtocol.swift */; };
279B87992B97CD4C00466392 /* PointsRating.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B87972B97CD4C00466392 /* PointsRating.swift */; };
279B879A2B97CD4C00466392 /* RatingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B87982B97CD4C00466392 /* RatingService.swift */; };
27E31E5B2BE40E4000580E59 /* SynchronizedLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27E31E5A2BE40E4000580E59 /* SynchronizedLock.swift */; };
3101F94725112C4100AC4010 /* AuthProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3101F94625112C4100AC4010 /* AuthProtocol.swift */; };
3101F94A25112C5900AC4010 /* AuthService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3101F94925112C5900AC4010 /* AuthService.swift */; };
310E61E9216C0F910043BB33 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 310E61E8216C0F910043BB33 /* Security.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
Expand Down Expand Up @@ -302,6 +303,7 @@
02F958042A182BC400828F9A /* SecureStoreProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecureStoreProtocol.swift; sourceTree = "<group>"; };
279B87972B97CD4C00466392 /* PointsRating.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PointsRating.swift; sourceTree = "<group>"; };
279B87982B97CD4C00466392 /* RatingService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RatingService.swift; sourceTree = "<group>"; };
27E31E5A2BE40E4000580E59 /* SynchronizedLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizedLock.swift; sourceTree = "<group>"; };
3101F94625112C4100AC4010 /* AuthProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthProtocol.swift; sourceTree = "<group>"; };
3101F94925112C5900AC4010 /* AuthService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthService.swift; sourceTree = "<group>"; };
310E61E8216C0F910043BB33 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -643,6 +645,7 @@
025B752C28A7041600A4AC98 /* ClassLoader.swift */,
0253CEC12A9FFF8D0033F064 /* CachedFileLoader.swift */,
648327432AF5665600012B75 /* NumericUtils.swift */,
27E31E5A2BE40E4000580E59 /* SynchronizedLock.swift */,
);
path = _Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -1380,6 +1383,7 @@
314B640023DCCF0200139EB3 /* MapArea.swift in Sources */,
6476164127E23FBC008ACFD9 /* LocalAuthenticator.swift in Sources */,
314B644823DCCF0200139EB3 /* NotificationToken.swift in Sources */,
27E31E5B2BE40E4000580E59 /* SynchronizedLock.swift in Sources */,
3162C00A24C377EF00DE648C /* PrompterFactory.swift in Sources */,
314B646C23DCCF0200139EB3 /* XibLoader.swift in Sources */,
279B879A2B97CD4C00466392 /* RatingService.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions Utilities/Utilities/_Utils/SynchronizedLock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SynchronizedLock.swift
// Utilities
//
// Created by Michael Maguire on 5/2/24.
// Copyright © 2024 dYdX Trading Inc. All rights reserved.
//


import Darwin

@propertyWrapper
public struct SynchronizedLock<Value> {
private var value: Value
private var lock = NSLock()

public var wrappedValue: Value {
get { lock.synchronized { value } }
set { lock.synchronized { value = newValue } }
}

public init(wrappedValue value: Value) {
self.value = value
}
}

private extension NSLock {

@discardableResult
func synchronized<T>(_ block: () -> T) -> T {
lock()
defer { unlock() }
return block()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private protocol dydxTakeProfitStopLossViewPresenterProtocol: HostedViewPresente

private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter<dydxTakeProfitStopLossViewModel>, dydxTakeProfitStopLossViewPresenterProtocol {
fileprivate var marketId: String?

@SynchronizedLock private var pendingOrders: Int?

deinit {
clearTriggersInput()
}
Expand Down Expand Up @@ -179,13 +180,13 @@ private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter<dydxTakeP

let formattedTakeProfitUsdcDiff = dydxFormatter.shared.raw(number: triggerOrdersInput?.takeProfitOrder?.price?.usdcDiff?.doubleValue, digits: 2) ?? ""
let formattedTakeProfitUsdcPercentage = dydxFormatter.shared.raw(number: triggerOrdersInput?.takeProfitOrder?.price?.percentDiff?.doubleValue, digits: 2) ?? ""
viewModel?.takeProfitStopLossInputAreaViewModel?.gainInputViewModel?.programmaticallySet(value: formattedTakeProfitUsdcDiff, forUnit: .dollars)
viewModel?.takeProfitStopLossInputAreaViewModel?.gainInputViewModel?.programmaticallySet(value: formattedTakeProfitUsdcPercentage, forUnit: .percentage)
viewModel?.takeProfitStopLossInputAreaViewModel?.gainInputViewModel?.dollars = formattedTakeProfitUsdcDiff
viewModel?.takeProfitStopLossInputAreaViewModel?.gainInputViewModel?.percentage = formattedTakeProfitUsdcPercentage

let formattedStopLossUsdcDiff = dydxFormatter.shared.raw(number: triggerOrdersInput?.stopLossOrder?.price?.usdcDiff?.doubleValue, digits: 2) ?? ""
let formattedStopLossUsdcPercentage = dydxFormatter.shared.raw(number: triggerOrdersInput?.stopLossOrder?.price?.percentDiff?.doubleValue, digits: 2) ?? ""
viewModel?.takeProfitStopLossInputAreaViewModel?.lossInputViewModel?.programmaticallySet(value: formattedStopLossUsdcDiff, forUnit: .dollars)
viewModel?.takeProfitStopLossInputAreaViewModel?.lossInputViewModel?.programmaticallySet(value: formattedStopLossUsdcPercentage, forUnit: .percentage)
viewModel?.takeProfitStopLossInputAreaViewModel?.lossInputViewModel?.dollars = formattedStopLossUsdcDiff
viewModel?.takeProfitStopLossInputAreaViewModel?.lossInputViewModel?.percentage = formattedStopLossUsdcPercentage

// logic primarily to pre-populate custom amount. need to account 3 situations: 1 take profit order or 1 stop loss order or both
if let customSize = triggerOrdersInput?.size?.doubleValue.magnitude, customSize != position?.size?.current?.doubleValue.magnitude {
Expand Down Expand Up @@ -321,24 +322,7 @@ private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter<dydxTakeP
viewModel.takeProfitStopLossInputAreaViewModel?.takeProfitPriceInputViewModel = .init(title: DataLocalizer.shared?.localize(path: "APP.TRIGGERS_MODAL.TP_PRICE", params: nil))
viewModel.takeProfitStopLossInputAreaViewModel?.stopLossPriceInputViewModel = .init(title: DataLocalizer.shared?.localize(path: "APP.TRIGGERS_MODAL.SL_PRICE", params: nil))

viewModel.takeProfitStopLossInputAreaViewModel?.gainInputViewModel = .init(triggerType: .takeProfit) { (value, unit) in
switch unit {
case .dollars:
AbacusStateManager.shared.triggerOrders(input: value, type: .takeprofitusdcdiff)
case .percentage:
AbacusStateManager.shared.triggerOrders(input: value, type: .takeprofitpercentdiff)
}
}
viewModel.takeProfitStopLossInputAreaViewModel?.lossInputViewModel = .init(triggerType: .stopLoss) { (value, unit) in
switch unit {
case .dollars:
AbacusStateManager.shared.triggerOrders(input: value, type: .stoplossusdcdiff)
case .percentage:
AbacusStateManager.shared.triggerOrders(input: value, type: .stoplosspercentdiff)
}
}

#if DEBUG
#if DEBUG
viewModel.shouldDisplayCustomLimitPriceViewModel = AbacusStateManager.shared.environment?.featureFlags.isSlTpLimitOrdersEnabled == true
#endif

Expand Down Expand Up @@ -366,6 +350,22 @@ private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter<dydxTakeP
viewModel.customLimitPriceViewModel?.stopLossPriceInputViewModel?.onEdited = {
AbacusStateManager.shared.triggerOrders(input: $0, type: .stoplosslimitprice)
}
viewModel.takeProfitStopLossInputAreaViewModel?.gainInputViewModel = .init(triggerType: .takeProfit) { (value, unit) in
switch unit {
case .dollars:
AbacusStateManager.shared.triggerOrders(input: value, type: .takeprofitusdcdiff)
case .percentage:
AbacusStateManager.shared.triggerOrders(input: value, type: .takeprofitpercentdiff)
}
}
viewModel.takeProfitStopLossInputAreaViewModel?.lossInputViewModel = .init(triggerType: .stopLoss) { (value, unit) in
switch unit {
case .dollars:
AbacusStateManager.shared.triggerOrders(input: value, type: .stoplossusdcdiff)
case .percentage:
AbacusStateManager.shared.triggerOrders(input: value, type: .stoplosspercentdiff)
}
}

// set up toggle interactions
viewModel.customLimitPriceViewModel?.toggleAction = { _ in
Expand Down Expand Up @@ -406,9 +406,6 @@ private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter<dydxTakeP

self.viewModel = viewModel
}

@SynchronizedLock var pendingOrders: Int?

}

private extension Abacus.OrderSide {
Expand All @@ -420,30 +417,3 @@ private extension Abacus.OrderSide {
}
}
}

import Darwin

@propertyWrapper
public struct SynchronizedLock<Value> {
private var value: Value
private var lock = NSLock()

public var wrappedValue: Value {
get { lock.synchronized { value } }
set { lock.synchronized { value = newValue } }
}

public init(wrappedValue value: Value) {
self.value = value
}
}

private extension NSLock {

@discardableResult
func synchronized<T>(_ block: () -> T) -> T {
lock()
defer { unlock() }
return block()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ extension AbacusStateManager {
case success
case failed(Abacus.ParsingError?)
}


/// places the currently drafted trigger order(s)
/// - Returns: the number of resulting cancel orders + place order requests
public func placeTriggerOrders(callback: @escaping ((SubmissionStatus) -> Void)) -> Int? {
let payload = asyncStateManager.commitTriggerOrders { successful, error, _ in
if successful.boolValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class dydxGainLossInputViewModel: PlatformViewModeling {
@Published fileprivate var onEdited: ((String?, Unit) -> Void)?
@Published fileprivate var isPresentingUnitOptions: Bool = false

@Published fileprivate var dollars: String = ""
@Published fileprivate var percentage: String = ""
@Published public var dollars: String = ""
@Published public var percentage: String = ""

fileprivate var displayText: String {
switch curUnit {
Expand All @@ -46,16 +46,6 @@ public class dydxGainLossInputViewModel: PlatformViewModeling {
}
}

/// Attempts to set the value for the unit. Will not set if actively editing/has focus.
public func programmaticallySet(value: String, forUnit unit: Unit) {
switch unit {
case .dollars:
dollars = value
case .percentage:
percentage = value
}
}

public func clear() {
dollars = ""
percentage = ""
Expand Down

0 comments on commit 5fc6a38

Please sign in to comment.