Skip to content

Commit 4eb6377

Browse files
committed
chore: update token lottery
1 parent 1568dac commit 4eb6377

File tree

5 files changed

+155
-63
lines changed

5 files changed

+155
-63
lines changed

project-8-token-lottery/Anchor.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ token_lottery = "2RTh2Y4e2N421EbSnUYTKdGqDHJH7etxZb3VrWDMpNMY"
1111
url = "https://api.apr.dev"
1212

1313
[provider]
14-
cluster = "Localnet"
14+
cluster = "localnet"
1515
wallet = "~/.config/solana/id.json"
1616

1717
[scripts]
1818
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
19+
20+
[[test.validator.clone]]
21+
address = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"

project-8-token-lottery/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

project-8-token-lottery/programs/token-lottery/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ anchor-lang = { version="0.30.1", features=["init-if-needed"]}
2121
anchor-spl = { version="0.30.1", features=["metadata"]}
2222
mpl-token-metadata = "4.1.2"
2323
solana-program = "1.18.17"
24-
switchboard-on-demand = "0.1.12"
24+
switchboard-on-demand = "0.1.13"

project-8-token-lottery/programs/token-lottery/src/lib.rs

Lines changed: 107 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ use anchor_spl::metadata::{
1010
MetadataAccount,
1111
Metadata,
1212
CreateMetadataAccountsV3,
13-
SetAndVerifySizedCollectionItem,
13+
CreateMasterEditionV3,
14+
SignMetadata,
15+
create_master_edition_v3,
1416
create_metadata_accounts_v3,
15-
set_and_verify_sized_collection_item,
17+
sign_metadata,
1618
mpl_token_metadata::{
17-
instructions::{
18-
CreateMasterEditionV3Cpi,
19-
CreateMasterEditionV3CpiAccounts,
20-
CreateMasterEditionV3InstructionArgs,
21-
CreateMetadataAccountV3Cpi,
22-
CreateMetadataAccountV3CpiAccounts,
23-
CreateMetadataAccountV3InstructionArgs,
24-
},
2519
types::{
26-
Collection,
20+
CollectionDetails,
2721
Creator,
2822
DataV2,
2923
},
@@ -46,17 +40,98 @@ pub mod token_lottery {
4640

4741
pub fn initialize(ctx: Context<Initialize>, start: u64, end: u64, price: u64) -> Result<()> {
4842
ctx.accounts.token_lottery.bump = ctx.bumps.token_lottery;
49-
ctx.accounts.token_lottery.mint = ctx.accounts.mint.key();
5043
ctx.accounts.token_lottery.lottery_start = start;
5144
ctx.accounts.token_lottery.lottery_end = end;
5245
ctx.accounts.token_lottery.price = price;
53-
ctx.accounts.token_lottery.authority = ctx.accounts.signer.key();
46+
ctx.accounts.token_lottery.authority = ctx.accounts.payer.key();
5447
ctx.accounts.token_lottery.randomness_account = Pubkey::default();
5548

56-
// Is this needed Jacob?
49+
let lottery_account_key = &ctx.accounts.token_lottery.key();
50+
5751
ctx.accounts.token_lottery.ticket_num = 0;
5852

5953
// Create Collection Mint
54+
let signer_seeds: &[&[&[u8]]] = &[&[
55+
lottery_account_key.as_ref(),
56+
&[ctx.bumps.collection_mint],
57+
]];
58+
59+
msg!("Creating mint accounts");
60+
mint_to(
61+
CpiContext::new_with_signer(
62+
ctx.accounts.token_program.to_account_info(),
63+
MintTo {
64+
mint: ctx.accounts.collection_mint.to_account_info(),
65+
to: ctx.accounts.collection_token_account.to_account_info(),
66+
authority: ctx.accounts.collection_mint.to_account_info(),
67+
},
68+
signer_seeds,
69+
),
70+
1,
71+
)?;
72+
73+
msg!("Creating metadata accounts");
74+
create_metadata_accounts_v3(
75+
CpiContext::new_with_signer(
76+
ctx.accounts.token_metadata_program.to_account_info(),
77+
CreateMetadataAccountsV3 {
78+
metadata: ctx.accounts.metadata.to_account_info(),
79+
mint: ctx.accounts.collection_mint.to_account_info(),
80+
mint_authority: ctx.accounts.collection_mint.to_account_info(), // use pda mint address as mint authority
81+
update_authority: ctx.accounts.collection_mint.to_account_info(), // use pda mint as update authority
82+
payer: ctx.accounts.payer.to_account_info(),
83+
system_program: ctx.accounts.system_program.to_account_info(),
84+
rent: ctx.accounts.rent.to_account_info(),
85+
},
86+
&signer_seeds,
87+
),
88+
DataV2 {
89+
name: NAME.to_string(),
90+
symbol: SYMBOL.to_string(),
91+
uri: URI.to_string(),
92+
seller_fee_basis_points: 0,
93+
creators: Some(vec![Creator {
94+
address: ctx.accounts.payer.key(),
95+
verified: false,
96+
share: 100,
97+
}]),
98+
collection: None,
99+
uses: None,
100+
},
101+
true,
102+
true,
103+
Some(CollectionDetails::V1 { size: 0 }), // set as collection nft
104+
)?;
105+
106+
msg!("Creating Master edition accounts");
107+
create_master_edition_v3(
108+
CpiContext::new_with_signer(
109+
ctx.accounts.token_metadata_program.to_account_info(),
110+
CreateMasterEditionV3 {
111+
payer: ctx.accounts.payer.to_account_info(),
112+
mint: ctx.accounts.collection_mint.to_account_info(),
113+
edition: ctx.accounts.master_edition.to_account_info(),
114+
mint_authority: ctx.accounts.collection_mint.to_account_info(),
115+
update_authority: ctx.accounts.collection_mint.to_account_info(),
116+
metadata: ctx.accounts.metadata.to_account_info(),
117+
token_program: ctx.accounts.token_program.to_account_info(),
118+
system_program: ctx.accounts.system_program.to_account_info(),
119+
rent: ctx.accounts.rent.to_account_info(),
120+
},
121+
&signer_seeds,
122+
),
123+
Some(0),
124+
)?;
125+
126+
msg!("verifying collection");
127+
sign_metadata(CpiContext::new(
128+
ctx.accounts.token_metadata_program.to_account_info(),
129+
SignMetadata {
130+
creator: ctx.accounts.collection_mint.to_account_info(),
131+
metadata: ctx.accounts.metadata.to_account_info(),
132+
},
133+
))?;
134+
60135

61136
Ok(())
62137
}
@@ -69,13 +144,6 @@ pub mod token_lottery {
69144
return Err(ErrorCode::LotteryNotOpen.into());
70145
}
71146

72-
let metadata = &ctx.accounts.metadata.to_account_info();
73-
let master_edition = &ctx.accounts.master_edition.to_account_info();
74-
let system_program = &ctx.accounts.system_program.to_account_info();
75-
let spl_token_program = &ctx.accounts.token_program.to_account_info();
76-
let spl_metadata_program = &ctx.accounts.token_metadata_program.to_account_info();
77-
78-
// Take Money
79147
system_program::transfer(
80148
CpiContext::new(
81149
ctx.accounts.system_program.to_account_info(),
@@ -133,30 +201,10 @@ pub mod token_lottery {
133201
None,
134202
)?;
135203

136-
set_and_verify_sized_collection_item(
137-
CpiContext::new_with_signer(
138-
ctx.accounts.token_metadata_program.to_account_info(),
139-
SetAndVerifySizedCollectionItem {
140-
metadata: ctx.accounts.metadata.to_account_info(),
141-
collection_authority: ctx.accounts.collection_mint.to_account_info(),
142-
payer: ctx.accounts.payer.to_account_info(),
143-
update_authority: ctx.accounts.collection_mint.to_account_info(),
144-
collection_mint: ctx.accounts.collection_mint.to_account_info(),
145-
collection_metadata: ctx.accounts.collection_metadata.to_account_info(),
146-
collection_master_edition: ctx
147-
.accounts
148-
.collection_master_edition
149-
.to_account_info(),
150-
},
151-
&signer_seeds,
152-
),
153-
None,
154-
)?;
155-
156204
Ok(())
157205
}
158206

159-
pub fn commit_a_winner(ctx: Context<CommitWinner>, randomness_account: Pubkey) -> Result<()> {
207+
pub fn commit_a_winner(ctx: Context<CommitWinner>) -> Result<()> {
160208
let clock = Clock::get()?;
161209
let token_lottery = &mut ctx.accounts.token_lottery;
162210

@@ -287,47 +335,51 @@ pub struct BuyTicket<'info> {
287335
#[derive(Accounts)]
288336
pub struct Initialize<'info> {
289337
#[account(mut)]
290-
pub signer: Signer<'info>,
338+
pub payer: Signer<'info>,
291339

292340
#[account(
293341
init,
294-
payer = signer,
342+
payer = payer,
295343
space = 8 + TokenLottery::INIT_SPACE,
296344
// Challenge: Make this be able to run more than 1 lottery at a time
297345
seeds = [b"token_lottery".as_ref()],
298346
bump
299347
)]
300-
pub token_lottery: Account<'info, TokenLottery>,
301-
302-
pub mint: InterfaceAccount<'info, Mint>,
348+
pub token_lottery: Box<Account<'info, TokenLottery>>,
303349

304350
#[account(
305351
init,
306-
payer = signer,
352+
payer = payer,
307353
mint::decimals = 0,
308354
mint::authority = collection_mint,
309355
seeds = [token_lottery.key().as_ref()],
310356
bump,
311357
)]
312-
pub collection_mint: InterfaceAccount<'info, Mint>,
358+
pub collection_mint: Box<InterfaceAccount<'info, Mint>>,
313359

360+
/// CHECK: This account will be initialized by the metaplex program
314361
#[account(mut)]
315-
pub metadata: Account<'info, MetadataAccount>,
362+
pub metadata: UncheckedAccount<'info>,
316363

364+
/// CHECK: This account will be initialized by the metaplex program
317365
#[account(mut)]
318-
pub master_edition: Account<'info, MasterEditionAccount>,
366+
pub master_edition: UncheckedAccount<'info>,
319367

320368
#[account(
321369
init_if_needed,
322-
payer = signer,
323-
associated_token::mint = collection_mint,
324-
associated_token::authority = collection_token_account
370+
payer = payer,
371+
seeds = [b"collection_token_account".as_ref()],
372+
bump,
373+
token::mint = collection_mint,
374+
token::authority = collection_token_account
325375
)]
326-
pub collection_token_account: InterfaceAccount<'info, TokenAccount>,
376+
pub collection_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
327377

