@@ -19,6 +19,7 @@ use namada_sdk::masp_primitives::zip32;
1919use namada_sdk:: signing:: SigningTxData ;
2020use namada_sdk:: time:: DateTimeUtc ;
2121use namada_sdk:: tx:: data:: GasLimit ;
22+ use namada_sdk:: tx:: either:: Either ;
2223use namada_sdk:: tx:: { Section , Tx } ;
2324use namada_sdk:: {
2425 address:: Address ,
@@ -308,6 +309,104 @@ pub fn redelegate_tx_args(
308309 Ok ( args)
309310}
310311
312+ #[ derive( BorshSerialize , BorshDeserialize , Debug ) ]
313+ #[ borsh( crate = "namada_sdk::borsh" ) ]
314+ pub enum Slippage {
315+ /// Specifies the minimum amount to be received
316+ MinOutputAmount ( String ) ,
317+ /// A time-weighted average price
318+ Twap {
319+ /// The maximum percentage difference allowed between the estimated and
320+ /// actual trade price. This must be a decimal number in the range
321+ /// `[0, 100]`.
322+ slippage_percentage : String ,
323+ /// The time period (in seconds) over which the average price is
324+ /// calculated
325+ window_seconds : u64 ,
326+ } ,
327+ }
328+
329+ impl From < Slippage > for args:: Slippage {
330+ fn from ( slippage : Slippage ) -> Self {
331+ match slippage {
332+ Slippage :: MinOutputAmount ( min_output_amount) => {
333+ let min_output_amount =
334+ Amount :: from_str ( & min_output_amount, 0u8 ) . expect ( "Amount to be valid." ) ;
335+ args:: Slippage :: MinOutputAmount ( min_output_amount)
336+ }
337+ Slippage :: Twap {
338+ slippage_percentage,
339+ window_seconds,
340+ } => args:: Slippage :: Twap {
341+ slippage_percentage,
342+ window_seconds,
343+ } ,
344+ }
345+ }
346+ }
347+
348+ #[ derive( BorshSerialize , BorshDeserialize , Debug ) ]
349+ #[ borsh( crate = "namada_sdk::borsh" ) ]
350+ pub struct OsmosisSwapMsg {
351+ pub transfer : Vec < u8 > ,
352+ /// The token we wish to receive (on Namada)
353+ pub output_denom : String ,
354+ /// Address of the recipient on Namada
355+ pub recipient : String ,
356+ /// Address to receive funds exceeding the minimum amount,
357+ /// in case of IBC shieldings
358+ pub overflow : String ,
359+ /// Constraints on the osmosis swap
360+ pub slippage : Slippage ,
361+ /// Recovery address (on Osmosis) in case of failure
362+ pub local_recovery_addr : String ,
363+ /// The route to take through Osmosis pools
364+ /// A REST rpc endpoint to Osmosis
365+ pub osmosis_rest_rpc : String ,
366+ }
367+
368+ pub fn osmosis_swap_tx_args (
369+ osmosis_swap_msg : & [ u8 ] ,
370+ tx_msg : & [ u8 ] ,
371+ ) -> Result < ( args:: TxOsmosisSwap , Option < StoredBuildParams > ) , JsError > {
372+ let osmosis_swap_msg = OsmosisSwapMsg :: try_from_slice ( osmosis_swap_msg) ?;
373+
374+ let OsmosisSwapMsg {
375+ transfer,
376+ output_denom,
377+ recipient,
378+ overflow,
379+ slippage,
380+ local_recovery_addr,
381+ osmosis_rest_rpc,
382+ } = osmosis_swap_msg;
383+
384+ let ( ibc_transfer_args, bparams) = ibc_transfer_tx_args ( & transfer, tx_msg) ?;
385+
386+ let recipient = match Address :: from_str ( & recipient) {
387+ Ok ( address) => Ok ( Either :: Left ( address) ) ,
388+ Err ( _) => match PaymentAddress :: from_str ( & recipient) {
389+ Ok ( pa) => Ok ( Either :: Right ( pa) ) ,
390+ Err ( _) => Err ( JsError :: new ( "Invalid recipient address" ) ) ,
391+ } ,
392+ } ?;
393+
394+ let overflow = Address :: from_str ( & overflow) ?;
395+
396+ let tx_osmosis_swap_args = args:: TxOsmosisSwap {
397+ transfer : ibc_transfer_args,
398+ output_denom,
399+ recipient,
400+ overflow : Some ( overflow) ,
401+ slippage : slippage. into ( ) ,
402+ local_recovery_addr,
403+ route : None ,
404+ osmosis_rest_rpc,
405+ } ;
406+
407+ Ok ( ( tx_osmosis_swap_args, bparams) )
408+ }
409+
311410#[ derive( BorshSerialize , BorshDeserialize , Debug ) ]
312411#[ borsh( crate = "namada_sdk::borsh" ) ]
313412pub struct VoteProposalMsg {
0 commit comments