Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Jun 26, 2024
1 parent bf0f814 commit a1d502e
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 105 deletions.
16 changes: 8 additions & 8 deletions templates/product-program/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use borsh::BorshDeserialize;
use clap::{Args, Parser, Subcommand};
use dephy_id_program_client::find_device_mint;
use dephy_id_product_program::{
instruction::{CreateVirtualDeviceArgs, InitArgs, ProgramInstruction},
instruction::{CreateDeviceArgs, InitArgs, ProgramInstruction},
utils::find_device,
state::ProgramAccount,
};
Expand Down Expand Up @@ -34,7 +34,7 @@ struct Cli {
#[derive(Debug, Subcommand)]
enum Commands {
InitProgram(InitProgramCliArgs),
CreateVirtualDevice(CreateVirtualDeviceCliArgs),
CreateDevice(CreateDeviceCliArgs),
}

#[derive(Debug, Args)]
Expand All @@ -51,7 +51,7 @@ struct InitProgramCliArgs {
}

#[derive(Debug, Args)]
struct CreateVirtualDeviceCliArgs {
struct CreateDeviceCliArgs {
#[arg(long = "owner")]
owner_keypair: String,
#[arg(long, default_value_t = 42)]
Expand Down Expand Up @@ -90,7 +90,7 @@ fn main() {

match args.command {
Commands::InitProgram(args) => init_program(args),
Commands::CreateVirtualDevice(args) => create_virtual_device(args),
Commands::CreateDevice(args) => create_device(args),
}
}

Expand Down Expand Up @@ -183,7 +183,7 @@ fn init_program(args: InitProgramCliArgs) {
};
}

fn create_virtual_device(args: CreateVirtualDeviceCliArgs) {
fn create_device(args: CreateDeviceCliArgs) {
let client = get_client(&args.common.url);
let program_id = args
.common
Expand Down Expand Up @@ -237,7 +237,7 @@ fn create_virtual_device(args: CreateVirtualDeviceCliArgs) {
let transaction = Transaction::new_signed_with_payer(
&[SolanaInstruction::new_with_borsh(
program_id,
&ProgramInstruction::CreateVirtualDevice(CreateVirtualDeviceArgs { challenge: 42 }),
&ProgramInstruction::CreateDevice(CreateDeviceArgs { challenge: 42 }),
vec![
// #[account(0, writable, name="program_pda", desc = "The program derived address of the program account to increment (seeds: ['Program'])")]
AccountMeta::new(program_account_pubkey, false),
Expand All @@ -257,7 +257,7 @@ fn create_virtual_device(args: CreateVirtualDeviceCliArgs) {
AccountMeta::new(product_mint_pubkey, false),
// #[account(8, name="owner", desc="The device's owner")]
AccountMeta::new(owner.pubkey(), false),
// #[account(9, name="device", desc = "PDA of the virtual device (seeds: ['DEVICE', owner])")]
// #[account(9, name="device", desc = "PDA of the device (seeds: ['DEVICE', owner])")]
AccountMeta::new(device_pubkey, false),
// #[account(10, writable, name="product_atoken", desc="The associated token account of the product")]
AccountMeta::new(product_atoken_pubkey, false),
Expand All @@ -275,7 +275,7 @@ fn create_virtual_device(args: CreateVirtualDeviceCliArgs) {
match client.send_and_confirm_transaction(&transaction) {
Ok(sig) => {
println!("Success: {:?}", sig);
println!("Virtual Device Created: {}", device_pubkey);
println!("Device Created: {}", device_pubkey);
}
Err(err) => {
eprintln!("Error: {:?}", err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ import {
import { PRODUCT_PROGRAM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export type CreateVirtualDeviceInstruction<
export type CreateDeviceInstruction<
TProgram extends string = typeof PRODUCT_PROGRAM_PROGRAM_ADDRESS,
TAccountProgramPda extends string | IAccountMeta<string> = string,
TAccountPayer extends string | IAccountMeta<string> = string,
TAccountSystemProgram extends
| string
| IAccountMeta<string> = '11111111111111111111111111111111',
TAccountToken2022Program extends
| string
| IAccountMeta<string> = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb',
TAccountToken2022Program extends string | IAccountMeta<string> = string,
TAccountAtaProgram extends
| string
| IAccountMeta<string> = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
Expand Down Expand Up @@ -100,14 +98,14 @@ export type CreateVirtualDeviceInstruction<
]
>;

export type CreateVirtualDeviceInstructionData = {
export type CreateDeviceInstructionData = {
discriminator: number;
challenge: number;
};

export type CreateVirtualDeviceInstructionDataArgs = { challenge: number };
export type CreateDeviceInstructionDataArgs = { challenge: number };

export function getCreateVirtualDeviceInstructionDataEncoder(): Encoder<CreateVirtualDeviceInstructionDataArgs> {
export function getCreateDeviceInstructionDataEncoder(): Encoder<CreateDeviceInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
Expand All @@ -117,24 +115,24 @@ export function getCreateVirtualDeviceInstructionDataEncoder(): Encoder<CreateVi
);
}

export function getCreateVirtualDeviceInstructionDataDecoder(): Decoder<CreateVirtualDeviceInstructionData> {
export function getCreateDeviceInstructionDataDecoder(): Decoder<CreateDeviceInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['challenge', getU8Decoder()],
]);
}

export function getCreateVirtualDeviceInstructionDataCodec(): Codec<
CreateVirtualDeviceInstructionDataArgs,
CreateVirtualDeviceInstructionData
export function getCreateDeviceInstructionDataCodec(): Codec<
CreateDeviceInstructionDataArgs,
CreateDeviceInstructionData
> {
return combineCodec(
getCreateVirtualDeviceInstructionDataEncoder(),
getCreateVirtualDeviceInstructionDataDecoder()
getCreateDeviceInstructionDataEncoder(),
getCreateDeviceInstructionDataDecoder()
);
}

export type CreateVirtualDeviceInput<
export type CreateDeviceInput<
TAccountProgramPda extends string = string,
TAccountPayer extends string = string,
TAccountSystemProgram extends string = string,
Expand All @@ -156,7 +154,7 @@ export type CreateVirtualDeviceInput<
/** The system program */
systemProgram?: Address<TAccountSystemProgram>;
/** The SPL Token 2022 program */
token2022Program?: Address<TAccountToken2022Program>;
token2022Program: Address<TAccountToken2022Program>;
/** The associated token program */
ataProgram?: Address<TAccountAtaProgram>;
/** DePHY ID program id */
Expand All @@ -175,10 +173,10 @@ export type CreateVirtualDeviceInput<
deviceMint: Address<TAccountDeviceMint>;
/** The associated token account for the device */
deviceAtoken: Address<TAccountDeviceAtoken>;
challenge: CreateVirtualDeviceInstructionDataArgs['challenge'];
challenge: CreateDeviceInstructionDataArgs['challenge'];
};

export function getCreateVirtualDeviceInstruction<
export function getCreateDeviceInstruction<
TAccountProgramPda extends string,
TAccountPayer extends string,
TAccountSystemProgram extends string,
Expand All @@ -193,7 +191,7 @@ export function getCreateVirtualDeviceInstruction<
TAccountDeviceMint extends string,
TAccountDeviceAtoken extends string,
>(
input: CreateVirtualDeviceInput<
input: CreateDeviceInput<
TAccountProgramPda,
TAccountPayer,
TAccountSystemProgram,
Expand All @@ -208,7 +206,7 @@ export function getCreateVirtualDeviceInstruction<
TAccountDeviceMint,
TAccountDeviceAtoken
>
): CreateVirtualDeviceInstruction<
): CreateDeviceInstruction<
typeof PRODUCT_PROGRAM_PROGRAM_ADDRESS,
TAccountProgramPda,
TAccountPayer,
Expand Down Expand Up @@ -259,10 +257,6 @@ export function getCreateVirtualDeviceInstruction<
accounts.systemProgram.value =
'11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
}
if (!accounts.token2022Program.value) {
accounts.token2022Program.value =
'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb' as Address<'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb'>;
}
if (!accounts.ataProgram.value) {
accounts.ataProgram.value =
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL' as Address<'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'>;
Expand All @@ -286,10 +280,10 @@ export function getCreateVirtualDeviceInstruction<
getAccountMeta(accounts.deviceAtoken),
],
programAddress,
data: getCreateVirtualDeviceInstructionDataEncoder().encode(
args as CreateVirtualDeviceInstructionDataArgs
data: getCreateDeviceInstructionDataEncoder().encode(
args as CreateDeviceInstructionDataArgs
),
} as CreateVirtualDeviceInstruction<
} as CreateDeviceInstruction<
typeof PRODUCT_PROGRAM_PROGRAM_ADDRESS,
TAccountProgramPda,
TAccountPayer,
Expand All @@ -309,7 +303,7 @@ export function getCreateVirtualDeviceInstruction<
return instruction;
}

export type ParsedCreateVirtualDeviceInstruction<
export type ParsedCreateDeviceInstruction<
TProgram extends string = typeof PRODUCT_PROGRAM_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
Expand Down Expand Up @@ -342,17 +336,17 @@ export type ParsedCreateVirtualDeviceInstruction<
/** The associated token account for the device */
deviceAtoken: TAccountMetas[12];
};
data: CreateVirtualDeviceInstructionData;
data: CreateDeviceInstructionData;
};

export function parseCreateVirtualDeviceInstruction<
export function parseCreateDeviceInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedCreateVirtualDeviceInstruction<TProgram, TAccountMetas> {
): ParsedCreateDeviceInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 13) {
// TODO: Coded error.
throw new Error('Not enough accounts');
Expand Down Expand Up @@ -380,8 +374,6 @@ export function parseCreateVirtualDeviceInstruction<
deviceMint: getNextAccount(),
deviceAtoken: getNextAccount(),
},
data: getCreateVirtualDeviceInstructionDataDecoder().decode(
instruction.data
),
data: getCreateDeviceInstructionDataDecoder().decode(instruction.data),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* @see https://github.com/kinobi-so/kinobi
*/

export * from './createVirtualDevice';
export * from './createDevice';
export * from './init';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { containsBytes, getU8Encoder, type Address } from '@solana/web3.js';
import {
type ParsedCreateVirtualDeviceInstruction,
type ParsedCreateDeviceInstruction,
type ParsedInitInstruction,
} from '../instructions';
import { Key, getKeyEncoder } from '../types';
Expand All @@ -34,7 +34,7 @@ export function identifyProductProgramAccount(

export enum ProductProgramInstruction {
Init,
CreateVirtualDevice,
CreateDevice,
}

export function identifyProductProgramInstruction(
Expand All @@ -46,7 +46,7 @@ export function identifyProductProgramInstruction(
return ProductProgramInstruction.Init;
}
if (containsBytes(data, getU8Encoder().encode(1), 0)) {
return ProductProgramInstruction.CreateVirtualDevice;
return ProductProgramInstruction.CreateDevice;
}
throw new Error(
'The provided instruction could not be identified as a productProgram instruction.'
Expand All @@ -60,5 +60,5 @@ export type ParsedProductProgramInstruction<
instructionType: ProductProgramInstruction.Init;
} & ParsedInitInstruction<TProgram>)
| ({
instructionType: ProductProgramInstruction.CreateVirtualDevice;
} & ParsedCreateVirtualDeviceInstruction<TProgram>);
instructionType: ProductProgramInstruction.CreateDevice;
} & ParsedCreateDeviceInstruction<TProgram>);
Loading

0 comments on commit a1d502e

Please sign in to comment.