Skip to content

Commit

Permalink
simplify caller interface (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Jul 1, 2022
1 parent c1da128 commit b5902f2
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 709 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ test: lint fmt
lint:
go list ./... | grep -v /vendor/ | xargs $(GOLINT)

.PHONY: mockgen
mockgen:
mockgen -destination=./iotex/interfaces_mock.go -source=./iotex/interfaces.go -package=iotex

.PHONY: examples
examples:
$(GOBUILD) -o ./examples/chaininfo/chaininfo ./examples/chaininfo
Expand Down
21 changes: 7 additions & 14 deletions iotex/caller_claimreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,49 @@ import (
"math/big"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/grpc"

"github.com/iotexproject/iotex-antenna-go/v2/errcodes"
)

type claimRewardCaller struct {
sendActionCaller
*sendActionCaller
amount *big.Int
}

func (c *claimRewardCaller) SetData(data []byte) ClaimRewardCaller {
if data == nil {
return c
}
c.payload = make([]byte, len(data))
copy(c.payload, data)
c.sendActionCaller.setPayload(data)
return c
}

func (c *claimRewardCaller) SetGasLimit(g uint64) ClaimRewardCaller {
c.gasLimit = g
c.sendActionCaller.setGasLimit(g)
return c
}

func (c *claimRewardCaller) SetGasPrice(g *big.Int) ClaimRewardCaller {
c.gasPrice = g
c.sendActionCaller.setGasPrice(g)
return c
}

func (c *claimRewardCaller) SetNonce(n uint64) ClaimRewardCaller {
c.nonce = n
c.sendActionCaller.setNonce(n)
return c
}

func (c *claimRewardCaller) API() iotexapi.APIServiceClient { return c.api }

func (c *claimRewardCaller) Call(ctx context.Context, opts ...grpc.CallOption) (hash.Hash256, error) {
if c.amount == nil {
return hash.ZeroHash256, errcodes.New("claim amount cannot be nil", errcodes.InvalidParam)
}

tx := &iotextypes.ClaimFromRewardingFund{
tx := iotextypes.ClaimFromRewardingFund{
Amount: c.amount.String(),
Data: c.payload,
}
c.core = &iotextypes.ActionCore{
Version: ProtocolVersion,
Action: &iotextypes.ActionCore_ClaimFromRewardingFund{ClaimFromRewardingFund: tx},
Action: &iotextypes.ActionCore_ClaimFromRewardingFund{ClaimFromRewardingFund: &tx},
}
return c.sendActionCaller.Call(ctx, opts...)
}
20 changes: 8 additions & 12 deletions iotex/caller_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

type deployContractCaller struct {
sendActionCaller
*sendActionCaller
abi *abi.ABI
args []interface{}
}
Expand All @@ -36,22 +36,20 @@ func (c *deployContractCaller) SetArgs(abi abi.ABI, args ...interface{}) DeployC
}

func (c *deployContractCaller) SetGasLimit(g uint64) DeployContractCaller {
c.gasLimit = g
c.sendActionCaller.setGasLimit(g)
return c
}

func (c *deployContractCaller) SetGasPrice(g *big.Int) DeployContractCaller {
c.gasPrice = g
c.sendActionCaller.setGasPrice(g)
return c
}

func (c *deployContractCaller) SetNonce(n uint64) DeployContractCaller {
c.nonce = n
c.sendActionCaller.setNonce(n)
return c
}

func (c *deployContractCaller) API() iotexapi.APIServiceClient { return c.api }

func (c *deployContractCaller) Call(ctx context.Context, opts ...grpc.CallOption) (hash.Hash256, error) {
if len(c.payload) == 0 {
return hash.ZeroHash256, errcodes.New("contract data can not empty", errcodes.InvalidParam)
Expand Down Expand Up @@ -88,7 +86,7 @@ type contractArgs struct {
}

type executeContractCaller struct {
sendActionCaller
*sendActionCaller
contractArgs
amount *big.Int
}
Expand All @@ -99,22 +97,20 @@ func (c *executeContractCaller) SetAmount(a *big.Int) ExecuteContractCaller {
}

func (c *executeContractCaller) SetGasLimit(g uint64) ExecuteContractCaller {
c.gasLimit = g
c.sendActionCaller.setGasLimit(g)
return c
}

func (c *executeContractCaller) SetGasPrice(g *big.Int) ExecuteContractCaller {
c.gasPrice = g
c.sendActionCaller.setGasPrice(g)
return c
}

func (c *executeContractCaller) SetNonce(n uint64) ExecuteContractCaller {
c.nonce = n
c.sendActionCaller.setNonce(n)
return c
}

func (c *executeContractCaller) API() iotexapi.APIServiceClient { return c.api }

func (c *executeContractCaller) Call(ctx context.Context, opts ...grpc.CallOption) (hash.Hash256, error) {
if c.method == "" {
return hash.ZeroHash256, errcodes.New("contract address and method can not empty", errcodes.InvalidParam)
Expand Down
84 changes: 38 additions & 46 deletions iotex/caller_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

type (
stakingCaller struct {
sendActionCaller
*sendActionCaller
action interface{}
}

Expand All @@ -33,131 +33,123 @@ type (
)

//Create Staking
func (c *stakingCaller) Create(candidateName string, amount *big.Int, duration uint32, autoStake bool) StakingAPICaller {
tx := &iotextypes.StakeCreate{
func (c *stakingCaller) Create(candidateName string, amount *big.Int, duration uint32, autoStake bool) SendActionCaller {
tx := iotextypes.StakeCreate{
CandidateName: candidateName,
StakedDuration: duration,
AutoStake: autoStake,
StakedAmount: amount.String(),
}
c.action = tx
c.action = &tx
return c
}

//Unstake Staking
func (c *stakingCaller) Unstake(bucketIndex uint64) StakingAPICaller {
tx := &iotextypes.StakeReclaim{
func (c *stakingCaller) Unstake(bucketIndex uint64) SendActionCaller {
tx := iotextypes.StakeReclaim{
BucketIndex: bucketIndex,
}
c.action = &reclaim{tx, false}
c.action = &reclaim{&tx, false}
return c
}

//Withdraw Staking
func (c *stakingCaller) Withdraw(bucketIndex uint64) StakingAPICaller {
tx := &iotextypes.StakeReclaim{
func (c *stakingCaller) Withdraw(bucketIndex uint64) SendActionCaller {
tx := iotextypes.StakeReclaim{
BucketIndex: bucketIndex,
}
c.action = &reclaim{tx, true}
c.action = &reclaim{&tx, true}
return c
}

//AddDeposit Staking
func (c *stakingCaller) AddDeposit(index uint64, amount *big.Int) StakingAPICaller {
tx := &iotextypes.StakeAddDeposit{
func (c *stakingCaller) AddDeposit(index uint64, amount *big.Int) SendActionCaller {
tx := iotextypes.StakeAddDeposit{
BucketIndex: index,
Amount: amount.String(),
}
c.action = tx
c.action = &tx
return c
}

//ChangeCandidate Staking
func (c *stakingCaller) ChangeCandidate(candName string, bucketIndex uint64) StakingAPICaller {
tx := &iotextypes.StakeChangeCandidate{
func (c *stakingCaller) ChangeCandidate(candName string, bucketIndex uint64) SendActionCaller {
tx := iotextypes.StakeChangeCandidate{
CandidateName: candName,
BucketIndex: bucketIndex,
}
c.action = tx
c.action = &tx
return c
}

//StakingTransfer Staking
func (c *stakingCaller) StakingTransfer(voterAddress address.Address, bucketIndex uint64) StakingAPICaller {
tx := &iotextypes.StakeTransferOwnership{
func (c *stakingCaller) StakingTransfer(voterAddress address.Address, bucketIndex uint64) SendActionCaller {
tx := iotextypes.StakeTransferOwnership{
VoterAddress: voterAddress.String(),
BucketIndex: bucketIndex,
}
c.action = tx
c.action = &tx
return c
}

//Restake Staking
func (c *stakingCaller) Restake(index uint64, duration uint32, autoStake bool) StakingAPICaller {
tx := &iotextypes.StakeRestake{
func (c *stakingCaller) Restake(index uint64, duration uint32, autoStake bool) SendActionCaller {
tx := iotextypes.StakeRestake{
BucketIndex: index,
StakedDuration: duration,
AutoStake: autoStake,
}
c.action = tx
c.action = &tx
return c
}

//Register Staking
func (c *stakingCaller) Register(name string, ownerAddr, operatorAddr, rewardAddr address.Address, amount *big.Int, duration uint32, autoStake bool, payload []byte) StakingAPICaller {
basic := &iotextypes.CandidateBasicInfo{
func (c *stakingCaller) Register(name string, ownerAddr, operatorAddr, rewardAddr address.Address, amount *big.Int, duration uint32, autoStake bool, payload []byte) SendActionCaller {
basic := iotextypes.CandidateBasicInfo{
Name: name,
OperatorAddress: operatorAddr.String(),
RewardAddress: rewardAddr.String(),
}
tx := &iotextypes.CandidateRegister{
Candidate: basic,
tx := iotextypes.CandidateRegister{
Candidate: &basic,
StakedAmount: amount.String(),
StakedDuration: duration,
AutoStake: autoStake,
OwnerAddress: ownerAddr.String(),
Payload: payload,
}
c.action = tx
c.action = &tx
return c
}

//Update Staking
func (c *stakingCaller) Update(name string, operatorAddr, rewardAddr address.Address) StakingAPICaller {
tx := &iotextypes.CandidateBasicInfo{
func (c *stakingCaller) Update(name string, operatorAddr, rewardAddr address.Address) SendActionCaller {
tx := iotextypes.CandidateBasicInfo{
Name: name,
OperatorAddress: operatorAddr.String(),
RewardAddress: rewardAddr.String(),
}
c.action = tx
c.action = &tx
return c
}

//SetGasLimit set basic data
func (c *stakingCaller) SetGasLimit(g uint64) StakingAPICaller {
c.gasLimit = g
func (c *stakingCaller) SetGasLimit(g uint64) SendActionCaller {
c.sendActionCaller.setGasLimit(g)
return c
}

//SetGasPrice set basic data
func (c *stakingCaller) SetGasPrice(g *big.Int) StakingAPICaller {
c.gasPrice = g
func (c *stakingCaller) SetGasPrice(g *big.Int) SendActionCaller {
c.sendActionCaller.setGasPrice(g)
return c
}

//SetNonce set basic data
func (c *stakingCaller) SetNonce(n uint64) StakingAPICaller {
c.nonce = n
func (c *stakingCaller) SetNonce(n uint64) SendActionCaller {
c.sendActionCaller.setNonce(n)
return c
}

//SetPayload set basic data
func (c *stakingCaller) SetPayload(pl []byte) StakingAPICaller {
if pl == nil {
return c
}
c.payload = make([]byte, len(pl))
copy(c.payload, pl)
func (c *stakingCaller) SetPayload(pl []byte) SendActionCaller {
c.sendActionCaller.setPayload(pl)
return c
}

Expand Down
6 changes: 3 additions & 3 deletions iotex/caller_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestCandidateCaller_Register(t *testing.T) {

testTimes := len(candidateRegisterTests)

stakingAPICaller := NewMockStakingAPICaller(ctrl)
stakingAPICaller := NewMockSendActionCaller(ctrl)
stakingAPICaller.EXPECT().SetGasPrice(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
stakingAPICaller.EXPECT().SetGasLimit(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
stakingAPICaller.EXPECT().SetPayload(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestStakingCaller_Create(t *testing.T) {

testTimes := len(stakeTests)

stakingAPICaller := NewMockStakingAPICaller(ctrl)
stakingAPICaller := NewMockSendActionCaller(ctrl)
stakingAPICaller.EXPECT().SetGasPrice(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
stakingAPICaller.EXPECT().SetGasLimit(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
stakingAPICaller.EXPECT().SetPayload(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestStakingCaller_Unstake(t *testing.T) {

testTimes := len(unstakeTests)

stakingAPICaller := NewMockStakingAPICaller(ctrl)
stakingAPICaller := NewMockSendActionCaller(ctrl)
stakingAPICaller.EXPECT().SetGasPrice(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
stakingAPICaller.EXPECT().SetGasLimit(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
stakingAPICaller.EXPECT().SetPayload(gomock.Any()).Return(stakingAPICaller).Times(testTimes)
Expand Down
Loading

0 comments on commit b5902f2

Please sign in to comment.