2021-01-21 01:33:02 -08:00
|
|
|
package rosetta
|
|
|
|
|
|
|
|
import (
|
2021-03-11 07:01:29 -08:00
|
|
|
"encoding/json"
|
|
|
|
"time"
|
2021-01-21 01:33:02 -08:00
|
|
|
|
2021-05-14 15:18:59 -07:00
|
|
|
crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"
|
2021-01-21 01:33:02 -08:00
|
|
|
)
|
|
|
|
|
2021-03-11 07:01:29 -08:00
|
|
|
// timeToMilliseconds converts time to milliseconds timestamp
|
|
|
|
func timeToMilliseconds(t time.Time) int64 {
|
|
|
|
return t.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
2021-01-21 01:33:02 -08:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:01:29 -08:00
|
|
|
// unmarshalMetadata unmarshals the given meta to the target
|
|
|
|
func unmarshalMetadata(meta map[string]interface{}, target interface{}) error {
|
|
|
|
b, err := json.Marshal(meta)
|
2021-01-21 01:33:02 -08:00
|
|
|
if err != nil {
|
2021-03-11 07:01:29 -08:00
|
|
|
return crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
|
2021-01-21 01:33:02 -08:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:01:29 -08:00
|
|
|
err = json.Unmarshal(b, target)
|
|
|
|
if err != nil {
|
|
|
|
return crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
|
2021-01-21 01:33:02 -08:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:01:29 -08:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-21 01:33:02 -08:00
|
|
|
|
2021-03-11 07:01:29 -08:00
|
|
|
// marshalMetadata marshals the given interface to map[string]interface{}
|
|
|
|
func marshalMetadata(o interface{}) (meta map[string]interface{}, err error) {
|
|
|
|
b, err := json.Marshal(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
|
2021-01-21 01:33:02 -08:00
|
|
|
}
|
2021-03-11 07:01:29 -08:00
|
|
|
meta = make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(b, &meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-21 01:33:02 -08:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:01:29 -08:00
|
|
|
return
|
2021-01-21 01:33:02 -08:00
|
|
|
}
|