-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Basic GetLatestValue for Solana
This commit fills in the functionality for `GetLatestValue` such that one or more accounts with an associated Anchor IDl can be read and mapped to native Go structs. Methods and prodecures can be defined in a config where an IDL and codec modifiers can be provided. Current limitations include completing enums and defining max length of strings.
- Loading branch information
1 parent
db0c09b
commit 6449ef4
Showing
12 changed files
with
1,586 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package solana | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gagliardetto/solana-go" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
// BinaryDataReader provides an interface for reading bytes from a source. This is likely a wrapper | ||
// for a solana client. | ||
type BinaryDataReader interface { | ||
ReadAll(context.Context, solana.PublicKey) ([]byte, error) | ||
} | ||
|
||
// accountReadBinding provides decoding and reading Solana Account data using a defined codec. The | ||
// `idlAccount` refers to the account name in the IDL for which the codec has a type mapping. | ||
type accountReadBinding struct { | ||
idlAccount string | ||
account solana.PublicKey | ||
codec commontypes.Codec | ||
reader BinaryDataReader | ||
} | ||
|
||
var _ readBinding = &accountReadBinding{} | ||
|
||
func (b *accountReadBinding) GetLatestValue(ctx context.Context, _ any, outVal any) error { | ||
bts, err := b.reader.ReadAll(ctx, b.account) | ||
if err != nil { | ||
return fmt.Errorf("%w: failed to get binary data", err) | ||
} | ||
|
||
return b.codec.Decode(ctx, bts, outVal, b.idlAccount) | ||
} | ||
|
||
func (b *accountReadBinding) Bind(contract commontypes.BoundContract) error { | ||
account, err := solana.PublicKeyFromBase58(contract.Address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b.account = account | ||
|
||
return nil | ||
} |
Oops, something went wrong.