Skip to content

Commit 64710d2

Browse files
committed
Add some tests for message go code generation
1 parent f2b28c3 commit 64710d2

File tree

6 files changed

+253
-0
lines changed

6 files changed

+253
-0
lines changed

packages/go-gen/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,16 @@ mod tests {
428428
compare_codes!(cosmwasm_std::WasmQuery);
429429
}
430430

431+
#[test]
432+
fn messages_work() {
433+
compare_codes!(cosmwasm_std::BankMsg);
434+
compare_codes!(cosmwasm_std::StakingMsg);
435+
compare_codes!(cosmwasm_std::DistributionMsg);
436+
compare_codes!(cosmwasm_std::IbcMsg);
437+
compare_codes!(cosmwasm_std::WasmMsg);
438+
// compare_codes!(cosmwasm_std::GovMsg); // TODO: currently fails because of VoteOption
439+
}
440+
431441
#[test]
432442
fn array_item_type_works() {
433443
#[cw_serde]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SendMsg contains instructions for a Cosmos-SDK/SendMsg
2+
// It has a fixed interface here and should be converted into the proper SDK format before dispatching
3+
type SendMsg struct {
4+
Amount []Coin `json:"amount"`
5+
ToAddress string `json:"to_address"`
6+
}
7+
8+
// BurnMsg will burn the given coins from the contract's account.
9+
// There is no Cosmos SDK message that performs this, but it can be done by calling the bank keeper.
10+
// Important if a contract controls significant token supply that must be retired.
11+
type BurnMsg struct {
12+
Amount []Coin `json:"amount"`
13+
}
14+
15+
type BankMsg struct {
16+
Send *SendMsg `json:"send,omitempty"`
17+
Burn *BurnMsg `json:"burn,omitempty"`
18+
}
19+
20+
// Coin is a string representation of the sdk.Coin type (more portable than sdk.Int)
21+
type Coin struct {
22+
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
23+
Denom string `json:"denom"` // type, eg. "ATOM"
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SetWithdrawAddressMsg is translated to a [MsgSetWithdrawAddress](https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/proto/cosmos/distribution/v1beta1/tx.proto#L29-L37).
2+
// `delegator_address` is automatically filled with the current contract's address.
3+
type SetWithdrawAddressMsg struct {
4+
// Address contains the `delegator_address` of a MsgSetWithdrawAddress
5+
Address string `json:"address"`
6+
}
7+
8+
// WithdrawDelegatorRewardMsg is translated to a [MsgWithdrawDelegatorReward](https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/proto/cosmos/distribution/v1beta1/tx.proto#L42-L50).
9+
// `delegator_address` is automatically filled with the current contract's address.
10+
type WithdrawDelegatorRewardMsg struct {
11+
// Validator contains `validator_address` of a MsgWithdrawDelegatorReward
12+
Validator string `json:"validator"`
13+
}
14+
15+
// FundCommunityPoolMsg is translated to a [MsgFundCommunityPool](https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/proto/cosmos/distribution/v1beta1/tx.proto#LL69C1-L76C2).
16+
// `depositor` is automatically filled with the current contract's address
17+
type FundCommunityPoolMsg struct {
18+
// Amount is the list of coins to be send to the community pool
19+
Amount []Coin `json:"amount"`
20+
}
21+
22+
type DistributionMsg struct {
23+
SetWithdrawAddress *SetWithdrawAddressMsg `json:"set_withdraw_address,omitempty"`
24+
WithdrawDelegatorReward *WithdrawDelegatorRewardMsg `json:"withdraw_delegator_reward,omitempty"`
25+
FundCommunityPool *FundCommunityPoolMsg `json:"fund_community_pool,omitempty"`
26+
}
27+
28+
// Coin is a string representation of the sdk.Coin type (more portable than sdk.Int)
29+
type Coin struct {
30+
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
31+
Denom string `json:"denom"` // type, eg. "ATOM"
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
type TransferMsg struct {
2+
Amount Coin `json:"amount"`
3+
ChannelID string `json:"channel_id"`
4+
Memo string `json:"memo,omitempty"` // this is not yet in wasmvm, but will be soon
5+
Timeout IBCTimeout `json:"timeout"`
6+
ToAddress string `json:"to_address"`
7+
}
8+
type SendPacketMsg struct {
9+
ChannelID string `json:"channel_id"`
10+
Data []byte `json:"data"`
11+
Timeout IBCTimeout `json:"timeout"`
12+
}
13+
type CloseChannelMsg struct {
14+
ChannelID string `json:"channel_id"`
15+
}
16+
17+
type IBCMsg struct {
18+
Transfer *TransferMsg `json:"transfer,omitempty"`
19+
SendPacket *SendPacketMsg `json:"send_packet,omitempty"`
20+
CloseChannel *CloseChannelMsg `json:"close_channel,omitempty"`
21+
}
22+
23+
// Coin is a string representation of the sdk.Coin type (more portable than sdk.Int)
24+
type Coin struct {
25+
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
26+
Denom string `json:"denom"` // type, eg. "ATOM"
27+
}
28+
29+
// IBCTimeout is the timeout for an IBC packet. At least one of block and timestamp is required.
30+
type IBCTimeout struct {
31+
Block *IBCTimeoutBlock `json:"block,omitempty"` // in wasmvm, this does not have "omitempty"
32+
// Nanoseconds since UNIX epoch
33+
Timestamp uint64 `json:"timestamp,omitempty"` // wasmvm has a "string" in here too
34+
}
35+
36+
// IBCTimeoutBlock Height is a monotonically increasing data type
37+
// that can be compared against another Height for the purposes of updating and
38+
// freezing clients.
39+
// Ordering is (revision_number, timeout_height)
40+
type IBCTimeoutBlock struct {
41+
// block height after which the packet times out.
42+
// the height within the given revision
43+
Height uint64 `json:"height"`
44+
// the version that the client is currently on
45+
// (eg. after reseting the chain this could increment 1 as height drops to 0)
46+
Revision uint64 `json:"revision"`
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type DelegateMsg struct {
2+
Amount Coin `json:"amount"`
3+
Validator string `json:"validator"`
4+
}
5+
type UndelegateMsg struct {
6+
Amount Coin `json:"amount"`
7+
Validator string `json:"validator"`
8+
}
9+
type RedelegateMsg struct {
10+
Amount Coin `json:"amount"`
11+
DstValidator string `json:"dst_validator"`
12+
SrcValidator string `json:"src_validator"`
13+
}
14+
15+
type StakingMsg struct {
16+
Delegate *DelegateMsg `json:"delegate,omitempty"`
17+
Undelegate *UndelegateMsg `json:"undelegate,omitempty"`
18+
Redelegate *RedelegateMsg `json:"redelegate,omitempty"`
19+
}
20+
21+
// Coin is a string representation of the sdk.Coin type (more portable than sdk.Int)
22+
type Coin struct {
23+
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
24+
Denom string `json:"denom"` // type, eg. "ATOM"
25+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// ExecuteMsg is used to call another defined contract on this chain.
2+
// The calling contract requires the callee to be defined beforehand,
3+
// and the address should have been defined in initialization.
4+
// And we assume the developer tested the ABIs and coded them together.
5+
//
6+
// Since a contract is immutable once it is deployed, we don't need to transform this.
7+
// If it was properly coded and worked once, it will continue to work throughout upgrades.
8+
type ExecuteMsg struct {
9+
// ContractAddr is the sdk.AccAddress of the contract, which uniquely defines
10+
// the contract ID and instance ID. The sdk module should maintain a reverse lookup table.
11+
ContractAddr string `json:"contract_addr"`
12+
// Send is an optional amount of coins this contract sends to the called contract
13+
Funds []Coin `json:"funds"`
14+
// Msg is assumed to be a json-encoded message, which will be passed directly
15+
// as `userMsg` when calling `Handle` on the above-defined contract
16+
Msg []byte `json:"msg"`
17+
}
18+
19+
// InstantiateMsg will create a new contract instance from a previously uploaded CodeID.
20+
// This allows one contract to spawn "sub-contracts".
21+
type InstantiateMsg struct {
22+
// Admin (optional) may be set here to allow future migrations from this address
23+
Admin string `json:"admin,omitempty"`
24+
// CodeID is the reference to the wasm byte code as used by the Cosmos-SDK
25+
CodeID uint64 `json:"code_id"`
26+
// Send is an optional amount of coins this contract sends to the called contract
27+
Funds []Coin `json:"funds"`
28+
// Label is optional metadata to be stored with a contract instance.
29+
Label string `json:"label"`
30+
// Msg is assumed to be a json-encoded message, which will be passed directly
31+
// as `userMsg` when calling `Instantiate` on a new contract with the above-defined CodeID
32+
Msg []byte `json:"msg"`
33+
}
34+
35+
// Instantiate2Msg will create a new contract instance from a previously uploaded CodeID
36+
// using the predictable address derivation.
37+
type Instantiate2Msg struct {
38+
// Admin (optional) may be set here to allow future migrations from this address
39+
Admin string `json:"admin,omitempty"`
40+
// CodeID is the reference to the wasm byte code as used by the Cosmos-SDK
41+
CodeID uint64 `json:"code_id"`
42+
// Send is an optional amount of coins this contract sends to the called contract
43+
Funds []Coin `json:"funds"`
44+
// Label is optional metadata to be stored with a contract instance.
45+
Label string `json:"label"`
46+
// Msg is assumed to be a json-encoded message, which will be passed directly
47+
// as `userMsg` when calling `Instantiate` on a new contract with the above-defined CodeID
48+
Msg []byte `json:"msg"`
49+
Salt []byte `json:"salt"`
50+
}
51+
52+
// MigrateMsg will migrate an existing contract from it's current wasm code (logic)
53+
// to another previously uploaded wasm code. It requires the calling contract to be
54+
// listed as "admin" of the contract to be migrated.
55+
type MigrateMsg struct {
56+
// ContractAddr is the sdk.AccAddress of the target contract, to migrate.
57+
ContractAddr string `json:"contract_addr"`
58+
// Msg is assumed to be a json-encoded message, which will be passed directly
59+
// as `userMsg` when calling `Migrate` on the above-defined contract
60+
Msg []byte `json:"msg"`
61+
// NewCodeID is the reference to the wasm byte code for the new logic to migrate to
62+
NewCodeID uint64 `json:"new_code_id"`
63+
}
64+
65+
// UpdateAdminMsg is the Go counterpart of WasmMsg::UpdateAdmin
66+
// (https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta5/packages/std/src/results/cosmos_msg.rs#L158-L160).
67+
type UpdateAdminMsg struct {
68+
// Admin is the sdk.AccAddress of the new admin.
69+
Admin string `json:"admin"`
70+
// ContractAddr is the sdk.AccAddress of the target contract.
71+
ContractAddr string `json:"contract_addr"`
72+
}
73+
74+
// ClearAdminMsg is the Go counterpart of WasmMsg::ClearAdmin
75+
// (https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta5/packages/std/src/results/cosmos_msg.rs#L158-L160).
76+
type ClearAdminMsg struct {
77+
// ContractAddr is the sdk.AccAddress of the target contract.
78+
ContractAddr string `json:"contract_addr"`
79+
}
80+
81+
// The message types of the wasm module.
82+
//
83+
// See https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto
84+
type WasmMsg struct {
85+
// Dispatches a call to another contract at a known address (with known ABI).
86+
//
87+
// This is translated to a [MsgExecuteContract](https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto#L68-L78). `sender` is automatically filled with the current contract's address.
88+
Execute *ExecuteMsg `json:"execute,omitempty"`
89+
// Instantiates a new contracts from previously uploaded Wasm code.
90+
//
91+
// The contract address is non-predictable. But it is guaranteed that when emitting the same Instantiate message multiple times, multiple instances on different addresses will be generated. See also Instantiate2.
92+
//
93+
// This is translated to a [MsgInstantiateContract](https://github.com/CosmWasm/wasmd/blob/v0.29.2/proto/cosmwasm/wasm/v1/tx.proto#L53-L71). `sender` is automatically filled with the current contract's address.
94+
Instantiate *InstantiateMsg `json:"instantiate,omitempty"`
95+
// Instantiates a new contracts from previously uploaded Wasm code using a predictable address derivation algorithm implemented in [`cosmwasm_std::instantiate2_address`].
96+
//
97+
// This is translated to a [MsgInstantiateContract2](https://github.com/CosmWasm/wasmd/blob/v0.29.2/proto/cosmwasm/wasm/v1/tx.proto#L73-L96). `sender` is automatically filled with the current contract's address. `fix_msg` is automatically set to false.
98+
Instantiate2 *Instantiate2Msg `json:"instantiate2,omitempty"`
99+
// Migrates a given contracts to use new wasm code. Passes a MigrateMsg to allow us to customize behavior.
100+
//
101+
// Only the contract admin (as defined in wasmd), if any, is able to make this call.
102+
//
103+
// This is translated to a [MsgMigrateContract](https://github.com/CosmWasm/wasmd/blob/v0.14.0/x/wasm/internal/types/tx.proto#L86-L96). `sender` is automatically filled with the current contract's address.
104+
Migrate *MigrateMsg `json:"migrate,omitempty"`
105+
// Sets a new admin (for migrate) on the given contract. Fails if this contract is not currently admin of the target contract.
106+
UpdateAdmin *UpdateAdminMsg `json:"update_admin,omitempty"`
107+
// Clears the admin on the given contract, so no more migration possible. Fails if this contract is not currently admin of the target contract.
108+
ClearAdmin *ClearAdminMsg `json:"clear_admin,omitempty"`
109+
}
110+
111+
// Coin is a string representation of the sdk.Coin type (more portable than sdk.Int)
112+
type Coin struct {
113+
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
114+
Denom string `json:"denom"` // type, eg. "ATOM"
115+
}

0 commit comments

Comments
 (0)