@@ -30,17 +30,27 @@ const (
30
30
DefaultChainID = pack .String ("testnet" )
31
31
// DefaultSignMode used in signing the tx
32
32
DefaultSignMode = 1
33
+ // DefaultDecimalsDivisor is used when estimating gas prices for some Cosmos
34
+ // chains, so that the result is an integer.
35
+ // For example, the recommended Terra gas price is currently 0.01133 uluna.
36
+ // To ensure we're only dealing with integers, the value can be represented
37
+ // as 1133. When the transaction builder is calculating fees, it will divide
38
+ // the total by the divisor (in this case 1e5), to calculate the actual
39
+ // value.
40
+ DefaultDecimalsDivisor = 1
33
41
)
34
42
35
43
// TxBuilderOptions only contains necessary options to build tx from tx builder
36
44
type TxBuilderOptions struct {
37
- ChainID pack.String
45
+ ChainID pack.String
46
+ DecimalsDivisor pack.U256
38
47
}
39
48
40
49
// DefaultTxBuilderOptions returns TxBuilderOptions with the default settings.
41
50
func DefaultTxBuilderOptions () TxBuilderOptions {
42
51
return TxBuilderOptions {
43
- ChainID : DefaultChainID ,
52
+ ChainID : DefaultChainID ,
53
+ DecimalsDivisor : pack .NewU256FromU64 (DefaultDecimalsDivisor ),
44
54
}
45
55
}
46
56
@@ -50,20 +60,27 @@ func (opts TxBuilderOptions) WithChainID(chainID pack.String) TxBuilderOptions {
50
60
return opts
51
61
}
52
62
63
+ func (opts TxBuilderOptions ) WithDecimalsDivisor (decimalDivisor pack.U256 ) TxBuilderOptions {
64
+ opts .DecimalsDivisor = decimalDivisor
65
+ return opts
66
+ }
67
+
53
68
type txBuilder struct {
54
- client * Client
55
- chainID pack.String
56
- signMode int32
69
+ client * Client
70
+ chainID pack.String
71
+ signMode int32
72
+ decimalsDivisor pack.U256
57
73
}
58
74
59
75
// NewTxBuilder returns an implementation of the transaction builder interface
60
76
// from the Cosmos Compat API, and exposes the functionality to build simple
61
77
// Cosmos based transactions.
62
78
func NewTxBuilder (options TxBuilderOptions , client * Client ) account.TxBuilder {
63
79
return txBuilder {
64
- signMode : DefaultSignMode ,
65
- client : client ,
66
- chainID : options .ChainID ,
80
+ signMode : DefaultSignMode ,
81
+ client : client ,
82
+ chainID : options .ChainID ,
83
+ decimalsDivisor : options .DecimalsDivisor ,
67
84
}
68
85
}
69
86
@@ -107,7 +124,7 @@ func (builder txBuilder) BuildTx(ctx context.Context, fromPubKey *id.PubKey, to
107
124
108
125
fees := Coins {Coin {
109
126
Denom : builder .client .opts .CoinDenom ,
110
- Amount : pack .NewU64 (gasPrice .Mul (gasLimit ).Int ().Uint64 ()),
127
+ Amount : pack .NewU64 (gasPrice .Mul (gasLimit ).Div ( builder . decimalsDivisor ). Int ().Uint64 ()),
111
128
}}
112
129
113
130
accountNumber , err := builder .client .AccountNumber (ctx , from )
0 commit comments