328378
pub token_program: Interface<'info, TokenInterface>,
329379
pub associated_token_program: Program<'info, AssociatedToken>,
330380
pub system_program: Program<'info, System>,
381+
pub token_metadata_program: Program<'info, Metadata>,
382+
pub rent: Sysvar<'info, Rent>,
331383
}
332384

333385
#[account]
@@ -339,7 +391,6 @@ pub struct TokenLottery {
339391
pub lottery_end: u64,
340392
// Is it good practice to store SOL on an account used for something else?
341393
pub lottery_pot_amount: u64,
342-
pub mint: Pubkey,
343394
pub ticket_num: u32,
344395
pub price: u64,
345396
pub randomness_account: Pubkey,
Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
11
import * as anchor from "@coral-xyz/anchor";
22
import { Program } from "@coral-xyz/anchor";
33
import { TokenLottery } from "../target/types/token_lottery";
4+
import { Keypair, Transaction, Connection } from "@solana/web3.js";
5+
import { TOKEN_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
46

57
describe("token-lottery", () => {
68
// Configure the client to use the local cluster.
7-
anchor.setProvider(anchor.AnchorProvider.env());
9+
const provider = anchor.AnchorProvider.env();
10+
const connection = provider.connection;
11+
const wallet = provider.wallet as anchor.Wallet;
12+
anchor.setProvider(provider);
813

914
const program = anchor.workspace.TokenLottery as Program<TokenLottery>;
1015

16+
const TOKEN_METADATA_PROGRAM_ID = new anchor.web3.PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
17+
1118
it("Is initialized!", async () => {
12-
// Add your test here.
13-
const tx = await program.methods.initialize().rpc();
14-
console.log("Your transaction signature", tx);
19+
20+
const mintKeypair = Keypair.generate();
21+
const mint = mintKeypair.publicKey;
22+
23+
const metadata = anchor.web3.PublicKey.findProgramAddressSync(
24+
[Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer()],
25+
TOKEN_METADATA_PROGRAM_ID,
26+
)[0];
27+
28+
const masterEdition = anchor.web3.PublicKey.findProgramAddressSync(
29+
[Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer(), Buffer.from('edition')],
30+
TOKEN_METADATA_PROGRAM_ID,
31+
)[0];
32+
33+
const initIx = await program.methods.initialize(
34+
new anchor.BN(0),
35+
new anchor.BN(1821791542),
36+
new anchor.BN(10000),
37+
).accounts({
38+
masterEdition: masterEdition,
39+
metadata: metadata,
40+
tokenProgram: TOKEN_PROGRAM_ID,
41+
})
42+
.instruction();
43+
44+
const blockhashContext = await connection.getLatestBlockhash();
45+
46+
const tx = new Transaction({
47+
blockhash: blockhashContext.blockhash,
48+
lastValidBlockHeight: blockhashContext.lastValidBlockHeight,
49+
feePayer: wallet.payer.publicKey,
50+
}).add(initIx);
51+
52+
const sig = await anchor.web3.sendAndConfirmTransaction(connection, tx, [wallet.payer]);
53+
console.log(sig);
1554
});
1655
});

0 commit comments

Comments
 (0)