Skip to content

Commit

Permalink
fix: help users not leave dust in the vault (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo authored Oct 15, 2024
1 parent 6b16c51 commit 43b096f
Show file tree
Hide file tree
Showing 4 changed files with 103 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.24"
version = "1.12.25"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kollections.toIList
import kotlinx.serialization.Serializable
import kotlin.js.JsExport
import kotlin.math.abs
import kotlin.math.floor

@JsExport
@Serializable
Expand Down Expand Up @@ -245,7 +244,17 @@ object VaultDepositWithdrawFormValidator {
if (shareValue == 0.0) {
return 0.0
}
return (amount / shareValue).toLong().toDouble()

val amountToUse = if (vaultAccount?.withdrawableUsdc != null &&
vaultAccount.withdrawableUsdc - amount >= 0 &&
vaultAccount.withdrawableUsdc - amount <= 0.01
) {
vaultAccount.withdrawableUsdc
} else {
amount
}

return (amountToUse / shareValue).toLong().toDouble()
}

fun validateVaultForm(
Expand All @@ -260,15 +269,8 @@ object VaultDepositWithdrawFormValidator {
var submissionData: VaultDepositWithdrawSubmissionData? = null

// Calculate post-operation values and slippage
val amount = formData.amount ?: 0.0

val shareValue = vaultAccount?.shareValue
val sharesToAttemptWithdraw = if (amount > 0 && shareValue != null && shareValue > 0) {
// shares must be whole numbers
floor(amount / shareValue)
} else {
null
}
val sharesToAttemptWithdraw = calculateSharesToWithdraw(vaultAccount, formData.amount ?: 0.0)
val amount = sharesToAttemptWithdraw * (vaultAccount?.shareValue ?: 0.0)

val withdrawnAmountIncludingSlippage = slippageResponse?.expectedQuoteQuantums?.let { it / 1_000_000.0 }
val postOpVaultBalance = when (formData.action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,94 @@ class VaultFormTests {
)
}

@Test
fun testShareCalculationCloseToZero() {
assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 500.0,
),
500000.0,
)

assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 499.02,
),
499020.0,
)

assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 499.07,
),
499070.0,
)

assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 499.02,
),
499020.0,
)

assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 499.989,
),
// actually off by one because of double precision + cast to long
499988.0,
)

assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 499.99,
),
500000.0,
)

assertEquals(
VaultDepositWithdrawFormValidator.calculateSharesToWithdraw(
vaultAccount = makeVaultAccount(
balanceUsdc = 600.0,
balanceShares = 600000.0,
withdrawableUsdc = 500.0,
),
amount = 499.992,
),
500000.0,
)
}

@Test
fun testDepositValidation() {
val result = VaultDepositWithdrawFormValidator.validateVaultForm(
Expand Down
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.24'
spec.version = '1.12.25'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 43b096f

Please sign in to comment.