feat: add PostMessage instruction

This commit is contained in:
Sebastian.Bor 2021-08-25 20:23:50 +02:00
parent e52d6acc61
commit 0b37707fc4
5 changed files with 168 additions and 0 deletions

View File

@ -0,0 +1,45 @@
import { Account, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { ParsedAccount } from '@oyster/common';
import { Proposal } from '../../models/accounts';
import { sendTransactionWithNotifications } from '../../tools/transactions';
import { RpcContext } from '../../models/core/api';
import { withPostMessage } from '../../models/chat/withPostMessage';
import { MessageBody } from '../../models/chat/accounts';
export const postMessage = async (
{ connection, wallet, programId, walletPubkey }: RpcContext,
proposal: ParsedAccount<Proposal>,
tokeOwnerRecord: PublicKey,
replyTo: PublicKey | undefined,
body: MessageBody,
) => {
let signers: Account[] = [];
let instructions: TransactionInstruction[] = [];
let governanceAuthority = walletPubkey;
let payer = walletPubkey;
await withPostMessage(
instructions,
signers,
programId,
proposal.info.governance,
proposal.pubkey,
tokeOwnerRecord,
governanceAuthority,
payer,
replyTo,
body,
);
await sendTransactionWithNotifications(
connection,
wallet,
instructions,
signers,
'Posting message',
'Message post',
);
};

View File

@ -1,6 +1,10 @@
import { PublicKey } from '@solana/web3.js'; import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js'; import BN from 'bn.js';
export const governanceChatProgramId = new PublicKey(
'gCHAtYKrUUktTVzE4hEnZdLV4LXrdBf6Hh9qMaJALET',
);
export enum GovernanceChatAccountType { export enum GovernanceChatAccountType {
Uninitialized = 0, Uninitialized = 0,
ChatMessage = 1, ChatMessage = 1,

View File

@ -0,0 +1,15 @@
import { MessageBody } from './accounts';
export enum GovernanceChatInstruction {
PostMessage = 0,
}
export class PostMessageArgs {
instruction: GovernanceChatInstruction =
GovernanceChatInstruction.PostMessage;
body: MessageBody;
constructor(args: { body: MessageBody }) {
this.body = args.body;
}
}

View File

@ -4,6 +4,7 @@ import {
MessageBody, MessageBody,
} from './accounts'; } from './accounts';
import { BorshAccountParser } from '../core/serialisation'; import { BorshAccountParser } from '../core/serialisation';
import { PostMessageArgs } from './instructions';
export const GOVERNANCE_CHAT_SCHEMA = new Map<any, any>([ export const GOVERNANCE_CHAT_SCHEMA = new Map<any, any>([
[ [
@ -30,6 +31,16 @@ export const GOVERNANCE_CHAT_SCHEMA = new Map<any, any>([
], ],
}, },
], ],
[
PostMessageArgs,
{
kind: 'struct',
fields: [
['instruction', 'u8'],
['body', MessageBody],
],
},
],
]); ]);
export const ChatAccountParser = (classType: GovernanceChatAccountClass) => export const ChatAccountParser = (classType: GovernanceChatAccountClass) =>

View File

@ -0,0 +1,93 @@
import { utils } from '@oyster/common';
import {
Account,
PublicKey,
SYSVAR_RENT_PUBKEY,
TransactionInstruction,
} from '@solana/web3.js';
import { GOVERNANCE_CHAT_SCHEMA } from './serialisation';
import { serialize } from 'borsh';
import { PostMessageArgs } from './instructions';
import { governanceChatProgramId, MessageBody } from './accounts';
export async function withPostMessage(
instructions: TransactionInstruction[],
signers: Account[],
governanceProgramId: PublicKey,
governance: PublicKey,
proposal: PublicKey,
tokenOwnerRecord: PublicKey,
governanceAuthority: PublicKey,
payer: PublicKey,
replyTo: PublicKey | undefined,
body: MessageBody,
) {
const { system: systemId } = utils.programIds();
const args = new PostMessageArgs({
body,
});
const data = Buffer.from(serialize(GOVERNANCE_CHAT_SCHEMA, args));
const chatMessage = new PublicKey('');
let keys = [
{
pubkey: governanceProgramId,
isWritable: false,
isSigner: false,
},
{
pubkey: governance,
isWritable: false,
isSigner: false,
},
{
pubkey: proposal,
isWritable: false,
isSigner: false,
},
{
pubkey: tokenOwnerRecord,
isWritable: false,
isSigner: false,
},
{
pubkey: governanceAuthority,
isWritable: false,
isSigner: true,
},
{
pubkey: chatMessage,
isWritable: true,
isSigner: true,
},
{
pubkey: payer,
isWritable: false,
isSigner: true,
},
{
pubkey: systemId,
isWritable: false,
isSigner: false,
},
];
if (replyTo) {
keys.push({
pubkey: replyTo,
isWritable: false,
isSigner: false,
});
}
instructions.push(
new TransactionInstruction({
keys,
programId: governanceChatProgramId,
data,
}),
);
}