Skip to content

Commit

Permalink
cosmos-sdk-rs: make staking amount non-nullable
Browse files Browse the repository at this point in the history
As noted on #82, the `amount` field of `MsgDelegate` is annotated as
non-nullable in the upstream cosmos-sdk:

https://github.com/cosmos/cosmos-sdk/blob/9fd866e3820b3510010ae172b682d71594cd8c14/proto/cosmos/staking/v1beta1/tx.proto#L89

```proto
message MsgDelegate {
  option (gogoproto.equal)           = false;
  option (gogoproto.goproto_getters) = false;

  string                   delegator_address = 1 [(gogoproto.moretags) = "yaml:\"delegator_address\""];
  string                   validator_address = 2 [(gogoproto.moretags) = "yaml:\"validator_address\""];
  cosmos.base.v1beta1.Coin amount            = 3 [(gogoproto.nullable) = false];
}
```

This commit changes the `amount` field of the domain type from
`Option<Coin>` to `Coin` to reflect that.
  • Loading branch information
tony-iqlusion committed Aug 25, 2021
1 parent 0e72592 commit 2d06f6a
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions cosmos-sdk-rs/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{
proto,
tx::{Msg, MsgType},
AccountId, Coin, Result,
AccountId, Coin, Error, Result,
};
use std::convert::{TryFrom, TryInto};

Expand All @@ -19,7 +19,7 @@ pub struct MsgDelegate {
pub validator_address: AccountId,

/// Amount to send
pub amount: Option<Coin>,
pub amount: Coin,
}

impl MsgType for MsgDelegate {
Expand All @@ -44,18 +44,18 @@ impl TryFrom<&proto::cosmos::staking::v1beta1::MsgDelegate> for MsgDelegate {
type Error = eyre::Report;

fn try_from(proto: &proto::cosmos::staking::v1beta1::MsgDelegate) -> Result<MsgDelegate> {
let amount = if let Some(amount) = &proto.amount {
Some(Coin {
denom: amount.denom.parse()?,
amount: amount.amount.parse()?,
})
} else {
None
};
let amount = proto
.amount
.as_ref()
.ok_or(Error::MissingField { name: "amount" })?;

Ok(MsgDelegate {
delegator_address: proto.delegator_address.parse()?,
validator_address: proto.validator_address.parse()?,
amount,
amount: Coin {
denom: amount.denom.parse()?,
amount: amount.amount.parse()?,
},
})
}
}
Expand All @@ -68,17 +68,15 @@ impl From<MsgDelegate> for proto::cosmos::staking::v1beta1::MsgDelegate {

impl From<&MsgDelegate> for proto::cosmos::staking::v1beta1::MsgDelegate {
fn from(msg: &MsgDelegate) -> proto::cosmos::staking::v1beta1::MsgDelegate {
let proto_amount = msg
.amount
.as_ref()
.map(|amount| proto::cosmos::base::v1beta1::Coin {
denom: amount.denom.to_string(),
amount: amount.amount.to_string(),
});
let amount = proto::cosmos::base::v1beta1::Coin {
denom: msg.amount.denom.to_string(),
amount: msg.amount.amount.to_string(),
};

proto::cosmos::staking::v1beta1::MsgDelegate {
delegator_address: msg.delegator_address.to_string(),
validator_address: msg.validator_address.to_string(),
amount: proto_amount,
amount: Some(amount),
}
}
}
Expand Down

0 comments on commit 2d06f6a

Please sign in to comment.