Skip to content

Commit

Permalink
Add NonceVersion and NonceState enums
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Dec 13, 2023
1 parent 2477d3a commit 1639c47
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 43 deletions.
33 changes: 19 additions & 14 deletions clients/js/src/generated/accounts/nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,44 @@ import {
getStructDecoder,
getStructEncoder,
} from '@solana/codecs-data-structures';
import {
getU32Decoder,
getU32Encoder,
getU64Decoder,
getU64Encoder,
} from '@solana/codecs-numbers';
import { getU64Decoder, getU64Encoder } from '@solana/codecs-numbers';
import { Context } from '../shared';
import {
NonceState,
NonceStateArgs,
NonceVersion,
NonceVersionArgs,
getNonceStateDecoder,
getNonceStateEncoder,
getNonceVersionDecoder,
getNonceVersionEncoder,
} from '../types';

export type Nonce<TAddress extends string = string> = Account<
NonceAccountData,
TAddress
>;

export type NonceAccountData = {
discriminator: number;
state: number;
version: NonceVersion;
state: NonceState;
authority: Address;
blockhash: Address;
lamportsPerSignature: bigint;
};

export type NonceAccountDataArgs = {
discriminator: number;
state: number;
version: NonceVersionArgs;
state: NonceStateArgs;
authority: Address;
blockhash: Address;
lamportsPerSignature: number | bigint;
};

