Skip to content

Commit

Permalink
Update ibc types as requested by Alex
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jan 18, 2021
1 parent 9cbc21d commit 62b9fc7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
41 changes: 29 additions & 12 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,33 @@ pub enum IbcMsg {
/// and a matching module on the remote chain.
/// We cannot select the port_id, this is whatever the local chain has bound the ibctransfer
/// module to.
Ics20Transfer {
Transfer {
/// exisiting channel to send the tokens over
channel_id: String,
/// address on the remote chain to receive these tokens
to_address: HumanAddr,
/// packet data only supports one coin
/// https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/ibc/applications/transfer/v1/transfer.proto#L11-L20
amount: Coin,
/// block height after which the packet times out.
/// at least one of timeout_height, timeout_timestamp is required
timeout_height: Option<IbcTimeoutHeight>,
/// block timestamp (in nanoseconds) after which the packet times out.
/// at least one of timeout_height, timeout_timestamp is required
timeout_timestamp: Option<u64>,
},
/// Sends an IBC packet with given data over the existing channel.
/// Data should be encoded in a format defined by the channel version,
/// and the module on the other side should know how to parse this.
SendPacket {
channel_id: String,
data: Binary,
timeout_height: IbcTimeoutHeight,
timeout_timestamp: u64,
version: u64,
/// block height after which the packet times out.
/// at least one of timeout_height, timeout_timestamp is required
timeout_height: Option<IbcTimeoutHeight>,
/// block timestamp (in nanoseconds) after which the packet times out.
/// at least one of timeout_height, timeout_timestamp is required
timeout_timestamp: Option<u64>,
},
/// This will close an existing channel that is owned by this contract.
/// Port is auto-assigned to the contracts' ibc port
Expand Down Expand Up @@ -133,6 +142,12 @@ pub struct IbcTimeoutHeight {
pub timeout_height: u64,
}

impl IbcTimeoutHeight {
pub fn is_zero(&self) -> bool {
self.revision_number == 0 && self.timeout_height == 0
}
}

impl PartialOrd for IbcTimeoutHeight {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
Expand All @@ -158,12 +173,12 @@ pub struct IbcPacket {
pub dest: IbcEndpoint,
/// The sequence number of the packet on the given channel
pub sequence: u64,
/// block height after which the packet times out
pub timeout_height: IbcTimeoutHeight,
/// block timestamp (in nanoseconds) after which the packet times out
pub timeout_timestamp: u64,
// the version that the client is currently on
pub version: u64,
/// block height after which the packet times out.
/// at least one of timeout_height, timeout_timestamp is required
pub timeout_height: Option<IbcTimeoutHeight>,
/// block timestamp (in nanoseconds) after which the packet times out.
/// at least one of timeout_height, timeout_timestamp is required
pub timeout_timestamp: Option<u64>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -240,13 +255,15 @@ mod tests {
#[test]
// added this to check json format for go compat, as I was unsure how some messages are snake encoded
fn serialize_msg() {
let msg = IbcMsg::Ics20Transfer {
let msg = IbcMsg::Transfer {
channel_id: "channel-123".to_string(),
to_address: "my-special-addr".into(),
amount: Coin::new(12345678, "uatom"),
timeout_height: None,
timeout_timestamp: Some(1234567890),
};
let encoded = to_string(&msg).unwrap();
let expected = r#"{"ics20_transfer":{"channel_id":"channel-123","to_address":"my-special-addr","amount":{"denom":"uatom","amount":"12345678"}}}"#;
let expected = r#"{"transfer":{"channel_id":"channel-123","to_address":"my-special-addr","amount":{"denom":"uatom","amount":"12345678"},"timeout_height":null,"timeout_timestamp":1234567890}}"#;
assert_eq!(encoded.as_str(), expected);
}

Expand Down
14 changes: 6 additions & 8 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,11 @@ pub fn mock_ibc_packet_recv<T: Serialize>(my_channel_id: &str, data: &T) -> StdR
channel_id: my_channel_id.into(),
},
sequence: 27,
timeout_height: IbcTimeoutHeight {
timeout_height: Some(IbcTimeoutHeight {
revision_number: 1,
timeout_height: 12345678,
},
timeout_timestamp: 0,
version: 1,
}),
timeout_timestamp: None,
})
}

Expand All @@ -213,12 +212,11 @@ pub fn mock_ibc_packet_ack<T: Serialize>(my_channel_id: &str, data: &T) -> StdRe
channel_id: "channel-1234".to_string(),
},
sequence: 29,
timeout_height: IbcTimeoutHeight {
timeout_height: Some(IbcTimeoutHeight {
revision_number: 1,
timeout_height: 432332552,
},
timeout_timestamp: 0,
version: 1,
}),
timeout_timestamp: None,
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/results/cosmos_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
#[cfg(feature = "stargate")]
Stargate {
type_url: String,
data: Binary,
value: Binary,
},
#[cfg(feature = "stargate")]
Ibc(IbcMsg),
Expand Down

0 comments on commit 62b9fc7

Please sign in to comment.