Skip to content

Commit

Permalink
fix: we have to handle null account data since that's what we get whe…
Browse files Browse the repository at this point in the history
…n a user withdraws 100% of their balance (#713)
  • Loading branch information
tyleroooo authored Oct 16, 2024
1 parent 98b2a5b commit bb5b492
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.12.28"
version = "1.12.29"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -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 = ''
Expand Down

0 comments on commit bb5b492

Please sign in to comment.