Skip to content

Commit

Permalink
refactor(experimental): add opaque type for serialized message (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Aug 30, 2023
1 parent 8c9cacd commit ab8b149
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { base58, base64, fixSerializer } from '@metaplex-foundation/umi-serializ
import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/json-rpc-errors';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import { Blockhash } from '@solana/transactions';
import { Blockhash, SerializedMessageBytesBase64 } from '@solana/transactions';
import fetchMock from 'jest-fetch-mock-fork';

import { Base64EncodedBytes, Commitment } from '../common';
import { Commitment } from '../common';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

// See scripts/fixtures/send-transaction-fee-payer.json
Expand Down Expand Up @@ -50,7 +50,7 @@ function getMockTransactionMessage(blockhash: Blockhash) {
0x00, // Number of address table lookups
]);
const messageBase64 = base64.deserialize(message)[0];
return messageBase64 as Base64EncodedBytes;
return messageBase64 as SerializedMessageBytesBase64;
}

describe('getFeeForMessage', () => {
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('getFeeForMessage', () => {
describe('when called with an invalid message', () => {
it('throws an error', async () => {
expect.assertions(1);
const sendPromise = rpc.getFeeForMessage('someInvalidMessage' as Base64EncodedBytes).send();
const sendPromise = rpc.getFeeForMessage('someInvalidMessage' as SerializedMessageBytesBase64).send();
await expect(sendPromise).rejects.toMatchObject({
code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_INVALID_PARAMS'],
message: expect.any(String),
Expand Down
7 changes: 4 additions & 3 deletions packages/rpc-core/src/rpc-methods/getFeeForMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Base64EncodedBytes, Commitment, RpcResponse, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';
import { SerializedMessageBytesBase64 } from '@solana/transactions';

import { Commitment, RpcResponse, Slot, U64UnsafeBeyond2Pow53Minus1 } from './common';

/** Fee corresponding to the message at the specified blockhash */
type GetFeeForMessageApiResponse = RpcResponse<U64UnsafeBeyond2Pow53Minus1 | null>;
Expand All @@ -8,8 +10,7 @@ export interface GetFeeForMessageApi {
* Returns the fee the network will charge for a particular Message
*/
getFeeForMessage(
/** Base-64 encoded message */
message: Base64EncodedBytes,
message: SerializedMessageBytesBase64,
config?: Readonly<{
commitment?: Commitment;
minContextSlot?: Slot;
Expand Down
7 changes: 4 additions & 3 deletions packages/transactions/src/serializers/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { getBase58EncodedAddressCodec } from '@solana/addresses';

import { CompiledMessage } from '../message';
import { SerializedMessageBytes } from '../types';
import { getAddressTableLookupCodec } from './address-table-lookup';
import { getMessageHeaderCodec } from './header';
import { getInstructionCodec } from './instruction';
Expand Down Expand Up @@ -42,9 +43,9 @@ function deserialize(bytes: Uint8Array, offset = 0): [CompiledMessage, number] {
];
}

function serialize(compiledMessage: CompiledMessage) {
function serialize(compiledMessage: CompiledMessage): SerializedMessageBytes {
if (compiledMessage.version === 'legacy') {
return struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
return struct(getPreludeStructSerializerTuple()).serialize(compiledMessage) as SerializedMessageBytes;
} else {
return mapSerializer(
struct([
Expand All @@ -60,7 +61,7 @@ function serialize(compiledMessage: CompiledMessage) {
addressTableLookups: value.addressTableLookups ?? [],
} as Exclude<CompiledMessage, { readonly version: 'legacy' }>;
}
).serialize(compiledMessage);
).serialize(compiledMessage) as SerializedMessageBytes;
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/transactions/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { IAccountMeta, IInstruction } from '@solana/instructions';

/** A string of bytes that are definitely a serialized message */
export type SerializedMessageBytes = Uint8Array & { readonly __serializedMessageBytes: unique symbol };
export type SerializedMessageBytesBase64 = string & { readonly __serializedMessageBytesBase64: unique symbol };

export type BaseTransaction = Readonly<{
instructions: readonly IInstruction[];
version: TransactionVersion;
Expand Down

0 comments on commit ab8b149

Please sign in to comment.