-
Notifications
You must be signed in to change notification settings - Fork 117
Add support for Token Escrow - XLS-85d #853
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
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
26f5694
add support for Token Escrow
Patel-Raj11 ecc375b
add unit test
Patel-Raj11 954da2a
WIP commit
Patel-Raj11 7a245a8
add Token Escrow IT
Patel-Raj11 c834388
fix typo
Patel-Raj11 b1c35f7
Merge branch 'main' of github.com:XRPLF/xrpl-py into feature/token-es…
Patel-Raj11 0b2bb53
fix typo
Patel-Raj11 3ce0e4d
update comment
Patel-Raj11 35221b1
Merge branch 'main' into feature/token-escrow
Patel-Raj11 9882fbf
fix lint error
Patel-Raj11 bcdb5ed
remove CancelAfter condition
Patel-Raj11 37a02c4
remove unused imports
Patel-Raj11 af8a450
refactor escrow_create IT
Patel-Raj11 368f8fb
coderabbitai suggestions
Patel-Raj11 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,24 @@ | ||
| from tests.integration.integration_test_case import IntegrationTestCase | ||
| from tests.integration.it_utils import ( | ||
| LEDGER_ACCEPT_REQUEST, | ||
| fund_wallet_async, | ||
| sign_and_reliable_submission_async, | ||
| test_async_and_sync, | ||
| ) | ||
| from tests.integration.reusable_values import DESTINATION, WALLET | ||
| from xrpl.models import EscrowCreate, Ledger | ||
| from xrpl.models import ( | ||
| EscrowCreate, | ||
| EscrowFinish, | ||
| Ledger, | ||
| MPTokenAuthorize, | ||
| MPTokenIssuanceCreate, | ||
| MPTokenIssuanceCreateFlag, | ||
| Payment, | ||
| ) | ||
| from xrpl.models.amounts import MPTAmount | ||
| from xrpl.models.requests.account_objects import AccountObjects, AccountObjectType | ||
| from xrpl.models.response import ResponseStatus | ||
| from xrpl.wallet.main import Wallet | ||
|
|
||
| ACCOUNT = WALLET.address | ||
|
|
||
|
|
@@ -36,3 +49,229 @@ async def test_all_fields(self, client): | |
| ) | ||
| self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
| self.assertEqual(response.result["engine_result"], "tesSUCCESS") | ||
|
|
||
| @test_async_and_sync(globals()) | ||
| async def test_mpt_based_escrow(self, client): | ||
| # Create issuer, source, and destination wallets. | ||
| issuer = Wallet.create() | ||
| await fund_wallet_async(issuer) | ||
khancode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| source = Wallet.create() | ||
| await fund_wallet_async(source) | ||
|
|
||
| destination = Wallet.create() | ||
| await fund_wallet_async(destination) | ||
|
||
|
|
||
| # Create MPToken that can be used in Escrow. | ||
| mp_token_issuance_tx = MPTokenIssuanceCreate( | ||
| account=issuer.classic_address, | ||
| flags=[ | ||
| MPTokenIssuanceCreateFlag.TF_MPT_CAN_ESCROW, | ||
| MPTokenIssuanceCreateFlag.TF_MPT_CAN_TRANSFER, | ||
| ], | ||
| ) | ||
|
|
||
| await sign_and_reliable_submission_async(mp_token_issuance_tx, issuer, client) | ||
|
|
||
| # confirm MPTokenIssuance ledger object was created | ||
| account_objects_response = await client.request( | ||
| AccountObjects(account=issuer.address, type=AccountObjectType.MPT_ISSUANCE) | ||
| ) | ||
|
|
||
| self.assertEqual(len(account_objects_response.result["account_objects"]), 1) | ||
| mpt_issuance_id = account_objects_response.result["account_objects"][0][ | ||
| "mpt_issuance_id" | ||
| ] | ||
|
|
||
| # Source account authorizes itself to hold MPToken. | ||
| mp_token_authorize_tx = MPTokenAuthorize( | ||
| account=source.classic_address, | ||
| mptoken_issuance_id=mpt_issuance_id, | ||
| ) | ||
|
|
||
| await sign_and_reliable_submission_async(mp_token_authorize_tx, source, client) | ||
|
|
||
| # Send some MPToken to the source wallet that can be further used in Escrow. | ||
| payment_tx = Payment( | ||
| account=issuer.address, | ||
| destination=source.address, | ||
| amount=MPTAmount( | ||
| mpt_issuance_id=mpt_issuance_id, | ||
| value="1000", | ||
| ), | ||
| ) | ||
|
|
||
| await sign_and_reliable_submission_async(payment_tx, issuer, client) | ||
|
|
||
| # Create Escrow with MPToken. | ||
| ledger = await client.request(Ledger(ledger_index="validated")) | ||
| close_time = ledger.result["ledger"]["close_time"] | ||
|
|
||
| escrowed_amount = MPTAmount( | ||
| mpt_issuance_id=mpt_issuance_id, | ||
| value="10", | ||
| ) | ||
|
|
||
| finish_after = close_time + 2 | ||
|
|
||
| escrow_create = EscrowCreate( | ||
| account=source.classic_address, | ||
| amount=escrowed_amount, | ||
| destination=destination.classic_address, | ||
| finish_after=finish_after, | ||
| cancel_after=close_time + 1000, | ||
| ) | ||
| response = await sign_and_reliable_submission_async( | ||
| escrow_create, source, client | ||
| ) | ||
| escrow_create_seq = response.result["tx_json"]["Sequence"] | ||
|
|
||
| self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
| self.assertEqual(response.result["engine_result"], "tesSUCCESS") | ||
|
|
||
| # Confirm EscrowCreate ledger object was created. | ||
| account_objects_response = await client.request( | ||
| AccountObjects(account=source.address, type=AccountObjectType.ESCROW) | ||
| ) | ||
|
|
||
| self.assertEqual(len(account_objects_response.result["account_objects"]), 1) | ||
| self.assertEqual( | ||
| account_objects_response.result["account_objects"][0]["Amount"], | ||
| escrowed_amount.to_dict(), | ||
| ) | ||
|
|
||
| # Confirm MPToken ledger object was created. | ||
| account_objects_response = await client.request( | ||
| AccountObjects(account=source.address, type=AccountObjectType.MPTOKEN) | ||
| ) | ||
|
|
||
| self.assertEqual(len(account_objects_response.result["account_objects"]), 1) | ||
| self.assertEqual( | ||
| account_objects_response.result["account_objects"][0]["LockedAmount"], | ||
| escrowed_amount.value, | ||
| ) | ||
| self.assertEqual( | ||
| account_objects_response.result["account_objects"][0]["MPTokenIssuanceID"], | ||
| escrowed_amount.mpt_issuance_id, | ||
| ) | ||
|
|
||
| # Confirm MPTokenIssuance ledger object was created. | ||
| account_objects_response = await client.request( | ||
| AccountObjects(account=issuer.address, type=AccountObjectType.MPT_ISSUANCE) | ||
| ) | ||
|
|
||
| self.assertEqual(len(account_objects_response.result["account_objects"]), 1) | ||
| self.assertEqual( | ||
| account_objects_response.result["account_objects"][0]["LockedAmount"], | ||
| escrowed_amount.value, | ||
| ) | ||
|
|
||
| # Wait for the finish_after time to pass before finishing the escrow. | ||
| close_time = 0 | ||
| while close_time <= finish_after: | ||
| await client.request(LEDGER_ACCEPT_REQUEST) | ||
| ledger = await client.request(Ledger(ledger_index="validated")) | ||
| close_time = ledger.result["ledger"]["close_time"] | ||
Patel-Raj11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Finish the escrow. | ||
| escrow_finish = EscrowFinish( | ||
| account=destination.classic_address, | ||
| owner=source.classic_address, | ||
| offer_sequence=escrow_create_seq, | ||
| ) | ||
| response = await sign_and_reliable_submission_async( | ||
| escrow_finish, destination, client | ||
| ) | ||
| self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
| self.assertEqual(response.result["engine_result"], "tesSUCCESS") | ||
|
|
||
| # Confirm MPToken ledger object was created for destination. | ||
| account_objects_response = await client.request( | ||
| AccountObjects(account=destination.address, type=AccountObjectType.MPTOKEN) | ||
| ) | ||
|
|
||
| self.assertEqual(len(account_objects_response.result["account_objects"]), 1) | ||
| self.assertEqual( | ||
| account_objects_response.result["account_objects"][0]["MPTAmount"], | ||
| escrowed_amount.value, | ||
| ) | ||
| self.assertEqual( | ||
| account_objects_response.result["account_objects"][0]["MPTokenIssuanceID"], | ||
| escrowed_amount.mpt_issuance_id, | ||
| ) | ||
|
|
||
| @test_async_and_sync(globals()) | ||
| async def test_mpt_based_escrow_failure(self, client): | ||
Patel-Raj11 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Create issuer, source, and destination wallets. | ||
| issuer = Wallet.create() | ||
| await fund_wallet_async(issuer) | ||
|
|
||
| source = Wallet.create() | ||
| await fund_wallet_async(source) | ||
|
|
||
| destination = Wallet.create() | ||
| await fund_wallet_async(destination) | ||
|
|
||
| # Create MPToken that can be used in Escrow. | ||
| mp_token_issuance_tx = MPTokenIssuanceCreate( | ||
| account=issuer.classic_address, | ||
| ) | ||
|
|
||
| await sign_and_reliable_submission_async(mp_token_issuance_tx, issuer, client) | ||
|
|
||
| # confirm MPTokenIssuance ledger object was created | ||
| account_objects_response = await client.request( | ||
| AccountObjects(account=issuer.address, type=AccountObjectType.MPT_ISSUANCE) | ||
| ) | ||
|
|
||
| self.assertEqual(len(account_objects_response.result["account_objects"]), 1) | ||
| mpt_issuance_id = account_objects_response.result["account_objects"][0][ | ||
| "mpt_issuance_id" | ||
| ] | ||
|
|
||
| # Source account authorizes itself to hold MPToken. | ||
| mp_token_authorize_tx = MPTokenAuthorize( | ||
| account=source.classic_address, | ||
| mptoken_issuance_id=mpt_issuance_id, | ||
| ) | ||
|
|
||
| await sign_and_reliable_submission_async(mp_token_authorize_tx, source, client) | ||
|
|
||
| # Send some MPToken to the source wallet that can be further used in Escrow. | ||
| payment_tx = Payment( | ||
| account=issuer.address, | ||
| destination=source.address, | ||
| amount=MPTAmount( | ||
| mpt_issuance_id=mpt_issuance_id, | ||
| value="1000", | ||
| ), | ||
| ) | ||
|
|
||
| await sign_and_reliable_submission_async(payment_tx, issuer, client) | ||
|
|
||
| # Create Escrow with MPToken. | ||
| ledger = await client.request(Ledger(ledger_index="validated")) | ||
| close_time = ledger.result["ledger"]["close_time"] | ||
|
|
||
| escrowed_amount = MPTAmount( | ||
| mpt_issuance_id=mpt_issuance_id, | ||
| value="10", | ||
| ) | ||
|
|
||
| finish_after = close_time + 2 | ||
|
|
||
| escrow_create = EscrowCreate( | ||
| account=source.classic_address, | ||
| amount=escrowed_amount, | ||
| destination=destination.classic_address, | ||
| finish_after=finish_after, | ||
| cancel_after=close_time + 1000, | ||
| ) | ||
|
|
||
| # Transaction fails with tecNO_PERMISSION because lsfMPTCanEscrow is not set. | ||
| response = await sign_and_reliable_submission_async( | ||
| escrow_create, source, client | ||
| ) | ||
|
|
||
| self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
| self.assertEqual(response.result["engine_result"], "tecNO_PERMISSION") | ||
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
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.
Can all this setup be done independently in
setupTestsor something (I might be misremembering the function name)? That way it doesn't have to execute 4 timesThere 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.
@mvadari I want to issue two different types of MPT's (with difference flags) for
test_mpt_based_escrowandtest_mpt_based_escrow_failure. UsingsetUpClassthat executes only once will not help here. Do you see any other benefit of moving it?Uh oh!
There was an error while loading. Please reload this page.
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.
The way the tests run, it runs each test once per client (Sync/Async, Json/Websocket) so each test actually runs 4 times under the hood. So you could still set up 2 MPTs in
setupClassand it would still save.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.
I have moved account and MPT setup to
setUpClassin af8a450.Haven't reused WALLET AND DESTINATION since, MPT ledger objects were being tested in other MPT specific IT's and they were being interfered by MPT ledger objects created through EscrowFinish transaction.