Skip to content

Commit bde524b

Browse files
committed
use decimals from Display denom
1 parent 6c1856c commit bde524b

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

precompiles/bank2/bank.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (p Precompile) Name(ctx sdk.Context, input []byte) ([]byte, error) {
8585
}
8686

8787
// Symbol
88+
//
8889
// input format: abi.encodePacked(string denom)
8990
// output format: abi.encodePacked(string)
9091
func (p Precompile) Symbol(ctx sdk.Context, input []byte) ([]byte, error) {
@@ -96,24 +97,41 @@ func (p Precompile) Symbol(ctx sdk.Context, input []byte) ([]byte, error) {
9697
return []byte(metadata.Symbol), nil
9798
}
9899

99-
// Decimals
100+
// Decimals returns the exponent of the display denom unit
101+
//
100102
// input format: abi.encodePacked(string denom)
101103
// output format: abi.encodePacked(uint8)
102104
func (p Precompile) Decimals(ctx sdk.Context, input []byte) ([]byte, error) {
103-
metadata, found := p.bankKeeper.GetDenomMetaData(ctx, string(input))
105+
m, found := p.bankKeeper.GetDenomMetaData(ctx, string(input))
104106
if !found {
105107
return nil, vm.ErrExecutionReverted
106108
}
107109

108-
if len(metadata.DenomUnits) == 0 {
110+
if len(m.DenomUnits) == 0 {
109111
return []byte{0}, nil
110112
}
111113

112-
if metadata.DenomUnits[0].Exponent > math.MaxUint8 {
114+
// look up Display denom unit
115+
index := -1
116+
for i, denomUnit := range m.DenomUnits {
117+
if denomUnit.Denom == m.Display {
118+
index = i
119+
break
120+
}
121+
}
122+
123+
var exponent uint32
124+
if index == -1 {
125+
exponent = 0
126+
} else {
127+
exponent = m.DenomUnits[index].Exponent
128+
}
129+
130+
if exponent > math.MaxUint8 {
113131
return nil, vm.ErrExecutionReverted
114132
}
115133

116-
return []byte{uint8(metadata.DenomUnits[0].Exponent)}, nil //nolint:gosec // G115: range is checked above
134+
return []byte{uint8(exponent)}, nil //nolint:gosec // G115: range is checked above
117135
}
118136

119137
// TotalSupply

0 commit comments

Comments
 (0)