Skip to content

Commit

Permalink
CLI-641: USDC sweep into subaccount does not work for fresh address (#…
Browse files Browse the repository at this point in the history
…227)

* improve auto-sweep, default to subaccount 0

* clean up

---------

Co-authored-by: Mike <[email protected]>
  • Loading branch information
mike-dydx and mike-dydx authored Aug 13, 2024
1 parent 590db8e commit 233a3e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,18 @@ 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) }
.eraseToAnyPublisher()
AbacusStateManager.shared.state.walletState
)
.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 233a3e6

Please sign in to comment.