-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR 211 rebased on master #244
Open
semyonoskin
wants to merge
4
commits into
master
Choose a base branch
from
dev-187-rebase-ergo-explorer-pr-211-on-top-of
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../src/main/scala/org/ergoplatform/explorer/http/api/v1/utils/BuildUnconfirmedBalance.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.ergoplatform.explorer.http.api.v1.utils | ||
|
||
import org.ergoplatform.explorer.http.api.models.AssetInstanceInfo | ||
import org.ergoplatform.explorer.http.api.v1.models._ | ||
import org.ergoplatform.explorer.{ErgoTree, HexString, TokenId} | ||
|
||
object BuildUnconfirmedBalance { | ||
|
||
/** Build unconfirmed balance considering MemPool Data | ||
* by reducing a `List[`[[org.ergoplatform.explorer.http.api.v1.models.UTransactionInfo]]`]` | ||
* into a [[org.ergoplatform.explorer.http.api.v1.models.Balance]] | ||
* | ||
* Unconfirmed Balance arithmetic is completed in three steps: | ||
* <li> determine if a [[org.ergoplatform.explorer.http.api.v1.models.UTransactionInfo]] is a credit or debit by | ||
* matching its [[org.ergoplatform.explorer.http.api.v1.models.UInputInfo]] to wallets </li> | ||
* <li> reducing similar [[org.ergoplatform.explorer.http.api.v1.models.UOutputInfo]] | ||
* sums (credits/debits) into a single value: `debitSum/creditSum` </li> | ||
* <li> subtracting or adding `debitSum/creditSum` into iterations current balance </li> | ||
* | ||
* @param items unsigned transactions from the MemPool | ||
* @param confirmedBalance last signed & unspent balance | ||
* @param ergoTree for transaction type identification | ||
* @param hexString for NSum evaluation | ||
* @return | ||
*/ | ||
def apply( | ||
items: List[UTransactionInfo], | ||
confirmedBalance: Balance, | ||
ergoTree: ErgoTree, | ||
hexString: HexString | ||
): Balance = | ||
items.foldLeft(confirmedBalance) { case (balance, transactionInfo) => | ||
transactionInfo.inputs.head.ergoTree match { | ||
case ieT if ieT == ergoTree => | ||
val debitSum = transactionInfo.outputs.foldLeft(0L) { case (sum, outputInfo) => | ||
if (outputInfo.ergoTree != hexString) sum + outputInfo.value | ||
else sum | ||
} | ||
|
||
val debitSumTokenGroups = transactionInfo.outputs | ||
.foldLeft(Map[TokenId, AssetInstanceInfo]()) { case (groupedTokens, outputInfo) => | ||
if (outputInfo.ergoTree != hexString) | ||
outputInfo.assets.foldLeft(groupedTokens) { case (gT, asset) => | ||
val gTAsset = gT.getOrElse(asset.tokenId, asset.copy(amount = 0L)) | ||
gT + (asset.tokenId -> gTAsset.copy(amount = gTAsset.amount + asset.amount)) | ||
} | ||
else groupedTokens | ||
} | ||
|
||
val newTokensBalance = balance.tokens.map { token => | ||
debitSumTokenGroups.get(token.tokenId).map { assetInfo => | ||
token.copy(amount = token.amount - assetInfo.amount) | ||
} match { | ||
case Some(value) => value | ||
case None => token | ||
} | ||
} | ||
|
||
Balance(balance.nanoErgs - debitSum, newTokensBalance) | ||
case _ => | ||
val creditSum = transactionInfo.outputs.foldLeft(0L) { case (sum, outputInfo) => | ||
if (outputInfo.ergoTree == hexString) sum + outputInfo.value | ||
else sum | ||
} | ||
|
||
val creditSumTokenGroups = transactionInfo.outputs | ||
.foldLeft(Map[TokenId, AssetInstanceInfo]()) { case (groupedTokens, outputInfo) => | ||
if (outputInfo.ergoTree == hexString) | ||
outputInfo.assets.foldLeft(groupedTokens) { case (gT, asset) => | ||
val gTAsset = gT.getOrElse(asset.tokenId, asset.copy(amount = 0L)) | ||
gT + (asset.tokenId -> gTAsset.copy(amount = gTAsset.amount + asset.amount)) | ||
} | ||
else groupedTokens | ||
} | ||
|
||
val newTokensBalance = balance.tokens.map { token => | ||
creditSumTokenGroups.get(token.tokenId).map { assetInfo => | ||
token.copy(amount = token.amount + assetInfo.amount) | ||
} match { | ||
case Some(value) => value | ||
case None => token | ||
} | ||
} | ||
|
||
Balance(balance.nanoErgs + creditSum, newTokensBalance) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...pi/src/test/scala/org/ergoplatform/explorer/v1/services/BuildUnconfirmedBalanceSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.ergoplatform.explorer.v1.services | ||
|
||
import org.ergoplatform.explorer.TokenId | ||
import org.ergoplatform.explorer.http.api.v1.utils.BuildUnconfirmedBalance | ||
import org.ergoplatform.explorer.v1.utils.TransactionSimulator.Simulator | ||
import org.ergoplatform.explorer.v1.utils.TransactionSimulator.constants.{SigUSD1, SigUSD2, TransactionFee, WalletT} | ||
import org.scalatest._ | ||
import org.scalatest.flatspec._ | ||
import org.scalatest.matchers._ | ||
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | ||
|
||
import scala.util.Success | ||
|
||
class BuildUnconfirmedBalanceSpec extends AnyFlatSpec with should.Matchers with ScalaCheckDrivenPropertyChecks { | ||
|
||
// 100Erg 100Token | ||
val senderWallet: WalletT = WalletT( | ||
100000000000L, | ||
Map( | ||
TokenId(SigUSD1) -> 10000L, | ||
TokenId(SigUSD2) -> 10000L | ||
) | ||
) | ||
|
||
// 10Erg 10 | ||
val receivingWallet: WalletT = WalletT( | ||
100000000000L, | ||
Map( | ||
TokenId(SigUSD1) -> 10000L, | ||
TokenId(SigUSD2) -> 10000L | ||
) | ||
) | ||
|
||
"BuildUnconfirmedBalance protocol" should "calculate sending wallet nanoErg unconfirmed balance" in { | ||
val toSend = 5000000000L // 5Erg | ||
val res = Simulator(senderWallet, receivingWallet, toSend, None) | ||
res shouldBe a[Success[_]] | ||
|
||
val txInfo = res.get.sender | ||
|
||
val unconfirmedBalance = | ||
BuildUnconfirmedBalance(txInfo.items, WalletT.toBalance(senderWallet), txInfo.ergoTree, txInfo.ergoTree.value) | ||
|
||
unconfirmedBalance.nanoErgs shouldBe (senderWallet.balance - TransactionFee - toSend) | ||
} | ||
|
||
it should "calculate receiving wallet nanoErg unconfirmed balance" in { | ||
val toSend = 8000000000L // 8Erg | ||
val res = Simulator(senderWallet, receivingWallet, toSend, None) | ||
res shouldBe a[Success[_]] | ||
|
||
val txInfo = res.get.receiver | ||
|
||
val unconfirmedBalance = | ||
BuildUnconfirmedBalance(txInfo.items, WalletT.toBalance(receivingWallet), txInfo.ergoTree, txInfo.ergoTree.value) | ||
|
||
unconfirmedBalance.nanoErgs shouldBe (receivingWallet.balance + toSend) | ||
} | ||
|
||
it should "calculate sending wallet assets unconfirmed balance" in { | ||
val tokenSentAmount = 100L | ||
val res = Simulator( | ||
senderWallet, | ||
receivingWallet, | ||
TransactionFee, | ||
Some( | ||
Map( | ||
TokenId(SigUSD1) -> tokenSentAmount, | ||
TokenId(SigUSD2) -> tokenSentAmount | ||
) | ||
) | ||
) | ||
res shouldBe a[Success[_]] | ||
|
||
val txInfo = res.get.sender | ||
val sWalletAsBalance = WalletT.toBalance(senderWallet) | ||
|
||
val unconfirmedBalance = | ||
BuildUnconfirmedBalance(txInfo.items, sWalletAsBalance, txInfo.ergoTree, txInfo.ergoTree.value) | ||
|
||
unconfirmedBalance.tokens shouldBe sWalletAsBalance.tokens.map(t => t.copy(amount = t.amount - tokenSentAmount)) | ||
|
||
} | ||
|
||
it should "calculate receiving wallet assets unconfirmed balance" in { | ||
val tokenSentAmount = 100L | ||
val res = Simulator( | ||
senderWallet, | ||
receivingWallet, | ||
TransactionFee, | ||
Some( | ||
Map( | ||
TokenId(SigUSD1) -> tokenSentAmount, | ||
TokenId(SigUSD2) -> tokenSentAmount | ||
) | ||
) | ||
) | ||
res shouldBe a[Success[_]] | ||
|
||
val txInfo = res.get.receiver | ||
val rWalletAsBalance = WalletT.toBalance(receivingWallet) | ||
|
||
val unconfirmedBalance = | ||
BuildUnconfirmedBalance(txInfo.items, rWalletAsBalance, txInfo.ergoTree, txInfo.ergoTree.value) | ||
|
||
unconfirmedBalance.tokens shouldBe rWalletAsBalance.tokens.map(t => t.copy(amount = t.amount + tokenSentAmount)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please apply formatting