mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-29 04:49:51 +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
|
@ -481,6 +481,9 @@ class SolanaWalletClient {
|
||||||
final destinationOwner = Ed25519HDPublicKey.fromBase58(destinationAddress);
|
final destinationOwner = Ed25519HDPublicKey.fromBase58(destinationAddress);
|
||||||
final mint = Ed25519HDPublicKey.fromBase58(tokenMint);
|
final mint = Ed25519HDPublicKey.fromBase58(tokenMint);
|
||||||
|
|
||||||
|
// Input by the user
|
||||||
|
final amount = (inputAmount * math.pow(10, tokenDecimals)).toInt();
|
||||||
|
|
||||||
ProgramAccount? associatedRecipientAccount;
|
ProgramAccount? associatedRecipientAccount;
|
||||||
ProgramAccount? associatedSenderAccount;
|
ProgramAccount? associatedSenderAccount;
|
||||||
|
|
||||||
|
@ -503,18 +506,46 @@ class SolanaWalletClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
associatedRecipientAccount ??= await _client!.createAssociatedTokenAccount(
|
if (associatedRecipientAccount == null) {
|
||||||
mint: mint,
|
final derivedAddress = await findAssociatedTokenAddress(
|
||||||
owner: destinationOwner,
|
owner: destinationOwner,
|
||||||
funder: ownerKeypair,
|
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) {
|
} catch (e) {
|
||||||
throw SolanaCreateAssociatedTokenAccountException(e.toString());
|
throw SolanaCreateAssociatedTokenAccountException(e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input by the user
|
|
||||||
final amount = (inputAmount * math.pow(10, tokenDecimals)).toInt();
|
|
||||||
|
|
||||||
final instruction = TokenInstruction.transfer(
|
final instruction = TokenInstruction.transfer(
|
||||||
source: Ed25519HDPublicKey.fromBase58(associatedSenderAccount.pubkey),
|
source: Ed25519HDPublicKey.fromBase58(associatedSenderAccount.pubkey),
|
||||||
destination: Ed25519HDPublicKey.fromBase58(associatedRecipientAccount.pubkey),
|
destination: Ed25519HDPublicKey.fromBase58(associatedRecipientAccount.pubkey),
|
||||||
|
@ -587,6 +618,8 @@ class SolanaWalletClient {
|
||||||
signedTransaction.encode(),
|
signedTransaction.encode(),
|
||||||
preflightCommitment: commitment,
|
preflightCommitment: commitment,
|
||||||
);
|
);
|
||||||
|
print("#########");
|
||||||
|
print(signature);
|
||||||
|
|
||||||
_client!.waitForSignatureStatus(signature, status: commitment);
|
_client!.waitForSignatureStatus(signature, status: commitment);
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,6 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:solana/base58.dart';
|
import 'package:solana/base58.dart';
|
||||||
import 'package:solana/metaplex.dart' as metaplex;
|
import 'package:solana/metaplex.dart' as metaplex;
|
||||||
import 'package:solana/solana.dart';
|
import 'package:solana/solana.dart';
|
||||||
import 'package:solana/src/crypto/ed25519_hd_keypair.dart';
|
|
||||||
|
|
||||||
part 'solana_wallet.g.dart';
|
part 'solana_wallet.g.dart';
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue