Skip to content

Commit

Permalink
fix fee payer and balance
Browse files Browse the repository at this point in the history
* Handle account not yet created for fee payer
* Use coin balance instead of resource reading due to FA migration
  • Loading branch information
davidiw committed Sep 25, 2024
1 parent a21e780 commit e335bce
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the Aptos Python SDK will be captured in this file. This

## Unreleased

## 0.9.1
- For `account_sequence_number`, return 0 if account has yet to be created to better support sponsored transactions create account if not exists
- For `account_balance`, Use `0x1::coin::balance` instead of reading the resource

## 0.9.0
- Add Multikey support for Python, with an example
- Deprecate and remove non-BCS transaction submission
Expand Down
19 changes: 13 additions & 6 deletions aptos_sdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ async def account_balance(
:param ledger_version: Ledger version to get state of account. If not provided, it will be the latest version.
:return: The Aptos coin balance associated with the account
"""
resource = await self.account_resource(
account_address,
"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
result = await self.view_bcs_payload(
"0x1::coin",
"balance",
[TypeTag(StructTag.from_str("0x1::aptos_coin::AptosCoin"))],
[TransactionArgument(account_address, Serializer.struct)],
ledger_version,
)
return int(resource["data"]["coin"]["value"])
return int(result[0])

async def account_sequence_number(
self, account_address: AccountAddress, ledger_version: Optional[int] = None
Expand All @@ -143,8 +145,13 @@ async def account_sequence_number(
:param ledger_version: Ledger version to get state of account. If not provided, it will be the latest version.
:return: The current sequence number for the specified address.
"""
account_res = await self.account(account_address, ledger_version)
return int(account_res["sequence_number"])
try:
account_res = await self.account(account_address, ledger_version)
return int(account_res["sequence_number"])
except ApiError as ae:
if ae.status_code != 404:
raise
return 0

async def account_resource(
self,
Expand Down
4 changes: 2 additions & 2 deletions examples/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# :!:>section_1
FAUCET_URL = os.getenv(
"APTOS_FAUCET_URL",
"http://localhost:8081",
"https://faucet.devnet.aptoslabs.com",
)
INDEXER_URL = os.getenv(
"APTOS_INDEXER_URL",
"https://api.devnet.aptoslabs.com/v1/graphql",
)
NODE_URL = os.getenv("APTOS_NODE_URL", "http://localhost:8080/v1")
NODE_URL = os.getenv("APTOS_NODE_URL", "https://api.devnet.aptoslabs.com/v1")
# <:!:section_1
18 changes: 3 additions & 15 deletions examples/fee_payer_transfer_coin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio

from aptos_sdk.account import Account
from aptos_sdk.async_client import ApiError, FaucetClient, ResourceNotFound, RestClient
from aptos_sdk.async_client import FaucetClient, RestClient
from aptos_sdk.authenticator import Authenticator, FeePayerAuthenticator
from aptos_sdk.bcs import Serializer
from aptos_sdk.transactions import (
Expand Down Expand Up @@ -38,20 +38,8 @@ async def main():

print("\n=== Initial Data ===")
# :!:>section_4
try:
alice_sequence_number = await rest_client.account_sequence_number(
alice.address()
)
raise Exception("This should never be accessed")
except ApiError:
alice_sequence_number = 0

try:
bob_balance = await rest_client.account_balance(bob.address())
raise Exception("This should never be accessed")
except ResourceNotFound:
bob_balance = None

alice_sequence_number = await rest_client.account_sequence_number(alice.address())
bob_balance = await rest_client.account_balance(bob.address())
sponsor_balance = await rest_client.account_balance(sponsor.address())
print(f"Alice sequence number: {alice_sequence_number}")
print(f"Bob balance: {bob_balance}")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aptos-sdk"
version = "0.9.0"
version = "0.9.1"
description = "Aptos SDK"
authors = ["Aptos Labs <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit e335bce

Please sign in to comment.