diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultAccount.kt b/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultAccount.kt index 482bd2fd5..6b5ff642b 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultAccount.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultAccount.kt @@ -71,19 +71,17 @@ object VaultAccountCalculator { } fun calculateUserVaultInfo( - vaultInfo: OnChainAccountVaultResponse, + vaultInfo: OnChainAccountVaultResponse?, vaultTransfers: IndexerTransferBetweenResponse, ): VaultAccount { - val presentValue = vaultInfo.equity?.let { it / 1_000_000 } + val presentValue = (vaultInfo?.equity ?: 0.0) / 1_000_000 val netTransfers = parser.asDouble(vaultTransfers.totalNetTransfers) - val withdrawable = vaultInfo.withdrawableEquity?.let { it / 1_000_000 } - val allTimeReturn = - if (presentValue != null && netTransfers != null) (presentValue - netTransfers) else null + val withdrawable = (vaultInfo?.withdrawableEquity ?: 0.0) / 1_000_000 + val allTimeReturn = if (netTransfers != null) (presentValue - netTransfers) else null val impliedShareValue: Double = if ( - vaultInfo.shares?.numShares != null && - vaultInfo.shares.numShares > 0 && - presentValue != null + vaultInfo?.shares?.numShares != null && + vaultInfo.shares.numShares > 0 ) { presentValue / vaultInfo.shares.numShares } else { @@ -92,8 +90,8 @@ object VaultAccountCalculator { return VaultAccount( balanceUsdc = presentValue, - balanceShares = vaultInfo.shares?.numShares, - lockedShares = vaultInfo.shareUnlocks?.sumOf { el -> el.shares?.numShares ?: 0.0 }, + balanceShares = vaultInfo?.shares?.numShares ?: 0.0, + lockedShares = vaultInfo?.shareUnlocks?.sumOf { el -> el.shares?.numShares ?: 0.0 } ?: 0.0, withdrawableUsdc = withdrawable, allTimeReturnUsdc = allTimeReturn, totalVaultTransfersCount = vaultTransfers.totalResults, @@ -110,7 +108,7 @@ object VaultAccountCalculator { transactionHash = el.transactionHash, ) }?.toIList(), - vaultShareUnlocks = vaultInfo.shareUnlocks?.map { el -> + vaultShareUnlocks = vaultInfo?.shareUnlocks?.map { el -> VaultShareUnlock( unlockBlockHeight = el.unlockBlockHeight, amountUsdc = el.shares?.numShares?.let { it * impliedShareValue }, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/Environment.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/Environment.kt index a84f3d590..06864aacf 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/Environment.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/Environment.kt @@ -552,6 +552,12 @@ data object StatsigConfig { var ff_enable_timestamp_nonce: Boolean = false } +@JsExport +@Suppress("PropertyName") +data object AutoSweepConfig { + var disable_autosweep: Boolean = false +} + @JsExport class AppSettings( val ios: AppSetting?, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/AccountSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/AccountSupervisor.kt index 1bb4c1f81..c294fbf84 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/AccountSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/AccountSupervisor.kt @@ -19,6 +19,7 @@ import exchange.dydx.abacus.state.app.adaptors.V4TransactionErrors import exchange.dydx.abacus.state.changes.Changes import exchange.dydx.abacus.state.changes.StateChanges import exchange.dydx.abacus.state.manager.ApiData +import exchange.dydx.abacus.state.manager.AutoSweepConfig import exchange.dydx.abacus.state.manager.BlockAndTime import exchange.dydx.abacus.state.manager.HistoricalTradingRewardsPeriod import exchange.dydx.abacus.state.manager.HumanReadableCancelAllOrdersPayload @@ -468,6 +469,9 @@ internal open class AccountSupervisor( nobleBalancesTimer = null return } + if (AutoSweepConfig.disable_autosweep) { + return + } val timer = helper.ioImplementations.timer ?: CoroutineTimer.instance nobleBalancesTimer = timer.schedule(0.0, nobleBalancePollingDuration) { diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultAccountTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultAccountTests.kt index 912ec99b5..647230825 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultAccountTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultAccountTests.kt @@ -76,4 +76,58 @@ class VaultAccountTests { assertEquals(expectedVaultAccount, vaultAccount) } + + @Test + fun calculateUserVaultInfo_empty() { + val vaultTransfers = IndexerTransferBetweenResponse( + totalResults = 2, + totalNetTransfers = "-500.0", + transfersSubset = arrayOf( + IndexerTransferResponseObject( + id = "1", + createdAt = Instant.fromEpochMilliseconds(1659465600000).toString(), + size = "6000.0", + type = IndexerTransferType.TRANSFER_OUT, + transactionHash = "tx1", + ), + IndexerTransferResponseObject( + id = "2", + createdAt = Instant.fromEpochMilliseconds(1659552000000).toString(), + size = "6500.0", + type = IndexerTransferType.TRANSFER_IN, + transactionHash = "tx2", + ), + ), + ) + + val vaultAccount = calculateUserVaultInfo(null, vaultTransfers) + + val expectedVaultAccount = VaultAccount( + balanceUsdc = 0.0, + withdrawableUsdc = 0.0, + allTimeReturnUsdc = 500.0, + totalVaultTransfersCount = 2, + balanceShares = 0.0, + lockedShares = 0.0, + vaultTransfers = iListOf( + VaultTransfer( + timestampMs = 1659465600000.0, + amountUsdc = 6000.0, + type = VaultTransferType.DEPOSIT, + id = "1", + transactionHash = "tx1", + ), + VaultTransfer( + timestampMs = 1659552000000.0, + amountUsdc = 6500.0, + type = VaultTransferType.WITHDRAWAL, + id = "2", + transactionHash = "tx2", + ), + ), + vaultShareUnlocks = null, + ) + + assertEquals(expectedVaultAccount, vaultAccount) + } }