Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
chore: clean-ups + comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Apr 11, 2024
1 parent b655abd commit e9651e1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion mocks/src/sovereign/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ where

self.runtime()
.ibc_transfer
.minted_token(token_denom, &mut working_set)
.minted_token_id(token_denom, &mut working_set)
.map(|token| token.token_id)
.ok()
}
Expand Down
53 changes: 28 additions & 25 deletions modules/sov-ibc-transfer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ use uint::FromDecStrErr;
use super::IbcTransfer;
use crate::utils::compute_escrow_address;

/// Using a different salt will result in a different token address. Since
/// ICS-20 tokens coming from other chains are guaranteed to have unique names,
/// we don't need to use different salt values, thus we just use 0.
const SALT: u64 = 0u64;

/// Maximum memo size allowed for ICS-20 transfers. This bound corresponds to
/// the `MaximumMemoLength` in the `ibc-go`
const MAXIMUM_MEMO_SIZE: usize = 32768;

/// We need to create a wrapper around the `Transfer` module and `WorkingSet`,
/// because we only get the `WorkingSet` at call-time from the Sovereign SDK,
/// which must be passed to `TokenTransferValidationContext` methods through
Expand All @@ -50,8 +59,8 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
}
}

/// Stores mapping from "denom to token ID" and vice versa for an IBC-minted
/// token.s
/// Stores mapping from "denom to token ID" and vice versa for an
/// IBC-created token.
fn record_minted_token(&self, token_id: TokenId, token_name: String) {
self.ibc_transfer.minted_token_id_to_name.set(
&token_id,
Expand Down Expand Up @@ -89,23 +98,15 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
/// Creates a new token with the specified `token_name` and mints an initial
/// balance to the `minter_address`.
///
/// Note: The mint authority must be held by the `IbcTransfer` module, so the
/// `authorized_minters` is set to the `IbcTransfer` address. Also, remember
/// that the `token_name` is a denom prefixed with IBC and originates from the
/// counterparty chain.
/// Note: The mint authority must be held by the `IbcTransfer` module, so
/// the `authorized_minters` is set to the `IbcTransfer` address. Also,
/// remember that the `token_name` is a denom prefixed with IBC and
/// originates from the counterparty chain.
fn create_token(
&self,
token_name: String,
minter_address: S::Address,
) -> Result<TokenId, TokenTransferError> {
// Using a different salt will result in a different token
// address. Since ICS-20 tokens coming from other chains are
// guaranteed to have unique names, we don't need to use
// different salt values.
let salt = 0u64;

let initial_balance = 0;

// Make sure to use `ibc_transfer` address as the sender
let context = Context::new(
self.ibc_transfer.address.clone(),
Expand All @@ -118,8 +119,8 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
.bank
.create_token(
token_name.clone(),
salt,
initial_balance,
SALT,
0,
minter_address,
vec![self.ibc_transfer.address.clone()],
&context,
Expand Down Expand Up @@ -208,10 +209,11 @@ where
coin: &PrefixedCoin,
memo: &Memo,
) -> Result<(), TokenTransferError> {
if !memo.as_ref().is_empty() {
return Err(TokenTransferError::Other(
"Memo must be empty when burning tokens".to_string(),
));
// Disallowing larges memos to prevent potential overloads on the system.
if memo.as_ref().len() > MAXIMUM_MEMO_SIZE {
return Err(TokenTransferError::Other(format!(
"Memo size exceeds maximum allowed size: {MAXIMUM_MEMO_SIZE}"
)));
}

let token_name = coin.denom.to_string();
Expand Down Expand Up @@ -255,10 +257,11 @@ where
coin: &PrefixedCoin,
memo: &Memo,
) -> Result<(), TokenTransferError> {
if !memo.as_ref().is_empty() {
return Err(TokenTransferError::Other(
"Memo must be empty when escrowing tokens".to_string(),
));
// Disallowing larges memos to prevent potential overloads on the system.
if memo.as_ref().len() > MAXIMUM_MEMO_SIZE {
return Err(TokenTransferError::Other(format!(
"Memo size exceeds maximum allowed size: {MAXIMUM_MEMO_SIZE}"
)));
}

let token_id = TokenId::from_str(&coin.denom.to_string()).map_err(|e| {
Expand Down Expand Up @@ -339,7 +342,7 @@ where
{
return Err(TokenTransferError::Other(format!(
"Token with ID '{token_id}' is an IBC-created token and cannot be unescrowed.\
Use '{token_name}' as denom for receiving an IBC token from the source chain"
Use '{token_name}' as denom for receiving an IBC token from the source chain"
)));
}

Expand Down
8 changes: 4 additions & 4 deletions modules/sov-ibc-transfer/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where
S: Spec,
{
#[rpc_method(name = "mintedTokenName")]
pub fn escrowed_token(
pub fn minted_token_name(
&self,
token_id: TokenId,
working_set: &mut WorkingSet<S>,
Expand All @@ -28,7 +28,7 @@ where
.minted_token_id_to_name
.get(&token_id, working_set)
.ok_or(to_jsonrpsee_error(format!(
"No IBC-minted token found for ID: '{token_id}'"
"No IBC-created token found for ID: '{token_id}'"
)))?;

Ok(MintedTokenResponse {
Expand All @@ -38,7 +38,7 @@ where
}

#[rpc_method(name = "mintedTokenId")]
pub fn minted_token(
pub fn minted_token_id(
&self,
token_name: String,
working_set: &mut WorkingSet<S>,
Expand All @@ -47,7 +47,7 @@ where
.minted_token_name_to_id
.get(&token_name, working_set)
.ok_or(to_jsonrpsee_error(format!(
"No IBC-minted token found for denom: '{token_name}'"
"No IBC-created token found for denom: '{token_name}'"
)))?;

Ok(MintedTokenResponse {
Expand Down

0 comments on commit e9651e1

Please sign in to comment.