Skip to content

Commit

Permalink
fix : handle single ix without discriminator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Dec 17, 2024
1 parent cfb4a3f commit 8574dc8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! <https://github.com/codama-idl/codama>
//!
use wen_transfer_guard_program_sdk::accounts::guardV1::Guardv1;
use wen_transfer_guard_program_sdk::accounts::guard_v1::GuardV1;
use wen_transfer_guard_program_sdk::ID;

/// WenTransferGuard Program State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ impl InstructionParser {
let accounts_len = ix.accounts.len();
let ix_discriminator: [u8; 8] = ix.data[0..8].try_into()?;
let mut ix_data = &ix.data[8..];

match ix_discriminator {
[251, 254, 17, 198, 219, 218, 154, 99] => {
check_min_accounts_req(accounts_len, 8)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ use dummy_program_sdk::ID;
/// Dummy Instructions
#[derive(Debug)]
pub enum DummyProgramIx {
Instruction1(Instruction1IxAccounts, Instruction1IxData),
Instruction2(Instruction2IxAccounts, Instruction2IxData),
Instruction1(Instruction1IxAccounts),
Instruction2(Instruction2IxAccounts),
Instruction3(Instruction3IxAccounts, Instruction3IxData),
Instruction4(Instruction4IxAccounts, Instruction4IxData),
Instruction5(Instruction5IxAccounts, Instruction5IxData),
Instruction6(Instruction6IxAccounts, Instruction6IxData),
Instruction7(Instruction7IxAccounts, Instruction7IxData),
Instruction6(Instruction6IxAccounts),
Instruction7(Instruction7IxAccounts),
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -71,7 +71,6 @@ impl InstructionParser {
let accounts_len = ix.accounts.len();
let ix_discriminator: [u8; 1] = ix.data[0..1].try_into()?;
let mut ix_data = &ix.data[1..];

match ix_discriminator {
[42] => {
check_min_accounts_req(accounts_len, 0)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ impl InstructionParser {
let accounts_len = ix.accounts.len();
let ix_discriminator: [u8; 1] = ix.data[0..1].try_into()?;
let mut ix_data = &ix.data[1..];

match ix_discriminator {
_ => Err(yellowstone_vixen_core::ParseError::from(
"Invalid Instruction discriminator".to_owned(),
)),
}
check_min_accounts_req(accounts_len, 0)?;
let de_ix_data: AddMemoIxData = BorshDeserialize::deserialize(&mut ix_data)?;
let ix_accounts = AddMemoIxAccounts {};
Ok(MemoProgramIx::AddMemo(ix_accounts, de_ix_data))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ impl InstructionParser {
let accounts_len = ix.accounts.len();
let ix_discriminator: [u8; 1] = ix.data[0..1].try_into()?;
let mut ix_data = &ix.data[1..];

match ix_discriminator {
[0] => {
check_min_accounts_req(accounts_len, 2)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#[derive(Debug)]
pub enum {{ programName | pascalCase }}ProgramState {
{% for account in accounts %}
{{ account.name | pascalCase }}({{ account.name | pascalCase }}),
{{ account.name | pascalCase }}({{ account.name | pascalCase }}),
{% endfor %}
}

Expand All @@ -20,16 +20,14 @@ impl {{ programName | pascalCase }}ProgramState {
let data_len = data_bytes.len();
match data_len {
{% for account in accounts %}
{{ account.name | pascalCase }}::LEN => Ok(
{{ programName | pascalCase }}ProgramState::{{ account.name | pascalCase }}(
{{ account.name | pascalCase }}::from_bytes(data_bytes)?
)
),
{{ account.name | pascalCase }}::LEN => Ok(
{{ programName | pascalCase }}ProgramState::{{ account.name | pascalCase }}(
{{ account.name | pascalCase }}::from_bytes(data_bytes)?
)
),
{% endfor %}
_ => Err(yellowstone_vixen_core::ParseError::from("Invalid Account data length".to_owned())),
}


}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#[derive(Debug)]
pub enum {{ programName | pascalCase }}ProgramIx {
{% for instruction in instructions %}
{{ instruction.name | pascalCase }}({{ instruction.name | pascalCase }}IxAccounts, {{ instruction.name | pascalCase }}IxData),
{% if instruction.hasArgs %}
{{ instruction.name | pascalCase }}({{ instruction.name | pascalCase }}IxAccounts, {{ instruction.name | pascalCase }}IxData),
{% else %}
{{ instruction.name | pascalCase }}({{ instruction.name | pascalCase }}IxAccounts),
{% endif %}
{% endfor %}
}

Expand Down Expand Up @@ -52,27 +56,34 @@ impl InstructionParser {
let accounts_len = ix.accounts.len();
let ix_discriminator:[u8; {{ IX_DATA_OFFSET }}] = ix.data[0..{{ IX_DATA_OFFSET }}].try_into()?;
let mut ix_data = &ix.data[{{ IX_DATA_OFFSET }}..];

match ix_discriminator {
{% for instruction in instructions %}
{% if instruction.discriminator != null %}
{{ instruction.discriminator }} => {
check_min_accounts_req(accounts_len, {{ instruction.accounts | length }})?;
let de_ix_data:{{ instruction.name | pascalCase }}IxData = BorshDeserialize::deserialize(&mut ix_data)?;
let ix_accounts = {{ instruction.name | pascalCase }}IxAccounts{
{% for account in instruction.accounts %}
{{ account.name | snakeCase }}: ix.accounts[{{ account.index }}],
{% endfor %}
};
Ok({{ programName | pascalCase }}ProgramIx::{{ instruction.name | pascalCase }}(ix_accounts, de_ix_data))


},
{% endif %}
{% endfor %}
_ => Err(yellowstone_vixen_core::ParseError::from("Invalid Instruction discriminator".to_owned())),
}

{% if hasDiscriminator %}
match ix_discriminator {
{% for instruction in instructions %}
{% if instruction.discriminator != null %}
{{ instruction.discriminator }} => {
check_min_accounts_req(accounts_len, {{ instruction.accounts | length }})?;
let de_ix_data:{{ instruction.name | pascalCase }}IxData = BorshDeserialize::deserialize(&mut ix_data)?;
let ix_accounts = {{ instruction.name | pascalCase }}IxAccounts{
{% for account in instruction.accounts %}
{{ account.name | snakeCase }}: ix.accounts[{{ account.index }}],
{% endfor %}
};
Ok({{ programName | pascalCase }}ProgramIx::{{ instruction.name | pascalCase }}(ix_accounts, de_ix_data))
},
{% endif %}
{% endfor %}
_ => Err(yellowstone_vixen_core::ParseError::from("Invalid Instruction discriminator".to_owned())),
}
{% else %}
check_min_accounts_req(accounts_len, {{ instructions[0].accounts | length }})?;
let de_ix_data:{{ instructions[0].name | pascalCase }}IxData = BorshDeserialize::deserialize(&mut ix_data)?;
let ix_accounts = {{ instructions[0].name | pascalCase }}IxAccounts{
{% for account in instructions[0].accounts %}
{{ account.name | snakeCase }}: ix.accounts[{{ account.index }}],
{% endfor %}
};
Ok({{ programName | pascalCase }}ProgramIx::{{ instructions[0].name | pascalCase }}(ix_accounts, de_ix_data))
{% endif %}
}

}
Expand Down
3 changes: 2 additions & 1 deletion packages/renderers-vixen-parser/src/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {

accounts.forEach(acc => {
accountParserImports.add(
`${codamaSdkName}::accounts::{${acc.name}::${toPascalCase(acc.name)}}`,
`${codamaSdkName}::accounts::{${toSnakeCase(acc.name)}::${fromCamelToPascalCase(acc.name)}}`,
);
});

Expand Down Expand Up @@ -151,6 +151,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
imports: instructionParserImports,
programName,
accounts,
hasDiscriminator: instructions.some(ix => ix.discriminator !== null),
instructions,
IX_DATA_OFFSET,
};
Expand Down

0 comments on commit 8574dc8

Please sign in to comment.