Skip to content

Commit 5eb5720

Browse files
fix(sdk-coin-sol): remove coin iteration to fetch programId from getAssociatedTokenAccountAddress
TICKET: WIN-5942
1 parent 4f4593b commit 5eb5720

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

modules/sdk-coin-sol/src/lib/ataInitializationBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class AtaInitializationBuilder extends TransactionBuilder {
163163
// Use the provided ataAddress if it exists, otherwise calculate it
164164
let ataPk = recipient.ataAddress;
165165
if (!ataPk) {
166-
ataPk = await getAssociatedTokenAccountAddress(tokenAddress, recipient.ownerAddress);
166+
ataPk = await getAssociatedTokenAccountAddress(tokenAddress, recipient.ownerAddress, false, programId);
167167
}
168168

169169
this._instructionsData.push({

modules/sdk-coin-sol/src/lib/tokenTransferBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class TokenTransferBuilder extends TransactionBuilder {
132132
} else {
133133
throw new Error(`Could not determine token information for ${sendParams.tokenName}`);
134134
}
135-
const sourceAddress = await getAssociatedTokenAccountAddress(tokenAddress, this._sender);
135+
const sourceAddress = await getAssociatedTokenAccountAddress(tokenAddress, this._sender, false, programId);
136136
return {
137137
type: InstructionBuilderTypes.TokenTransfer,
138138
params: {
@@ -172,7 +172,7 @@ export class TokenTransferBuilder extends TransactionBuilder {
172172
// Use the provided ataAddress if it exists, otherwise calculate it
173173
let ataAddress = recipient.ataAddress;
174174
if (!ataAddress) {
175-
ataAddress = await getAssociatedTokenAccountAddress(tokenAddress, recipient.ownerAddress);
175+
ataAddress = await getAssociatedTokenAccountAddress(tokenAddress, recipient.ownerAddress, false, programId);
176176
}
177177
return {
178178
type: InstructionBuilderTypes.CreateAssociatedTokenAccount,

modules/sdk-coin-sol/src/lib/transferBuilderV2.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class TransferBuilderV2 extends TransactionBuilder {
152152
throw new Error(`Could not determine token information for ${sendParams.tokenName}`);
153153
}
154154

155-
const sourceAddress = await getAssociatedTokenAccountAddress(tokenAddress, this._sender);
155+
const sourceAddress = await getAssociatedTokenAccountAddress(tokenAddress, this._sender, false, programId);
156156
return {
157157
type: InstructionBuilderTypes.TokenTransfer,
158158
params: {
@@ -198,7 +198,12 @@ export class TransferBuilderV2 extends TransactionBuilder {
198198
} else {
199199
throw new Error(`Could not determine token information for ${recipient.tokenName}`);
200200
}
201-
const recipientTokenAddress = await getAssociatedTokenAccountAddress(tokenAddress, recipient.ownerAddress);
201+
const recipientTokenAddress = await getAssociatedTokenAccountAddress(
202+
tokenAddress,
203+
recipient.ownerAddress,
204+
false,
205+
programId
206+
);
202207
return {
203208
type: InstructionBuilderTypes.CreateAssociatedTokenAccount,
204209
params: {

modules/sdk-coin-sol/src/lib/utils.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ export function getSolTokenFromTokenName(tokenName: string): Readonly<SolCoin> |
533533
export async function getAssociatedTokenAccountAddress(
534534
tokenMintAddress: string,
535535
ownerAddress: string,
536-
allowOwnerOffCurve = false
536+
allowOwnerOffCurve = false,
537+
programId?: string
537538
): Promise<string> {
538539
const mintPublicKey = new PublicKey(tokenMintAddress);
539540
const ownerPublicKey = new PublicKey(ownerAddress);
@@ -543,15 +544,16 @@ export async function getAssociatedTokenAccountAddress(
543544
throw new UtilsError('Invalid ownerAddress - address off ed25519 curve, got: ' + ownerAddress);
544545
}
545546

546-
const coin = getSolTokenFromAddressOnly(tokenMintAddress);
547-
let ataAddress: PublicKey;
548-
let programId: string;
549-
if (coin && coin instanceof SolCoin && (coin as any).programId) {
550-
programId = (coin as any).programId.toString();
551-
} else {
552-
programId = TOKEN_PROGRAM_ID.toString();
547+
if (!programId) {
548+
const coin = getSolTokenFromAddressOnly(tokenMintAddress);
549+
if (coin && coin instanceof SolCoin && (coin as any).programId) {
550+
programId = (coin as any).programId.toString();
551+
} else {
552+
programId = TOKEN_PROGRAM_ID.toString();
553+
}
553554
}
554555

556+
let ataAddress: PublicKey;
555557
if (programId === TOKEN_2022_PROGRAM_ID.toString()) {
556558
ataAddress = await getAssociatedTokenAddress(mintPublicKey, ownerPublicKey, false, TOKEN_2022_PROGRAM_ID);
557559
} else {

modules/sdk-coin-sol/src/sol.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ export class Sol extends BaseCoin {
286286
return getAssociatedTokenAccountAddress(
287287
tokenMintAddress!.tokenAddress,
288288
recipientFromUser.address,
289-
true
289+
true,
290+
tokenMintAddress!.programId
290291
).then((ata: string) => {
291292
return ata === recipientFromTx.address;
292293
});
@@ -846,7 +847,9 @@ export class Sol extends BaseCoin {
846847

847848
const recipientTokenAccount = await getAssociatedTokenAccountAddress(
848849
tokenAccount.info.mint,
849-
params.recoveryDestination
850+
params.recoveryDestination,
851+
false,
852+
params.programId?.toString()
850853
);
851854
const tokenName = tokenAccount.tokenName as string;
852855
txBuilder.send({

modules/sdk-coin-sol/test/unit/sol.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,11 +2091,15 @@ describe('SOL:', function () {
20912091

20922092
const source2022TokenAccount = await getAssociatedTokenAccountAddress(
20932093
t22mintAddress,
2094-
testData.wrwUser.walletAddress0
2094+
testData.wrwUser.walletAddress0,
2095+
false,
2096+
TOKEN_2022_PROGRAM_ID.toString()
20952097
);
20962098
const destination2022TokenAccount = await getAssociatedTokenAccountAddress(
20972099
t22mintAddress,
2098-
testData.keys.destinationPubKey2
2100+
testData.keys.destinationPubKey2,
2101+
false,
2102+
TOKEN_2022_PROGRAM_ID.toString()
20992103
);
21002104
should.equal(instructionsData[1].type, 'TokenTransfer');
21012105
should.equal(instructionsData[1].params.fromAddress, testData.wrwUser.walletAddress0);

modules/sdk-coin-sol/test/unit/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
stakingWithdrawInstructionsIndexes,
1010
} from '../../src/lib/constants';
1111
import BigNumber from 'bignumber.js';
12+
import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
1213

1314
describe('SOL util library', function () {
1415
describe('isValidAddress', function () {
@@ -320,14 +321,22 @@ describe('SOL util library', function () {
320321
const tokenAddress = 'JC2HAixRNrhBgNjCsV2PBfZEdyitEnWqJPNGokFD8xSA';
321322
it('should succeed for native address as owner address', async function () {
322323
const ownerAddress = testData.sol2022AuthAccount.pub;
323-
const result = await Utils.getAssociatedTokenAccountAddress(ai16ZMintAddress, ownerAddress);
324+
const result = await Utils.getAssociatedTokenAccountAddress(
325+
ai16ZMintAddress,
326+
ownerAddress,
327+
false,
328+
TOKEN_2022_PROGRAM_ID.toString()
329+
);
324330
result.should.be.equal(tokenAddress);
325331
});
326332
it('should fail for token address as owner address', async function () {
327333
const invalidOwnerAddress = tokenAddress;
328-
await Utils.getAssociatedTokenAccountAddress(ai16ZMintAddress, invalidOwnerAddress).should.be.rejectedWith(
329-
'Invalid ownerAddress - address off ed25519 curve, got: ' + invalidOwnerAddress
330-
);
334+
await Utils.getAssociatedTokenAccountAddress(
335+
ai16ZMintAddress,
336+
invalidOwnerAddress,
337+
false,
338+
TOKEN_2022_PROGRAM_ID.toString()
339+
).should.be.rejectedWith('Invalid ownerAddress - address off ed25519 curve, got: ' + invalidOwnerAddress);
331340
});
332341
});
333342

0 commit comments

Comments
 (0)