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

[solana] Fix vesting config initialization with pre-created vault ATA #260

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solana/app/deploy/devnet/tests/getProgramIdBytes32Hex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Usage: npx ts-node app/deploy/getProgramIdBytes32Hex.ts
// Usage: npx ts-node app/deploy/devnet/tests/getProgramIdBytes32Hex.ts
// AFuHPdrQGsW8rNQ4oEFF35sm5fg36gwrxyqjkjKvi6ap

import { PublicKey } from "@solana/web3.js";
Expand Down
2 changes: 1 addition & 1 deletion solana/programs/staking/src/contexts/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Initialize<'info> {
mint: Account<'info, Mint>,
// Initialize a vault for us to store our money in escrow for vesting
#[account(
init,
init_if_needed,
payer = admin,
associated_token::mint = mint,
associated_token::authority = config,
Expand Down
46 changes: 46 additions & 0 deletions solana/tests/vesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
createAssociatedTokenAccountIdempotentInstruction,
createAssociatedTokenAccountInstruction,
createInitializeMintInstruction,
createMintToInstruction,
createTransferCheckedInstruction,
Expand Down Expand Up @@ -77,12 +78,15 @@ describe("vesting", () => {
const vesterWithoutAccount = Keypair.generate();
const seed = new BN(randomBytes(8));
const seed2 = new BN(randomBytes(8));
const seed3 = new BN(randomBytes(8));

let accounts,
config,
config2,
config3,
vault,
vault2,
vault3,
vesterTa,
vester2Ta,
vester3Ta,
Expand Down Expand Up @@ -161,6 +165,14 @@ describe("vesting", () => {
],
stakeConnection.program.programId,
)[0];
config3 = PublicKey.findProgramAddressSync(
[
Buffer.from(wasm.Constants.VESTING_CONFIG_SEED()),
whMintAccount.publicKey.toBuffer(),
seed3.toBuffer("le", 8),
],
stakeConnection.program.programId,
)[0];
vault = getAssociatedTokenAddressSync(
whMintAccount.publicKey,
config,
Expand All @@ -173,6 +185,12 @@ describe("vesting", () => {
true,
TOKEN_PROGRAM_ID,
);
vault3 = getAssociatedTokenAddressSync(
whMintAccount.publicKey,
config3,
true,
TOKEN_PROGRAM_ID,
);
vesterTa = getAssociatedTokenAddressSync(
whMintAccount.publicKey,
vester.publicKey,
Expand Down Expand Up @@ -670,6 +688,34 @@ describe("vesting", () => {
}
});

it("should successfully initialize vesting config if vault ATA is pre-created by another user", async () => {
const tx = new Transaction();
tx.add(
createAssociatedTokenAccountInstruction(
vester.publicKey, // payer
vault3, // associatedToken
config3, // owner
whMintAccount.publicKey, // mint
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
),
);
await vesterStakeConnection.provider.sendAndConfirm(tx, [vester])
.then(confirm);
await sleep(1500);

await stakeConnection.program.methods
.initializeVestingConfig(seed3)
.accounts({
...accounts,
config: config3,
vault: vault3,
})
.signers([whMintAuthority])
.rpc()
.then(confirm);
});

it("Initialize config", async () => {
await stakeConnection.program.methods
.initializeVestingConfig(seed)
Expand Down
Loading