transfer_tokens_unsafe.move 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. use sui::tx_context::TxContext;
  2. // Highly unsafe token transfer method, that accepts an `amount_to_bridge`
  3. // that is actually passed to the message publication.
  4. public fun prepare_transfer_unsafe<CoinType>(
  5. asset_info: VerifiedAsset<CoinType>,
  6. funded: Coin<CoinType>,
  7. amount_to_bridge: u64,
  8. recipient_chain: u16,
  9. recipient: vector<u8>,
  10. relayer_fee: u64,
  11. nonce: u32
  12. ): (
  13. TransferTicket<CoinType>,
  14. Coin<CoinType>
  15. ) {
  16. let (
  17. bridged_in,
  18. _
  19. ) = take_truncated_amount(&asset_info, &mut funded);
  20. let decimals = token_registry::coin_decimals(&asset_info);
  21. let norm_amount = normalized_amount::from_raw(amount_to_bridge, decimals);
  22. let ticket =
  23. TransferTicket {
  24. asset_info,
  25. bridged_in,
  26. norm_amount,
  27. relayer_fee,
  28. recipient_chain,
  29. recipient,
  30. nonce
  31. };
  32. // The remaining amount of funded may have dust depending on the
  33. // decimals of this asset.
  34. (ticket, funded)
  35. }
  36. public fun transfer_tokens_unsafe<CoinType>(
  37. token_bridge_state: &mut State,
  38. ticket: TransferTicket<CoinType>,
  39. ctx: &mut TxContext
  40. ): (
  41. MessageTicket,
  42. Coin<CoinType>
  43. ) {
  44. // This capability ensures that the current build version is used.
  45. let latest_only = state::assert_latest_only(token_bridge_state);
  46. let TransferTicket {
  47. asset_info,
  48. bridged_in,
  49. norm_amount,
  50. recipient_chain,
  51. recipient,
  52. relayer_fee,
  53. nonce
  54. } = ticket;
  55. // Ensure that the recipient is a 32-byte address.
  56. let recipient = external_address::new(bytes32::from_bytes(recipient));
  57. let token_chain = token_registry::token_chain(&asset_info);
  58. let token_address = token_registry::token_address(&asset_info);
  59. let encoded_transfer =
  60. transfer::serialize(
  61. transfer::new(
  62. norm_amount,
  63. token_address,
  64. token_chain,
  65. recipient,
  66. recipient_chain,
  67. normalized_amount::from_raw(
  68. relayer_fee,
  69. token_registry::coin_decimals(&asset_info)
  70. )
  71. )
  72. );
  73. // Prepare Wormhole message with encoded `Transfer`.
  74. (state::prepare_wormhole_message(
  75. &latest_only,
  76. token_bridge_state,
  77. nonce,
  78. encoded_transfer
  79. ), coin::from_balance(bridged_in, ctx))
  80. }