Skip to main content

Fibrous Finance Python Client.

Project description

Fibrous Finance Router Client for Python

Python client for Fibrous Router. It makes it easy to get the tokens and protocols supported by Fibrous, choose the best route for your swap from the Fibrous API and build those swap transactions. Compatible with Starknetpy.

Installation

You can install fibrous-python via Pip.

pip install fibrous-python

Or

git clone https://github.com/Fibrous-Finance/router-python-sdk
cd fibrous_python
pip install .

Usage

Create Fibrous client:

from fibrous_python import FibrousClient

client = FibrousClient()

# or you can specify custom API url (you probably won't need this)
client = FibrousClient(route_url="https://api.fibrous.finance",
                       graph_url="https://graph.fibrous.finance",
                       router_address="0x00f6f4CF62E3C010E0aC2451cC7807b5eEc19a40b0FaaCd00CCA3914280FDf5a")

Get supported tokens by Fibrous.

chainName="" #starknet or scroll
tokens = client.supported_tokens(chainId)
tokens["eth"]

# expected output:
Token(
    address='0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
    name='Ether',
    symbol='eth',
    decimals=18,
    price='3841.3',
    imageUrl='https://assets.coingecko.com/coins/images/279/small/ethereum.png?1696501628',
    valuable=True,
    verified=True,
    category=None
)

Get best route:

chainName="" #starknet or scroll
tokens = client.supported_tokens(chainId)
route = client.get_best_route(amount=10**12,
                        token_in_address=tokens["eth"].address,
                        token_out_address=tokens["usdc"].address),
                        chainName
route

# expected output:
RouteSuccess(
    success=True,
    inputToken=Token(...),
    inputAmount='1000000000000',
    outputToken=Token(...),
    outputAmount='4117',
    route=[Route...],
    Slippage(input_token_value=0.00381994, output_token_value=0.005948858756000001, slippage=0.5573173285444276),
    estimatedGasUsed='8975364622720',
    bestQuotesByProtocols=['3821', '3876', '3815', '3878', '3838', '4117', '3883', '0', '3838', '5467'],
    time=1.354,
    initial=True
)

The slippage object contains the slippage value calculated by processing the input/output amount and token prices. In the example, the slipage object shows that the value of the tokens we send to fibrous is $0.00381, in return we will receive an output token with a value of $0.0059 and we will profit 55.73% from this transaction.

 Slippage(input_token_value=0.00381994, output_token_value=0.005948858756000001, slippage=0.5573173285444276)

Build transaction:

chainName="" # Chain name should be "starknet" for StarkNet and "scroll" for Scroll
tokens    = client.supported_tokens()
swap_call = client.build_transaction(input_amount=10**12,
                        token_in_address=tokens["eth"].address,
                        token_out_address=tokens["usdc"].address,
                        slippage=0.01,
                        # starknet address
                        destination="0x07bfe36393355f52844e45622ef0f0fd9bcb18c63f9004060effc8cc0970f8e1",
                        chain_name
                        )

# expected output:
Call(
    to_addr=436333995167355148017722927569021171726157728206902557777108018048487382874,
    selector=602962535134499854912799851629033993488593928113527484350375636311213640489,
    calldata=[int, int, int....]
)

Swap example with Starknet.py

import asyncio

from starknet_py.net.account.account import Account
from starknet_py.net.full_node_client import FullNodeClient
from starknet_py.net.models.chains import StarknetChainId
from starknet_py.net.signer.stark_curve_signer import KeyPair

from fibrous_python.core import FibrousClient
from fibrous_python.utils import build_approve_call


async def main():
    your_public_key = "0x123456"
    your_private_key = "0x123456"
    account0 = Account(
        address=your_public_key,
        client=FullNodeClient("https://rpc.starknet.lava.build:443"),
        key_pair=KeyPair(private_key=your_private_key,
                         public_key=your_public_key),
        chain=StarknetChainId.MAINNET)
    chainName="starknet"
    client = FibrousClient()
    tokens = client.supported_tokens(chainName)

    # amount to swap
    amount: int = int(0.001 * (10**tokens["eth"].decimals))

    # swap call
    swap_call = client.build_transaction(
        amount,
        tokens["eth"].address,
        tokens["usdc"].address,
        0.01,
        your_public_key,
        chainName)

    # approve call
    approve_call = build_approve_call(
            token_address=tokens["eth"].address,
            amount=amount)

    txre = await account0.execute(calls=[approve_call,
                                         swap_call],
                                  max_fee=int(1e16))

    print(f"Transaction hash: {hex(txre.transaction_hash)}")


if __name__ == "__main__":
    asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fibrous-python-0.1.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fibrous_python-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file fibrous-python-0.1.0.tar.gz.

File metadata

  • Download URL: fibrous-python-0.1.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.0

File hashes

Hashes for fibrous-python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 746ebb6c07ddbd4734845355dc7f5acb6b74b4872183b0c736443b8b00b6c9e1
MD5 242731f73393d3472db4acc599d6bd63
BLAKE2b-256 79c70fdc5af87395c6844c662dec307b617686d7cb788320c5154fdd696e1857

See more details on using hashes here.

File details

Details for the file fibrous_python-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fibrous_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.0

File hashes

Hashes for fibrous_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80a102af178618d348a04e1e0d75a37302488df98eea8f23673ef2603269b9a9
MD5 f64c9e1858eb9de165bda2744bd544b3
BLAKE2b-256 66041e63c7b520da5083fcb65d7eee539ff228b6dbddf0e159e0a42a2f3d493c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page