Skip to content

Commit

Permalink
Add GetProgramIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Apr 27, 2024
1 parent d3cb1f1 commit 2d7c474
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions keys.go
Expand Up @@ -567,7 +567,7 @@ const (
// Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L159
func CreateWithSeed(base PublicKey, seed string, owner PublicKey) (PublicKey, error) {
if len(seed) > MaxSeedLength {
return PublicKey{}, errors.New("Max seed length exceeded")
return PublicKey{}, ErrMaxSeedLengthExceeded
}

// let owner = owner.as_ref();
Expand All @@ -588,7 +588,7 @@ func CreateWithSeed(base PublicKey, seed string, owner PublicKey) (PublicKey, er

const PDA_MARKER = "ProgramDerivedAddress"

var ErrMaxSeedLengthExceeded = errors.New("Max seed length exceeded")
var ErrMaxSeedLengthExceeded = errors.New("max seed length exceeded")

// Create a program address.
// Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L204
Expand Down
28 changes: 28 additions & 0 deletions transaction.go
Expand Up @@ -699,6 +699,34 @@ func (tx *Transaction) VerifySignatures() error {
return nil
}

// GetProgramIDs returns the program IDs of the instructions in the transaction.
// NOTE: this does NOT include the program IDs of the internal instructions.
func (tx *Transaction) GetProgramIDs() PublicKeySlice {
programIDs := make(PublicKeySlice, 0)
for ixi, inst := range tx.Message.Instructions {
progKey, err := tx.ResolveProgramIDIndex(inst.ProgramIDIndex)
if err == nil {
programIDs = append(programIDs, progKey)
} else {
panic(fmt.Errorf("cannot resolve program ID for instruction %d: %w", ixi, err))
}
}
return programIDs
}

func (tx *Transaction) GetProgramIDsWithError() (PublicKeySlice, error) {
programIDs := make(PublicKeySlice, 0)
for ixi, inst := range tx.Message.Instructions {
progKey, err := tx.ResolveProgramIDIndex(inst.ProgramIDIndex)
if err == nil {
programIDs = append(programIDs, progKey)
} else {
return nil, fmt.Errorf("cannot resolve program ID for instruction %d: %w", ixi, err)
}
}
return programIDs, nil
}

func (tx *Transaction) NumWriteableAccounts() int {
return countWriteableAccounts(tx)
}
Expand Down

0 comments on commit 2d7c474

Please sign in to comment.