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

new instruction to add an expense. #25

Closed
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
61 changes: 58 additions & 3 deletions packages/programs/solidr-program/client/SolidrClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export type Global = {
sessionCount: BN;
};

type InternalSessionStatus = ({ closed?: never } & { opened: Record<string, never> }) | ({ opened?: never } & { closed: Record<string, never> });
type InternalSessionStatus = ({ closed?: never } & { opened: Record<string, never> }) | ({ opened?: never } & {
closed: Record<string, never>
});

type InternalSession = {
sessionId: BN;
Expand Down Expand Up @@ -47,6 +49,14 @@ export type SessionMember = {

export const MISSING_INVITATION_HASH = new Array(32).fill(0).toString();

export type Expense = {
expenseId: BN;
name: string;
member: PublicKey;
date: BN;
participants: Array<PublicKey>;
}

export class SolidrClient extends AbstractSolanaClient<Solidr> {
public readonly globalAccountPubkey: PublicKey;

Expand Down Expand Up @@ -121,7 +131,10 @@ export class SolidrClient extends AbstractSolanaClient<Solidr> {
});
}

public async generateSessionLink(admin: Wallet, sessionId: string): Promise<ITransactionResult<{ token: string; hash: string }>> {
public async generateSessionLink(admin: Wallet, sessionId: string): Promise<ITransactionResult<{
token: string;
hash: string
}>> {
return this.wrapFn(async () => {
const sessionAccountPubkey = this.findSessionAccountAddress(sessionId);

Expand Down Expand Up @@ -193,7 +206,10 @@ export class SolidrClient extends AbstractSolanaClient<Solidr> {
});
}

public async listUserSessions(memberAccountPubkey: PublicKey, paginationOptions?: { page: number; perPage: number }): Promise<Session[]> {
public async listUserSessions(memberAccountPubkey: PublicKey, paginationOptions?: {
page: number;
perPage: number
}): Promise<Session[]> {
return this.wrapFn(async () => {
const memberAccountDiscriminator = Buffer.from(sha256.digest('account:MemberAccount')).subarray(0, 8);
const accounts = await this.connection.getProgramAccounts(this.program.programId, {
Expand Down Expand Up @@ -269,4 +285,43 @@ export class SolidrClient extends AbstractSolanaClient<Solidr> {
invitationHash: internalSession.invitationHash.toString(),
};
};

public async addExpense(member: Wallet, sessionId: bigint, name: any, amount: any) {
return this.wrapFn(async () => {
const sessionAccountPubkey = this.findSessionAccountAddress(sessionId);
const memberAccountAddress = this.findSessionMemberAccountAddress(sessionId, member.publicKey);
const expenseId = await this.getNextExpenseId(sessionAccountPubkey);
const expenseAccountPubkey = this.findExpenseAccountAddress(sessionId, expenseId);
const tx = await this.program.methods
.addExpense(name, amount)
.accountsPartial({
authority: member.publicKey,
session: sessionAccountPubkey,
member: memberAccountAddress,
expense: expenseAccountPubkey,
})
.transaction();

return this.signAndSendTransaction(member, tx, {
expenseAccountPubkey,
});
});
}

public async getNextExpenseId(sessionAccountPubkey): Promise<BN> {
return this.wrapFn(async () => {
return (await this.program.account.sessionAccount.fetch(sessionAccountPubkey)).expensesCount || new BN(0);
});
}

public findExpenseAccountAddress(sessionId: BN, expenseId: BN): PublicKey {
const [expenseAccountPubkey] = PublicKey.findProgramAddressSync([Buffer.from('expense'), sessionId.toBuffer('le', 8), expenseId.toBuffer('le', 2)], this.program.programId);
return expenseAccountPubkey;
}

public async getExpense(expenseAccountPubkey: PublicKey): Promise<Expense> {
return this.wrapFn(async () => {
return await this.program.account.expenseAccount.fetch(expenseAccountPubkey);
});
}
}
Loading
Loading