github.com/gagliardetto/solana-go@v1.11.0/programs/system/CreateAccountWithSeed_test.go (about)

     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 system
    16  
    17  import (
    18  	"bytes"
    19  	"strconv"
    20  	"testing"
    21  
    22  	bin "github.com/gagliardetto/binary"
    23  	ag_gofuzz "github.com/gagliardetto/gofuzz"
    24  	"github.com/gagliardetto/solana-go"
    25  	ag_require "github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestEncodeDecode_CreateAccountWithSeed(t *testing.T) {
    29  	fu := ag_gofuzz.New().NilChance(0)
    30  	for i := 0; i < 1; i++ {
    31  		t.Run("CreateAccountWithSeed"+strconv.Itoa(i), func(t *testing.T) {
    32  			{
    33  				params := new(CreateAccountWithSeed)
    34  				fu.Fuzz(params)
    35  				params.AccountMetaSlice = nil
    36  				buf := new(bytes.Buffer)
    37  				err := encodeT(*params, buf)
    38  				ag_require.NoError(t, err)
    39  				//
    40  				got := new(CreateAccountWithSeed)
    41  				err = decodeT(got, buf.Bytes())
    42  				got.AccountMetaSlice = nil
    43  				ag_require.NoError(t, err)
    44  				ag_require.Equal(t, params, got)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestEncDec(t *testing.T) {
    51  	instr := []byte{204, 95, 77, 127, 148, 25, 135, 127, 89, 146, 22, 90, 233, 80, 113, 3, 70, 176, 165, 222, 81, 200, 100, 223, 117, 165, 155, 44, 53, 225, 124, 20, 5, 0, 0, 0, 0, 0, 0, 0, 104, 101, 108, 108, 111, 192, 4, 14, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 57, 111, 59, 111, 183, 248, 249, 251, 128, 174, 206, 0, 81, 22, 3, 173, 244, 104, 15, 249, 239, 112, 33, 255, 66, 169, 29, 66, 7, 106, 231, 230}
    52  
    53  	{
    54  		payerPrivateKey := solana.MustPrivateKeyFromBase58("5LRLfrUP22VtiNaPGAEgHPucoJmG8ejmomMVmpn4fkXjexYsT7RQGfGuMePG5PKvecZxMGrqa6EP2RmYcm7TYQvX")
    55  		payerAccount, _ := solana.WalletFromPrivateKeyBase58(payerPrivateKey.String())
    56  		programID := "4sCcZNQR8vfWckyi5L9KdptdaiLxdiMjVgKQay7HxzmK"
    57  		programPubKey := solana.MustPublicKeyFromBase58(programID)
    58  
    59  		newSubAccount, err := solana.CreateWithSeed(
    60  			payerAccount.PublicKey(),
    61  			"hello",
    62  			programPubKey,
    63  		)
    64  		ag_require.NoError(t, err)
    65  
    66  		instruction := NewCreateAccountWithSeedInstruction(
    67  			payerAccount.PublicKey(),
    68  			"hello",
    69  			918720,
    70  			4,
    71  			programPubKey,
    72  			payerAccount.PublicKey(),
    73  			newSubAccount,
    74  			payerAccount.PublicKey(),
    75  		)
    76  
    77  		{
    78  			buffer := new(bytes.Buffer)
    79  			err = bin.NewBinEncoder(buffer).Encode(instruction)
    80  			ag_require.NoError(t, err)
    81  			ag_require.Equal(t, instr, buffer.Bytes())
    82  		}
    83  
    84  		{
    85  			got := new(CreateAccountWithSeed)
    86  			err = decodeT(got, instr)
    87  			got.AccountMetaSlice = solana.AccountMetaSlice{
    88  				solana.Meta(payerAccount.PublicKey()).WRITE().SIGNER(),
    89  				solana.Meta(newSubAccount).WRITE(),
    90  				solana.Meta(payerAccount.PublicKey()).SIGNER(),
    91  			}
    92  			ag_require.NoError(t, err)
    93  			ag_require.Equal(t, instruction, got)
    94  		}
    95  	}
    96  }