forked from byeongsu-hong/terra.go
/
account.go
160 lines (139 loc) · 4.07 KB
/
account.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package terra
import (
"context"
"fmt"
"sync"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
cosmosauth "github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto"
terraassets "github.com/terra-project/core/types/assets"
terraauth "github.com/terra-project/core/x/auth"
)
var (
DefaultGasAdjustment = 1.2
DefaultGasPrice = cosmostypes.DecCoins{{
Denom: terraassets.MicroLunaDenom,
Amount: cosmostypes.NewDecWithPrec(150000, 6),
}}
)
type Account interface {
GetClient() Client
GetChainId() string
Update(ctx context.Context) error
CreateTx(ctx context.Context, opts CreateTxOptions) (terraauth.StdSignMsg, error)
CreateAndSignTx(ctx context.Context, opts CreateTxOptions) (terraauth.StdTx, terraauth.StdSignMsg, error)
cosmosauth.Account
}
type keyedAccount struct {
key Key
client Client
chainId string
mutex sync.Mutex
cosmosauth.Account
}
func NewAccount(ctx context.Context, client Client, key Key) (Account, error) {
nodeInfo, err := client.Tendermint().GetNodeInfo(ctx)
if err != nil {
return nil, errors.Wrap(err, "fetch node info")
}
acc := &keyedAccount{
key: key,
client: client,
chainId: nodeInfo.Network,
}
if err := acc.Update(ctx); err != nil {
return nil, errors.Wrap(err, "update account")
}
return acc, nil
}
func (a *keyedAccount) GetAddress() cosmostypes.AccAddress { return a.key.AccAddress() }
func (a *keyedAccount) GetPubKey() crypto.PubKey { return a.key.PubKey() }
func (a *keyedAccount) GetClient() Client { return a.client }
func (a *keyedAccount) GetChainId() string { return a.chainId }
func (a *keyedAccount) Update(ctx context.Context) error {
a.mutex.Lock()
defer a.mutex.Unlock()
accInfo, err := a.client.Auth().GetAccountInfo(ctx, a.key.AccAddress())
if err != nil {
return errors.Wrap(err, "fetch account info")
}
a.Account = accInfo
return nil
}
type CreateTxOptions struct {
Msgs []cosmostypes.Msg
Fee *terraauth.StdFee
GasAdjustment float64
GasPrices cosmostypes.DecCoins
Sequence *uint64
Memo string
}
func (a *keyedAccount) CreateTx(ctx context.Context, opts CreateTxOptions) (terraauth.StdSignMsg, error) {
if opts.GasAdjustment == 0 {
opts.GasAdjustment = DefaultGasAdjustment
}
if opts.GasPrices == nil || len(opts.GasPrices) == 0 {
opts.GasPrices = DefaultGasPrice
}
if err := a.Update(ctx); err != nil {
return terraauth.StdSignMsg{}, errors.Wrap(err, "update account")
}
resp, err := a.client.Bank().GetBalance(ctx, a.GetAddress())
if err != nil {
return terraauth.StdSignMsg{},
errors.Wrapf(err, "get balance of %s", a.GetAddress().String())
}
for index, coin := range resp.Balance {
resp.Balance[index] = cosmostypes.NewCoin(
coin.Denom,
cosmostypes.NewInt(1),
)
}
sequence := a.GetSequence()
if opts.Sequence != nil {
if sequence < *opts.Sequence {
sequence = *opts.Sequence
}
}
var fee terraauth.StdFee
if opts.Fee == nil {
fee, err = a.client.Transaction().EstimateFee(
ctx,
a.GetAddress().String(),
terraauth.StdSignMsg{
ChainID: a.chainId,
AccountNumber: a.GetAccountNumber(),
Sequence: sequence,
Msgs: opts.Msgs,
Memo: opts.Memo,
},
fmt.Sprintf("%f", opts.GasAdjustment),
opts.GasPrices,
)
if err != nil {
return terraauth.StdSignMsg{}, errors.Wrap(err, "estimate fee")
}
} else {
fee = *opts.Fee
}
return terraauth.StdSignMsg{
ChainID: a.chainId,
AccountNumber: a.GetAccountNumber(),
Sequence: sequence,
Fee: fee,
Msgs: opts.Msgs,
Memo: opts.Memo,
}, nil
}
func (a *keyedAccount) CreateAndSignTx(ctx context.Context, opts CreateTxOptions) (terraauth.StdTx, terraauth.StdSignMsg, error) {
signMsg, err := a.CreateTx(ctx, opts)
if err != nil {
return terraauth.StdTx{}, terraauth.StdSignMsg{}, errors.Wrap(err, "create tx")
}
signedTx, err := a.key.SignTx(signMsg)
if err != nil {
return terraauth.StdTx{}, terraauth.StdSignMsg{}, errors.Wrap(err, "sign tx")
}
return signedTx, signMsg, nil
}