-
Notifications
You must be signed in to change notification settings - Fork 34
EIP-0031 babel fees #204
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
Merged
Merged
EIP-0031 babel fees #204
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
44218b4
EIP-31 Basic implementation of data classes and transaction to create…
MrStahlfelge c8a613a
EIP-0031 add BabelFeeOperations.cancelBabelFeeContract
MrStahlfelge 448ff3d
EIP-0031 Add BabelFeeOperations#getBabelFeeTransactionBuilder
MrStahlfelge d440b20
EIP-0031 BabelFeeOperations#findBabelFeeBox
MrStahlfelge fd5ac70
Fix Java 8 compile
MrStahlfelge daa5610
eip-31-babelfees: typo fixes
aslesarenko 61b6657
eip-31-babelfees: some formatting fixes
aslesarenko 5102a52
EIP-0031 use ErgoTreeSerializer.DefaultSerializer.substituteConstants…
MrStahlfelge 31f229f
Improve exception message
MrStahlfelge 77b7096
EIP-0031 test for findBabelFeeBox, documentation changes, improve Bab…
MrStahlfelge 6610b7f
Merge branch 'transactionbuilder-addmethods' into eip-31-babelfees
MrStahlfelge 933f8a7
EIP-0031 use new methods from #205
MrStahlfelge 93f2eb7
EIP-0031 use ErgoTreeTemplate
MrStahlfelge c2c965a
Merge remote-tracking branch 'origin/develop' into eip-31-babelfees
MrStahlfelge 15fee58
transactionbuilder-addmethods: correct implementation of ErgoTreeTemp…
aslesarenko fe3f0fd
transactionbuilder-addmethods: tests for ErgoTreeTemplate
aslesarenko 9ec3f36
Added BabelFeeOperations.findBabelFeeBox parameter to search through …
MrStahlfelge e4400fc
Linked to EIP version from PR
MrStahlfelge 93faed4
eip-31-babelfees: fix box loading + formatting
aslesarenko 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
138 changes: 138 additions & 0 deletions
138
appkit/src/test/scala/org/ergoplatform/appkit/BabelFeeSpec.scala
This file contains hidden or 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,138 @@ | ||
| package org.ergoplatform.appkit | ||
|
|
||
| import org.ergoplatform.appkit.babelfee.{BabelFeeBoxContract, BabelFeeBoxState, BabelFeeOperations} | ||
| import org.scalatest.{Matchers, PropSpec} | ||
| import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | ||
|
|
||
| import java.util | ||
| import java.util.Arrays | ||
|
|
||
| class BabelFeeSpec extends PropSpec with Matchers with ScalaCheckDrivenPropertyChecks | ||
| with HttpClientTesting | ||
| with AppkitTestingCommon { | ||
|
|
||
| val mockTokenId = "f9e5ce5aa0d95f5d54a7bc89c46730d9662397067250aa18a0039631c0f5b809" | ||
|
|
||
| property("babel fee box creation and revoke") { | ||
| val ergoClient = createMockedErgoClient(MockData(Nil, Nil)) | ||
| ergoClient.execute { ctx: BlockchainContext => | ||
| val creator = address | ||
|
|
||
| val amountToSend = Parameters.OneErg * 100 | ||
|
|
||
| val input1 = ctx.newTxBuilder.outBoxBuilder | ||
| .value(amountToSend + Parameters.MinFee) | ||
| .contract(creator.toErgoContract) | ||
| .build().convertToInputWith(mockTokenId, 0) | ||
|
|
||
| val txCreate = BabelFeeOperations.createNewBabelContractTx( | ||
| BoxOperations.createForSender(creator, ctx) | ||
| .withAmountToSpend(amountToSend) | ||
| .withInputBoxesLoader(new MockedBoxesLoader(Arrays.asList(input1))), | ||
| ErgoId.create(mockTokenId), | ||
| Parameters.OneErg | ||
| ) | ||
|
|
||
| ctx.newProverBuilder().build().reduce(txCreate, 0) | ||
|
|
||
| val babelFeeErgoBox = txCreate.getOutputs.get(0).convertToInputWith(mockTokenId, 0) | ||
|
|
||
| // now we cancel the babel box | ||
| val txCancel = BabelFeeOperations.cancelBabelFeeContract( | ||
| BoxOperations.createForSender(creator, ctx) | ||
| .withInputBoxesLoader(new MockedBoxesLoader(Arrays.asList(input1))), | ||
| babelFeeErgoBox) | ||
|
|
||
| ctx.newProverBuilder() | ||
| .withMnemonic(mnemonic, SecretString.empty(), false) | ||
| .build() | ||
| .sign(txCancel) | ||
|
|
||
| // check if the contract really is for the token | ||
| new BabelFeeBoxContract(babelFeeErgoBox.getErgoTree).getTokenId.toString shouldBe mockTokenId | ||
| // check template hash | ||
| ErgoTreeTemplate.fromErgoTree(babelFeeErgoBox.getErgoTree).getTemplateHashHex shouldBe BabelFeeBoxContract.templateHash | ||
| } | ||
| } | ||
|
|
||
| property("babel fee box use") { | ||
| val ergoClient = createMockedErgoClient(MockData(Nil, Nil)) | ||
| ergoClient.execute { ctx: BlockchainContext => | ||
| val sender = address | ||
|
|
||
| val txB = ctx.newTxBuilder | ||
| val babelFeeBoxState = BabelFeeBoxState.newBuilder().withValue(Parameters.OneErg) | ||
| .withTokenId(ErgoId.create(mockTokenId)) | ||
| .withBoxCreator(Address.create(secondEip3AddrStr)) | ||
| .withPricePerToken(Parameters.MinFee) | ||
| .build() | ||
|
|
||
| val fee = Parameters.MinFee | ||
|
|
||
| val output = txB.outBoxBuilder | ||
| .value(Parameters.MinChangeValue) | ||
| .contract(sender.toErgoContract) | ||
| .tokens(new ErgoToken( | ||
| ErgoId.create(mockTokenId), | ||
| 1000 - babelFeeBoxState.calcTokensToSellForErgAmount(fee))) | ||
| .build() | ||
|
|
||
| val input = txB.outBoxBuilder | ||
| .value(Parameters.MinChangeValue) | ||
| .contract(sender.toErgoContract) | ||
| .tokens(new ErgoToken(ErgoId.create(mockTokenId), 1000)) | ||
| .build().convertToInputWith(mockTokenId, 0) | ||
|
|
||
| txB.fee(fee) | ||
| .outputs(output) | ||
| .boxesToSpend(util.Arrays.asList(input)) | ||
| .sendChangeTo(sender) | ||
|
|
||
| BabelFeeOperations.addBabelFeeBoxes(txB, | ||
| babelFeeBoxState.buildOutbox(txB, null) | ||
| .convertToInputWith(mockTokenId, 0), | ||
| fee) | ||
|
|
||
| val tx = txB.build() | ||
|
|
||
| ctx.newProverBuilder() | ||
| .withMnemonic(mnemonic, SecretString.empty(), false) | ||
| .build() | ||
| .sign(tx) | ||
| } | ||
| } | ||
|
|
||
| property("fetch babel fee boxes") { | ||
| val ergoClient = createMockedErgoClient(MockData(Nil, Nil)) | ||
| ergoClient.execute { ctx: BlockchainContext => | ||
| val creator = address | ||
|
|
||
| val tockenId = ErgoId.create(mockTokenId) | ||
|
|
||
| // find no boxes | ||
| val babelBox1 = BabelFeeOperations.findBabelFeeBox(ctx, new MockedBoxesLoader(new util.ArrayList[InputBox]()), | ||
| tockenId, Parameters.MinFee) | ||
|
|
||
| babelBox1 shouldBe (null) | ||
|
|
||
| val inputBabelBox = BabelFeeBoxState.newBuilder() | ||
| .withValue(Parameters.OneErg) | ||
| .withTokenId(tockenId) | ||
| .withPricePerToken(Parameters.MinFee) | ||
| .withBoxCreator(creator) | ||
| .build().buildOutbox(ctx.newTxBuilder(), null) | ||
| .convertToInputWith(mockTokenId, 0) | ||
|
|
||
| val babelBox2 = BabelFeeOperations.findBabelFeeBox(ctx, new MockedBoxesLoader(util.Arrays.asList(inputBabelBox)), | ||
| tockenId, Parameters.MinFee) | ||
|
|
||
| babelBox2 shouldBe inputBabelBox | ||
|
|
||
| // the amount needed (2 ERG) is more than inputBabelBox can offer, so it is discarded | ||
| val babelBox3 = BabelFeeOperations.findBabelFeeBox(ctx, new MockedBoxesLoader(util.Arrays.asList(inputBabelBox)), | ||
| tockenId, Parameters.OneErg * 2) | ||
|
|
||
| babelBox3 shouldBe (null) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
48 changes: 48 additions & 0 deletions
48
lib-api/src/main/java/org/ergoplatform/appkit/babelfee/BabelFeeBoxContract.java
This file contains hidden or 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,48 @@ | ||
| package org.ergoplatform.appkit.babelfee; | ||
|
|
||
| import org.ergoplatform.appkit.ErgoId; | ||
| import org.ergoplatform.appkit.ErgoTreeTemplate; | ||
| import org.ergoplatform.appkit.ErgoValue; | ||
| import org.ergoplatform.appkit.ScalaHelpers; | ||
|
|
||
| import scorex.util.encode.Base16; | ||
| import sigmastate.Values; | ||
| import special.collection.Coll; | ||
|
|
||
| public class BabelFeeBoxContract { | ||
|
|
||
| /** | ||
| * babel fee box ErgoTreeTemplateHash to be used for Explorer requests | ||
| */ | ||
| public static final String templateHash = "4e83fa68ef3ed9794bbab5d8799998a9e09fb10a0afe8d1bf928936dfd11c465"; | ||
| // this was calced by ErgoTreeTemplate.fromTemplateBytes(contractTemplate).getTemplateHashHex() | ||
|
|
||
| private static final String contractTemplateHex = "100604000e000400040005000500d803d601e30004d602e4c6a70408d603e4c6a7050595e67201d804d604b2a5e4720100d605b2db63087204730000d606db6308a7d60799c1a7c17204d1968302019683050193c27204c2a7938c720501730193e4c672040408720293e4c672040505720393e4c67204060ec5a796830201929c998c7205029591b1720673028cb272067303000273047203720792720773057202"; | ||
| private static final byte[] contractTemplate; | ||
|
|
||
| static { | ||
| contractTemplate = Base16.decode(contractTemplateHex).get(); | ||
| } | ||
|
aslesarenko marked this conversation as resolved.
|
||
|
|
||
| private final ErgoId tokenId; | ||
| private final Values.ErgoTree ergoTree; | ||
|
|
||
| public BabelFeeBoxContract(ErgoId tokenId) { | ||
| this.tokenId = tokenId; | ||
| byte[] idBytes = tokenId.getBytes(); | ||
| ergoTree = ErgoTreeTemplate.fromErgoTreeBytes(contractTemplate).applyParameters(new ErgoValue[]{ErgoValue.of(idBytes)}); | ||
| } | ||
|
|
||
| public BabelFeeBoxContract(Values.ErgoTree ergoTree) { | ||
| this.ergoTree = ergoTree; | ||
| tokenId = new ErgoId(ScalaHelpers.collByteToByteArray((Coll<Byte>) ErgoTreeTemplate.fromErgoTree(ergoTree).getParameter(0).getValue())); | ||
| } | ||
|
|
||
| public Values.ErgoTree getErgoTree() { | ||
| return ergoTree; | ||
| } | ||
|
|
||
| public ErgoId getTokenId() { | ||
| return tokenId; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.