Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: associated_token constraints don't work when setting token_program #2603

Merged
merged 8 commits into from
Aug 23, 2023
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ fn generate_constraint_init_group(
return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::ConstraintAssociatedTokenTokenProgram).with_account_name(#name_str).with_pubkeys((*owner_program, #token_program.key())));
}

if pa.key() != ::anchor_spl::associated_token::get_associated_token_address(&#owner.key(), &#mint.key()) {
if pa.key() != ::anchor_spl::associated_token::get_associated_token_address_with_program_id(&#owner.key(), &#mint.key(), &#token_program.key()) {
return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::AccountNotAssociatedTokenAccount).with_account_name(#name_str));
}
}
Expand Down
28 changes: 16 additions & 12 deletions tests/misc/tests/misc/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const { assert, expect } = require("chai");
const nativeAssert = require("assert");
const miscIdl = require("../../target/idl/misc.json");

const TOKEN_2022_PROGRAM_ID = new anchor.web3.PublicKey(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
);

const miscTest = (
program: anchor.Program<Misc> | anchor.Program<MiscOptional>
) => {
Expand Down Expand Up @@ -1060,14 +1064,14 @@ const miscTest = (
mint: newMint.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_2022_PROGRAM_ID,
},
signers: [newMint],
});

const associatedToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
newMint.publicKey,
provider.wallet.publicKey
);
Expand All @@ -1078,15 +1082,15 @@ const miscTest = (
mint: newMint.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
associatedTokenTokenProgram: TOKEN_PROGRAM_ID,
associatedTokenTokenProgram: TOKEN_2022_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
},
});

const token = new Token(
program.provider.connection,
newMint.publicKey,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
wallet.payer
);
const ataAccount = await token.getAccountInfo(associatedToken);
Expand Down Expand Up @@ -1767,14 +1771,14 @@ const miscTest = (
mint: newMint.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_2022_PROGRAM_ID,
},
signers: [newMint],
});

const associatedToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
newMint.publicKey,
provider.wallet.publicKey
);
Expand All @@ -1786,15 +1790,15 @@ const miscTest = (
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
associatedTokenTokenProgram: TOKEN_PROGRAM_ID,
associatedTokenTokenProgram: TOKEN_2022_PROGRAM_ID,
authority: provider.wallet.publicKey,
},
});

const mintClient = new Token(
provider.connection,
newMint.publicKey,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
wallet.payer
);
const ataAccount = await mintClient.getAccountInfo(associatedToken);
Expand Down Expand Up @@ -2439,14 +2443,14 @@ const miscTest = (
mint: mint.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_2022_PROGRAM_ID,
},
signers: [mint],
});

const associatedToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
mint.publicKey,
provider.wallet.publicKey
);
Expand All @@ -2457,7 +2461,7 @@ const miscTest = (
mint: mint.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_2022_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
},
});
Expand All @@ -2468,7 +2472,7 @@ const miscTest = (
mint: mint.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
associatedTokenTokenProgram: TOKEN_PROGRAM_ID,
associatedTokenTokenProgram: TOKEN_2022_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
authority: provider.wallet.publicKey,
},
Expand Down