|
| 1 | +package dev.amg.payguard.collateral.domain; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.math.RoundingMode; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +@SuppressWarnings({"PMD.AvoidFieldNameMatchingMethodName"}) |
| 11 | +public final class Collateral { |
| 12 | + private final UUID id; |
| 13 | + private final String ownerId; |
| 14 | + private final UUID loanId; |
| 15 | + private final BigDecimal amount; |
| 16 | + private final String assetCurrency; |
| 17 | + private final BigDecimal warningLtv; |
| 18 | + private final BigDecimal marginLtv; |
| 19 | + private final BigDecimal liquidationLtv; |
| 20 | + private CollateralStatus status; |
| 21 | + private Valuation latest; |
| 22 | + private final List<Valuation> history = new ArrayList<>(); |
| 23 | + |
| 24 | + public Collateral( |
| 25 | + UUID id, |
| 26 | + String ownerId, |
| 27 | + UUID loanId, |
| 28 | + BigDecimal amount, |
| 29 | + String assetCurrency, |
| 30 | + BigDecimal warningLtv, |
| 31 | + BigDecimal marginLtv, |
| 32 | + BigDecimal liquidationLtv) { |
| 33 | + this.id = Objects.requireNonNull(id); |
| 34 | + this.ownerId = Objects.requireNonNull(ownerId); |
| 35 | + this.loanId = Objects.requireNonNull(loanId); |
| 36 | + if (amount == null || amount.signum() <= 0) throw new IllegalArgumentException("amount"); |
| 37 | + this.amount = amount; |
| 38 | + this.assetCurrency = Objects.requireNonNull(assetCurrency); |
| 39 | + this.warningLtv = threshold(warningLtv); |
| 40 | + this.marginLtv = threshold(marginLtv); |
| 41 | + this.liquidationLtv = threshold(liquidationLtv); |
| 42 | + if (this.warningLtv.compareTo(this.marginLtv) >= 0 |
| 43 | + || this.marginLtv.compareTo(this.liquidationLtv) >= 0) |
| 44 | + throw new IllegalArgumentException("threshold order"); |
| 45 | + status = CollateralStatus.AVAILABLE; |
| 46 | + } |
| 47 | + |
| 48 | + public void lock() { |
| 49 | + if (status != CollateralStatus.AVAILABLE) |
| 50 | + throw new IllegalStateException("collateral unavailable"); |
| 51 | + status = CollateralStatus.LOCKED; |
| 52 | + } |
| 53 | + |
| 54 | + public Risk updateValuation(Valuation valuation, BigDecimal exposure) { |
| 55 | + Objects.requireNonNull(valuation); |
| 56 | + if (status == CollateralStatus.RELEASED || status == CollateralStatus.LIQUIDATED) |
| 57 | + throw new IllegalStateException("collateral closed"); |
| 58 | + latest = valuation; |
| 59 | + history.add(valuation); |
| 60 | + BigDecimal ltv = |
| 61 | + exposure.signum() == 0 |
| 62 | + ? BigDecimal.ZERO |
| 63 | + : exposure.divide(valuation.amount(), 18, RoundingMode.HALF_EVEN); |
| 64 | + if (ltv.compareTo(liquidationLtv) >= 0) status = CollateralStatus.LIQUIDATING; |
| 65 | + else if (ltv.compareTo(marginLtv) >= 0) status = CollateralStatus.MARGIN_CALL; |
| 66 | + else if (status == CollateralStatus.MARGIN_CALL && ltv.compareTo(warningLtv) < 0) |
| 67 | + status = CollateralStatus.LOCKED; |
| 68 | + return new Risk(ltv, status); |
| 69 | + } |
| 70 | + |
| 71 | + public void markLiquidated() { |
| 72 | + if (status != CollateralStatus.LIQUIDATING && status != CollateralStatus.MARGIN_CALL) |
| 73 | + throw new IllegalStateException("not liquidatable"); |
| 74 | + status = CollateralStatus.LIQUIDATED; |
| 75 | + } |
| 76 | + |
| 77 | + public void release() { |
| 78 | + if (status != CollateralStatus.LOCKED && status != CollateralStatus.MARGIN_CALL) |
| 79 | + throw new IllegalStateException("not releasable"); |
| 80 | + status = CollateralStatus.RELEASED; |
| 81 | + } |
| 82 | + |
| 83 | + private static BigDecimal threshold(BigDecimal v) { |
| 84 | + if (v == null || v.signum() < 0 || v.compareTo(BigDecimal.ONE) > 0) |
| 85 | + throw new IllegalArgumentException("LTV threshold"); |
| 86 | + return v; |
| 87 | + } |
| 88 | + |
| 89 | + public record Risk(BigDecimal ltv, CollateralStatus status) {} |
| 90 | + |
| 91 | + public UUID id() { |
| 92 | + return id; |
| 93 | + } |
| 94 | + |
| 95 | + public String ownerId() { |
| 96 | + return ownerId; |
| 97 | + } |
| 98 | + |
| 99 | + public UUID loanId() { |
| 100 | + return loanId; |
| 101 | + } |
| 102 | + |
| 103 | + public BigDecimal amount() { |
| 104 | + return amount; |
| 105 | + } |
| 106 | + |
| 107 | + public String assetCurrency() { |
| 108 | + return assetCurrency; |
| 109 | + } |
| 110 | + |
| 111 | + public CollateralStatus status() { |
| 112 | + return status; |
| 113 | + } |
| 114 | + |
| 115 | + public Valuation latest() { |
| 116 | + return latest; |
| 117 | + } |
| 118 | + |
| 119 | + public List<Valuation> history() { |
| 120 | + return List.copyOf(history); |
| 121 | + } |
| 122 | +} |
0 commit comments