Skip to content

Commit 78b0e7a

Browse files
fix: STUD-475 USD->NEWM calculation for royalties
1 parent a96fc74 commit 78b0e7a

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

newm-server/src/main/kotlin/io/newm/server/features/earnings/repo/EarningsRepositoryImpl.kt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class EarningsRepositoryImpl(
9797
songId: SongId,
9898
royaltyRequest: AddSongRoyaltyRequest
9999
) {
100+
val streamTokenTotalSupply = 100_000_000.toBigDecimal() // 100 million stream tokens
101+
100102
require((royaltyRequest.newmAmount != null) xor (royaltyRequest.usdAmount != null)) {
101103
"Either newmAmount or usdAmount must be provided, but not both."
102104
}
@@ -113,15 +115,15 @@ class EarningsRepositoryImpl(
113115

114116
// should be 100m stream tokens
115117
val totalSupply = snapshotMap["total_supply"] ?: error("No total supply found in snapshot.")
116-
val unstakedSupply = 100_000_000.toBigDecimal() - totalSupply
118+
val unstakedSupply = streamTokenTotalSupply - totalSupply
117119

118120
val now = LocalDateTime.now()
119121
var exchangeRate = ""
120122
val totalNewmAmount =
121123
royaltyRequest.newmAmount?.toBigDecimal() ?: run {
122124
val usdAmount = royaltyRequest.usdAmount!!
123125
val newmUsdPrice = cardanoRepository.queryNEWMUSDPrice()
124-
val newmAmount = (newmUsdPrice.toBigInteger() * usdAmount).toBigDecimal()
126+
val newmAmount = (usdAmount.toBigDecimal() / newmUsdPrice.toBigDecimal()).movePointRight(6)
125127
exchangeRate = " @ 1 NEWM = ${newmUsdPrice.toBigDecimal().movePointLeft(6).toPlainString()} USD"
126128
newmAmount
127129
}
@@ -136,13 +138,7 @@ class EarningsRepositoryImpl(
136138
Earning(
137139
songId = songId,
138140
stakeAddress = stakeAddress,
139-
amount = (
140-
totalNewmAmount * (
141-
streamTokenAmount.setScale(6) / 100_000_000
142-
.toBigDecimal()
143-
.setScale(6)
144-
)
145-
).toLong(),
141+
amount = (totalNewmAmount * streamTokenAmount / streamTokenTotalSupply).toLong(),
146142
memo = "Royalty for: ${song.title} - ${user.stageOrFullName}$exchangeRate",
147143
createdAt = now,
148144
startDate = if (cardanoRepository.isMainnet()) {
@@ -165,13 +161,7 @@ class EarningsRepositoryImpl(
165161
} else {
166162
"foundation"
167163
},
168-
amount = (
169-
totalNewmAmount * (
170-
unstakedSupply.setScale(6) / 100_000_000
171-
.toBigDecimal()
172-
.setScale(6)
173-
)
174-
).toLong(),
164+
amount = (totalNewmAmount * unstakedSupply / streamTokenTotalSupply).toLong(),
175165
memo = "Royalty for: ${song.title} - ${user.stageOrFullName}$exchangeRate",
176166
createdAt = now,
177167
startDate = if (cardanoRepository.isMainnet()) {

newm-server/src/main/kotlin/io/newm/server/features/walletconnection/WalletConnectionRoutes.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ fun Routing.createWalletConnectionRoutes() {
4848
}
4949
route("{connectionId}") {
5050
get {
51-
respond(walletConnectionRepository.connect(connectionId, myUserId))
51+
val connId = try {
52+
connectionId
53+
} catch (ignored: Throwable) {
54+
respond(HttpStatusCode.BadRequest, "Invalid connectionId: ${parameters["connectionId"]!!}")
55+
return@get
56+
}
57+
respond(walletConnectionRepository.connect(connId, myUserId))
5258
}
5359
delete {
5460
walletConnectionRepository.disconnect(connectionId, myUserId)

0 commit comments

Comments
 (0)