Page cover image

๐Ÿ”นSwap

Dojoswap operates differently from traditional exchanges in terms of how it executes swaps. In centralized exchanges, prices for stocks and cryptocurrencies fluctuate based on bidding and the dynamics of supply and demand, enabling real-time transactions that occur in milliseconds. However, blockchain-based exchanges face challenges due to block times, making immediate transaction execution nearly impossible.

To address this, Dojoswap uses algorithmic pricing, a distinct approach that relies on monitoring the ratio of paired assets within the liquidity pool. This method ensures that swaps occur based on the predetermined ratio of assets in the pool rather than real-time market dynamics. For a more detailed understanding of this approach, you can refer to the mechanism section for comprehensive insights.

Prerequisites

  • Tokens and pairs should be deployed on the chain.

  • You should know the addresses of tokens and a pair for trade.

Swap by Using CLI

When offer_asset Is Native & IBC Token

All transactions can be executed with the below command:

injectived tx wasm execute <contract-address> <handle-msg> <coins>
  • contract-address: The pair contract address in a swap transaction

  • handle-msg: The method and parameters of the execution, which will be explained following lines

  • coins: Transaction execution fee

To learn more about the general rules for handle-msg, please refer to this link.

{
  "swap": {
    "offer_asset": {
      "info": {
        "native_token": {
          "denom": "inj"
        }
      },
      "amount": "10"
    },
    "to": "<Addr>"
  }
}

swap.offer_asset represents your source asset. It is mandatory to acknowledge the decimal of the token setting when entering the value of swap.offer_asset.amount for an accurate transaction you want. For instance, the decimal of INJ is 18, and it implies the value 10 of swap.offer_asset.amount expresses โ€˜10 x 10^-18in the actual amount. That means you should multiply with the matching value,10^(decimal)`.

swap.to is the destination token address. You donโ€™t need to enter the amount to swap into since DojoSwap calculates the price algorithmically.

After filling them out, you may choose to change it into an inline string (not necessary if you can make it with multiline):

'{"swap":{"offer_asset": {"info" : {"native_token": {"denom": "inj"}},"amount": "10"},"to": "<Addr>",}}'

This is your handle-msg. The handle-msg can be used to complete the CLI command to swap tokens.

When offer_asset Is Contract-minted Token

Swapping contract-minted token to native token is executed with the same logic as above. However, it requires a different handle-msg due to the token systemโ€™s difference and implementation. The message looks like:

{
  "send": {
    "contract": "<Addr>",
    "amount": "10",
    "msg": Base64({
      "swap": {}
    })
  }
}

The method is a little bit tricky. Please keep your concentration on!

injectived tx wasm execute <contract-address> <handle-msg> <coins>

In the CLI,

  • contract-address: Enter token address

In the message:

  • send.contract: Enter pair address

  • send.amount: The amount of the origin token to swap from

send.msg.swap is an optional attribute, which is not required for basic swap executions.

Encode {"swap":{}} into base64 encoding. It should look something like: eyJzd2FwIjp7fX0=.

Enter the base64 encoded value for msg of the JSON:

{
  "send": {
    "contract": "<Addr>",
    "amount": "10",
    "msg": "eyJzd2FwIjp7fX0="
  }
}

After then, you may proceed as the above.

Last updated