diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TransferInputCalculatorV2.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TransferInputCalculatorV2.kt index d8e6f1b98..71e673930 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TransferInputCalculatorV2.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TransferInputCalculatorV2.kt @@ -16,7 +16,7 @@ internal class TransferInputCalculatorV2( wallet: InternalWalletState, subaccountNumber: Int?, ): InternalTransferInputState { - if (wallet.isWalletConnected && transfer.type != null) { + if (wallet.isAccountConnected && transfer.type != null) { finalize(transfer) subaccountTransformer.applyTransferToWallet( @@ -27,7 +27,6 @@ internal class TransferInputCalculatorV2( period = CalculationPeriod.post, ) } - return transfer } diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/payload/BaseTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/payload/BaseTests.kt index dec1ef03e..819b04cd9 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/payload/BaseTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/payload/BaseTests.kt @@ -94,7 +94,7 @@ internal typealias VerificationFunction = (response: StateResponse) -> Unit open class BaseTests( private val maxSubaccountNumber: Int, private val useParentSubaccount: Boolean, - private val staticTyping: Boolean = false, // turn on static typing for testing + private val staticTyping: Boolean = true, // turn on static typing for testing ) { open val doAsserts = true internal val deploymentUri = "https://api.examples.com" diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/payload/TransferInputTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/payload/TransferInputTests.kt index ba8d35aa5..c5f0f6d11 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/payload/TransferInputTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/payload/TransferInputTests.kt @@ -1,11 +1,14 @@ package exchange.dydx.abacus.payload +import exchange.dydx.abacus.calculator.CalculationPeriod +import exchange.dydx.abacus.output.input.TransferType import exchange.dydx.abacus.payload.v4.V4BaseTests import exchange.dydx.abacus.state.model.TransferInputField import exchange.dydx.abacus.state.model.transfer import exchange.dydx.abacus.tests.extensions.log import exchange.dydx.abacus.utils.ServerTime import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertTrue class TransferInputTests : V4BaseTests() { @@ -48,11 +51,17 @@ class TransferInputTests : V4BaseTests() { transfer("1.3", TransferInputField.usdcFee) // if fee is deducted from usdcSize */ - test( - { - perp.transfer("DEPOSIT", TransferInputField.type, environment = mock.v4Environment) - }, - """ + if (perp.staticTyping) { + perp.transfer("DEPOSIT", TransferInputField.type, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.deposit, transfer?.type) + } else { + test( + { + perp.transfer("DEPOSIT", TransferInputField.type, environment = mock.v4Environment) + }, + """ { "input": { "current": "transfer", @@ -61,14 +70,38 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("1", TransferInputField.usdcSize, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer("1", TransferInputField.usdcSize, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.deposit, transfer?.type) + assertEquals("1", transfer?.size?.usdcSize) + + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(108117.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(106641.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(99873.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.27310138690337965, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.013655069345169024, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2132827.5345397857, calculatedPostOrder.buyingPower) + } else { + test( + { + perp.transfer("1", TransferInputField.usdcSize, environment = mock.v4Environment) + }, + """ { "input": { "current": "transfer", @@ -112,14 +145,39 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("5000.0", TransferInputField.usdcSize, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer("5000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.deposit, transfer?.type) + assertEquals("5000.0", transfer?.size?.usdcSize) + assertEquals(5000.0, transfer?.summary?.usdcSize) + + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(113116.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(111640.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(104872.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.2610321394033229, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.013051606970166163, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2232807.5345397857, calculatedPostOrder.buyingPower) + } else { + test( + { + perp.transfer("5000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -169,18 +227,42 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) + """.trimIndent(), + ) + } /* size = 1000.0 */ + if (perp.staticTyping) { + perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.deposit, transfer?.type) + assertEquals("1000.0", transfer?.size?.usdcSize) + assertEquals(1000.0, transfer?.summary?.usdcSize) - test( - { - perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) - }, - """ + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(109116.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(107640.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(100872.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.2706010528035248, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.01353005264017626, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2152807.5345397857, calculatedPostOrder.buyingPower) + } else { + test( + { + perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -230,14 +312,41 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("10.0", TransferInputField.usdcFee, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer("10.0", TransferInputField.usdcFee, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.deposit, transfer?.type) + assertEquals("1000.0", transfer?.size?.usdcSize) + assertEquals(1000.0, transfer?.summary?.usdcSize) + assertEquals(10.0, transfer?.summary?.fee) + assertEquals(true, transfer?.summary?.filled) + + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(109106.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(107630.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(100862.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.27062585430277314, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.013531292715138643, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2152607.5345397857, calculatedPostOrder.buyingPower) + } else { + test( + { + perp.transfer("10.0", TransferInputField.usdcFee, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -290,8 +399,9 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) + """.trimIndent(), + ) + } } private fun testSlowWithdrawalTransferInput() { @@ -314,11 +424,36 @@ class TransferInputTests : V4BaseTests() { perp.transfer("5000.0", TransferInputField.usdcSize, environment = mock.v4Environment) }, null) - test( - { - perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) - }, - """ + if (perp.staticTyping) { + perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.withdrawal, transfer?.type) + assertEquals("1000.0", transfer?.size?.usdcSize) + assertEquals(1000.0, transfer?.summary?.usdcSize) + assertEquals(true, transfer?.summary?.filled) + + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(107116.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(105640.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(98872.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.2756535044256519, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.013782675221282625, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2112807.5345397857, calculatedPostOrder.buyingPower) + } else { + test( + { + perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -369,14 +504,40 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("10.0", TransferInputField.usdcFee, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer("10.0", TransferInputField.usdcFee, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.withdrawal, transfer?.type) + assertEquals("1000.0", transfer?.size?.usdcSize) + assertEquals(1000.0, transfer?.summary?.usdcSize) + assertEquals(true, transfer?.summary?.filled) + + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(107106.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(105630.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(98862.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.2756792407635699, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.013783962038178554, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2112607.5345397857, calculatedPostOrder.buyingPower) + } else { + test( + { + perp.transfer("10.0", TransferInputField.usdcFee, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -427,8 +588,9 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) + """.trimIndent(), + ) + } } private fun testTransferOutTransferInput() { @@ -451,11 +613,39 @@ class TransferInputTests : V4BaseTests() { perp.transfer("test memo", TransferInputField.MEMO, environment = mock.v4Environment) }, null) - test( - { - perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) - }, - """ + if (perp.staticTyping) { + perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.transferOut, transfer?.type) + assertEquals("1000.0", transfer?.size?.usdcSize) + assertEquals(1000.0, transfer?.summary?.usdcSize) + assertEquals(true, transfer?.summary?.filled) + + val subaccount = perp.internalState.wallet.account.subaccounts[0]!! + val calculatedCurrent = subaccount.calculated[CalculationPeriod.current]!! + val calculatedPostOrder = subaccount.calculated[CalculationPeriod.post]!! + assertEquals(108116.7318528828, calculatedCurrent.equity) + assertEquals(107116.7318528828, calculatedPostOrder.equity) + assertEquals(106640.3767269893, calculatedCurrent.freeCollateral) + assertEquals(105640.3767269893, calculatedPostOrder.freeCollateral) + assertEquals(99872.368956, calculatedCurrent.quoteBalance) + assertEquals(98872.368956, calculatedPostOrder.quoteBalance) + assertEquals(0.2731039128897115, calculatedCurrent.leverage) + assertEquals(0.2756535044256519, calculatedPostOrder.leverage) + assertEquals(0.013655195644485585, calculatedCurrent.marginUsage) + assertEquals(0.013782675221282625, calculatedPostOrder.marginUsage) + assertEquals(2132807.5345397857, calculatedCurrent.buyingPower) + assertEquals(2112807.5345397857, calculatedPostOrder.buyingPower) + + assertTrue { perp.state?.input?.transfer?.transferOutOptions?.assets?.count() == 2 } + assertTrue { perp.state?.input?.transfer?.transferOutOptions?.chains?.count() == 1 } + } else { + test( + { + perp.transfer("1000.0", TransferInputField.usdcSize, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -506,20 +696,28 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - { response -> - assertTrue { response.state?.input?.transfer?.transferOutOptions?.assets?.count() == 2 } - assertTrue { response.state?.input?.transfer?.transferOutOptions?.chains?.count() == 1 } - }, - ) + """.trimIndent(), + { response -> + assertTrue { response.state?.input?.transfer?.transferOutOptions?.assets?.count() == 2 } + assertTrue { response.state?.input?.transfer?.transferOutOptions?.chains?.count() == 1 } + }, + ) + } } private fun testTransferInputTypeChange() { - test( - { - perp.transfer("DEPOSIT", TransferInputField.type, environment = mock.v4Environment) - }, - """ + if (perp.staticTyping) { + perp.transfer(data = "DEPOSIT", type = TransferInputField.type, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.deposit, transfer?.type) + assertEquals(null, transfer?.memo) + } else { + test( + { + perp.transfer(data = "DEPOSIT", type = TransferInputField.type, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -528,14 +726,22 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("TRANSFER_OUT", TransferInputField.type, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer(data = "TRANSFER_OUT", type = TransferInputField.type, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.transferOut, transfer?.type) + assertEquals(null, transfer?.memo) + } else { + test( + { + perp.transfer(data = "TRANSFER_OUT", type = TransferInputField.type, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -544,14 +750,26 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("test memo", TransferInputField.MEMO, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer(data = "test memo", type = TransferInputField.MEMO, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.transferOut, transfer?.type) + assertEquals("test memo", transfer?.memo) + } else { + test( + { + perp.transfer( + data = "test memo", + type = TransferInputField.MEMO, + environment = mock.v4Environment, + ) + }, + """ { "input": { "transfer": { @@ -560,14 +778,22 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) - - test( - { - perp.transfer("WITHDRAWAL", TransferInputField.type, environment = mock.v4Environment) - }, - """ + """.trimIndent(), + ) + } + + if (perp.staticTyping) { + perp.transfer("WITHDRAWAL", TransferInputField.type, environment = mock.v4Environment) + + val transfer = perp.state?.input?.transfer + assertEquals(TransferType.withdrawal, transfer?.type) + assertEquals(null, transfer?.memo) + } else { + test( + { + perp.transfer("WITHDRAWAL", TransferInputField.type, environment = mock.v4Environment) + }, + """ { "input": { "transfer": { @@ -576,7 +802,8 @@ class TransferInputTests : V4BaseTests() { } } } - """.trimIndent(), - ) + """.trimIndent(), + ) + } } } diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4BaseTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4BaseTests.kt index cce685e8b..e052dfebc 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4BaseTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4BaseTests.kt @@ -28,7 +28,9 @@ import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue -open class V4BaseTests(useParentSubaccount: Boolean = false) : BaseTests(127, useParentSubaccount) { +open class V4BaseTests( + useParentSubaccount: Boolean = false +) : BaseTests(127, useParentSubaccount) { internal val testWsUrl = AbUrl.fromString("wss://indexer.v4staging.dydx.exchange/v4/ws") internal val testRestUrl =