Skip to content

Commit 099384a

Browse files
authored
Merge pull request #1572 from CosmWasm/fix-VoteWeighted
Fix VoteWeighted
2 parents 2eb9707 + 76365af commit 099384a

File tree

7 files changed

+154
-11
lines changed

7 files changed

+154
-11
lines changed

contracts/ibc-reflect-send/schema/ibc-reflect-send.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,12 @@
378378
"minimum": 0.0
379379
},
380380
"vote": {
381-
"$ref": "#/definitions/VoteOption"
381+
"description": "The vote option.\n\nThis should be called \"option\" for consistency with Cosmos SDK. Sorry for that. See <https://github.com/CosmWasm/cosmwasm/issues/1571>.",
382+
"allOf": [
383+
{
384+
"$ref": "#/definitions/VoteOption"
385+
}
386+
]
382387
}
383388
}
384389
}

contracts/ibc-reflect-send/schema/packet_msg.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,12 @@
316316
"minimum": 0.0
317317
},
318318
"vote": {
319-
"$ref": "#/definitions/VoteOption"
319+
"description": "The vote option.\n\nThis should be called \"option\" for consistency with Cosmos SDK. Sorry for that. See <https://github.com/CosmWasm/cosmwasm/issues/1571>.",
320+
"allOf": [
321+
{
322+
"$ref": "#/definitions/VoteOption"
323+
}
324+
]
320325
}
321326
}
322327
}

contracts/ibc-reflect-send/schema/raw/execute.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,12 @@
367367
"minimum": 0.0
368368
},
369369
"vote": {
370-
"$ref": "#/definitions/VoteOption"
370+
"description": "The vote option.\n\nThis should be called \"option\" for consistency with Cosmos SDK. Sorry for that. See <https://github.com/CosmWasm/cosmwasm/issues/1571>.",
371+
"allOf": [
372+
{
373+
"$ref": "#/definitions/VoteOption"
374+
}
375+
]
371376
}
372377
}
373378
}

contracts/ibc-reflect/schema/packet_msg.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@
245245
"minimum": 0.0
246246
},
247247
"vote": {
248-
"$ref": "#/definitions/VoteOption"
248+
"description": "The vote option.\n\nThis should be called \"option\" for consistency with Cosmos SDK. Sorry for that. See <https://github.com/CosmWasm/cosmwasm/issues/1571>.",
249+
"allOf": [
250+
{
251+
"$ref": "#/definitions/VoteOption"
252+
}
253+
]
249254
}
250255
}
251256
}

contracts/reflect/schema/raw/execute.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@
362362
"minimum": 0.0
363363
},
364364
"vote": {
365-
"$ref": "#/definitions/VoteOption"
365+
"description": "The vote option.\n\nThis should be called \"option\" for consistency with Cosmos SDK. Sorry for that. See <https://github.com/CosmWasm/cosmwasm/issues/1571>.",
366+
"allOf": [
367+
{
368+
"$ref": "#/definitions/VoteOption"
369+
}
370+
]
366371
}
367372
}
368373
}

contracts/reflect/schema/reflect.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,12 @@
372372
"minimum": 0.0
373373
},
374374
"vote": {
375-
"$ref": "#/definitions/VoteOption"
375+
"description": "The vote option.\n\nThis should be called \"option\" for consistency with Cosmos SDK. Sorry for that. See <https://github.com/CosmWasm/cosmwasm/issues/1571>.",
376+
"allOf": [
377+
{
378+
"$ref": "#/definitions/VoteOption"
379+
}
380+
]
376381
}
377382
}
378383
}

packages/std/src/results/cosmos_msg.rs

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,19 @@ pub enum WasmMsg {
208208
#[serde(rename_all = "snake_case")]
209209
pub enum GovMsg {
210210
/// This maps directly to [MsgVote](https://github.com/cosmos/cosmos-sdk/blob/v0.42.5/proto/cosmos/gov/v1beta1/tx.proto#L46-L56) in the Cosmos SDK with voter set to the contract address.
211-
Vote { proposal_id: u64, vote: VoteOption },
211+
Vote {
212+
proposal_id: u64,
213+
/// The vote option.
214+
///
215+
/// This should be called "option" for consistency with Cosmos SDK. Sorry for that.
216+
/// See <https://github.com/CosmWasm/cosmwasm/issues/1571>.
217+
vote: VoteOption,
218+
},
212219
/// This maps directly to [MsgVoteWeighted](https://github.com/cosmos/cosmos-sdk/blob/v0.45.8/proto/cosmos/gov/v1beta1/tx.proto#L66-L78) in the Cosmos SDK with voter set to the contract address.
213220
#[cfg(feature = "cosmwasm_1_2")]
214221
VoteWeighted {
215222
proposal_id: u64,
216-
vote: WeightedVoteOption,
223+
options: Vec<WeightedVoteOption>,
217224
},
218225
}
219226

@@ -324,13 +331,76 @@ mod tests {
324331
}
325332
}
326333

