github.com/gagliardetto/solana-go@v1.11.0/programs/serum/instruction_test.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  // This file has been modified by github.com/gagliardetto
     3  //
     4  // Copyright 2020 dfuse Platform Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package serum
    19  
    20  import (
    21  	"encoding/binary"
    22  	"encoding/hex"
    23  	"testing"
    24  
    25  	bin "github.com/gagliardetto/binary"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestDecodeInstruction(t *testing.T) {
    31  	tests := []struct {
    32  		name              string
    33  		hexData           string
    34  		expectInstruction *Instruction
    35  	}{
    36  		{
    37  			name:    "New Order",
    38  			hexData: "000900000001000000b80600000000000010eb09000000000000000000168106e091da511601000000",
    39  			expectInstruction: &Instruction{
    40  				BaseVariant: bin.BaseVariant{
    41  					TypeID: bin.TypeIDFromUint32(9, binary.LittleEndian),
    42  					Impl: &InstructionNewOrderV2{
    43  						Side:              SideAsk,
    44  						LimitPrice:        1720,
    45  						MaxQuantity:       650000,
    46  						OrderType:         OrderTypeLimit,
    47  						ClientID:          1608306862011613462,
    48  						SelfTradeBehavior: SelfTradeBehaviorCancelProvide,
    49  					},
    50  				},
    51  				Version: 0,
    52  			},
    53  		},
    54  		{
    55  			name:    "Match Order",
    56  			hexData: "0002000000ffff",
    57  			expectInstruction: &Instruction{
    58  				BaseVariant: bin.BaseVariant{
    59  					TypeID: bin.TypeIDFromUint32(2, binary.LittleEndian),
    60  					Impl: &InstructionMatchOrder{
    61  						Limit: 65535,
    62  					},
    63  				},
    64  				Version: 0,
    65  			},
    66  		},
    67  		{
    68  			name:    "Settle Funds",
    69  			hexData: "0005000000",
    70  			expectInstruction: &Instruction{
    71  				BaseVariant: bin.BaseVariant{
    72  					TypeID: bin.TypeIDFromUint32(5, binary.LittleEndian),
    73  					Impl:   &InstructionSettleFunds{},
    74  				},
    75  				Version: 0,
    76  			},
    77  		},
    78  	}
    79  	for _, test := range tests {
    80  		t.Run(test.name, func(t *testing.T) {
    81  			data, err := hex.DecodeString(test.hexData)
    82  			require.NoError(t, err)
    83  			var instruction *Instruction
    84  			err = bin.NewBinDecoder(data).Decode(&instruction)
    85  			require.NoError(t, err)
    86  			assert.Equal(t, test.expectInstruction, instruction)
    87  		})
    88  	}
    89  }