export function getNonceAccountDataEncoder() {
return getStructEncoder<NonceAccountDataArgs>([
['discriminator', getU32Encoder()],
['state', getU32Encoder()],
['version', getNonceVersionEncoder()],
['state', getNonceStateEncoder()],
['authority', getAddressEncoder()],
['blockhash', getAddressEncoder()],
['lamportsPerSignature', getU64Encoder()],
Expand All @@ -65,8 +70,8 @@ export function getNonceAccountDataEncoder() {

export function getNonceAccountDataDecoder() {
return getStructDecoder<NonceAccountData>([
['discriminator', getU32Decoder()],
['state', getU32Decoder()],
['version', getNonceVersionDecoder()],
['state', getNonceStateDecoder()],
['authority', getAddressDecoder()],
['blockhash', getAddressDecoder()],
['lamportsPerSignature', getU64Decoder()],
Expand Down
1 change: 1 addition & 0 deletions clients/js/src/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './errors';
export * from './instructions';
export * from './programs';
export * from './shared';
export * from './types';
10 changes: 10 additions & 0 deletions clients/js/src/generated/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

export * from './nonceState';
export * from './nonceVersion';
37 changes: 37 additions & 0 deletions clients/js/src/generated/types/nonceState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Codec, Decoder, Encoder, combineCodec } from '@solana/codecs-core';
import {
getScalarEnumDecoder,
getScalarEnumEncoder,
} from '@solana/codecs-data-structures';
import { getU32Decoder, getU32Encoder } from '@solana/codecs-numbers';

export enum NonceState {
Uninitialized,
Initialized,
}

export type NonceStateArgs = NonceState;

export function getNonceStateEncoder() {
return getScalarEnumEncoder(NonceState, {
size: getU32Encoder(),
}) satisfies Encoder<NonceStateArgs>;
}

export function getNonceStateDecoder() {
return getScalarEnumDecoder(NonceState, {
size: getU32Decoder(),
}) satisfies Decoder<NonceState>;
}

export function getNonceStateCodec(): Codec<NonceStateArgs, NonceState> {
return combineCodec(getNonceStateEncoder(), getNonceStateDecoder());
}
37 changes: 37 additions & 0 deletions clients/js/src/generated/types/nonceVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Codec, Decoder, Encoder, combineCodec } from '@solana/codecs-core';
import {
getScalarEnumDecoder,
getScalarEnumEncoder,
} from '@solana/codecs-data-structures';
import { getU32Decoder, getU32Encoder } from '@solana/codecs-numbers';

export enum NonceVersion {
Legacy,
Current,
}

export type NonceVersionArgs = NonceVersion;

export function getNonceVersionEncoder() {
return getScalarEnumEncoder(NonceVersion, {
size: getU32Encoder(),
}) satisfies Encoder<NonceVersionArgs>;
}

export function getNonceVersionDecoder() {
return getScalarEnumDecoder(NonceVersion, {
size: getU32Decoder(),
}) satisfies Decoder<NonceVersion>;
}

export function getNonceVersionCodec(): Codec<NonceVersionArgs, NonceVersion> {
return combineCodec(getNonceVersionEncoder(), getNonceVersionDecoder());
}
9 changes: 9 additions & 0 deletions clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
setTransactionFeePayer,
setTransactionLifetimeUsingBlockhash,
} from '@solana/web3.js';
import { fetchEncodedAccount, fetchEncodedAccounts } from '@solana/accounts';
import { Context } from '../src';

type Client = {
rpc: ReturnType<typeof createSolanaRpc>;
Expand All @@ -42,6 +44,13 @@ export const createClient = (): Client => {
return { rpc, rpcSubscriptions };
};

export const createContext = (client: Client): Context => ({
fetchEncodedAccount: async (address, config) =>
fetchEncodedAccount(client.rpc, address, config),
fetchEncodedAccounts: async (addresses, config) =>
fetchEncodedAccounts(client.rpc, addresses, config),
});

export const generateKeyPairSignerWithSol = async (
client: Client,
putativeLamports: bigint = 1_000_000_000n
Expand Down
43 changes: 20 additions & 23 deletions clients/js/test/initializeNonceAccount.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { fetchEncodedAccount } from '@solana/accounts';
import { pipe } from '@solana/functional';
import { generateKeyPairSigner } from '@solana/signers';
import { appendTransactionInstruction } from '@solana/web3.js';
import test from 'ava';
import {
Context,
Nonce,
NonceState,
NonceVersion,
SPL_SYSTEM_PROGRAM_ADDRESS,
fetchNonce,
getCreateAccountInstruction,
getInitializeNonceAccountInstruction,
} from '../src';
import {
createClient,
createContext,
createDefaultTransaction,
generateKeyPairSignerWithSol,
signAndSendTransaction,
} from './_setup';

test('it can create and initialize a durable nonce account', async (t) => {
// Given
// Given some brand now payer, authority, and nonce KeyPairSigners.
const client = createClient();
const context = createContext(client);
const payer = await generateKeyPairSignerWithSol(client);
const nonce = await generateKeyPairSigner();
const nonceAuthority = await generateKeyPairSigner();

// When
// When we use them to create and initialize a nonce account.
const rent = await client.rpc.getMinimumBalanceForRentExemption(80n).send();
const createAccount = getCreateAccountInstruction({
payer,
Expand All @@ -35,7 +38,7 @@ test('it can create and initialize a durable nonce account', async (t) => {
});
const initializeNonceAccount = getInitializeNonceAccountInstruction({
nonceAccount: nonce.address,
nonceAuthority: payer.address,
nonceAuthority: nonceAuthority.address,
});
await pipe(
await createDefaultTransaction(client, payer.address),
Expand All @@ -44,22 +47,16 @@ test('it can create and initialize a durable nonce account', async (t) => {
(tx) => signAndSendTransaction(client, tx)
);

// Then
const context = {
fetchEncodedAccount: (
address: Parameters<typeof fetchEncodedAccount>[1],
config: Parameters<typeof fetchEncodedAccount>[2]
) => fetchEncodedAccount(client.rpc, address, config),
} as Pick<Context, 'fetchEncodedAccount'>;
const account = await fetchNonce(context, nonce.address, {
commitment: 'confirmed',
});
t.like(account, <Nonce>{
address: nonce.address,
data: {
discriminator: 1,
state: 1,
authority: payer.address,
},
});
// Then we expect the nonce account to exist with the following data.
t.like(
await fetchNonce(context, nonce.address, { commitment: 'confirmed' }),
<Nonce>{
address: nonce.address,
data: {
version: NonceVersion.Current,
state: NonceState.Initialized,
authority: nonceAuthority.address,
},
}
);
});
6 changes: 4 additions & 2 deletions clients/rust/src/generated/accounts/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
//! [https://github.com/metaplex-foundation/kinobi]
//!
use crate::generated::types::NonceState;
use crate::generated::types::NonceVersion;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Nonce {
pub discriminator: u32,
pub state: u32,
pub version: NonceVersion,
pub state: NonceState,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
Expand Down
1 change: 1 addition & 0 deletions clients/rust/src/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub mod accounts;
pub mod errors;
pub mod instructions;
pub mod programs;
pub mod types;

pub(crate) use programs::*;
12 changes: 12 additions & 0 deletions clients/rust/src/generated/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! [https://github.com/metaplex-foundation/kinobi]
//!
pub(crate) mod r#nonce_state;
pub(crate) mod r#nonce_version;

pub use self::r#nonce_state::*;
pub use self::r#nonce_version::*;
16 changes: 16 additions & 0 deletions clients/rust/src/generated/types/nonce_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! [https://github.com/metaplex-foundation/kinobi]
//!
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NonceState {
Uninitialized,
Initialized,
}
16 changes: 16 additions & 0 deletions clients/rust/src/generated/types/nonce_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! [https://github.com/metaplex-foundation/kinobi]
//!
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NonceVersion {
Legacy,
Current,
}
25 changes: 21 additions & 4 deletions idls/spl_system.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"kind": "struct",
"fields": [
{
"name": "discriminator",
"type": "u32"
"name": "version",
"type": { "defined": "NonceVersion" }
},
{
"name": "state",
"type": "u32"
"type": { "defined": "NonceState" }
},
{
"name": "authority",
Expand Down Expand Up @@ -420,7 +420,24 @@
}
}
],
"types": [],
"types": [
{
"name": "NonceVersion",
"type": {
"kind": "enum",
"size": "u32",
"variants": [{ "name": "Legacy" }, { "name": "Current" }]
}
},
{
"name": "NonceState",
"type": {
"kind": "enum",
"size": "u32",
"variants": [{ "name": "Uninitialized" }, { "name": "Initialized" }]
}
}
],
"errors": [
{
"code": 0,
Expand Down

0 comments on commit 1639c47

Please sign in to comment.