Skip to content

Commit

Permalink
Moved PDA seed IDL type builder out of the config
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed Jan 16, 2025
1 parent 4a9b5ee commit d1cb908
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 5 additions & 5 deletions pkg/solana/chainreader/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ func TestSolanaChainReaderService_GetLatestValue(t *testing.T) {
ReadType: config.Account,
PDADefiniton: codec.PDATypeDef{
Prefix: prefixString,
Seeds: []codec.IdlField{
Seeds: []codec.PDASeed{
{
Name: "PubKey",
Type: codec.NewIdlStringType(codec.IdlTypePublicKey),
Type: codec.IdlTypePublicKey,
},
{
Name: "Uint64Seed",
Type: codec.NewIdlStringType(codec.IdlTypeU64),
Type: codec.IdlTypeU64,
},
},
},
Expand Down Expand Up @@ -350,10 +350,10 @@ func TestSolanaChainReaderService_GetLatestValue(t *testing.T) {
ReadType: config.Account,
PDADefiniton: codec.PDATypeDef{
Prefix: prefixString,
Seeds: []codec.IdlField{
Seeds: []codec.PDASeed{
{
Name: "PubKey",
Type: codec.NewIdlStringType(codec.IdlTypePublicKey),
Type: codec.IdlTypePublicKey,
},
},
},
Expand Down
7 changes: 6 additions & 1 deletion pkg/solana/codec/anchoridl.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ type IdlField struct {
// It is needed to encode seeds to calculate the address for PDA account reads
type PDATypeDef struct {
Prefix string `json:"prefix,omitempty"`
Seeds []IdlField `json:"seeds,omitempty"`
Seeds []PDASeed `json:"seeds,omitempty"`
}

type PDASeed struct {
Name string `json:"name"`
Type IdlTypeAsString `json:"type"`
}

type IdlTypeAsString string
Expand Down
13 changes: 12 additions & 1 deletion pkg/solana/codec/codec_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewAccountEntry(offchainName string, idlTypes AccountIDLTypes, includeDiscr

func NewPDAEntry(offchainName string, pdaTypeDef PDATypeDef, mod codec.Modifier, builder commonencodings.Builder) (Entry, error) {
// PDA seeds do not have any dependecies in the IDL so the type def slice can be left empty for refs
_, accCodec, err := asStruct(pdaTypeDef.Seeds, createRefs(IdlTypeDefSlice{}, builder), offchainName, false, false)
_, accCodec, err := asStruct(pdaSeedsToIdlField(pdaTypeDef.Seeds), createRefs(IdlTypeDefSlice{}, builder), offchainName, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,3 +221,14 @@ func eventFieldsToFields(evFields []IdlEventField) []IdlField {
}
return idlFields
}

func pdaSeedsToIdlField(seeds []PDASeed) []IdlField {
idlFields := make([]IdlField, 0, len(seeds))
for _, seed := range seeds {
idlFields = append(idlFields, IdlField{
Name: seed.Name,
Type: NewIdlStringType(seed.Type),
})
}
return idlFields
}

0 comments on commit d1cb908

Please sign in to comment.