Code Monkey home page Code Monkey logo

1inch-api-v3's Introduction

1inch .NET API Client (v3)

A .NET client library targeting v3 of the 1inch DeFi aggregator API. The intention is to provide a simple wrapper to consume and model each of the available REST end points.

Each of the objects in the domain are meant to provide the inbound and outbound structures needed to interact with the API in a straight forward and object oriented way.

NOTE: This project has no affiliation with the 1inch team or protocol and is simply meant as an effort for the .NET community.

Supported Blockchains

  • ✅ Ethereum
  • ✅ Binance Smart Chain
  • ✅ Polygon
  • ✅ Optimism
  • ✅ Arbitrum

Getting Started

The 1inch .NET API is available on NuGet:


dotnet add package OneInch.Api

Quick Examples

Initial client setup:

// httpClient as some instance of HttpClient (DI container, self invoked, etc.)
using(var api = new OneInchClient(httpClient))
{
    // ...
}

Simple health check:

var status = await api.HealthCheck.GetStatus();

Console.WriteLine("API Status is: " + status.status)

// API Status is: OK

Requesting all supported token information:

var tokenList = await api.Token.GetAll();

foreach(var token in tokenList.tokens)
{

    Console.WriteLine("Token Symbol: " + token.symbol);

}

// Token Symbol: SNX

// Token Symbol: ETH

// Token Symbol: ZRX

// Token Symbol: MATIC

// ... etc.

Chain switching (Ethereum is default target):

var tokenList = await api
                        .SwitchBlockchain(BlockchainEnum.POLYGON) // switch to Polygon 
                        .Token
                        .GetAll();

Request quote -> 1 DAI for 1 USDC:

// https://etherscan.io/token/0x6b175474e89094c44da98b954eedeac495271d0f
const string DAI_TOKEN_ADDRESS = "0x6b175474e89094c44da98b954eedeac495271d0f";

// https://etherscan.io/token/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48
const string USDC_TOKEN_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";

var request = new QuoteRequest()
{
    fromTokenAddress = DAI_TOKEN_ADDRESS,
    toTokenAddress = USDC_TOKEN_ADDRESS,
    amount = 100000000000000000 // 1 DAI
};

await api
        .Quote
        .GetQuote(request);

Request swap:

var request = new SwapRequest()
{
    fromTokenAddress = DAI_TOKEN_ADDRESS,
    toTokenAddress = USDC_TOKEN_ADDRESS,
    fromAddress = "<wallet_address>",
    amount =  100000000000000000, 
    slippage = 1
};

await api
        .Swap
        .GetSwap(request);

Hello DeFi

A functional "Hello DeFi" example application exists here. This application works through the fundamental steps of approving token spend, requesting a swap quote, and then requesting the final swap transaction data through the OneInch.Api client.

  /// <summary>
  /// Executes basic swap of DAI for USDC.
  /// </summary>
  static void SwapDAIForUSDC()
  {
      // Step 1
      var approval = ApproveDaiSpend().Result;
      
      // Step 2
      SignAndSendTransaction(approval.data);

      // Step 3
      var quote = GetQuote().Result;

      // Step 4
      var swap = GetSwap(quote).Result;
      
      // Step 5
      SignAndSendTransaction(swap.tx.data);
  }

Below is a visualization of the overall process being executed when the SwapDAIForUSDC() method is called:

Basic Swap

NOTE: SignAndSendTransaction is a placeholder method for those stages of the process. No transaction data is being sent to be mined/verifed on-chain.

    /// <summary>
    /// Signs transaction data with wallet and submits transaction to EVM blockchain to be mined.
    /// </summary>
    /// <param name="data">Transaction data string.</param>
    static void SignAndSendTransaction(string data)
    {
        // implement web3 client like Nethereum to submit transactions.
    }

Supported Frameworks

Available Clients

You can find all of the official Swagger documentation for the various chain clients here.

Road Map

  • Unit Test Coverage
  • Swapping logic flow diagrams
  • Advanced API usage examples
  • Examples to sign and send swap transactions via Nethereum.

1inch-api-v3's People

Contributors

0xdelco avatar cj-technical avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.