Skip to content

Commit fc04e76

Browse files
authored
fix: allow empty accounts in solana additional fields for transactions (#324)
This pull request includes changes to the Solana SDK to allow empty accounts in the additional fields for transactions. The changes allow solana transactions to include empty account slices or `nil` account slices. ### Validation updates: * [`sdk/solana/transaction.go`](diffhunk://#diff-818b97cb2182dd9070d9b993ec0a302ce1b35ca68828dd0013efb2523dd67567L20-R20): Modified the `AdditionalFields` struct to make the `Accounts` field optional by changing the validation tag from `required` to `omitempty`. Also updated the error message in `ValidateAdditionalFields` to correctly reference Solana instead of EVM. [[1]](diffhunk://#diff-818b97cb2182dd9070d9b993ec0a302ce1b35ca68828dd0013efb2523dd67567L20-R20) [[2]](diffhunk://#diff-818b97cb2182dd9070d9b993ec0a302ce1b35ca68828dd0013efb2523dd67567L32-R32) ### Test additions: * [`sdk/solana/transaction_test.go`](diffhunk://#diff-31bead2377fc131bd4de0aad5e09a1f88d4e5219ac05024bb3be8903af83e8e1R4-R5): Added import statements for `encoding/json` and `math/big`. * [`sdk/solana/transaction_test.go`](diffhunk://#diff-31bead2377fc131bd4de0aad5e09a1f88d4e5219ac05024bb3be8903af83e8e1R64-R172): Added `TestValidateAdditionalFields` to test various scenarios for the `ValidateAdditionalFields` function, including valid fields, missing accounts, empty accounts, malformed JSON, and empty input. * [`sdk/solana/transaction_test.go`](diffhunk://#diff-31bead2377fc131bd4de0aad5e09a1f88d4e5219ac05024bb3be8903af83e8e1R64-R172): Added `TestAdditionalFieldsValidate` to test the `Validate` method of the `AdditionalFields` struct with different account configurations. ### Documentation: * [`.changeset/silver-beans-design.md`](diffhunk://#diff-9774278eb988dd2dfd819b6b73aa54cc8c79ac5a8054349aaa9891f3bdf75b24R1-R5): Added a changeset to document the patch for allowing empty accounts in Solana additional fields for transactions.
1 parent f19d5dc commit fc04e76

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed

.changeset/silver-beans-design.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smartcontractkit/mcms": patch
3+
---
4+
5+
allow empty accounts in solana additional fields for transactions

sdk/solana/transaction.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func ValidateAdditionalFields(additionalFields json.RawMessage) error {
1717
}
1818
if len(additionalFields) != 0 {
1919
if err := json.Unmarshal(additionalFields, &fields); err != nil {
20-
return fmt.Errorf("failed to unmarshal EVM additional fields: %w", err)
20+
return fmt.Errorf("failed to unmarshal solana additional fields: %w", err)
2121
}
2222
}
2323

@@ -29,7 +29,7 @@ func ValidateAdditionalFields(additionalFields json.RawMessage) error {
2929
}
3030

3131
type AdditionalFields struct {
32-
Accounts []*solana.AccountMeta `json:"accounts" validate:"required"`
32+
Accounts []*solana.AccountMeta `json:"accounts" validate:"omitempty"`
3333
Value *big.Int `json:"value" validate:"omitempty"`
3434
}
3535

sdk/solana/transaction_test.go

+120
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package solana
22

33
import (
4+
"encoding/json"
5+
"math/big"
46
"testing"
57

68
"github.com/gagliardetto/solana-go"
@@ -59,3 +61,121 @@ func TestNewTransactionFromInstruction(t *testing.T) {
5961
})
6062
}
6163
}
64+
65+
func TestValidateAdditionalFields(t *testing.T) {
66+
t.Parallel()
67+
68+
validAccount := &solana.AccountMeta{
69+
PublicKey: solana.MustPublicKeyFromBase58("11111111111111111111111111111111"),
70+
IsSigner: true,
71+
}
72+
tests := []struct {
73+
name string
74+
input json.RawMessage
75+
wantErr bool
76+
errContains string
77+
}{
78+
{
79+
name: "valid fields",
80+
input: func() json.RawMessage {
81+
fields := AdditionalFields{
82+
Accounts: []*solana.AccountMeta{validAccount},
83+
Value: big.NewInt(100),
84+
}
85+
data, err := json.Marshal(fields)
86+
require.NoError(t, err)
87+
88+
return data
89+
}(),
90+
wantErr: false,
91+
},
92+
{
93+
name: "missing accounts field",
94+
input: []byte(`{"value": 100}`),
95+
wantErr: false,
96+
},
97+
{
98+
name: "empty accounts slice",
99+
input: []byte(`{"accounts": [], "value": 100}`),
100+
wantErr: false,
101+
},
102+
{
103+
name: "malformed json",
104+
input: []byte(`invalid json`),
105+
wantErr: true,
106+
errContains: "failed to unmarshal",
107+
},
108+
{
109+
name: "empty input",
110+
input: []byte(""),
111+
},
112+
}
113+
114+
for _, tc := range tests {
115+
t.Run(tc.name, func(t *testing.T) {
116+
t.Parallel()
117+
118+
err := ValidateAdditionalFields(tc.input)
119+
if tc.wantErr {
120+
require.Error(t, err)
121+
require.Contains(t, err.Error(), tc.errContains)
122+
} else {
123+
require.NoError(t, err)
124+
}
125+
})
126+
}
127+
}
128+
129+
func TestAdditionalFieldsValidate(t *testing.T) {
130+
t.Parallel()
131+
132+
validAccount := &solana.AccountMeta{
133+
PublicKey: solana.MustPublicKeyFromBase58("11111111111111111111111111111111"),
134+
IsSigner: true,
135+
}
136+
tests := []struct {
137+
name string
138+
fields AdditionalFields
139+
wantErr bool
140+
errContains string
141+
}{
142+
{
143+
name: "valid additional fields",
144+
fields: AdditionalFields{
145+
Accounts: []*solana.AccountMeta{validAccount},
146+
Value: big.NewInt(123),
147+
},
148+
wantErr: false,
149+
},
150+
{
151+
name: "nil accounts",
152+
fields: AdditionalFields{
153+
Accounts: nil,
154+
Value: big.NewInt(123),
155+
},
156+
wantErr: false,
157+
},
158+
{
159+
name: "empty accounts",
160+
fields: AdditionalFields{
161+
Accounts: []*solana.AccountMeta{},
162+
Value: big.NewInt(123),
163+
},
164+
wantErr: false,
165+
},
166+
}
167+
168+
for _, tc := range tests {
169+
t.Run(tc.name, func(t *testing.T) {
170+
t.Parallel()
171+
172+
err := tc.fields.Validate()
173+
if tc.wantErr {
174+
require.Error(t, err)
175+
require.Contains(t, err.Error(), tc.errContains)
176+
} else {
177+
require.NoError(t, err)
178+
}
179+
})
180+
}
181+
}

0 commit comments

Comments
 (0)