@@ -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()) {
0 commit comments