github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stake_composite_buckets_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 "google.golang.org/protobuf/types/known/timestamppb" 13 ) 14 15 func TestBuildReadStateRequestCompositeBuckets(t *testing.T) { 16 r := require.New(t) 17 18 // data, err := _compositeBucketsMethod.Inputs.Pack(uint32(1), uint32(5)) 19 // r.NoError(err) 20 // data = append(_compositeBucketsMethod.ID, data...) 21 // t.Logf("data: %s", hex.EncodeToString(data)) 22 23 data, _ := hex.DecodeString("40f086d600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000005") 24 req, err := BuildReadStateRequest(data) 25 26 r.Nil(err) 27 r.EqualValues("*ethabi.CompositeBucketsStateContext", reflect.TypeOf(req).String()) 28 29 method := &iotexapi.ReadStakingDataMethod{ 30 Method: iotexapi.ReadStakingDataMethod_COMPOSITE_BUCKETS, 31 } 32 methodBytes, _ := proto.Marshal(method) 33 r.EqualValues(methodBytes, req.Parameters().MethodName) 34 35 arguments := &iotexapi.ReadStakingDataRequest{ 36 Request: &iotexapi.ReadStakingDataRequest_Buckets{ 37 Buckets: &iotexapi.ReadStakingDataRequest_VoteBuckets{ 38 Pagination: &iotexapi.PaginationParam{ 39 Offset: 1, 40 Limit: 5, 41 }, 42 }, 43 }, 44 } 45 argumentsBytes, _ := proto.Marshal(arguments) 46 r.EqualValues([][]byte{argumentsBytes}, req.Parameters().Arguments) 47 } 48 49 func TestEncodeCompositeVoteBucketListToEth(t *testing.T) { 50 r := require.New(t) 51 52 buckets := make([]*iotextypes.VoteBucket, 2) 53 54 buckets[0] = &iotextypes.VoteBucket{ 55 Index: 1, 56 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqryn4k9fw", 57 StakedAmount: "1000000000000000000", 58 StakedDuration: 1_000_000, 59 CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_000}, 60 StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_001}, 61 UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_002}, 62 AutoStake: true, 63 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgce2xkh", 64 ContractAddress: "", 65 } 66 buckets[1] = &iotextypes.VoteBucket{ 67 Index: 2, 68 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqr9wrzs5u", 69 StakedAmount: "2000000000000000000", 70 AutoStake: false, 71 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", 72 ContractAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", 73 StakedDurationBlockNumber: 1_000_000, 74 CreateBlockHeight: 1_000_000_000, 75 StakeStartBlockHeight: 1_000_000_001, 76 UnstakeStartBlockHeight: 1_000_000_002, 77 } 78 79 data, err := encodeVoteBucketListToEth(_compositeBucketsMethod.Outputs, &iotextypes.VoteBucketList{ 80 Buckets: buckets, 81 }) 82 t.Logf("data: %s\n", data) 83 r.Nil(err) 84 r.EqualValues("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003b9aca01000000000000000000000000000000000000000000000000000000003b9aca02000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000001bc16d674ec800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003b9aca01000000000000000000000000000000000000000000000000000000003b9aca02", data) 85 }