Execute Swap

Slippage is an important concept in trading that can have a significant impact on your trading outcomes. It refers to the difference between the expected price of a trade and the actual price at which the trade is executed. Slippage can occur in various market conditions and for several reasons, and it's crucial for traders to be aware of its potential effects.

Fibrous Finance uses slippage settings to prevent you from losing money; there is no need to increase your slippage when you spot an arbitrage opportunity.

Fibrous Finance is still in alpha version. Please make sure you have adjusted your slippage properly.

Generate transaction data for calling the Fibrous Finance Router contract

import { Router as FibrousRouter } from "fibrous-router-sdk";
import { BigNumber } from "@ethersproject/bignumber";
import { parseUnits } from "ethers";

const fibrous = new FibrousRouter();

const tokens = await fibrous.supportedTokens();
const tokenInAddress = tokens["eth"].address;
const tokenOutAddress = tokens["usdc"].address;
const tokenInDecimals = tokens["eth"].decimals;
const inputAmount = BigNumber.from(parseUnits("1", tokenInDecimals));

Usage on website

import { connect, disconnect } from '@argent/get-starknet'
import { Call } from "starknet";

const starknet = await connect({ showList: false })

await starknet.enable()

if (starknet.isConnected) {

  // Call the buildTransaction method in order to build the transaction
  // slippage: The maximum acceptable slippage of the buyAmount amount. 
  // slippage formula = slippage * 100
  // value 0.005 is %0.5, 0.05 is 5%, 0.01 is %1, 0.001 is %0.1 ...
  const slippage = 0.01; // %1 slippage
  const receiverAddress = starknet.selectedAddress;

  const approveCall:Call = await fibrous.buildApprove(
      inputAmount,
      tokenInAddress,
  );

  const swapCall:Call = await fibrous.buildTransaction(
    inputAmount,
    tokenInAddress,
    tokenOutAddress,
    slippage, 
    receiverAddress,
  );

  await starknet.account.execute([approveCall,swapCall]);
}

Usage on backend

import { Account, Provider } from "starknet";
import { Call } from "starknet";

const provider = new Provider();
const privateKey = "YOUR_PRIVATE_KEY";
const accountAddress = "YOUR_WALLET_ADDRESS";

// https://www.starknetjs.com/docs/guides/connect_account
// If this account is based on a Cairo v2 contract (for example OpenZeppelin account 0.7.0 or later), do not forget to add the parameter "1" after the privateKey parameter
const account = new Account(provider, accountAddress0, privateKey0,"1");

// Call the buildTransaction method in order to build the transaction
// slippage: The maximum acceptable slippage of the buyAmount amount. 
// slippage formula = slippage * 100
// value 0.005 is %0.5, 0.05 is 5%, 0.01 is %1, 0.001 is %0.1 ...
const slippage = 0.01; // %1 slippage
const receiverAddress = accountAddress;

const approveCall:Call = await fibrous.buildApprove(
      inputAmount,
      tokenInAddress,
);

const swapCall:Call = await fibrous.buildTransaction(
    inputAmount,
    tokenInAddress,
    tokenOutAddress,
    slippage,
    receiverAddress,
);

await account.execute([approveToken, swapCall])

swapCall:Call

{
    contractAddress:ROUTER_ADDRESS,
    entrypoint: "swap",
    calldata: calldata,
}

Last updated