Skip to content

Commit

Permalink
improve auto-sweep, default to subaccount 0
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Aug 13, 2024
1 parent 590db8e commit 6f50c87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,19 @@ final class dydxTransferSubaccountWorker: BaseWorker {
super.start()

AbacusStateManager.shared.state.accountBalance(of: AbacusStateManager.shared.environment?.usdcTokenInfo?.denom)
.filter { value in
(value ?? 0) > dydxTransferSubaccountWorker.balanceRetainAmount
}
.withLatestFrom(
Publishers.CombineLatest(
AbacusStateManager.shared.state.walletState,
AbacusStateManager.shared.state.selectedSubaccount
)
.map { (walletState: $0, subaccount: $1) }
AbacusStateManager.shared.state.walletState
.eraseToAnyPublisher()
)
.sink { [weak self] balance, state in
let subaccountNumber: Int
if let subaccount = state.subaccount {
subaccountNumber = Int(subaccount.subaccountNumber)
} else {
subaccountNumber = 0
}
let depositAmount = (balance ?? 0) - dydxTransferSubaccountWorker.balanceRetainAmount
.sink { [weak self] balance, walletState in
guard let balance, balance > dydxTransferSubaccountWorker.balanceRetainAmount else { return }
let depositAmount = balance - dydxTransferSubaccountWorker.balanceRetainAmount
let amountString = dydxFormatter.shared.decimalLocaleAgnostic(number: NSNumber(value: depositAmount),
digits: dydxTokenConstants.usdcTokenDecimal)
if let amountString = amountString {
self?.depositToSubaccount(amount: amountString,
subaccount: subaccountNumber,
walletState: state.walletState)
subaccount: AbacusStateManager.shared.selectedSubaccountNumber,
walletState: walletState)
} else {
Console.shared.log("dydxTransferSubaccountWorker: Invalid amount")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public final class AbacusStateManager: NSObject {
public var environment: V4Environment? {
asyncStateManager.environment
}

public var selectedSubaccountNumber: Int {
Int(asyncStateManager.subaccountNumber)
}

public var appSetting: AppSetting? {
asyncStateManager.appSettings?.ios
Expand Down Expand Up @@ -134,7 +138,7 @@ public final class AbacusStateManager: NSObject {

private var isStarted = false

private lazy var asyncStateManager: AsyncAbacusStateManagerProtocol & AsyncAbacusStateManagerSingletonProtocol = {
private lazy var asyncStateManager: SingletonAsyncAbacusStateManagerProtocol = {
UIImplementations.reset(language: nil)

let deployment: String
Expand Down

0 comments on commit 6f50c87

Please sign in to comment.