From 769e0f130edafe6bcde9694139acede4206ce5b7 Mon Sep 17 00:00:00 2001 From: xbtmatt <90358481+xbtmatt@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:25:49 -0700 Subject: [PATCH] Created a transfer_object function in RestClient class and altered the transfer_token function in the AptosTokenClient to just call transfer_object. Left it in for ease of use --- .../sdk/aptos_sdk/aptos_token_client.py | 19 ++--------------- .../python/sdk/aptos_sdk/async_client.py | 21 +++++++++++++++++++ ecosystem/python/sdk/examples/aptos-token.py | 13 +++++++++++- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/ecosystem/python/sdk/aptos_sdk/aptos_token_client.py b/ecosystem/python/sdk/aptos_sdk/aptos_token_client.py index ae9b18fd29ad0a..a8204ce73127d5 100644 --- a/ecosystem/python/sdk/aptos_sdk/aptos_token_client.py +++ b/ecosystem/python/sdk/aptos_sdk/aptos_token_client.py @@ -519,24 +519,9 @@ async def mint_soul_bound_token( # :!:>transfer_token async def transfer_token( - self, sender: Account, token: AccountAddress, to: AccountAddress + self, owner: Account, token: AccountAddress, to: AccountAddress ) -> str: - payload = EntryFunction.natural( - "0x1::object", - "transfer", - [TypeTag(StructTag.from_str("0x4::token::Token"))], - [ - TransactionArgument(token, Serializer.struct), - TransactionArgument(to, Serializer.struct), - ], - ) - - signed_transaction = await self.client.create_bcs_signed_transaction( - sender, TransactionPayload(payload) - ) - return await self.client.submit_bcs_transaction( - signed_transaction - ) # <:!:transfer_token + return await self.client.transfer_object(owner, token, to) # <:!:transfer_token async def burn_token(self, creator: Account, token: AccountAddress) -> str: payload = EntryFunction.natural( diff --git a/ecosystem/python/sdk/aptos_sdk/async_client.py b/ecosystem/python/sdk/aptos_sdk/async_client.py index a5236cae07782c..0d0cb679bba6e1 100644 --- a/ecosystem/python/sdk/aptos_sdk/async_client.py +++ b/ecosystem/python/sdk/aptos_sdk/async_client.py @@ -751,6 +751,27 @@ async def get_collection( "0x3::token::CollectionData", collection_name, ) + + async def transfer_object( + self, owner: Account, object: AccountAddress, to: AccountAddress + ) -> str: + transaction_arguments = [ + TransactionArgument(object, Serializer.struct), + TransactionArgument(to, Serializer.struct), + ] + + payload = EntryFunction.natural( + "0x1::object", + "transfer_call", + [], + transaction_arguments, + ) + + signed_transaction = await self.create_bcs_signed_transaction( + owner, + TransactionPayload(payload), + ) + return await self.submit_bcs_transaction(signed_transaction) class FaucetClient: diff --git a/ecosystem/python/sdk/examples/aptos-token.py b/ecosystem/python/sdk/examples/aptos-token.py index 3cc024be586bf0..8f324abc90b524 100644 --- a/ecosystem/python/sdk/examples/aptos-token.py +++ b/ecosystem/python/sdk/examples/aptos-token.py @@ -5,7 +5,7 @@ from aptos_sdk.account import Account from aptos_sdk.account_address import AccountAddress -from aptos_sdk.aptos_token_client import AptosTokenClient, Property, PropertyMap +from aptos_sdk.aptos_token_client import AptosTokenClient, Property, PropertyMap, Object from aptos_sdk.async_client import FaucetClient, RestClient from .common import FAUCET_URL, NODE_URL @@ -107,6 +107,17 @@ async def main(): token_data = await token_client.read_object(token_addr) print(f"Alice's token: {token_data}") + print("\n=== Transferring the Token from Alice to Bob ===") + print(f"Alice: {alice.address()}") + print(f"Bob: {bob.address()}") + print(f"Token: {token_addr}\n") + print(f"Owner: {token_data.resources[Object].owner}") + print(" ...transferring... ") + txn_hash = await rest_client.transfer_object(alice, token_addr, bob.address()) + await rest_client.wait_for_transaction(txn_hash) + token_data = await token_client.read_object(token_addr) + print(f"Owner: {token_data.resources[Object].owner}\n") + await rest_client.close()