Skip to content

Commit

Permalink
Created a transfer_object function in RestClient class and altered th…
Browse files Browse the repository at this point in the history
…e transfer_token function in the AptosTokenClient to just call transfer_object. Left it in for ease of use
  • Loading branch information
xbtmatt committed Aug 10, 2023
1 parent 1fdfd07 commit 769e0f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
19 changes: 2 additions & 17 deletions ecosystem/python/sdk/aptos_sdk/aptos_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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 @@ -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:
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, Property, PropertyMap, Object
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 769e0f1

Please sign in to comment.