From caca55340a0eac72586ef3c41d59e6675d3c532b Mon Sep 17 00:00:00 2001 From: John Rees Date: Fri, 21 Jan 2022 02:11:15 +0000 Subject: [PATCH] refactor: use BorshClass for DRYer classes --- packages/governance-sdk/src/chat/accounts.ts | 33 ++++++-------------- packages/governance-sdk/src/tools/borsh.ts | 6 ++++ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/packages/governance-sdk/src/chat/accounts.ts b/packages/governance-sdk/src/chat/accounts.ts index 59d752d3..e3ca26b3 100644 --- a/packages/governance-sdk/src/chat/accounts.ts +++ b/packages/governance-sdk/src/chat/accounts.ts @@ -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', @@ -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 {} -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 { + accountType = GovernanceChatAccountType.ChatMessage; } diff --git a/packages/governance-sdk/src/tools/borsh.ts b/packages/governance-sdk/src/tools/borsh.ts index 27a664e2..d17e5134 100644 --- a/packages/governance-sdk/src/tools/borsh.ts +++ b/packages/governance-sdk/src/tools/borsh.ts @@ -102,3 +102,9 @@ export function deserializeBorsh( const reader = new BinaryReader(buffer); return deserializeStruct(schema, classType, reader); } + +export abstract class BorshClass { + constructor(args: T) { + Object.assign(this, args); + } +}