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("starknet");
const tokenInAddress = tokens["eth"].address;
const tokenOutAddress = tokens["usdc"].address;
const tokenInDecimals = Number(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. 
  const slippage = 1; // %1 slippage
  const receiverAddress = starknet.selectedAddress;

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

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

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

Usage on backend on Starknet

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. 
const slippage = 1; // %1 slippage
const receiverAddress = accountAddress;

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

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

await account.execute([approveToken, swapCall])

swapCall:Call

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

Usage on backend on EVM Networks

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

const RPC_URL = "https://rpc.scroll.io";
const destination = "<YOUR-DESTINATION-SCROLL-ACCOUNT-ADDRESS>";
const privateKey = "<YOUR-PRIVATE-KEY>";

const fibrous = new FibrousRouter();

const account0 = account(privateKey, RPC_URL);
const contractWithWallet = await fibrous.getContractWAccount(
    account0,
    "scroll",
);

const tokens = await fibrous.supportedTokens("scroll");

const tokenInAddress = tokens["usdt"].address;
const tokenOutAddress = tokens["usdc"].address;
const tokenInDecimals = Number(tokens["usdt"].decimals);
const inputAmount = BigNumber.from(parseUnits("5", tokenInDecimals)); // 5 USDT

// Call the buildTransaction method in order to build the transaction
// slippage: The maximum acceptable slippage of the buyAmount amount. 
const slippage = 1; // %1 slippage
const swapCall = await fibrous.buildTransaction(
    inputAmount,
    tokenInAddress,
    tokenOutAddress,
    slippage,
    destination,
    "scroll",
);

const approveResponse = await fibrous.buildApproveEVM(
    inputAmount,
    tokenInAddress,
    account0,
    "scroll",
);

if (approveResponse === true) {

    const tx = await contractWithWallet.swap(
        swapCall.route,
        swapCall.swap_parameters,
    );

    await tx.wait();
}

Last updated