forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
32 lines (24 loc) · 1017 Bytes
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package solana
import (
"fmt"
)
// InstructionDecoder receives the AccountMeta FOR THAT INSTRUCTION,
// and not the accounts of the *Message object. Resolve with
// CompiledInstruction.ResolveInstructionAccounts(message) beforehand.
type InstructionDecoder func(instructionAccounts []*AccountMeta, data []byte) (interface{}, error)
var InstructionDecoderRegistry = map[string]InstructionDecoder{}
func RegisterInstructionDecoder(programID PublicKey, decoder InstructionDecoder) {
p := programID.String()
if _, found := InstructionDecoderRegistry[p]; found {
panic(fmt.Sprintf("unable to re-register instruction decoder for program %q", p))
}
InstructionDecoderRegistry[p] = decoder
}
func DecodeInstruction(programID PublicKey, accounts []*AccountMeta, data []byte) (interface{}, error) {
p := programID.String()
decoder, found := InstructionDecoderRegistry[p]
if !found {
return nil, fmt.Errorf("unknown programID, cannot find any instruction decoder %q", p)
}
return decoder(accounts, data)
}