Fixed problem with TokenAccount commitments.
This commit is contained in:
parent
aad71929ae
commit
847628dfd6
|
@ -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,22 +95,28 @@ 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:
|
||||
# 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:
|
||||
# There is an old-style account so use that.
|
||||
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:
|
||||
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:
|
||||
# There is an old-style account so use that.
|
||||
return largest.address
|
||||
|
||||
# There is no old-style token account either, so create the proper associated token account.
|
||||
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
|
||||
def from_layout(layout: layouts.TOKEN_ACCOUNT, account_info: AccountInfo, token: Token) -> "TokenAccount":
|
||||
token_value = TokenValue(token, token.shift_to_decimals(layout.amount))
|
||||
|
|
Loading…
Reference in New Issue