-
Notifications
You must be signed in to change notification settings - Fork 77
Spike: Token Muxed Extension #1443
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8e2e2c
add token muxed extension
leighmcculloch 728ebb6
assume bytes to scval conversion never errors
leighmcculloch 803c5df
add convenience conversions
leighmcculloch 51a46a2
simplify token mux to id-only mode
leighmcculloch 5ddc8b0
remove token muxed extension import
leighmcculloch 8bd5b78
remove arbitrary impl for muxed account extension
leighmcculloch bec58a9
Merge branch 'main' into token-muxed-extension
leighmcculloch a4aae37
emit transfer event
leighmcculloch 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,110 @@ | ||
use crate::{ | ||
contractclient, contractspecfn, Address, BytesN, ConversionError, Env, String, TryFromVal, | ||
TryIntoVal, Val, | ||
}; | ||
use core::fmt::Debug; | ||
|
||
/// Extension interface for Token contracts that implement the transfer_muxed | ||
/// extension, such as the Stellar Asset Contract. | ||
/// | ||
/// Defined by [SEP-??]. | ||
/// | ||
/// [SEP-??]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-00??.md | ||
/// | ||
/// Tokens allow holders of the token to transfer tokens to other addresses, and | ||
/// the transfer_muxed extension allows a token to be transferred with an | ||
/// accompanying muxed ID for both the from and to addresses. The muxed IDs are | ||
/// emitted in respective events. | ||
/// | ||
/// Tokens implementing the extension expose a single function for doing so: | ||
/// - [`transfer_muxed`][Self::transfer_muxed] | ||
#[contractspecfn(name = "super::StellarAssetSpec", export = false)] | ||
#[contractclient(crate_path = "crate", name = "TokenMuxedExtTransferClient")] | ||
pub trait TokenMuxedExtInterface { | ||
/// Transfer `amount` from `from` to `to`. | ||
/// | ||
/// Passess through the `from_id` and `to_id` to the event. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `from` - The address holding the balance of tokens which will be | ||
/// withdrawn from. | ||
/// * `from_mux` - The muxed ID of the sender to be emitted in the event. | ||
/// * `to` - The address which will receive the transferred tokens. | ||
/// * `to_mux` - The muxed ID of the receiver to be emitted in the event. | ||
/// * `amount` - The amount of tokens to be transferred. | ||
/// | ||
/// # Events | ||
/// | ||
/// Emits an event with topics `["transfer_muxed", from: Address, to: Address], | ||
/// data = {amount: i128, from_mux: Mux, to_mux: Mux}` | ||
fn transfer_muxed( | ||
env: Env, | ||
from: Address, | ||
from_mux: Mux, | ||
to: Address, | ||
to_mux: Mux, | ||
amount: i128, | ||
); | ||
} | ||
|
||
/// Mux is a value that off-chain identifies a sub-identifier of an Address | ||
/// on-chain. | ||
/// | ||
/// A mux is also commonly referred to as a memo. | ||
/// | ||
/// A mux may be a void (none), an ID (64-bit number), a String, or a 32-byte | ||
/// Hash. | ||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
pub enum Mux { | ||
None, | ||
Id(u64), | ||
Text(String), | ||
Hash(BytesN<32>), | ||
} | ||
|
||
impl TryFromVal<Env, Val> for Mux { | ||
type Error = ConversionError; | ||
|
||
fn try_from_val(env: &Env, v: &Val) -> Result<Self, Self::Error> { | ||
if v.is_void() { | ||
Ok(Self::None) | ||
} else if let Ok(v) = v.try_into_val(env) { | ||
Ok(Self::Id(v)) | ||
} else if let Ok(v) = v.try_into_val(env) { | ||
Ok(Self::Text(v)) | ||
} else if let Ok(v) = v.try_into_val(env) { | ||
Ok(Self::Hash(v)) | ||
} else { | ||
Err(ConversionError) | ||
} | ||
} | ||
} | ||
|
||
impl TryFromVal<Env, Mux> for Val { | ||
type Error = ConversionError; | ||
|
||
fn try_from_val(env: &Env, v: &Mux) -> Result<Self, Self::Error> { | ||
match v { | ||
Mux::None => Ok(Val::VOID.to_val()), | ||
Mux::Id(v) => v.try_into_val(env).map_err(|_| ConversionError), | ||
Mux::Text(v) => v.try_into_val(env).map_err(|_| ConversionError), | ||
Mux::Hash(v) => v.try_into_val(env).map_err(|_| ConversionError), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
use crate::env::internal::xdr::ScVal; | ||
|
||
#[cfg(not(target_family = "wasm"))] | ||
impl From<&Mux> for ScVal { | ||
fn from(v: &Mux) -> Self { | ||
match v { | ||
Mux::None => ScVal::Void, | ||
Mux::Id(v) => ScVal::U64(*v), | ||
Mux::Text(v) => v.into(), | ||
Mux::Hash(v) => v.into(), | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.