327-
#[cosmwasm_schema::cw_serde]
328-
enum ExecuteMsg {
329-
Mint { coin: Coin },
334+
#[test]
335+
fn wasm_msg_serializes_to_correct_json() {
336+
// Instantiate with admin
337+
let msg = WasmMsg::Instantiate {
338+
admin: Some("king".to_string()),
339+
code_id: 7897,
340+
msg: br#"{"claim":{}}"#.into(),
341+
funds: vec![],
342+
label: "my instance".to_string(),
343+
};
344+
let json = to_binary(&msg).unwrap();
345+
assert_eq!(
346+
String::from_utf8_lossy(&json),
347+
r#"{"instantiate":{"admin":"king","code_id":7897,"msg":"eyJjbGFpbSI6e319","funds":[],"label":"my instance"}}"#,
348+
);
349+
350+
// Instantiate without admin
351+
let msg = WasmMsg::Instantiate {
352+
admin: None,
353+
code_id: 7897,
354+
msg: br#"{"claim":{}}"#.into(),
355+
funds: vec![],
356+
label: "my instance".to_string(),
357+
};
358+
let json = to_binary(&msg).unwrap();
359+
assert_eq!(
360+
String::from_utf8_lossy(&json),
361+
r#"{"instantiate":{"admin":null,"code_id":7897,"msg":"eyJjbGFpbSI6e319","funds":[],"label":"my instance"}}"#,
362+
);
363+
364+
// Instantiate with funds
365+
let msg = WasmMsg::Instantiate {
366+
admin: None,
367+
code_id: 7897,
368+
msg: br#"{"claim":{}}"#.into(),
369+
funds: vec![coin(321, "stones")],
370+
label: "my instance".to_string(),
371+
};
372+
let json = to_binary(&msg).unwrap();
373+
assert_eq!(
374+
String::from_utf8_lossy(&json),
375+
r#"{"instantiate":{"admin":null,"code_id":7897,"msg":"eyJjbGFpbSI6e319","funds":[{"denom":"stones","amount":"321"}],"label":"my instance"}}"#,
376+
);
377+
378+
// Instantiate2
379+
#[cfg(feature = "cosmwasm_1_2")]
380+
{
381+
let msg = WasmMsg::Instantiate2 {
382+
admin: None,
383+
code_id: 7897,
384+
label: "my instance".to_string(),
385+
msg: br#"{"claim":{}}"#.into(),
386+
funds: vec![coin(321, "stones")],
387+
salt: Binary::from_base64("UkOVazhiwoo=").unwrap(),
388+
};
389+
let json = to_binary(&msg).unwrap();
390+
assert_eq!(
391+
String::from_utf8_lossy(&json),
392+
r#"{"instantiate2":{"admin":null,"code_id":7897,"label":"my instance","msg":"eyJjbGFpbSI6e319","funds":[{"denom":"stones","amount":"321"}],"salt":"UkOVazhiwoo="}}"#,
393+
);
394+
}
330395
}
331396

332397
#[test]
333398
fn wasm_msg_debug_decodes_binary_string_when_possible() {
399+
#[cosmwasm_schema::cw_serde]
400+
enum ExecuteMsg {
401+
Mint { coin: Coin },
402+
}
403+
334404
let msg = WasmMsg::Execute {
335405
contract_addr: "joe".to_string(),
336406
msg: to_binary(&ExecuteMsg::Mint {
@@ -359,4 +429,47 @@ mod tests {
359429
"Execute { contract_addr: \"joe\", msg: Binary(009f9296), funds: [] }"
360430
);
361431
}
432+
433+
#[test]
434+
#[cfg(feature = "stargate")]
435+
fn gov_msg_serializes_to_correct_json() {
436+
// Vote
437+
let msg = GovMsg::Vote {
438+
proposal_id: 4,
439+
vote: VoteOption::NoWithVeto,
440+
};
441+
let json = to_binary(&msg).unwrap();
442+
assert_eq!(
443+
String::from_utf8_lossy(&json),
444+
r#"{"vote":{"proposal_id":4,"vote":"no_with_veto"}}"#,
445+
);
446+
447+
// VoteWeighted
448+
#[cfg(feature = "cosmwasm_1_2")]
449+
{
450+
let msg = GovMsg::VoteWeighted {
451+
proposal_id: 25,
452+
options: vec![
453+
WeightedVoteOption {
454+
weight: Decimal::percent(25),
455+
option: VoteOption::Yes,
456+
},
457+
WeightedVoteOption {
458+
weight: Decimal::percent(25),
459+
option: VoteOption::No,
460+
},
461+
WeightedVoteOption {
462+
weight: Decimal::percent(50),
463+
option: VoteOption::Abstain,
464+
},
465+
],
466+
};
467+
468+
let json = to_binary(&msg).unwrap();
469+
assert_eq!(
470+
String::from_utf8_lossy(&json),
471+
r#"{"vote_weighted":{"proposal_id":25,"options":[{"option":"yes","weight":"0.25"},{"option":"no","weight":"0.25"},{"option":"abstain","weight":"0.5"}]}}"#,
472+
);
473+
}
474+
}
362475
}

0 commit comments

Comments
 (0)