Skip to main content

Swap

Dual investments will contribute liquidity to the pools within Dyson Finance, consisting of tradable tokens. Within the Dyson pair, we have the option to swap for either token0 or token1.

Fetch Dyson Pairs information

We utilize the same code for dual investments. Please refer to the section for more details.

Calculate a quote

  1. About fee: please refer to Current Fee on Dual Investment, we build a hooks called useFeeValue.
  2. Make a adapter for dyson pair and ui selector to check witch token is input token.
  3. The result includes perInputToOutput and perOutputToInput, indicating the value of each token relative to the compared token.
  4. The variable priceImpact refers to the degree of influence exerted by the user on the pair.
  import {
calcPriceImpact,
calcSwappedAmount,
calcSwappedInputAmount,
calcMinOutput,
} from '@dyson-finance/dyson-interface-sdk/calculations'

// ...
const [calcFee0, calcFee1] = useFeeValue(dysonPair)
const calcFee = BigInt(isInputToken0 ? calcFee0 : calcFee1)
const swapOutput = calcSwappedAmount(
inputBigNumber,
poolInputAmount,
poolOutputAmount,
calcFee,
)
const swapOutputFloat = parseFloat(formatUnits(swapOutput, outputToken?.decimals || 18))
const priceImpact = calcPriceImpact(
parseFloat(inputTokenAmount),
swapOutputFloat,
parseFloat(formatUnits(poolInputAmount, inputToken?.decimals || 18)),
parseFloat(formatUnits(poolOutputAmount, outputToken?.decimals || 18)),
calcFee,
)
const minOutput = calcMinOutput(swapOutput, slippage, outputTokenData?.decimals || 18)
const perInputToOutput = swapOutputFloat / parseFloat(inputTokenAmount)
const perOutputToInput = parseFloat(inputTokenAmount) / swapOutputFloat
// ...

Performing a trade

In this section, we implement a code snippet to execute dex swap action.
We create an interface for the swap action parameters; please refer to ISwapParams.

Let's highlight a few key points for further clarification:

  1. addressTo: Users can swap the token to others. If you wish to restrict this function, simply utilize the user's address.
  2. wrappedNativeToken: We will employ different contract functions(swapETHOut, swapETHIn) for wrapped native tokens like WETH on Ethereum to handle pairs such as (WETH/USDC) and allow users to swap ETH directly.
  3. minOutput: Utilize pre-calculated swapOutput and adjust based on the user's settled slippage.
  4. routerAddress: We use Router serves as an entry point for swapping, depositing, and withdrawing.
import { prepareDexSwap } from '@dyson-finance/dyson-interface-sdk/actions'
import {
prepareWriteContract,
writeContract,
waitForTransaction,
} from 'wagmi/actions'
//....
// client is Wallet Client
const { addressTo, inputBigNumber, minOutput, tokenIn, tokenOut } = swapParams

const prepareResult = prepareDexSwap(client, {
tokenIn, // token address
tokenOut, // token address
addressTo,
wrappedNativeToken: WRAPPED_NATIVE_TOKEN[chainId] as Address,
inputBigNumber,
minOutput,
})

const { request } = await prepareWriteContract({
...prepareResult,
address: routerAddress,
})

// Send transaction
const tx = await writeContract(request)

// Once the transaction is completed, developers can utilize the receipt to update the UI status.
const receipt = await waitForTransaction(tx)
//....