Skip to content

Commit

Permalink
Updated SubmitTransaction to apply input modifications to args before…
Browse files Browse the repository at this point in the history
… lookups
  • Loading branch information
silaslenihan committed Jan 24, 2025
1 parent 22e6a53 commit 7052c72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
28 changes: 20 additions & 8 deletions pkg/solana/chainwriter/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ type SolanaChainWriterService struct {
ge fees.Estimator
config ChainWriterConfig

parsed *codec.ParsedTypes
encoder types.Encoder
parsed *codec.ParsedTypes
encoder types.Encoder
modifiers map[string]commoncodec.Modifier

services.StateMachine
}
Expand Down Expand Up @@ -63,12 +64,13 @@ type MethodConfig struct {

func NewSolanaChainWriterService(logger logger.Logger, reader client.Reader, txm txm.TxManager, ge fees.Estimator, config ChainWriterConfig) (*SolanaChainWriterService, error) {
w := SolanaChainWriterService{
lggr: logger,
reader: reader,
txm: txm,
ge: ge,
config: config,
parsed: &codec.ParsedTypes{EncoderDefs: map[string]codec.Entry{}, DecoderDefs: map[string]codec.Entry{}},
lggr: logger,
reader: reader,
txm: txm,
ge: ge,
config: config,
parsed: &codec.ParsedTypes{EncoderDefs: map[string]codec.Entry{}, DecoderDefs: map[string]codec.Entry{}},
modifiers: make(map[string]commoncodec.Modifier),
}

if err := w.parsePrograms(config); err != nil {
Expand Down Expand Up @@ -99,6 +101,7 @@ func (s *SolanaChainWriterService) parsePrograms(config ChainWriterConfig) error
if err != nil {
return fmt.Errorf("failed to create input modifications for method %s.%s, error: %w", program, method, err)
}
s.modifiers[codec.WrapItemType(true, program, method, "")] = inputMod

input, err := codec.CreateCodecEntry(idlDef, methodConfig.ChainSpecificName, idl, inputMod)
if err != nil {
Expand Down Expand Up @@ -258,6 +261,15 @@ func (s *SolanaChainWriterService) SubmitTransaction(ctx context.Context, contra

encodedPayload, err := s.encoder.Encode(ctx, args, codec.WrapItemType(true, contractName, method, ""))

modifier, exists := s.modifiers[codec.WrapItemType(true, contractName, method, "")]
if exists {
// apply codec modifiers to args before doing address lookups
args, err = modifier.TransformToOnChain(args, "")
if err != nil {
return errorWithDebugID(fmt.Errorf("error transforming input args: %w", err), debugID)
}
}

if err != nil {
return errorWithDebugID(fmt.Errorf("error encoding transaction payload: %w", err), debugID)
}
Expand Down
20 changes: 15 additions & 5 deletions pkg/solana/chainwriter/chain_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

commoncodec "github.com/smartcontractkit/chainlink-common/pkg/codec"
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

Expand Down Expand Up @@ -429,7 +430,10 @@ func TestChainWriter_SubmitTransaction(t *testing.T) {
"contract_reader_interface": {
Methods: map[string]chainwriter.MethodConfig{
"initializeLookupTable": {
FromAddress: admin.String(),
FromAddress: admin.String(),
InputModifications: commoncodec.ModifiersConfig{
&commoncodec.RenameModifierConfig{Fields: map[string]string{"Lookup": "LookupTable"}},
},
ChainSpecificName: "initializeLookupTable",
LookupTables: chainwriter.LookupTables{
DerivedLookupTables: []chainwriter.DerivedLookupTable{
Expand Down Expand Up @@ -557,10 +561,16 @@ func TestChainWriter_SubmitTransaction(t *testing.T) {
return true
}), &txID, mock.Anything).Return(nil).Once()

args := Arguments{
LookupTable: account2,
Seed1: seed1,
Seed2: seed2,
type ArgumentsOffChain struct {
Lookup solana.PublicKey
Seed1 []byte
Seed2 []byte
}

args := ArgumentsOffChain{
Lookup: account2,
Seed1: seed1,
Seed2: seed2,
}

submitErr := cw.SubmitTransaction(ctx, "contract_reader_interface", "initializeLookupTable", args, txID, programID.String(), nil, nil)
Expand Down

0 comments on commit 7052c72

Please sign in to comment.