Fixed problem with TokenAccount commitments.

This commit is contained in:
Geoff Taylor 2021-08-17 19:17:09 +01:00
parent aad71929ae
commit 847628dfd6
1 changed files with 20 additions and 13 deletions

View File

@ -15,16 +15,17 @@
import typing
import spl.token.instructions as spl_token
from solana.account import Account
from solana.publickey import PublicKey
from solana.rpc.types import TokenAccountOpts
from spl.token.client import Token as SplToken
from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import get_associated_token_address
from .accountinfo import AccountInfo
from .addressableaccount import AddressableAccount
from .combinableinstructions import CombinableInstructions
from .context import Context
from .layouts import layouts
from .token import Token
@ -94,9 +95,12 @@ class TokenAccount(AddressableAccount):
@staticmethod
def find_or_create_token_address_to_use(context: Context, wallet: Wallet, owner: PublicKey, token: Token) -> PublicKey:
# This is a root wallet account - get the token account to use.
associated_token_address = get_associated_token_address(owner, token.mint)
associated_token_address = spl_token.get_associated_token_address(owner, token.mint)
token_account: typing.Optional[TokenAccount] = TokenAccount.load(context, associated_token_address)
if token_account is None:
if token_account is not None:
# The associated token account exists so use it
return associated_token_address
# There is no associated token account. See if they have an old-style non-associated token account.
largest = TokenAccount.fetch_largest_for_owner_and_token(context, owner, token)
if largest is not None:
@ -104,10 +108,13 @@ class TokenAccount(AddressableAccount):
return largest.address
# There is no old-style token account either, so create the proper associated token account.
spl_token = SplToken(context.client.compatible_client, token.mint, TOKEN_PROGRAM_ID, wallet.account)
return spl_token.create_associated_token_account(owner)
else:
# The associated token account exists so use it
signer = CombinableInstructions.from_wallet(wallet)
create_instruction = spl_token.create_associated_token_account(wallet.address, owner, token.mint)
create = CombinableInstructions.from_instruction(create_instruction)
transaction_ids = (signer + create).execute(context)
context.client.wait_for_confirmation(transaction_ids)
return associated_token_address
@staticmethod