Interacting with Minitswap

Interacting with Minitswap

Provide

provide is a function that allows users to supply INIT tokens to Initia L1. Providers earn swap fees from transactions processed via Minitswap.

Move Function Interface:

public entry fun provide(
  account: &signer,
  amount: u64,
  min_return_amount: Option<u64>,
)
  • amount: The amount of L1 INIT to provide.

  • min_return_amount: Ensures the transaction fails if the return amount is less than specified.

Example Usage:

initiad tx move execute 0x1 minitswap provide \ 
  --args "u64:100 option<u64>:100" \
  --from [key-name] \
  --gas auto --gas-adjustment 1.5 --gas-prices 0.15uinit \
  --node [rpc-url]:[rpc-port] --chain-id [chain-id]

Withdraw

withdraw allows users to reclaim INIT tokens previously deposited.

Move Function Interface:

public entry fun withdraw(
  account: &signer,
  amount: u64,
  min_return_amount: Option<u64>,
)
  • amount: Specifies the share amount to withdraw.

  • min_return_amount: Transaction fails if the return is less than specified.

Example Usage:

initiad tx move execute 0x1 minitswap withdraw \ 
  --args "u64:100 option<u64>:100" \
  --from [key-name] \
  --gas auto --gas-adjustment 1.5 --gas-prices 0.15uinit \
  --node [rpc-url]:[rpc-port] --chain-id [chain-id]

Swap

swap allows the exchange of L1 INIT for L2 INIT transferred via IBC.

Move Function Interface:

public entry fun swap(
    account: &signer,
    offer_asset_metadata: Object<Metadata>,
    return_asset_metadata: Object<Metadata>,
    amount: u64,
    min_return_amount: Option<u64>
)
  • offer_asset_metadata: Metadata for the provided coin.

  • return_asset_metadata: Metadata for the coin to be returned.

  • amount: Coin amount provided.

  • min_return_amount: Ensures the swap fails if the return is less than specified.

For more information on metadata, please refer to obtaining metadata.

Example Usage:

initiad tx move execute 0x1 minitswap swap \ 
  --args "object:0x12.. object: 0x12.. u64:100 option<u64>:100" \
  --from [key-name] \
  --gas auto --gas-adjustment 1.5 --gas-prices 0.15uinit \
  --node [rpc-url]:[rpc-port] --chain-id [chain-id]

Rebalance

Rebalance allows the Pegkeeper to buy the L2 INIT in the average price that the Pegkeeper has bought.

Move Function Interface:

public entry fun rebalance(
    account: &signer,
    l2_asset_metadata: Object<Metadata>,
    amount: u64,
    min_return_amount: Option<u64>
)
  • l2_asset_metadata: Metadata of the L2 INIT to be purchased.

  • amount: Amount of L1 INIT to be provided.

  • min_return_amount: Minimum amount of L2 INIT to be returned. If the actual return amount is smaller than min_return_amount, the tx will fail.

Example Usage:

initiad tx move execute 0x1 minitswap rebalance \ 
  --args "object:0x12.. u64:100 option<u64>:100" \
  --from [key-name] \
  --gas auto --gas-adjustment 1.5 --gas-prices 0.15uinit \
  --node [rpc-url]:[rpc-port] --chain-id [chain-id]

Swap simulate

swap_simulation is a view function to estimate the return value of said swap.

#[view]
public fun swap_simulation(
    offer_metadata: Object<Metadata>,
    return_metadata: Object<Metadata>,
    offer_amount: u64,
): (u64, u64) // (return amount, fee amount)
  • offer_asset_metadata: Metadata of the provided coin.

  • return_asset_metadata: Metadata of the coin that will be returned.

  • amount: Amount of coin provided.

Example Usage:

curl -X POST "[LCD_URI]/initia/move/v1/accounts/0x1/modules/minitswap/view_functions/swap_simulation" \
  -H "accept: application/json" \
  -H "Content-Type: application/json" \
  -d "{ \"args\": [ \"[BCS_ENCODED_OBJECT, BCS_ENCODED_OBJECT, BCS_ENCODED_OFFER_AMOUNT]\" ]}"

  
