-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EIP-15 AgeUSD: build exchange transactions
- Loading branch information
1 parent
7b77aba
commit 1abb7ab
Showing
10 changed files
with
597 additions
and
74 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
appkit/src/test/resources/mockwebserver/explorer_responses/ageusd/bankboxbytoken.json
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,58 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"boxId": "2342c916b272d8ede91da7e00bd3029f41beaa742bc75af3976916600de64f98", | ||
"transactionId": "4bfb000123026edec42cf7252206a3aaf0b089af80d992f461322d1e1ec107d3", | ||
"blockId": "15f39c5e65a0cdba80d4d01d925e12eaac01fabb58127423c0d5b7919a604833", | ||
"value": 1458253206733705, | ||
"index": 0, | ||
"globalIndex": 15490910, | ||
"creationHeight": 727025, | ||
"settlementHeight": 727027, | ||
"ergoTree": "102a0400040004000e20011d3364de07e5a26f0c4eef0852cddb387039a921b7154ef3cab22c6eda887f0400040204020400040004020500050005c8010500050005feffffffffffffffff0105000580897a05000580897a040405c80104c0933805c00c0580a8d6b907050005c8010580dac40905000500040404040500050005a0060101050005a0060100040004000e20239c170b7e82f94e6b05416f14b8a2a57e0bfff0e3c93f4abbcd160b6a5b271ad801d601db6501fed1ec9591b172017300d821d602b27201730100d603938cb2db63087202730200017303d604b2a5730400d605c17204d606db6308a7d607b27206730500d6088c720702d609db63087204d60ab27209730600d60b8c720a02d60c947208720bd60db27206730700d60e8c720d02d60fb27209730800d6108c720f02d61194720e7210d612e4c6a70505d613e4c672040505d614e4c6a70405d615e4c672040405d616b2a5730900d617e4c672160405d61895720c730a7217d61995720c7217730bd61ac1a7d61be4c672160505d61c9de4c672020405730cd61da2a1721a9c7214721c730dd61e9572119ca1721c95937214730e730f9d721d72147218d801d61e99721a721d9c9593721e7310731195937212731273139d721e72127219d61f9d9c721e7e7314057315d6209c7215721cd6219591a3731673177318d62295937220731972219d9c7205731a7220edededed7203ededededed927205731b93c27204c2a7edec720c7211efed720c7211ed939a720872129a720b7213939a720e72149a72107215edededed939a721472187215939a721272197213939a721a721b7205927215731c927213731deded938c720f018c720d01938c720a018c720701938cb27209731e00018cb27206731f000193721b9a721e958f721f7320f0721f721f957211959172187321927222732273239591721973249072227221927222732572037326938cb2db6308b2a4732700732800017329", | ||
"address": "MUbV38YgqHy7XbsoXWF5z7EZm524Ybdwe5p9WDrbhruZRtehkRPT92imXer2eTkjwPDfboa1pR3zb3deVKVq3H7Xt98qcTqLuSBSbHb7izzo5jphEpcnqyKJ2xhmpNPVvmtbdJNdvdopPrHHDBbAGGeW7XYTQwEeoRfosXzcDtiGgw97b2aqjTsNFmZk7khBEQywjYfmoDc9nUCJMZ3vbSspnYo3LarLe55mh2Np8MNJqUN9APA6XkhZCrTTDRZb1B4krgFY1sVMswg2ceqguZRvC9pqt3tUUxmSnB24N6dowfVJKhLXwHPbrkHViBv1AKAJTmEaQW2DN1fRmD9ypXxZk8GXmYtxTtrj3BiunQ4qzUCu1eGzxSREjpkFSi2ATLSSDqUwxtRz639sHM6Lav4axoJNPCHbY8pvuBKUxgnGRex8LEGM8DeEJwaJCaoy8dBw9Lz49nq5mSsXLeoC4xpTUmp47Bh7GAZtwkaNreCu74m9rcZ8Di4w1cmdsiK1NWuDh9pJ2Bv7u3EfcurHFVqCkT3P86JUbKnXeNxCypfrWsFuYNKYqmjsix82g9vWcGMmAcu5nagxD4iET86iE2tMMfZZ5vqZNvntQswJyQqv2Wc6MTh4jQx1q2qJZCQe4QdEK63meTGbZNNKMctHQbp3gRkZYNrBtxQyVtNLR8xEY8zGp85GeQKbb37vqLXxRpGiigAdMe3XZA4hhYPmAAU5hpSMYaRAjtvvMT3bNiHRACGrfjvSsEG9G2zY5in2YWz5X9zXQLGTYRsQ4uNFkYoQRCBdjNxGv6R58Xq74zCgt19TxYZ87gPWxkXpWwTaHogG1eps8WXt8QzwJ9rVx6Vu9a5GjtcGsQxHovWmYixgBU8X9fPNJ9UQhYyAWbjtRSuVBtDAmoV1gCBEPwnYVP5GCGhCocbwoYhZkZjFZy6ws4uxVLid3FxuvhWvQrVEDYp7WRvGXbNdCbcSXnbeTrPMey1WPaXX", | ||
"assets": [ | ||
{ | ||
"tokenId": "03faf2cb329f2e90d6d23b58d91bbb6c046aa143261cc21f52fbe2824bfcbf04", | ||
"index": 0, | ||
"amount": 9999844128263, | ||
"name": "SigUSD", | ||
"decimals": 2, | ||
"type": "EIP-004" | ||
}, | ||
{ | ||
"tokenId": "003bd19d0187117f130b62e1bcab0939929ff5c7709f843c5c4dd158949285d0", | ||
"index": 1, | ||
"amount": 9998636056232, | ||
"name": "SigRSV", | ||
"decimals": 0, | ||
"type": "EIP-004" | ||
}, | ||
{ | ||
"tokenId": "7d672d1def471720ca5782fd6473e47e796d9ac0c138d9911346f118b2f6d9d9", | ||
"index": 2, | ||
"amount": 1, | ||
"name": "SUSD Bank V2 NFT", | ||
"decimals": 0, | ||
"type": "EIP-004" | ||
} | ||
], | ||
"additionalRegisters": { | ||
"R4": { | ||
"serializedValue": "05f4a7d39401", | ||
"sigmaType": "SLong", | ||
"renderedValue": "155871738" | ||
}, | ||
"R5": { | ||
"serializedValue": "05b285e1940a", | ||
"sigmaType": "SLong", | ||
"renderedValue": "1363943769" | ||
} | ||
}, | ||
"spentTransactionId": null, | ||
"mainChain": true | ||
} | ||
], | ||
"total": 1 | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
appkit/src/test/resources/mockwebserver/explorer_responses/ageusd/rateboxtoken.json
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,48 @@ | ||
{ | ||
|
||
"items": [ | ||
{ | ||
"boxId": "390a8fe7875910ba2fa7d836235afa10b81e878ccac9c5d61b3a3a5ab879560b", | ||
"transactionId": "33ad33a0f81af1422ce784b10900cba682bbbffb4ff4fad3db76d3c9592490cb", | ||
"blockId": "093ee04f0c8249963baa1c382787e9b8696cdfd5528d749722e5c78b47871c0d", | ||
"value": 3471500000, | ||
"index": 0, | ||
"globalIndex": 15493690, | ||
"creationHeight": 727095, | ||
"settlementHeight": 727097, | ||
"ergoTree": "1014040004000e208c27dd9d8a35aac1e3167d58858c0a8b4059b277da790552e37eba22df9b903504000400040204020101040205a0c21e040204080500040c040204a0c21e0402050a05c8010402d806d601b2a5730000d602b5db6501fed9010263ed93e4c67202050ec5a7938cb2db63087202730100017302d603b17202d604e4c6b272027303000605d605d90105049590720573047204e4c6b272029972057305000605d606b07202860273067307d901063c400163d803d6088c720601d6098c720801d60a8c72060286029a72097308ededed8c72080293c2b2a5720900d0cde4c6720a040792c1b2a5720900730992da720501997209730ae4c6720a0605ea02d1ededededededed93cbc27201e4c6a7060e927203730b93db63087201db6308a793e4c6720104059db07202730cd9010741639a8c720701e4c68c72070206057e72030593e4c6720105049ae4c6a70504730d92c1720199c1a77e9c9a7203730e730f058c72060292da720501998c72060173109972049d9c720473117312b2ad7202d9010763cde4c672070407e4c6b2a5731300040400", | ||
"address": "NTkuk55NdwCXkF1e2nCABxq7bHjtinX3wH13zYPZ6qYT71dCoZBe1gZkh9FAr7GeHo2EpFoibzpNQmoi89atUjKRrhZEYrTapdtXrWU4kq319oY7BEWmtmRU9cMohX69XMuxJjJP5hRM8WQLfFnffbjshhEP3ck9CKVEkFRw1JDYkqVke2JVqoMED5yxLVkScbBUiJJLWq9BSbE1JJmmreNVskmWNxWE6V7ksKPxFMoqh1SVePh3UWAaBgGQRZ7TWf4dTBF5KMVHmRXzmQqEu2Fz2yeSLy23sM3pfqa78VuvoFHnTFXYFFxn3DNttxwq3EU3Zv25SmgrWjLKiZjFcEcqGgH6DJ9FZ1DfucVtTXwyDJutY3ksUBaEStRxoUQyRu4EhDobixL3PUWRcxaRJ8JKA9b64ALErGepRHkAoVmS8DaE6VbroskyMuhkTo7LbrzhTyJbqKurEzoEfhYxus7bMpLTePgKcktgRRyB7MjVxjSpxWzZedvzbjzZaHLZLkWZESk1WtdM25My33wtVLNXiTvficEUbjA23sNd24pv1YQ72nY1aqUHa2", | ||
"assets": [ | ||
{ | ||
"tokenId": "011d3364de07e5a26f0c4eef0852cddb387039a921b7154ef3cab22c6eda887f", | ||
"index": 0, | ||
"amount": 1, | ||
"name": "ERGUSD-NFT", | ||
"decimals": 0, | ||
"type": "EIP-004" | ||
} | ||
], | ||
"additionalRegisters": { | ||
"R4": { | ||
"serializedValue": "0584e19fdc01", | ||
"sigmaType": "SLong", | ||
"renderedValue": "230946882" | ||
}, | ||
"R5": { | ||
"serializedValue": "04fee058", | ||
"sigmaType": "SInt", | ||
"renderedValue": "727103" | ||
}, | ||
"R6": { | ||
"serializedValue": "0e20f7ef73c4a4ab91b84bb0a2905108d534114472ec057be3a57a9dfc9b1fbd85c1", | ||
"sigmaType": "Coll[SByte]", | ||
"renderedValue": "f7ef73c4a4ab91b84bb0a2905108d534114472ec057be3a57a9dfc9b1fbd85c1" | ||
} | ||
}, | ||
"spentTransactionId": null, | ||
"mainChain": true | ||
} | ||
], | ||
"total": 1 | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...est/resources/mockwebserver/explorer_responses/ageusd/response_boxesByAddressUnspent.json
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,36 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"boxId": "976c21fbd859cca4113a0711706c33b3d1f1052958a0196d61480f4b950fdf91", | ||
"transactionId": "093eb4a9a963d52a304cb0f8f8150775854a55bd59db8b797fc2ae4d525dba65", | ||
"blockId": "54406fd13168c6be1f4306e5d7b896783c12223e043e7ef2cc189bc72e6ee39e", | ||
"value": 40981000000, | ||
"creationHeight": 130509, | ||
"settlementHeight": 505913, | ||
"ergoTree": "0008cd02472963123ce32c057907c7a7268bc09f45d9ca57819d3327b9e7497d7b1cc347", | ||
"address": "9hHDQb26AjnJUXxcqriqY1mnhpLuUeC81C4pggtK7tupr92Ea1K", | ||
"assets": [ | ||
{ | ||
"tokenId": "03faf2cb329f2e90d6d23b58d91bbb6c046aa143261cc21f52fbe2824bfcbf04", | ||
"index": 0, | ||
"amount": 1000, | ||
"name": "SigUSD", | ||
"decimals": 2, | ||
"type": "EIP-004" | ||
}, | ||
{ | ||
"tokenId": "003bd19d0187117f130b62e1bcab0939929ff5c7709f843c5c4dd158949285d0", | ||
"index": 1, | ||
"amount": 1000, | ||
"name": "SigRSV", | ||
"decimals": 0, | ||
"type": "EIP-004" | ||
} | ||
], | ||
"additionalRegisters": {}, | ||
"spentTransactionId": null, | ||
"mainChain": true | ||
} | ||
], | ||
"total": 1 | ||
} |
19 changes: 19 additions & 0 deletions
19
appkit/src/test/resources/mockwebserver/node_responses/ageusd/response_Box1.json
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,19 @@ | ||
{ | ||
"boxId": "976c21fbd859cca4113a0711706c33b3d1f1052958a0196d61480f4b950fdf91", | ||
"value": 40981000000, | ||
"ergoTree": "0008cd036ba5cfbc03ea2471fdf02737f64dbcd58c34461a7ec1e586dcd713dacbf89a12", | ||
"creationHeight": 130509, | ||
"assets": [ | ||
{ | ||
"tokenId": "03faf2cb329f2e90d6d23b58d91bbb6c046aa143261cc21f52fbe2824bfcbf04", | ||
"amount": 1000 | ||
}, | ||
{ | ||
"tokenId": "003bd19d0187117f130b62e1bcab0939929ff5c7709f843c5c4dd158949285d0", | ||
"amount": 1000 | ||
} | ||
], | ||
"additionalRegisters": {}, | ||
"transactionId": "c5710af17f5124a232a5ef731fdf94a493025334c2a7d5a79e9923210972b962", | ||
"index": 2 | ||
} |
28 changes: 28 additions & 0 deletions
28
appkit/src/test/resources/mockwebserver/node_responses/ageusd/response_bankbox.json
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,28 @@ | ||
{ | ||
|
||
"boxId": "2342c916b272d8ede91da7e00bd3029f41beaa742bc75af3976916600de64f98", | ||
"value": 1458253206733705, | ||
"ergoTree": "102a0400040004000e20011d3364de07e5a26f0c4eef0852cddb387039a921b7154ef3cab22c6eda887f0400040204020400040004020500050005c8010500050005feffffffffffffffff0105000580897a05000580897a040405c80104c0933805c00c0580a8d6b907050005c8010580dac40905000500040404040500050005a0060101050005a0060100040004000e20239c170b7e82f94e6b05416f14b8a2a57e0bfff0e3c93f4abbcd160b6a5b271ad801d601db6501fed1ec9591b172017300d821d602b27201730100d603938cb2db63087202730200017303d604b2a5730400d605c17204d606db6308a7d607b27206730500d6088c720702d609db63087204d60ab27209730600d60b8c720a02d60c947208720bd60db27206730700d60e8c720d02d60fb27209730800d6108c720f02d61194720e7210d612e4c6a70505d613e4c672040505d614e4c6a70405d615e4c672040405d616b2a5730900d617e4c672160405d61895720c730a7217d61995720c7217730bd61ac1a7d61be4c672160505d61c9de4c672020405730cd61da2a1721a9c7214721c730dd61e9572119ca1721c95937214730e730f9d721d72147218d801d61e99721a721d9c9593721e7310731195937212731273139d721e72127219d61f9d9c721e7e7314057315d6209c7215721cd6219591a3731673177318d62295937220731972219d9c7205731a7220edededed7203ededededed927205731b93c27204c2a7edec720c7211efed720c7211ed939a720872129a720b7213939a720e72149a72107215edededed939a721472187215939a721272197213939a721a721b7205927215731c927213731deded938c720f018c720d01938c720a018c720701938cb27209731e00018cb27206731f000193721b9a721e958f721f7320f0721f721f957211959172187321927222732273239591721973249072227221927222732572037326938cb2db6308b2a4732700732800017329", | ||
"assets": [ | ||
{ | ||
"tokenId": "03faf2cb329f2e90d6d23b58d91bbb6c046aa143261cc21f52fbe2824bfcbf04", | ||
"amount": 9999844128263 | ||
}, | ||
{ | ||
"tokenId": "003bd19d0187117f130b62e1bcab0939929ff5c7709f843c5c4dd158949285d0", | ||
"amount": 9998636056232 | ||
}, | ||
{ | ||
"tokenId": "7d672d1def471720ca5782fd6473e47e796d9ac0c138d9911346f118b2f6d9d9", | ||
"amount": 1 | ||
} | ||
], | ||
"creationHeight": 727025, | ||
"additionalRegisters": { | ||
"R4": "05f4a7d39401", | ||
"R5": "05b285e1940a" | ||
}, | ||
"transactionId": "4bfb000123026edec42cf7252206a3aaf0b089af80d992f461322d1e1ec107d3", | ||
"index": 0 | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
appkit/src/test/resources/mockwebserver/node_responses/ageusd/response_ratebox.json
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,21 @@ | ||
{ | ||
|
||
"boxId": "390a8fe7875910ba2fa7d836235afa10b81e878ccac9c5d61b3a3a5ab879560b", | ||
"value": 3471500000, | ||
"ergoTree": "1014040004000e208c27dd9d8a35aac1e3167d58858c0a8b4059b277da790552e37eba22df9b903504000400040204020101040205a0c21e040204080500040c040204a0c21e0402050a05c8010402d806d601b2a5730000d602b5db6501fed9010263ed93e4c67202050ec5a7938cb2db63087202730100017302d603b17202d604e4c6b272027303000605d605d90105049590720573047204e4c6b272029972057305000605d606b07202860273067307d901063c400163d803d6088c720601d6098c720801d60a8c72060286029a72097308ededed8c72080293c2b2a5720900d0cde4c6720a040792c1b2a5720900730992da720501997209730ae4c6720a0605ea02d1ededededededed93cbc27201e4c6a7060e927203730b93db63087201db6308a793e4c6720104059db07202730cd9010741639a8c720701e4c68c72070206057e72030593e4c6720105049ae4c6a70504730d92c1720199c1a77e9c9a7203730e730f058c72060292da720501998c72060173109972049d9c720473117312b2ad7202d9010763cde4c672070407e4c6b2a5731300040400", | ||
"assets": [ | ||
{ | ||
"tokenId": "011d3364de07e5a26f0c4eef0852cddb387039a921b7154ef3cab22c6eda887f", | ||
"amount": 1 | ||
} | ||
], | ||
"creationHeight": 727095, | ||
"additionalRegisters": { | ||
"R4": "0584e19fdc01", | ||
"R5": "04fee058", | ||
"R6": "0e20f7ef73c4a4ab91b84bb0a2905108d534114472ec057be3a57a9dfc9b1fbd85c1" | ||
}, | ||
"transactionId": "33ad33a0f81af1422ce784b10900cba682bbbffb4ff4fad3db76d3c9592490cb", | ||
"index": 0 | ||
|
||
} |
180 changes: 180 additions & 0 deletions
180
appkit/src/test/scala/org/ergoplatform/appkit/AgeUsdBankSpec.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,180 @@ | ||
package org.ergoplatform.appkit | ||
|
||
import org.ergoplatform.appkit.impl.{AgeUsdBank, AgeUsdExchangeTransactionBuilder} | ||
import org.ergoplatform.appkit.testing.AppkitTesting | ||
import org.scalatest.{Matchers, PropSpec} | ||
|
||
class AgeUsdBankSpec extends PropSpec with Matchers | ||
with AppkitTesting | ||
with HttpClientTesting { | ||
|
||
property("Age USD calculation tests") { | ||
val ageUsdBank = new AgeUsdBank(210526315, 160402193, 1375438973, 1477201069508651L) | ||
|
||
ageUsdBank.getStableCoinPrice shouldBe 2105263 | ||
ageUsdBank.getReserveCoinPrice shouldBe 828471 | ||
ageUsdBank.getCurrentReserveRatio shouldBe 437 | ||
} | ||
|
||
property("Age USD exchange tx tests") { | ||
// Withdraw from bank | ||
val ageUsdBank = new AgeUsdBank(230946882, 155871738, 1363943769, 1458253206733705L) | ||
ageUsdBank.getCurrentReserveRatio shouldBe 405 | ||
ageUsdBank.getStableCoinPrice shouldBe 2309468 | ||
|
||
// redeem stable coin | ||
testAgeUsdTransaction(ageUsdBank, { txBuilder: AgeUsdExchangeTransactionBuilder => | ||
txBuilder.buildStableCoinExchangeTransaction(100) | ||
}) | ||
|
||
// mint stable coin | ||
testAgeUsdTransaction(ageUsdBank, { txBuilder: AgeUsdExchangeTransactionBuilder => | ||
txBuilder.buildStableCoinExchangeTransaction(-100) | ||
}) | ||
|
||
// redeem reserve coin | ||
testAgeUsdTransaction(ageUsdBank, { txBuilder: AgeUsdExchangeTransactionBuilder => | ||
txBuilder.buildReserveCoinExchangeTransaction(1000) | ||
}) | ||
|
||
// mint reserve coin | ||
testAgeUsdTransaction(ageUsdBank, { txBuilder: AgeUsdExchangeTransactionBuilder => | ||
txBuilder.buildReserveCoinExchangeTransaction(-1000) | ||
}) | ||
} | ||
|
||
private def testAgeUsdTransaction(ageUsdBank: AgeUsdBank, action: AgeUsdExchangeTransactionBuilder => UnsignedTransaction) = { | ||
val data = MockData( | ||
Seq( | ||
loadNodeResponse("ageusd/response_Box1.json"), | ||
loadNodeResponse("ageusd/response_bankbox.json"), | ||
loadNodeResponse("ageusd/response_ratebox.json") | ||
), | ||
Seq( | ||
loadExplorerResponse("ageusd/response_boxesByAddressUnspent.json"), | ||
loadExplorerResponse("ageusd/bankboxbytoken.json"), | ||
loadExplorerResponse("ageusd/rateboxtoken.json") | ||
)) | ||
|
||
val ergoClient = createMockedErgoClient(data) | ||
val storage = SecretStorage.loadFrom("storage/E2.json") | ||
storage.unlock("abc") | ||
|
||
val unsignedTransaction = ergoClient.execute { ctx: BlockchainContext => | ||
action(ageUsdBank.getExchangeTransactionBuilder(BoxOperations.createForSender(storage.getAddressFor(NetworkType.MAINNET), ctx))) | ||
} | ||
|
||
unsignedTransaction.getDataInputs.size() shouldBe 1 | ||
unsignedTransaction.getOutputs.size() shouldBe 4 | ||
checkSmartContractRun(unsignedTransaction) shouldBe true | ||
ergoClient.execute { ctx: BlockchainContext => | ||
ctx.newProverBuilder().withSecretStorage(storage).build().sign(unsignedTransaction) | ||
} | ||
} | ||
|
||
def checkSmartContractRun(unsignedTransaction: UnsignedTransaction): Boolean = { | ||
// this is the code for EIP-15, see https://github.com/ergoplatform/eips/blob/master/eip-0015.md | ||
// it is not necessary to have this here as script signing above will reduce the ErgoTree | ||
// and test. However, with having this here debugging is possible | ||
|
||
val rateBox = unsignedTransaction.getDataInputs.get(0) | ||
val bankBoxIn = unsignedTransaction.getInputs.get(0) | ||
val bankBoxOut = unsignedTransaction.getOutputs.get(0) | ||
val receiptBox = unsignedTransaction.getOutputs.get(1) | ||
val rate = rateBox.getRegisters.get(0).getValue.asInstanceOf[Long] / 100 | ||
|
||
val scCircIn = bankBoxIn.getRegisters.get(0).getValue.asInstanceOf[Long] | ||
val rcCircIn = bankBoxIn.getRegisters.get(1).getValue.asInstanceOf[Long] | ||
val bcReserveIn = bankBoxIn.getValue | ||
|
||
val scTokensIn = bankBoxIn.getTokens.get(0).getValue | ||
val rcTokensIn = bankBoxIn.getTokens.get(1).getValue | ||
|
||
val scCircOut = bankBoxOut.getRegisters.get(0).getValue.asInstanceOf[Long] | ||
val rcCircOut = bankBoxOut.getRegisters.get(1).getValue.asInstanceOf[Long] | ||
val bcReserveOut = bankBoxOut.getValue | ||
|
||
val scTokensOut = bankBoxOut.getTokens.get(0).getValue | ||
val rcTokensOut = bankBoxOut.getTokens.get(1).getValue | ||
|
||
val totalScIn = scTokensIn + scCircIn | ||
val totalScOut = scTokensOut + scCircOut | ||
|
||
val totalRcIn = rcTokensIn + rcCircIn | ||
val totalRcOut = rcTokensOut + rcCircOut | ||
|
||
val rcExchange = rcTokensIn != rcTokensOut | ||
val scExchange = scTokensIn != scTokensOut | ||
|
||
val rcExchangeXorScExchange = (rcExchange || scExchange) && !(rcExchange && scExchange) | ||
|
||
val circDelta = receiptBox.getRegisters.get(0).getValue.asInstanceOf[Long] | ||
val bcReserveDelta = receiptBox.getRegisters.get(1).getValue.asInstanceOf[Long] | ||
|
||
val rcCircDelta = if (rcExchange) circDelta else 0L | ||
val scCircDelta = if (rcExchange) 0L else circDelta | ||
|
||
val validDeltas = (scCircIn + scCircDelta == scCircOut) && | ||
(rcCircIn + rcCircDelta == rcCircOut) && | ||
(bcReserveIn + bcReserveDelta == bcReserveOut) && | ||
scCircOut >= 0 && rcCircOut >= 0 | ||
|
||
val coinsConserved = totalRcIn == totalRcOut && totalScIn == totalScOut | ||
|
||
val tokenIdsConserved = bankBoxOut.getTokens.get(0).getId == bankBoxIn.getTokens.get(0).getId && | ||
bankBoxOut.getTokens.get(1).getId == bankBoxIn.getTokens.get(1).getId && | ||
bankBoxOut.getTokens.get(2).getId == bankBoxIn.getTokens.get(2).getId | ||
|
||
val mandatoryRateConditions = true // simplified | ||
val mandatoryBankConditions = bankBoxOut.getValue >= 0 && | ||
bankBoxOut.getErgoTree == bankBoxIn.getErgoTree && | ||
rcExchangeXorScExchange && | ||
coinsConserved && | ||
validDeltas && | ||
tokenIdsConserved | ||
|
||
// exchange equations | ||
val bcReserveNeededOut = scCircOut * rate | ||
val bcReserveNeededIn = scCircIn * rate | ||
val liabilitiesIn = Math.max(Math.min(bcReserveIn, bcReserveNeededIn), 0) | ||
val maxReserveRatioPercent = AgeUsdBank.MAX_RESERVE_RATIO_PERCENT | ||
|
||
val reserveRatioPercentOut = if (bcReserveNeededOut == 0) maxReserveRatioPercent else bcReserveOut * 100 / bcReserveNeededOut | ||
|
||
val validReserveRatio = if (scExchange) { | ||
if (scCircDelta > 0) { | ||
reserveRatioPercentOut >= AgeUsdBank.MIN_RESERVE_RATIO_PERCENT | ||
} else true | ||
} else { | ||
if (rcCircDelta > 0) { | ||
reserveRatioPercentOut <= maxReserveRatioPercent | ||
} else { | ||
reserveRatioPercentOut >= AgeUsdBank.MIN_RESERVE_RATIO_PERCENT | ||
} | ||
} | ||
|
||
val brDeltaExpected = if (scExchange) { // sc | ||
val liableRate = if (scCircIn == 0) Long.MaxValue else liabilitiesIn / scCircIn | ||
val scNominalPrice = Math.min(rate, liableRate) | ||
scNominalPrice * scCircDelta | ||
} else { // rc | ||
val equityIn = bcReserveIn - liabilitiesIn | ||
val equityRate = if (rcCircIn == 0) AgeUsdBank.RC_DEFAULT_PRICE else equityIn / rcCircIn | ||
val rcNominalPrice = if (equityIn == 0) AgeUsdBank.RC_DEFAULT_PRICE else equityRate | ||
rcNominalPrice * rcCircDelta | ||
} | ||
|
||
val fee = brDeltaExpected * AgeUsdBank.FEE_PERCENT / 100 | ||
val actualFee = if (fee < 0) { | ||
-fee | ||
} else fee | ||
|
||
// actualFee is always positive, irrespective of brDeltaExpected | ||
val brDeltaExpectedWithFee = brDeltaExpected + actualFee | ||
|
||
mandatoryRateConditions && | ||
mandatoryBankConditions && | ||
bcReserveDelta == brDeltaExpectedWithFee && | ||
validReserveRatio | ||
} | ||
} |
Oops, something went wrong.