-
Notifications
You must be signed in to change notification settings - Fork 18
/
api_distribution.go
103 lines (96 loc) · 3.72 KB
/
api_distribution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package client
import (
"context"
"cosmossdk.io/math"
gnfdsdktypes "github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
)
type IDistributionClient interface {
SetWithdrawAddress(ctx context.Context, withdrawAddr string, txOption gnfdsdktypes.TxOption) (string, error)
WithdrawValidatorCommission(ctx context.Context, txOption gnfdsdktypes.TxOption) (string, error)
WithdrawDelegatorReward(ctx context.Context, validatorAddr string, txOption gnfdsdktypes.TxOption) (string, error)
FundCommunityPool(ctx context.Context, amount math.Int, txOption gnfdsdktypes.TxOption) (string, error)
}
// SetWithdrawAddress - Set the withdrawal address for a delegator (or validator self-delegation).
//
// - ctx: Context variables for the current API call.
//
// - withdrawAddr: The withdrawal address to set.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Transaction hash of the transaction.
//
// - ret2: Return error if the transaction failed, otherwise return nil.
func (c *Client) SetWithdrawAddress(ctx context.Context, withdrawAddr string, txOption gnfdsdktypes.TxOption) (string, error) {
withdraw, err := sdk.AccAddressFromHexUnsafe(withdrawAddr)
if err != nil {
return "", err
}
msg := distrtypes.NewMsgSetWithdrawAddress(c.MustGetDefaultAccount().GetAddress(), withdraw)
resp, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}
// WithdrawValidatorCommission - Withdraw accumulated commission by validator.
//
// - ctx: Context variables for the current API call.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Transaction hash of the transaction.
//
// - ret2: Return error if the transaction failed, otherwise return nil.
func (c *Client) WithdrawValidatorCommission(ctx context.Context, txOption gnfdsdktypes.TxOption) (string, error) {
msg := distrtypes.NewMsgWithdrawValidatorCommission(c.MustGetDefaultAccount().GetAddress())
resp, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}
// WithdrawDelegatorReward - Withdraw rewards by a delegator.
//
// - ctx: Context variables for the current API call.
//
// - validatorAddr: The validator address to withdraw from.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Transaction hash of the transaction.
//
// - ret2: Return error if the transaction failed, otherwise return nil.
func (c *Client) WithdrawDelegatorReward(ctx context.Context, validatorAddr string, txOption gnfdsdktypes.TxOption) (string, error) {
validator, err := sdk.AccAddressFromHexUnsafe(validatorAddr)
if err != nil {
return "", err
}
msg := distrtypes.NewMsgWithdrawDelegatorReward(c.MustGetDefaultAccount().GetAddress(), validator)
resp, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}
// FundCommunityPool - Sends coins directly from the sender to the community pool.
//
// - ctx: Context variables for the current API call.
//
// - amount: The amount of BNB to send.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Transaction hash of the transaction.
//
// - ret2: Return error if the transaction failed, otherwise return nil.
func (c *Client) FundCommunityPool(ctx context.Context, amount math.Int, txOption gnfdsdktypes.TxOption) (string, error) {
msg := distrtypes.NewMsgFundCommunityPool(sdk.Coins{sdk.Coin{Denom: gnfdsdktypes.Denom, Amount: amount}}, c.MustGetDefaultAccount().GetAddress())
resp, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}