-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add staking functions to cosmos-sdk #82
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
efb58fb
add staking to tx
kimonp ad6d485
fix path
kimonp 3fdc389
fix try_from
kimonp 002804e
add staking to lib
kimonp c82f0c3
fix use
kimonp 4808929
add back tryinto
kimonp 63739f3
fix staking reference
kimonp 9483824
remove vec from coin
kimonp fb0258b
try into
kimonp 3e15941
try none
kimonp 3875842
try none for both
kimonp 03f293c
convert amount by hand
kimonp 5edaa82
convert amount by hand
kimonp efafb22
add Undelegate
kimonp 29eb1da
add redelegate
kimonp c09866f
correct fields
kimonp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
|
||
pub mod bank; | ||
pub mod crypto; | ||
pub mod staking; | ||
pub mod tx; | ||
|
||
#[cfg(feature = "dev")] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
//! Staking module support | ||
//! | ||
//! <https://docs.cosmos.network/master/modules/staking/> | ||
|
||
use crate::{ | ||
proto, | ||
tx::{Msg, MsgType}, | ||
AccountId, Coin, Result, | ||
}; | ||
use std::convert::{TryFrom, TryInto}; | ||
|
||
/// MsgDelegate represents a message to delegate coins to a validator. | ||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct MsgDelegate { | ||
/// Delegator's address. | ||
pub delegator_address: AccountId, | ||
|
||
/// Validator's address. | ||
pub validator_address: AccountId, | ||
|
||
/// Amount to send | ||
pub amount: Option<Coin>, | ||
} | ||
|
||
impl MsgType for MsgDelegate { | ||
fn from_msg(msg: &Msg) -> Result<Self> { | ||
proto::cosmos::staking::v1beta1::MsgDelegate::from_msg(msg).and_then(TryInto::try_into) | ||
} | ||
|
||
fn to_msg(&self) -> Result<Msg> { | ||
proto::cosmos::staking::v1beta1::MsgDelegate::from(self).to_msg() | ||
} | ||
} | ||
|
||
impl TryFrom<proto::cosmos::staking::v1beta1::MsgDelegate> for MsgDelegate { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: proto::cosmos::staking::v1beta1::MsgDelegate) -> Result<MsgDelegate> { | ||
MsgDelegate::try_from(&proto) | ||
} | ||
} | ||
|
||
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 | ||
}; | ||
Ok(MsgDelegate { | ||
delegator_address: proto.delegator_address.parse()?, | ||
validator_address: proto.validator_address.parse()?, | ||
amount, | ||
}) | ||
} | ||
} | ||
|
||
impl From<MsgDelegate> for proto::cosmos::staking::v1beta1::MsgDelegate { | ||
fn from(coin: MsgDelegate) -> proto::cosmos::staking::v1beta1::MsgDelegate { | ||
proto::cosmos::staking::v1beta1::MsgDelegate::from(&coin) | ||
} | ||
} | ||
|
||
impl From<&MsgDelegate> for proto::cosmos::staking::v1beta1::MsgDelegate { | ||
fn from(msg: &MsgDelegate) -> proto::cosmos::staking::v1beta1::MsgDelegate { | ||
let proto_amount = if let Some(amount) = &msg.amount { | ||
Some(proto::cosmos::base::v1beta1::Coin { | ||
denom: amount.denom.to_string(), | ||
amount: amount.amount.to_string(), | ||
}) | ||
} else { | ||
None | ||
}; | ||
proto::cosmos::staking::v1beta1::MsgDelegate { | ||
delegator_address: msg.delegator_address.to_string(), | ||
validator_address: msg.validator_address.to_string(), | ||
amount: proto_amount, | ||
} | ||
} | ||
} | ||
|
||
/// MsgUndelegate represents a message to undelegate coins from a validator. | ||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct MsgUndelegate { | ||
/// Delegator's address. | ||
pub delegator_address: AccountId, | ||
|
||
/// Validator's address. | ||
pub validator_address: AccountId, | ||
|
||
/// Amount to UnDelegate | ||
pub amount: Option<Coin>, | ||
} | ||
|
||
impl MsgType for MsgUndelegate { | ||
fn from_msg(msg: &Msg) -> Result<Self> { | ||
proto::cosmos::staking::v1beta1::MsgUndelegate::from_msg(msg).and_then(TryInto::try_into) | ||
} | ||
|
||
fn to_msg(&self) -> Result<Msg> { | ||
proto::cosmos::staking::v1beta1::MsgUndelegate::from(self).to_msg() | ||
} | ||
} | ||
|
||
impl TryFrom<proto::cosmos::staking::v1beta1::MsgUndelegate> for MsgUndelegate { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: proto::cosmos::staking::v1beta1::MsgUndelegate) -> Result<MsgUndelegate> { | ||
MsgUndelegate::try_from(&proto) | ||
} | ||
} | ||
|
||
impl TryFrom<&proto::cosmos::staking::v1beta1::MsgUndelegate> for MsgUndelegate { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: &proto::cosmos::staking::v1beta1::MsgUndelegate) -> Result<MsgUndelegate> { | ||
let amount = if let Some(amount) = &proto.amount { | ||
Some(Coin { | ||
denom: amount.denom.parse()?, | ||
amount: amount.amount.parse()?, | ||
}) | ||
} else { | ||
None | ||
}; | ||
Ok(MsgUndelegate { | ||
delegator_address: proto.delegator_address.parse()?, | ||
validator_address: proto.validator_address.parse()?, | ||
amount, | ||
}) | ||
} | ||
} | ||
|
||
impl From<MsgUndelegate> for proto::cosmos::staking::v1beta1::MsgUndelegate { | ||
fn from(coin: MsgUndelegate) -> proto::cosmos::staking::v1beta1::MsgUndelegate { | ||
proto::cosmos::staking::v1beta1::MsgUndelegate::from(&coin) | ||
} | ||
} | ||
|
||
impl From<&MsgUndelegate> for proto::cosmos::staking::v1beta1::MsgUndelegate { | ||
fn from(msg: &MsgUndelegate) -> proto::cosmos::staking::v1beta1::MsgUndelegate { | ||
let proto_amount = if let Some(amount) = &msg.amount { | ||
Some(proto::cosmos::base::v1beta1::Coin { | ||
denom: amount.denom.to_string(), | ||
amount: amount.amount.to_string(), | ||
}) | ||
} else { | ||
None | ||
}; | ||
proto::cosmos::staking::v1beta1::MsgUndelegate { | ||
delegator_address: msg.delegator_address.to_string(), | ||
validator_address: msg.validator_address.to_string(), | ||
amount: proto_amount, | ||
} | ||
} | ||
} | ||
|
||
/// MsgBeginRedelegate represents a message to redelegate coins from one validator to another. | ||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct MsgBeginRedelegate { | ||
/// Delegator's address. | ||
pub delegator_address: AccountId, | ||
|
||
/// Source validator's address. | ||
pub validator_src_address: AccountId, | ||
|
||
/// Destination validator's address. | ||
pub validator_dst_address: AccountId, | ||
|
||
/// Amount to UnDelegate | ||
pub amount: Option<Coin>, | ||
} | ||
|
||
impl MsgType for MsgBeginRedelegate { | ||
fn from_msg(msg: &Msg) -> Result<Self> { | ||
proto::cosmos::staking::v1beta1::MsgBeginRedelegate::from_msg(msg).and_then(TryInto::try_into) | ||
} | ||
|
||
fn to_msg(&self) -> Result<Msg> { | ||
proto::cosmos::staking::v1beta1::MsgBeginRedelegate::from(self).to_msg() | ||
} | ||
} | ||
|
||
impl TryFrom<proto::cosmos::staking::v1beta1::MsgBeginRedelegate> for MsgBeginRedelegate { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: proto::cosmos::staking::v1beta1::MsgBeginRedelegate) -> Result<MsgBeginRedelegate> { | ||
MsgBeginRedelegate::try_from(&proto) | ||
} | ||
} | ||
|
||
impl TryFrom<&proto::cosmos::staking::v1beta1::MsgBeginRedelegate> for MsgBeginRedelegate { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: &proto::cosmos::staking::v1beta1::MsgBeginRedelegate) -> Result<MsgBeginRedelegate> { | ||
let amount = if let Some(amount) = &proto.amount { | ||
Some(Coin { | ||
denom: amount.denom.parse()?, | ||
amount: amount.amount.parse()?, | ||
}) | ||
} else { | ||
None | ||
}; | ||
Ok(MsgBeginRedelegate { | ||
delegator_address: proto.delegator_address.parse()?, | ||
validator_src_address: proto.validator_src_address.parse()?, | ||
validator_dst_address: proto.validator_dst_address.parse()?, | ||
amount, | ||
}) | ||
} | ||
} | ||
|
||
impl From<MsgBeginRedelegate> for proto::cosmos::staking::v1beta1::MsgBeginRedelegate { | ||
fn from(coin: MsgBeginRedelegate) -> proto::cosmos::staking::v1beta1::MsgBeginRedelegate { | ||
proto::cosmos::staking::v1beta1::MsgBeginRedelegate::from(&coin) | ||
} | ||
} | ||
|
||
impl From<&MsgBeginRedelegate> for proto::cosmos::staking::v1beta1::MsgBeginRedelegate { | ||
fn from(msg: &MsgBeginRedelegate) -> proto::cosmos::staking::v1beta1::MsgBeginRedelegate { | ||
let proto_amount = if let Some(amount) = &msg.amount { | ||
Some(proto::cosmos::base::v1beta1::Coin { | ||
denom: amount.denom.to_string(), | ||
amount: amount.amount.to_string(), | ||
}) | ||
} else { | ||
None | ||
}; | ||
proto::cosmos::staking::v1beta1::MsgBeginRedelegate { | ||
delegator_address: msg.delegator_address.to_string(), | ||
validator_src_address: msg.validator_src_address.to_string(), | ||
validator_dst_address: msg.validator_dst_address.to_string(), | ||
amount: proto_amount, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious... when would this ever be
None
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proto::cosmos::staking::v1beta1::MsgDelegate's amount can be None, so I'm mirroring that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Rust code generated from the protos has no way to express non-optional fields for messages, so that's not a great thing to go off of.
Looking at the raw Protobuf definition, it's:
https://github.com/cosmos/cosmos-sdk/blob/9fd866e3820b3510010ae172b682d71594cd8c14/proto/cosmos/staking/v1beta1/tx.proto#L89
It's annotated as non-nullable (using a custom gogoproto directive which the Rust proto parser doesn't understand), so this should be changed to
pub amount: Coin
and messages missing this field should be rejected.