Skip to content

Commit

Permalink
[Python SDK] Add a token_transfer function to the python SDK (#9422)
Browse files Browse the repository at this point in the history
* Adding transfer_token function to aptos_token_client.py and adding documentation tags for the new your_first_nft tutorial.

* Fixed strange formatting

* 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
  • Loading branch information
xbtmatt committed Aug 13, 2023
1 parent 7232619 commit 1f14deb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ecosystem/python/sdk/aptos_sdk/aptos_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def create_collection_payload(

return TransactionPayload(payload)

# :!:>create_collection
async def create_collection(
self,
creator: Account,
Expand All @@ -401,7 +402,7 @@ async def create_collection(
tokens_freezable_by_creator: bool,
royalty_numerator: int,
royalty_denominator: int,
) -> str:
) -> str: # <:!:create_collection
payload = AptosTokenClient.create_collection_payload(
description,
max_supply,
Expand Down Expand Up @@ -458,6 +459,7 @@ def mint_token_payload(

return TransactionPayload(payload)

# :!:>mint_token
async def mint_token(
self,
creator: Account,
Expand All @@ -466,7 +468,7 @@ async def mint_token(
name: str,
uri: str,
properties: PropertyMap,
) -> str:
) -> str: # <:!:mint_token
payload = AptosTokenClient.mint_token_payload(
collection, description, name, uri, properties
)
Expand Down Expand Up @@ -515,6 +517,12 @@ async def mint_soul_bound_token(
)
return await self.client.submit_bcs_transaction(signed_transaction)

# :!:>transfer_token
async def transfer_token(
self, owner: Account, token: AccountAddress, to: AccountAddress
) -> str:
return await self.client.transfer_object(owner, token, to) # <:!:transfer_token

async def burn_token(self, creator: Account, token: AccountAddress) -> str:
payload = EntryFunction.natural(
"0x4::aptos_token",
Expand Down
21 changes: 21 additions & 0 deletions ecosystem/python/sdk/aptos_sdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,27 @@ async def get_collection(
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:
"""Faucet creates and funds accounts. This is a thin wrapper around that."""
Expand Down
13 changes: 12 additions & 1 deletion ecosystem/python/sdk/examples/aptos-token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, Object, Property, PropertyMap
from aptos_sdk.async_client import FaucetClient, RestClient

from .common import FAUCET_URL, NODE_URL
Expand Down Expand Up @@ -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()


Expand Down

0 comments on commit 1f14deb

Please sign in to comment.