Skip to content

Commit

Permalink
In tests, only create token account if needed (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian authored Apr 24, 2023
1 parent b21c82e commit bf60726
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions sdk/tests/utils/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,25 @@ export async function createAndMintToAssociatedTokenAccount(
return tokenAccount;
}

const tokenAccount = await createAssociatedTokenAccount(
provider,
mint,
destinationWalletKey,
payerKey
);
const tokenAccounts = await provider.connection.getParsedTokenAccountsByOwner(destinationWalletKey, {
programId: TOKEN_PROGRAM_ID,
});

let tokenAccount = tokenAccounts.value.map((account) => {
if(account.account.data.parsed.info.mint === mint.toString()) {
return account.pubkey
}
}).filter(Boolean)[0];

if(!tokenAccount) {
tokenAccount = await createAssociatedTokenAccount(
provider,
mint,
destinationWalletKey,
payerKey
);
}

await mintToByAuthority(provider, mint, tokenAccount, new u64(amount.toString()));
return tokenAccount;
}
Expand Down

0 comments on commit bf60726

Please sign in to comment.