@@ -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)
9091func (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)
102104func (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