-
Notifications
You must be signed in to change notification settings - Fork 170
/
stargate.go
47 lines (40 loc) · 1.54 KB
/
stargate.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
package client
import (
sdkerrors "cosmossdk.io/errors"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// ConvertProtoToJsonMarshal unmarshals the given bytes into a proto message and then marshals it to json.
// This is done so that clients calling stargate queries do not need to define their own proto unmarshalers,
// being able to use response directly by json `marshaling, which is supported in cosmwasm.
func ConvertProtoToJSONMarshal(protoResponseType codec.ProtoMarshaler, bz []byte, cdc codec.Codec) ([]byte, error) {
// unmarshal binary into stargate response data structure
err := cdc.Unmarshal(bz, protoResponseType)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to unmarshal")
}
bz, err = cdc.MarshalJSON(protoResponseType)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to marshal")
}
protoResponseType.Reset()
return bz, nil
}
// ConvertSdkCoinsToWasmCoins converts sdk type coins to wasm vm type coins
func ConvertSdkCoinsToWasmCoins(coins []sdk.Coin) wasmvmtypes.Coins {
var toSend wasmvmtypes.Coins
for _, coin := range coins {
c := ConvertSdkCoinToWasmCoin(coin)
toSend = append(toSend, c)
}
return toSend
}
// ConvertSdkCoinToWasmCoin converts a sdk type coin to a wasm vm type coin
func ConvertSdkCoinToWasmCoin(coin sdk.Coin) wasmvmtypes.Coin {
return wasmvmtypes.Coin{
Denom: coin.Denom,
// Note: gamm tokens have 18 decimal places, so 10^22 is common, no longer in u64 range
Amount: coin.Amount.String(),
}
}