From 54e6bba59fb1bae86a4a159f10a648244a2aa34d Mon Sep 17 00:00:00 2001 From: Tyler Date: Tue, 15 Oct 2024 19:22:02 -0400 Subject: [PATCH] empty handle --- build.gradle.kts | 2 +- .../functional/vault/VaultAccount.kt | 20 ++++--- .../functional/vault/VaultAccountTests.kt | 54 +++++++++++++++++++ v4_abacus.podspec | 2 +- 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f824b3693..e0742adb3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -52,7 +52,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.12.28" +version = "1.12.29" repositories { google() 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/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) + } } diff --git a/v4_abacus.podspec b/v4_abacus.podspec index 0ef587c71..933d5126f 100644 --- a/v4_abacus.podspec +++ b/v4_abacus.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'v4_abacus' - spec.version = '1.12.28' + spec.version = '1.12.29' spec.homepage = 'https://github.com/dydxprotocol/v4-abacus' spec.source = { :http=> ''} spec.authors = ''