-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFeeRelayerState.kt
40 lines (34 loc) · 1.22 KB
/
FeeRelayerState.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.p2p.wallet.send.model
import java.math.BigInteger
import org.p2p.solanaj.kits.TokenExtensionsMap
sealed interface FeeRelayerState {
object Idle : FeeRelayerState
data class ReduceAmount(
val fee: SendSolanaFee,
val newInputAmount: BigInteger
) : FeeRelayerState
data class UpdateFee(
val solanaFee: SendSolanaFee?,
val tokenExtensions: TokenExtensionsMap
) : FeeRelayerState {
val hasToken2022Fee: Boolean
get() = tokenExtensions.isNotEmpty()
}
data class Failure(
val previousState: FeeRelayerState,
val errorStateError: FeeRelayerStateError
) : FeeRelayerState, Throwable() {
fun isFeeCalculationError(): Boolean {
return errorStateError is FeeRelayerStateError.FeesCalculationError
}
}
fun isValidState(): Boolean = this is UpdateFee || this is ReduceAmount || this is Idle
}
fun FeeRelayerState.getFee(): SendSolanaFee? {
return when (this) {
is FeeRelayerState.UpdateFee -> solanaFee
is FeeRelayerState.ReduceAmount -> fee
is FeeRelayerState.Failure -> if (previousState !is FeeRelayerState.Failure) previousState.getFee() else null
else -> null
}
}