github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stakecandidatebyaddress_test.go (about)

     1  package ethabi
     2  
     3  import (
     4  	"encoding/hex"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
     9  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    10  	"github.com/stretchr/testify/require"
    11  	"google.golang.org/protobuf/proto"
    12  )
    13  
    14  func TestBuildReadStateRequestCandidateByAddress(t *testing.T) {
    15  	r := require.New(t)
    16  
    17  	data, _ := hex.DecodeString("43f75ae40000000000000000000000000000000000000000000000000000000000000001")
    18  	req, err := BuildReadStateRequest(data)
    19  
    20  	r.Nil(err)
    21  	r.EqualValues("*ethabi.CandidateByAddressStateContext", reflect.TypeOf(req).String())
    22  
    23  	method := &iotexapi.ReadStakingDataMethod{
    24  		Method: iotexapi.ReadStakingDataMethod_CANDIDATE_BY_ADDRESS,
    25  	}
    26  	methodBytes, _ := proto.Marshal(method)
    27  	r.EqualValues(methodBytes, req.Parameters().MethodName)
    28  
    29  	arguments := &iotexapi.ReadStakingDataRequest{
    30  		Request: &iotexapi.ReadStakingDataRequest_CandidateByAddress_{
    31  			CandidateByAddress: &iotexapi.ReadStakingDataRequest_CandidateByAddress{
    32  				OwnerAddr: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv",
    33  			},
    34  		},
    35  	}
    36  	argumentsBytes, _ := proto.Marshal(arguments)
    37  	r.EqualValues([][]byte{argumentsBytes}, req.Parameters().Arguments)
    38  }
    39  
    40  func TestCandidateByAddressToEth(t *testing.T) {
    41  	r := require.New(t)
    42  
    43  	candidate := &iotextypes.CandidateV2{
    44  		OwnerAddress:       "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv",
    45  		OperatorAddress:    "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8gn",
    46  		RewardAddress:      "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzsj4p",
    47  		Name:               "hello",
    48  		TotalWeightedVotes: "10000000000000000000",
    49  		SelfStakeBucketIdx: 100,
    50  		SelfStakingTokens:  "5000000000000000000",
    51  	}
    52  
    53  	candidateBytes, _ := proto.Marshal(candidate)
    54  	resp := &iotexapi.ReadStateResponse{
    55  		Data: candidateBytes,
    56  	}
    57  
    58  	ctx := &CandidateByAddressStateContext{}
    59  	data, err := ctx.EncodeToEth(resp)
    60  	r.Nil(err)
    61  	r.EqualValues("000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000008ac7230489e8000000000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000004563918244f40000000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", data)
    62  }