diff --git a/go.mod b/go.mod index 85c6898ee..29debbdcd 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/smartcontractkit/chainlink-common v0.3.1-0.20241112140826-0e2daed34ef6 github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12 github.com/stretchr/testify v1.9.0 + github.com/test-go/testify v1.1.4 go.uber.org/zap v1.27.0 golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 golang.org/x/sync v0.8.0 diff --git a/pkg/solana/chainwriter/lookups_test.go b/pkg/solana/chainwriter/lookups_test.go new file mode 100644 index 000000000..b535c024a --- /dev/null +++ b/pkg/solana/chainwriter/lookups_test.go @@ -0,0 +1,137 @@ +package chainwriter_test + +import ( + "testing" + + "github.com/gagliardetto/solana-go" + "github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter" + "github.com/test-go/testify/require" +) + +type TestArgs struct { + Inner []InnerArgs +} + +type InnerArgs struct { + Address []byte +} + +func TestAccountContant(t *testing.T) { + + t.Run("AccountConstant resolves valid address", func(t *testing.T) { + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" + expectedMeta := []*solana.AccountMeta{ + { + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr), + IsSigner: true, + IsWritable: true, + }, + } + constantConfig := chainwriter.AccountConstant{ + Name: "TestAccount", + Address: expectedAddr, + IsSigner: true, + IsWritable: true, + } + result, err := constantConfig.Resolve(nil, nil, nil, "") + require.NoError(t, err) + require.Equal(t, expectedMeta, result) + }) +} +func TestAccountLookups(t *testing.T) { + t.Run("AccountLookup resolves valid address with just one address", func(t *testing.T) { + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" + testArgs := TestArgs{ + Inner: []InnerArgs{ + {Address: solana.MustPublicKeyFromBase58(expectedAddr).Bytes()}, + }, + } + expectedMeta := []*solana.AccountMeta{ + { + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr), + IsSigner: true, + IsWritable: true, + }, + } + + lookupConfig := chainwriter.AccountLookup{ + Name: "TestAccount", + Location: "Inner.Address", + IsSigner: true, + IsWritable: true, + } + result, err := lookupConfig.Resolve(nil, testArgs, nil, "") + require.NoError(t, err) + require.Equal(t, expectedMeta, result) + }) + + t.Run("AccountLookup resolves valid address with just multiple addresses", func(t *testing.T) { + expectedAddr1 := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" + expectedAddr2 := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6N" + testArgs := TestArgs{ + Inner: []InnerArgs{ + {Address: solana.MustPublicKeyFromBase58(expectedAddr1).Bytes()}, + {Address: solana.MustPublicKeyFromBase58(expectedAddr2).Bytes()}, + }, + } + expectedMeta := []*solana.AccountMeta{ + { + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr1), + IsSigner: true, + IsWritable: true, + }, + { + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr2), + IsSigner: true, + IsWritable: true, + }, + } + + lookupConfig := chainwriter.AccountLookup{ + Name: "TestAccount", + Location: "Inner.Address", + IsSigner: true, + IsWritable: true, + } + result, err := lookupConfig.Resolve(nil, testArgs, nil, "") + require.NoError(t, err) + for i, meta := range result { + require.Equal(t, expectedMeta[i], meta) + } + }) + + t.Run("AccountLookup fails when address isn't in args", func(t *testing.T) { + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" + testArgs := TestArgs{ + Inner: []InnerArgs{ + {Address: solana.MustPublicKeyFromBase58(expectedAddr).Bytes()}, + }, + } + lookupConfig := chainwriter.AccountLookup{ + Name: "InvalidAccount", + Location: "Invalid.Directory", + IsSigner: true, + IsWritable: true, + } + _, err := lookupConfig.Resolve(nil, testArgs, nil, "") + require.Error(t, err) + }) +} + +func TestPDALookups(t *testing.T) { + t.Run("PDALookup resolves valid address", func(t *testing.T) { + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" + expectedMeta := []*solana.AccountMeta{ + { + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr), + IsSigner: true, + IsWritable: true, + }, + } + lookupConfig := chainwriter.PDALookups{ + Name: "TestAccount", + PublicKey: + } + + }) +}