Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
refactor: use BorshClass for DRYer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrees committed Jan 21, 2022
1 parent fc519b4 commit caca553
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
33 changes: 9 additions & 24 deletions packages/governance-sdk/src/chat/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
import { BorshClass } from '../tools/borsh';

export const GOVERNANCE_CHAT_PROGRAM_ID = new PublicKey(
'gCHAtYKrUUktTVzE4hEnZdLV4LXrdBf6Hh9qMaJALET',
Expand All @@ -21,36 +22,20 @@ export enum ChatMessageBodyType {
Reaction = 1,
}

export class ChatMessageBody {
export interface ChatMessageBody {
type: ChatMessageBodyType;
value: string;

constructor(args: { type: ChatMessageBodyType; value: string }) {
this.type = args.type;
this.value = args.value;
}
}
export class ChatMessageBody extends BorshClass<ChatMessageBody> {}

export class ChatMessage {
accountType = GovernanceChatAccountType.ChatMessage;

interface ChatMessageProps {
proposal: PublicKey;
author: PublicKey;
postedAt: BN;
replyTo: PublicKey | undefined;
replyTo?: PublicKey;
body: ChatMessageBody;

constructor(args: {
proposal: PublicKey;
author: PublicKey;
postedAt: BN;
replyTo: PublicKey | undefined;
body: ChatMessageBody;
}) {
this.proposal = args.proposal;
this.author = args.author;
this.postedAt = args.postedAt;
this.replyTo = args.replyTo;
this.body = args.body;
}
}
export interface ChatMessage extends ChatMessageProps {}
export class ChatMessage extends BorshClass<ChatMessageProps> {
accountType = GovernanceChatAccountType.ChatMessage;
}
6 changes: 6 additions & 0 deletions packages/governance-sdk/src/tools/borsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ export function deserializeBorsh(
const reader = new BinaryReader(buffer);
return deserializeStruct(schema, classType, reader);
}

export abstract class BorshClass<T> {
constructor(args: T) {
Object.assign(this, args);
}
}

0 comments on commit caca553

Please sign in to comment.