Skip to content

Commit

Permalink
Move lifetime token compile into transaction-messages (#2455)
Browse files Browse the repository at this point in the history
More of the same from this stack, but no codec to move since they just compile to a string

Ignore the hacky cast in message.ts, that'll go away when the input is the new transaction-message types!
  • Loading branch information
mcintyre94 authored Apr 8, 2024
1 parent 1bb084f commit d9cfe21
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Blockhash } from '@solana/rpc-types';

import { getCompiledLifetimeToken } from '../compile-lifetime-token';
import { Nonce } from '../durable-nonce';
import { getCompiledLifetimeToken } from '../../compile/lifetime-token';
import { NewNonce } from '../../durable-nonce';

describe('getCompiledLifetimeToken', () => {
it('compiles a recent blockhash lifetime constraint', () => {
Expand All @@ -13,7 +13,7 @@ describe('getCompiledLifetimeToken', () => {
});
it('compiles a nonce lifetime constraint', () => {
const token = getCompiledLifetimeToken({
nonce: 'abc' as Nonce,
nonce: 'abc' as NewNonce,
});
expect(token).toBe('abc');
});
Expand Down
1 change: 1 addition & 0 deletions packages/transaction-messages/src/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './accounts';
export * from './address-table-lookups';
export * from './header';
export * from './instructions';
export * from './lifetime-token';
13 changes: 13 additions & 0 deletions packages/transaction-messages/src/compile/lifetime-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IDurableNonceTransactionMessage, ITransactionMessageWithBlockhashLifetime } from '../index';

export function getCompiledLifetimeToken(
lifetimeConstraint: (
| IDurableNonceTransactionMessage
| ITransactionMessageWithBlockhashLifetime
)['lifetimeConstraint'],
): string {
if ('nonce' in lifetimeConstraint) {
return lifetimeConstraint.nonce;
}
return lifetimeConstraint.blockhash;
}
4 changes: 2 additions & 2 deletions packages/transactions/src/__tests__/message-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Address } from '@solana/addresses';
import {
getCompiledAddressTableLookups,
getCompiledInstructions,
getCompiledLifetimeToken,
getCompiledMessageHeader,
} from '@solana/transaction-messages';

import { ITransactionWithBlockhashLifetime } from '../blockhash';
import { getCompiledLifetimeToken } from '../compile-lifetime-token';
import { getCompiledStaticAccounts } from '../compile-static-accounts';
import { ITransactionWithFeePayer } from '../fee-payer';
import { compileTransactionMessage } from '../message';
Expand All @@ -16,9 +16,9 @@ jest.mock('@solana/transaction-messages', () => ({
...jest.requireActual('@solana/transaction-messages'),
getCompiledAddressTableLookups: jest.fn(),
getCompiledInstructions: jest.fn(),
getCompiledLifetimeToken: jest.fn(),
getCompiledMessageHeader: jest.fn(),
}));
jest.mock('../compile-lifetime-token');
jest.mock('../compile-static-accounts');

const MOCK_LIFETIME_CONSTRAINT =
Expand Down
10 changes: 0 additions & 10 deletions packages/transactions/src/compile-lifetime-token.ts

This file was deleted.

8 changes: 6 additions & 2 deletions packages/transactions/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
getAddressMapFromInstructions,
getCompiledAddressTableLookups,
getCompiledInstructions,
getCompiledLifetimeToken,
getCompiledMessageHeader,
getOrderedAccountsFromAddressMap,
} from '@solana/transaction-messages';

import { CompilableTransaction } from './compilable-transaction';
import { getCompiledLifetimeToken } from './compile-lifetime-token';
import { getCompiledStaticAccounts } from './compile-static-accounts';

type BaseCompiledMessage = Readonly<{
Expand Down Expand Up @@ -43,7 +43,11 @@ export function compileTransactionMessage(transaction: CompilableTransaction): C
: null),
header: getCompiledMessageHeader(orderedAccounts),
instructions: getCompiledInstructions(transaction.instructions, orderedAccounts),
lifetimeToken: getCompiledLifetimeToken(transaction.lifetimeConstraint),
// TODO later: remove this cast, gross hack because of renamed types
// won't apply when the input is from transaction-messages
lifetimeToken: getCompiledLifetimeToken(
transaction.lifetimeConstraint as Parameters<typeof getCompiledLifetimeToken>[0],
),
staticAccounts: getCompiledStaticAccounts(orderedAccounts),
version: transaction.version,
};
Expand Down

0 comments on commit d9cfe21

Please sign in to comment.