Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create instructions codec #954

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions pkg/solana/codec/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ func NewNamedModifierCodec(original types.RemoteCodec, itemType string, modifier
return modCodec, err
}

func NewIDLInstructionsCodec(idl IDL, builder encodings.Builder) (types.RemoteCodec, error) {
typeCodecs := make(encodings.LenientCodecFromTypeCodec)
caser := cases.Title(language.English)
refs := &codecRefs{
builder: builder,
codecs: make(map[string]encodings.TypeCodec),
typeDefs: idl.Types,
dependencies: make(map[string][]string),
}

for _, instruction := range idl.Instructions {
name, instCodec, err := asStruct(instruction.Args, refs, instruction.Name, caser, false)
if err != nil {
return nil, err
}

typeCodecs[name] = instCodec
}

return typeCodecs, nil
}

// NewIDLAccountCodec is for Anchor custom types
func NewIDLAccountCodec(idl IDL, builder encodings.Builder) (types.RemoteCodec, error) {
return newIDLCoded(idl, builder, idl.Accounts, true)
Expand Down Expand Up @@ -115,7 +137,7 @@ func createNamedCodec(

switch def.Type.Kind {
case IdlTypeDefTyKindStruct:
return asStruct(def, refs, name, caser, includeDiscriminator)
return asStruct(*def.Type.Fields, refs, name, caser, includeDiscriminator)
case IdlTypeDefTyKindEnum:
variants := def.Type.Variants
if !variants.IsAllUint8() {
Expand All @@ -129,7 +151,7 @@ func createNamedCodec(
}

func asStruct(
def IdlTypeDef,
fields []IdlField,
refs *codecRefs,
name string, // name is the struct name and can be used in dependency checks
caser cases.Caser,
Expand All @@ -139,13 +161,13 @@ func asStruct(
if includeDiscriminator {
desLen = 1
}
named := make([]encodings.NamedTypeCodec, len(*def.Type.Fields)+desLen)
named := make([]encodings.NamedTypeCodec, len(fields)+desLen)

if includeDiscriminator {
named[0] = encodings.NamedTypeCodec{Name: "Discriminator" + name, Codec: NewDiscriminator(name)}
}

for idx, field := range *def.Type.Fields {
for idx, field := range fields {
fieldName := field.Name

typedCodec, err := processFieldType(name, field.Type, refs)
Expand Down
Loading