Skip to content

Commit

Permalink
✨ Add option to change order asc/desc of existing leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Dec 29, 2023
1 parent abe71fa commit 2709988
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 7 deletions.
24 changes: 24 additions & 0 deletions client/sdk/src/idl/soar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ export type Soar = {
name: "leaderboard";
isMut: true;
isSigner: false;
},
{
name: "topEntries";
isMut: true;
isSigner: false;
isOptional: true;
}
];
args: [
Expand Down Expand Up @@ -278,6 +284,12 @@ export type Soar = {
option: "u64";
};
},
{
name: "newIsAscending";
type: {
option: "bool";
};
},
{
name: "newAllowMultipleScores";
type: {
Expand Down Expand Up @@ -1994,6 +2006,12 @@ export const IDL: Soar = {
isMut: true,
isSigner: false,
},
{
name: "topEntries",
isMut: true,
isSigner: false,
isOptional: true,
},
],
args: [
{
Expand All @@ -2020,6 +2038,12 @@ export const IDL: Soar = {
option: "u64",
},
},
{
name: "newIsAscending",
type: {
option: "bool",
},
},
{
name: "newAllowMultipleScores",
type: {
Expand Down
5 changes: 4 additions & 1 deletion client/sdk/src/instructions/accountsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,13 @@ export class AccountsBuilder {
updateLeaderboardAccounts = async (
authority: PublicKey,
leaderboard: PublicKey,
game?: PublicKey
game?: PublicKey,
topEntries?: PublicKey
): Promise<{
authority: PublicKey;
game: PublicKey;
leaderboard: PublicKey;
topEntries: PublicKey | null;
}> => {
const gameAddress =
game ?? (await this.program.account.leaderBoard.fetch(leaderboard)).game;
Expand All @@ -550,6 +552,7 @@ export class AccountsBuilder {
authority,
game: gameAddress,
leaderboard,
topEntries: topEntries ?? null,
};
};

Expand Down
6 changes: 4 additions & 2 deletions client/sdk/src/instructions/ixBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ export class InstructionBuilder {
args: UpdateLeaderboardArgs,
authority: PublicKey,
leaderboard: PublicKey,
game?: PublicKey
game?: PublicKey,
topEntries?: PublicKey
): Promise<
[
InstructionBuilder,
Expand All @@ -275,7 +276,8 @@ export class InstructionBuilder {
const accounts = await this.accounts.updateLeaderboardAccounts(
authority,
leaderboard,
game
game,
topEntries
);
const instruction = await updateLeaderBoardInstruction(
this.program,
Expand Down
2 changes: 2 additions & 0 deletions client/sdk/src/instructions/rawInstructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export const updateLeaderBoardInstruction = async (
authority: PublicKey;
game: PublicKey;
leaderboard: PublicKey;
topEntries: PublicKey | null;
},
pre?: TransactionInstruction[]
): Promise<TransactionInstruction> => {
Expand All @@ -330,6 +331,7 @@ export const updateLeaderBoardInstruction = async (
args.newNftMeta,
args.newMinScore,
args.newMaxScore,
args.newIsAscending,
args.newAllowMultipleScores
)
.accounts(accounts)
Expand Down
9 changes: 7 additions & 2 deletions client/sdk/src/soar.program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ export class SoarProgram {
newNftMeta?: PublicKey,
newMinScore?: BN,
newMaxScore?: BN,
newAllowMultipleScores?: boolean
newIsAscending?: boolean,
newAllowMultipleScores?: boolean,
topEntries?: PublicKey
): Promise<InstructionResult.UpdateLeaderboard> {
this.builder.clean();
if (newDescription === undefined && newNftMeta === undefined) {
Expand All @@ -287,10 +289,13 @@ export class SoarProgram {
newNftMeta: newNftMeta ?? null,
newMinScore: newMinScore ?? null,
newMaxScore: newMaxScore ?? null,
newIsAscending: newIsAscending ?? null,
newAllowMultipleScores: newAllowMultipleScores ?? null,
},
authority,
leaderboard
leaderboard,
undefined,
topEntries ?? undefined
);

return { transaction: step[0].build() };
Expand Down
1 change: 1 addition & 0 deletions client/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface UpdateLeaderboardArgs {
newNftMeta: PublicKey | null;
newMinScore: BN | null;
newMaxScore: BN | null;
newIsAscending: boolean | null;
newAllowMultipleScores: boolean | null;
}
export interface UpdatePlayerArgs {
Expand Down
58 changes: 56 additions & 2 deletions client/tests/soar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,62 @@ describe("soar", () => {
expect(info.nftMeta.toBase58()).to.equal(newMeta.toBase58());
});

it("Can update a leaderboard with top entries", async () => {
const leaderboard = leaderBoards[0];
const newDescription = "newDescription";
const newMeta = Keypair.generate().publicKey;

let info = await client.fetchLeaderBoardAccount(leaderboard);
let entries = await gameClient.program.fetchLeaderBoardTopEntriesAccount(
info.topEntries
);

let prevIsAscending = entries.isAscending;
let prevAllowMultipleScores = info.allowMultipleScores;

const { transaction } = await client.updateGameLeaderboard(
auths[0].publicKey,
leaderboard,
newDescription,
newMeta,
info.minScore,
info.maxScore,
!prevIsAscending,
!prevAllowMultipleScores,
info.topEntries
);
await client.sendAndConfirmTransaction(transaction, [auths[0]]);

info = await client.fetchLeaderBoardAccount(leaderboard);
expect(info.allowMultipleScores).to.be.not.equal(prevAllowMultipleScores);

entries = await gameClient.program.fetchLeaderBoardTopEntriesAccount(
info.topEntries
);
expect(entries.isAscending).to.be.not.equal(prevIsAscending);

const { transaction: transactionR } = await client.updateGameLeaderboard(
auths[0].publicKey,
leaderboard,
newDescription,
newMeta,
info.minScore,
info.maxScore,
prevIsAscending,
prevAllowMultipleScores,
info.topEntries
);
await client.sendAndConfirmTransaction(transactionR, [auths[0]]);

info = await client.fetchLeaderBoardAccount(leaderboard);
expect(info.allowMultipleScores).to.be.equal(prevAllowMultipleScores);

entries = await gameClient.program.fetchLeaderBoardTopEntriesAccount(
info.topEntries
);
expect(entries.isAscending).to.be.equal(prevIsAscending);
});

it("Can't add a leaderboard with the wrong authority", async () => {
const dummyKeypair = Keypair.generate();
const expectedFail = await gameClient
Expand Down Expand Up @@ -1335,8 +1391,6 @@ describe("soar", () => {
);
await client.sendAndConfirmTransaction(transaction, [auths[1]]);

console.log("leaderboard", newLeaderBoard.toBase58());

const info = await gameClient.program.fetchLeaderBoardAccount(
newLeaderBoard
);
Expand Down
7 changes: 7 additions & 0 deletions programs/soar/src/instructions/update_leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn handler(
new_nft_meta: Option<Pubkey>,
new_min_score: Option<u64>,
new_max_score: Option<u64>,
new_is_ascending: Option<bool>,
new_allow_multiple_scores: Option<bool>,
) -> Result<()> {
let leaderboard = &mut ctx.accounts.leaderboard;
Expand All @@ -24,6 +25,12 @@ pub fn handler(
if let Some(min_score) = new_min_score {
leaderboard.min_score = min_score;
}
if let Some(is_ascending) = new_is_ascending {
let top_entries = &mut ctx.accounts.top_entries;
if let Some(top_entries) = top_entries {
top_entries.is_ascending = is_ascending;
}
}
if let Some(allow_multiple_scores) = new_allow_multiple_scores {
leaderboard.allow_multiple_scores = allow_multiple_scores;
}
Expand Down
4 changes: 4 additions & 0 deletions programs/soar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub mod soar {
new_nft_meta: Option<Pubkey>,
new_min_score: Option<u64>,
new_max_score: Option<u64>,
new_is_ascending: Option<bool>,
new_allow_multiple_scores: Option<bool>,
) -> Result<()> {
update_leaderboard::handler(
Expand All @@ -95,6 +96,7 @@ pub mod soar {
new_nft_meta,
new_min_score,
new_max_score,
new_is_ascending,
new_allow_multiple_scores,
)
}
Expand Down Expand Up @@ -319,6 +321,8 @@ pub struct UpdateLeaderBoard<'info> {
has_one = game
)]
pub leaderboard: Account<'info, LeaderBoard>,
#[account(mut)]
pub top_entries: Option<Account<'info, LeaderTopEntries>>,
}

#[derive(Accounts)]
Expand Down

0 comments on commit 2709988

Please sign in to comment.