|
| 1 | +package chainwriter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/gagliardetto/solana-go" |
| 7 | + "github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter" |
| 8 | + "github.com/test-go/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +type TestArgs struct { |
| 12 | + Inner []InnerArgs |
| 13 | +} |
| 14 | + |
| 15 | +type InnerArgs struct { |
| 16 | + Address []byte |
| 17 | +} |
| 18 | + |
| 19 | +func TestAccountContant(t *testing.T) { |
| 20 | + |
| 21 | + t.Run("AccountConstant resolves valid address", func(t *testing.T) { |
| 22 | + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" |
| 23 | + expectedMeta := []*solana.AccountMeta{ |
| 24 | + { |
| 25 | + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr), |
| 26 | + IsSigner: true, |
| 27 | + IsWritable: true, |
| 28 | + }, |
| 29 | + } |
| 30 | + constantConfig := chainwriter.AccountConstant{ |
| 31 | + Name: "TestAccount", |
| 32 | + Address: expectedAddr, |
| 33 | + IsSigner: true, |
| 34 | + IsWritable: true, |
| 35 | + } |
| 36 | + result, err := constantConfig.Resolve(nil, nil, nil, "") |
| 37 | + require.NoError(t, err) |
| 38 | + require.Equal(t, expectedMeta, result) |
| 39 | + }) |
| 40 | +} |
| 41 | +func TestAccountLookups(t *testing.T) { |
| 42 | + t.Run("AccountLookup resolves valid address with just one address", func(t *testing.T) { |
| 43 | + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" |
| 44 | + testArgs := TestArgs{ |
| 45 | + Inner: []InnerArgs{ |
| 46 | + {Address: solana.MustPublicKeyFromBase58(expectedAddr).Bytes()}, |
| 47 | + }, |
| 48 | + } |
| 49 | + expectedMeta := []*solana.AccountMeta{ |
| 50 | + { |
| 51 | + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr), |
| 52 | + IsSigner: true, |
| 53 | + IsWritable: true, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + lookupConfig := chainwriter.AccountLookup{ |
| 58 | + Name: "TestAccount", |
| 59 | + Location: "Inner.Address", |
| 60 | + IsSigner: true, |
| 61 | + IsWritable: true, |
| 62 | + } |
| 63 | + result, err := lookupConfig.Resolve(nil, testArgs, nil, "") |
| 64 | + require.NoError(t, err) |
| 65 | + require.Equal(t, expectedMeta, result) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("AccountLookup resolves valid address with just multiple addresses", func(t *testing.T) { |
| 69 | + expectedAddr1 := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" |
| 70 | + expectedAddr2 := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6N" |
| 71 | + testArgs := TestArgs{ |
| 72 | + Inner: []InnerArgs{ |
| 73 | + {Address: solana.MustPublicKeyFromBase58(expectedAddr1).Bytes()}, |
| 74 | + {Address: solana.MustPublicKeyFromBase58(expectedAddr2).Bytes()}, |
| 75 | + }, |
| 76 | + } |
| 77 | + expectedMeta := []*solana.AccountMeta{ |
| 78 | + { |
| 79 | + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr1), |
| 80 | + IsSigner: true, |
| 81 | + IsWritable: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr2), |
| 85 | + IsSigner: true, |
| 86 | + IsWritable: true, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + lookupConfig := chainwriter.AccountLookup{ |
| 91 | + Name: "TestAccount", |
| 92 | + Location: "Inner.Address", |
| 93 | + IsSigner: true, |
| 94 | + IsWritable: true, |
| 95 | + } |
| 96 | + result, err := lookupConfig.Resolve(nil, testArgs, nil, "") |
| 97 | + require.NoError(t, err) |
| 98 | + for i, meta := range result { |
| 99 | + require.Equal(t, expectedMeta[i], meta) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("AccountLookup fails when address isn't in args", func(t *testing.T) { |
| 104 | + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" |
| 105 | + testArgs := TestArgs{ |
| 106 | + Inner: []InnerArgs{ |
| 107 | + {Address: solana.MustPublicKeyFromBase58(expectedAddr).Bytes()}, |
| 108 | + }, |
| 109 | + } |
| 110 | + lookupConfig := chainwriter.AccountLookup{ |
| 111 | + Name: "InvalidAccount", |
| 112 | + Location: "Invalid.Directory", |
| 113 | + IsSigner: true, |
| 114 | + IsWritable: true, |
| 115 | + } |
| 116 | + _, err := lookupConfig.Resolve(nil, testArgs, nil, "") |
| 117 | + require.Error(t, err) |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +func TestPDALookups(t *testing.T) { |
| 122 | + t.Run("PDALookup resolves valid address", func(t *testing.T) { |
| 123 | + expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M" |
| 124 | + expectedMeta := []*solana.AccountMeta{ |
| 125 | + { |
| 126 | + PublicKey: solana.MustPublicKeyFromBase58(expectedAddr), |
| 127 | + IsSigner: true, |
| 128 | + IsWritable: true, |
| 129 | + }, |
| 130 | + } |
| 131 | + lookupConfig := chainwriter.PDALookups{ |
| 132 | + Name: "TestAccount", |
| 133 | + PublicKey: |
| 134 | + } |
| 135 | + |
| 136 | + }) |
| 137 | +} |
0 commit comments