mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-02-26 13:00:34 +00:00
fix creating associated token account
This commit is contained in:
parent
eb8c6a76e2
commit
c880dbd83c
2 changed files with 41 additions and 9 deletions
cw_solana/lib
|
@ -481,6 +481,9 @@ class SolanaWalletClient {
|
|||
final destinationOwner = Ed25519HDPublicKey.fromBase58(destinationAddress);
|
||||
final mint = Ed25519HDPublicKey.fromBase58(tokenMint);
|
||||
|
||||
// Input by the user
|
||||
final amount = (inputAmount * math.pow(10, tokenDecimals)).toInt();
|
||||
|
||||
ProgramAccount? associatedRecipientAccount;
|
||||
ProgramAccount? associatedSenderAccount;
|
||||
|
||||
|
@ -503,18 +506,46 @@ class SolanaWalletClient {
|
|||
}
|
||||
|
||||
try {
|
||||
associatedRecipientAccount ??= await _client!.createAssociatedTokenAccount(
|
||||
mint: mint,
|
||||
owner: destinationOwner,
|
||||
funder: ownerKeypair,
|
||||
);
|
||||
if (associatedRecipientAccount == null) {
|
||||
final derivedAddress = await findAssociatedTokenAddress(
|
||||
owner: destinationOwner,
|
||||
mint: mint,
|
||||
);
|
||||
|
||||
final instruction = AssociatedTokenAccountInstruction.createAccount(
|
||||
mint: mint,
|
||||
address: derivedAddress,
|
||||
owner: ownerKeypair.publicKey,
|
||||
funder: ownerKeypair.publicKey,
|
||||
);
|
||||
|
||||
final _signedTx = await _signTransactionInternal(
|
||||
message: Message.only(instruction),
|
||||
signers: [ownerKeypair],
|
||||
commitment: commitment,
|
||||
latestBlockhash: await _getLatestBlockhash(commitment),
|
||||
);
|
||||
|
||||
await sendTransaction(
|
||||
signedTransaction: _signedTx,
|
||||
commitment: commitment,
|
||||
);
|
||||
|
||||
associatedRecipientAccount = ProgramAccount(
|
||||
pubkey: derivedAddress.toBase58(),
|
||||
account: Account(
|
||||
owner: destinationOwner.toBase58(),
|
||||
lamports: 0,
|
||||
executable: false,
|
||||
rentEpoch: BigInt.zero,
|
||||
data: null,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
throw SolanaCreateAssociatedTokenAccountException(e.toString());
|
||||
}
|
||||
|
||||
// Input by the user
|
||||
final amount = (inputAmount * math.pow(10, tokenDecimals)).toInt();
|
||||
|
||||
final instruction = TokenInstruction.transfer(
|
||||
source: Ed25519HDPublicKey.fromBase58(associatedSenderAccount.pubkey),
|
||||
destination: Ed25519HDPublicKey.fromBase58(associatedRecipientAccount.pubkey),
|
||||
|
@ -587,6 +618,8 @@ class SolanaWalletClient {
|
|||
signedTransaction.encode(),
|
||||
preflightCommitment: commitment,
|
||||
);
|
||||
print("#########");
|
||||
print(signature);
|
||||
|
||||
_client!.waitForSignatureStatus(signature, status: commitment);
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||
import 'package:solana/base58.dart';
|
||||
import 'package:solana/metaplex.dart' as metaplex;
|
||||
import 'package:solana/solana.dart';
|
||||
import 'package:solana/src/crypto/ed25519_hd_keypair.dart';
|
||||
|
||||
part 'solana_wallet.g.dart';
|
||||
|
||||
|
|
Loading…
Reference in a new issue