Skip to content

Commit

Permalink
feat(evm): generate erc20 rust bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
aeryz committed Oct 31, 2023
1 parent 3069bdb commit fe58746
Show file tree
Hide file tree
Showing 11 changed files with 49,035 additions and 878 deletions.
35 changes: 20 additions & 15 deletions cosmwasm/ucs01-relay-api/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::Debug;

use cosmwasm_std::{
Addr, Binary, CosmosMsg, Event, IbcBasicResponse, IbcEndpoint, IbcMsg, IbcOrder,
IbcReceiveResponse, Response, SubMsg, Timestamp,
attr, Addr, Binary, CanonicalAddr, CosmosMsg, Event, IbcBasicResponse, IbcEndpoint, IbcMsg,
IbcOrder, IbcReceiveResponse, Response, SubMsg, Timestamp,
};
use thiserror::Error;

Expand Down Expand Up @@ -69,6 +69,11 @@ pub trait TransferProtocol {

fn self_addr(&self) -> &Addr;

fn common_to_protocol_packet(
&self,
packet: TransferPacketCommon<PacketExtensionOf<Self>>,
) -> Result<Self::Packet, EncodingError>;

fn ack_success() -> Self::Ack;

fn ack_failure(error: String) -> Self::Ack;
Expand All @@ -80,22 +85,22 @@ pub trait TransferProtocol {

fn send_tokens(
&mut self,
sender: &str,
receiver: &<Self::Packet as TransferPacket>::Receiver,
sender: &<Self::Packet as TransferPacket>::Addr,
receiver: &<Self::Packet as TransferPacket>::Addr,
tokens: Vec<TransferToken>,
) -> Result<Vec<CosmosMsg<Self::CustomMsg>>, Self::Error>;

fn send_tokens_success(
&mut self,
sender: &str,
receiver: &<Self::Packet as TransferPacket>::Receiver,
sender: &<Self::Packet as TransferPacket>::Addr,
receiver: &<Self::Packet as TransferPacket>::Addr,
tokens: Vec<TransferToken>,
) -> Result<Vec<CosmosMsg<Self::CustomMsg>>, Self::Error>;

fn send_tokens_failure(
&mut self,
sender: &str,
receiver: &<Self::Packet as TransferPacket>::Receiver,
sender: &<Self::Packet as TransferPacket>::Addr,
receiver: &<Self::Packet as TransferPacket>::Addr,
tokens: Vec<TransferToken>,
) -> Result<Vec<CosmosMsg<Self::CustomMsg>>, Self::Error>;

Expand All @@ -110,8 +115,8 @@ pub trait TransferProtocol {
.map(|token| self.normalize_for_ibc_transfer(token))
.collect::<Result<Vec<_>, _>>()?;

let packet = Self::Packet::try_from(TransferPacketCommon {
sender: input.sender.to_string(),
let packet = self.common_to_protocol_packet(TransferPacketCommon {
sender: input.sender.clone().to_string(),
receiver: input.receiver.clone(),
tokens: input.tokens.clone(),
extension: extension.clone(),
Expand Down Expand Up @@ -181,7 +186,7 @@ pub trait TransferProtocol {
packet_event
.add_attributes([
("module", MODULE_NAME),
("sender", packet.sender()),
("sender", packet.sender().to_string().as_str()),
("receiver", packet.receiver().to_string().as_str()),
("acknowledgement", &raw_ack.into().to_string()),
])
Expand Down Expand Up @@ -214,7 +219,7 @@ pub trait TransferProtocol {
timeout_event
.add_attributes([
("module", MODULE_NAME),
("refund_receiver", packet.sender()),
("refund_receiver", packet.sender().to_string().as_str()),
])
.add_attributes(packet.tokens().into_iter().map(
|TransferToken { denom, amount }| (format!("denom:{}", denom), amount),
Expand All @@ -225,7 +230,7 @@ pub trait TransferProtocol {

fn receive_transfer(
&mut self,
receiver: &str,
receiver: &<Self::Packet as TransferPacket>::Addr,
tokens: Vec<TransferToken>,
) -> Result<Vec<CosmosMsg<Self::CustomMsg>>, Self::Error>;

Expand All @@ -242,7 +247,7 @@ pub trait TransferProtocol {
// the reply handler via the `receive_error` for the acknowledgement
// to be overwritten.
let transfer_msgs = self
.receive_transfer(packet.receiver().to_string().as_str(), packet.tokens())?
.receive_transfer(packet.receiver(), packet.tokens())?
.into_iter()
.map(|msg| SubMsg::reply_on_error(msg, Self::RECEIVE_REPLY_ID));

Expand All @@ -259,7 +264,7 @@ pub trait TransferProtocol {
packet_event
.add_attributes([
("module", MODULE_NAME),
("sender", packet.sender()),
("sender", packet.sender().to_string().as_str()),
("receiver", packet.receiver().to_string().as_str()),
("success", "true"),
])
Expand Down
68 changes: 35 additions & 33 deletions cosmwasm/ucs01-relay-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ impl From<Coin> for TransferToken {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Ucs01TransferPacket {
/// the sender address
sender: String,
sender: HexBinary,
/// the recipient address on the destination chain
receiver: HexBinary,
/// the transferred tokens
tokens: Vec<TransferToken>,
}

impl Ucs01TransferPacket {
pub fn sender(&self) -> &String {
pub fn sender(&self) -> &HexBinary {
&self.sender
}

Expand All @@ -58,7 +58,7 @@ impl Ucs01TransferPacket {
&self.tokens
}

pub fn new(sender: String, receiver: HexBinary, tokens: Vec<TransferToken>) -> Self {
pub fn new(sender: HexBinary, receiver: HexBinary, tokens: Vec<TransferToken>) -> Self {
Self {
sender,
receiver,
Expand All @@ -72,7 +72,7 @@ impl TryFrom<Ucs01TransferPacket> for Binary {

fn try_from(value: Ucs01TransferPacket) -> Result<Binary, Self::Error> {
Ok(ethabi::encode(&[
Token::String(value.sender),
Token::Bytes(value.sender.into()),
Token::Bytes(value.receiver.into()),
Token::Array(
value
Expand All @@ -97,8 +97,8 @@ impl TryFrom<Binary> for Ucs01TransferPacket {
fn try_from(value: Binary) -> Result<Self, Self::Error> {
let encoded_packet = ethabi::decode(
&[
ParamType::String,
ParamType::String,
ParamType::Bytes,
ParamType::Bytes,
ParamType::Array(Box::new(ParamType::Tuple(vec![
ParamType::String,
ParamType::Uint(256),
Expand All @@ -110,9 +110,9 @@ impl TryFrom<Binary> for Ucs01TransferPacket {
// NOTE: at this point, it is technically impossible to have any other branch than the one we
// match unless there is a bug in the underlying `ethabi` crate
match &encoded_packet[..] {
[Token::String(sender), Token::Bytes(receiver), Token::Array(tokens)] => {
[Token::Bytes(sender), Token::Bytes(receiver), Token::Array(tokens)] => {
Ok(Ucs01TransferPacket {
sender: sender.clone(),
sender: sender.clone().into(),
receiver: receiver.clone().into(),
tokens: tokens
.iter()
Expand Down Expand Up @@ -165,19 +165,17 @@ impl TryFrom<Binary> for Ics20Packet {
}
}

pub trait TransferPacket:
TryFrom<TransferPacketCommon<Self::Extension>, Error = EncodingError>
{
pub trait TransferPacket {
type Extension: Into<String> + Clone;
type Receiver: ToString;
type Addr: ToString;

// NOTE: can't ref here because cosmwasm_std::Coins don't impl iterator nor
// exposes the underlying BTreeMap...
fn tokens(&self) -> Vec<TransferToken>;

fn sender(&self) -> &str;
fn sender(&self) -> &Self::Addr;

fn receiver(&self) -> &Self::Receiver;
fn receiver(&self) -> &Self::Addr;

fn extension(&self) -> &Self::Extension;
}
Expand All @@ -193,13 +191,13 @@ impl From<NoExtension> for String {

impl TransferPacket for Ucs01TransferPacket {
type Extension = NoExtension;
type Receiver = HexBinary;
type Addr = HexBinary;

fn tokens(&self) -> Vec<TransferToken> {
self.tokens().clone()
}

fn sender(&self) -> &str {
fn sender(&self) -> &HexBinary {
&self.sender
}

Expand All @@ -214,7 +212,7 @@ impl TransferPacket for Ucs01TransferPacket {

impl TransferPacket for Ics20Packet {
type Extension = String;
type Receiver = String;
type Addr = String;

fn tokens(&self) -> Vec<TransferToken> {
vec![TransferToken {
Expand All @@ -223,7 +221,7 @@ impl TransferPacket for Ics20Packet {
}]
}

fn sender(&self) -> &str {
fn sender(&self) -> &String {
&self.sender
}

Expand Down Expand Up @@ -307,20 +305,24 @@ impl From<Ics20Ack> for GenericAck {
}
}

impl TryFrom<TransferPacketCommon<NoExtension>> for Ucs01TransferPacket {
type Error = EncodingError;

fn try_from(
TransferPacketCommon {
sender,
receiver,
tokens,
..
}: TransferPacketCommon<NoExtension>,
) -> Result<Self, Self::Error> {
Ok(Self::new(sender, vec![].into(), tokens))
}
}
// impl TryFrom<TransferPacketCommon<NoExtension>> for Ucs01TransferPacket {
// type Error = EncodingError;

// fn try_from(
// TransferPacketCommon {
// sender,
// receiver,
// tokens,
// ..
// }: TransferPacketCommon<NoExtension>,
// ) -> Result<Self, Self::Error> {
// Ok(Self::new(
// HexBinary::from_hex(&sender).map_err(|_| EncodingError::InvalidEncoding)?,
// HexBinary::from_hex(&receiver).map_err(|_| EncodingError::InvalidEncoding)?,
// tokens,
// ))
// }
// }

impl TryFrom<TransferPacketCommon<String>> for Ics20Packet {
type Error = EncodingError;
Expand Down Expand Up @@ -386,7 +388,7 @@ mod tests {
#[test]
fn ucs01_packet_encode_decode_iso() {
let packet = Ucs01TransferPacket {
sender: "a".into(),
sender: b"a".into(),
receiver: b"b".into(),
tokens: vec![
TransferToken {
Expand Down
Loading

0 comments on commit fe58746

Please sign in to comment.