|
| 1 | +// Copyright 2021 github.com/gagliardetto |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package associatedtokenaccount |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/hex" |
| 19 | + "testing" |
| 20 | + |
| 21 | + bin "github.com/gagliardetto/binary" |
| 22 | + solana "github.com/gagliardetto/solana-go" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestEncodingInstruction(t *testing.T) { |
| 28 | + t.Run("should encode", func(t *testing.T) { |
| 29 | + t.Run("Create", func(t *testing.T) { |
| 30 | + // Build an instruction and ensure current encoding matches implementation |
| 31 | + payer := solana.NewWallet().PublicKey() |
| 32 | + wallet := solana.NewWallet().PublicKey() |
| 33 | + mint := solana.NewWallet().PublicKey() |
| 34 | + ix := NewCreateInstructionBuilder(). |
| 35 | + SetPayer(payer). |
| 36 | + SetWallet(wallet). |
| 37 | + SetMint(mint). |
| 38 | + Build() |
| 39 | + data, err := ix.Data() |
| 40 | + require.NoError(t, err) |
| 41 | + encodedHex := hex.EncodeToString(data) |
| 42 | + // Current ATA Create encodes no payload bytes |
| 43 | + require.Equal(t, "", encodedHex) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + hexData string |
| 50 | + expectInstruction *Instruction |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "Create", |
| 54 | + hexData: "", |
| 55 | + expectInstruction: &Instruction{ |
| 56 | + BaseVariant: bin.BaseVariant{ |
| 57 | + TypeID: bin.TypeIDFromUint8(0), |
| 58 | + Impl: &Create{}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + t.Run("should encode", func(t *testing.T) { |
| 65 | + for _, test := range tests { |
| 66 | + t.Run(test.name, func(t *testing.T) { |
| 67 | + data, err := test.expectInstruction.Data() |
| 68 | + require.NoError(t, err) |
| 69 | + encodedHex := hex.EncodeToString(data) |
| 70 | + require.Equal(t, test.hexData, encodedHex) |
| 71 | + }) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("should decode", func(t *testing.T) { |
| 76 | + for _, test := range tests { |
| 77 | + t.Run(test.name, func(t *testing.T) { |
| 78 | + data, err := hex.DecodeString(test.hexData) |
| 79 | + require.NoError(t, err) |
| 80 | + var instruction *Instruction |
| 81 | + err = bin.NewBinDecoder(data).Decode(&instruction) |
| 82 | + require.NoError(t, err) |
| 83 | + assert.Equal(t, test.expectInstruction, instruction) |
| 84 | + }) |
| 85 | + } |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +func TestDecodeSetsAccountsAndGetters(t *testing.T) { |
| 90 | + payer := solana.NewWallet().PublicKey() |
| 91 | + wallet := solana.NewWallet().PublicKey() |
| 92 | + mint := solana.NewWallet().PublicKey() |
| 93 | + |
| 94 | + // Build an instruction to obtain correctly ordered accounts and data |
| 95 | + ix := NewCreateInstructionBuilder(). |
| 96 | + SetPayer(payer). |
| 97 | + SetWallet(wallet). |
| 98 | + SetMint(mint). |
| 99 | + Build() |
| 100 | + |
| 101 | + accounts := ix.Accounts() |
| 102 | + data, err := ix.Data() |
| 103 | + require.NoError(t, err) |
| 104 | + |
| 105 | + decoded, err := DecodeInstruction(accounts, data) |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + create, ok := decoded.Impl.(*Create) |
| 109 | + require.True(t, ok) |
| 110 | + |
| 111 | + // Check decoded fields populated via SetAccounts |
| 112 | + assert.Equal(t, payer, create.Payer) |
| 113 | + assert.Equal(t, wallet, create.Wallet) |
| 114 | + assert.Equal(t, mint, create.Mint) |
| 115 | + |
| 116 | + // Check getters return expected account metas |
| 117 | + require.NotNil(t, create.GetPayerAccount()) |
| 118 | + require.NotNil(t, create.GetAssociatedTokenAddressAccount()) |
| 119 | + require.NotNil(t, create.GetWalletAccount()) |
| 120 | + require.NotNil(t, create.GetMintAccount()) |
| 121 | + |
| 122 | + assert.True(t, create.GetPayerAccount().IsSigner) |
| 123 | + assert.True(t, create.GetPayerAccount().IsWritable) |
| 124 | + assert.Equal(t, payer, create.GetPayerAccount().PublicKey) |
| 125 | + assert.Equal(t, wallet, create.GetWalletAccount().PublicKey) |
| 126 | + assert.Equal(t, mint, create.GetMintAccount().PublicKey) |
| 127 | + |
| 128 | + // Verify associated token address is correctly derived and placed at index 1 |
| 129 | + ata, _, err := solana.FindAssociatedTokenAddress(wallet, mint) |
| 130 | + require.NoError(t, err) |
| 131 | + assert.Equal(t, ata, create.GetAssociatedTokenAddressAccount().PublicKey) |
| 132 | +} |
0 commit comments