#{
#  "data": "[\"100\",\"100\"]",
#  "events": [],
#  "gas_used": "5699"
#}
  

Swap via IBC hook

Generally, users will utilize minitswap in order to swap L2 INIT to L1 INIT. However, using minitswap requires two steps: 1. L2 INIT should be sent to L1 through IBC, and 2. swap the transferred L2 INIT to L1 INIT. In this case, the users are burdened from having to send two transactions from both L2 and L1. To resolve such burden, one can use the swap_transfer module and IBC hook so that the steps above can be done in one transaction.

Swap transfer

Swap transfer is a module built for seamless interchain swap and transfer. The published address is as follows:

  • mahalo-2: 0x42cd8467b1c86e59bf319e5664a09b6b5840bb3fac64f5ce690b5041c530565a

Mintswap to

minit_swap_to is a function for using minitswap through IBC hook. The Move function interface is as follows:

public entry fun minit_swap_to(
    account: &signer,
    offer_asset_metadata: Object<Metadata>,
    return_asset_metadata: Object<Metadata>,
    amount: u64,
    min_return_amount: Option<u64>,
    to: address,
)
  • offer_asset_metadata: Metadata for the coin to provide

  • return_asset_metadata: Metadata for the coin to be returned.

  • amount: Amount of coin to provide.

  • min_return_amount: Minimum value for the swap. If the return amount is smaller than min_return_amount, the tx will fail.

  • to: Address to receive the returned coin.

Example Usage:

import {
  bcs,
  Coin,
  Height,
  LCDClient,
  MnemonicKey,
  MsgExecute,
  MsgTransfer,
  Wallet,
} from '@initia/initia.js';

async function main() {
  const lcd = new LCDClient('[rest-url]', {
    gasPrices: '0.15uinit',
    gasAdjustment: '1.5',
  });

  const key = new MnemonicKey({
    mnemonic: 'beauty sniff protect ...',
  });
  const wallet = new Wallet(lcd, key);

  const moduleAddresss = '0x...';
  const offerAmount = 1000;
  const hookMsg = {
    move: {
      message: {
        module_address: moduleAddresss,
        module_name: 'swap_trasnfer',
        function_name: 'minit_swap_to',
        type_args: [],
        args: [
          bcs.object().serialize('0x...').toBase64(), // metadata to offer
          bcs.object().serialize('0x...').toBase64(), // metadata to return
          bcs.u64().serialize(100).toBase64(), // offer amount
          bcs.option(bcs.u64()).serialize(offerAmount).toBase64(), // min return amount
          bcs.address().serialize('init1...').toBase64(), // to
        ],
      },
    },
  };

  const msgs = [
    new MsgTransfer(
      'transfer',
      'channel-..',
      new Coin('l2/...', offerAmount),
      key.accAddress,
      `${moduleAddresss}::${hookMsg.move.message.module_name}::${hookMsg.move.message.function_name}`,
      new Height(0, 0),
      ((new Date().valueOf() + 1000 * 60) * 1000000).toString(), // timeout
      JSON.stringify(hookMsg)
    ),
  ];

  // sign tx
  const signedTx = await wallet.createAndSignTx({ msgs });
  // send(broadcast) tx
  lcd.tx.broadcastSync(signedTx).then(res => console.log(res));
  // {
  //   height: 0,
  //   txhash: '0F2B255EE75FBA407267BB57A6FF3E3349522DA6DBB31C0356DB588CC3933F37',
  //   raw_log: '[]'
  // }
}

main();

Conclusion

Minitswap on Initia L1 provides a robust mechanism for liquidity provision, withdrawal, and token swapping, enabling users to engage actively in the decentralized finance ecosystem while earning transaction fees. By following the detailed examples and function interfaces provided, users can effectively interact with Minitswap for optimal asset management and trading strategies.

Last updated

Logo

© 2024 Initia Foundation, All rights reserved.