Velora SDK

repository·master·Indexed 20 days ago

https://github.com/veloradex/sdk

A lightweight, modular interface for interacting with the Velora API, supporting web3 providers such as ethers, web3, and viem. The SDK enables developers to execute market swaps, manage intent-based Delta orders (including TWAP and server-side order construction), and handle OTC orders. It offers three instantiation approaches—Simple, Full, and Partial—to optimize for either ease of use or bundle size.

Tokens
138.4K
Snippets
513
Records
696
Agent score
70%

What's inside @velora-dex/sdk

  1. Overview of Velora SDK features

    master

    The Velora SDK provides a lightweight and versatile interface for interacting with the Velora API.

    Key characteristics:

    • Versatility: Compatible with web3 or ethers without requiring them as direct dependencies. It also supports viem (which is used internally for utilities).
    • Canonical: Designed to allow developers to import only the specific functions they need.
    • Lightweight: The minimal variant is approximately 10KB Gzipped.
  2. Reference the @velora-dex/sdk global symbols

    master

    The @velora-dex/sdk package provides a comprehensive set of tools for interacting with the Velora API and blockchain contracts. The SDK is organized into several key categories:

    Enumerations

    Used for defining fixed sets of values, such as:

    • ContractMethodV5 / ContractMethodV6: Methods for different Ethers versions.
    • SwapSide: Defines the direction of a swap.

    Classes

    • FetcherError: Represents errors encountered during data fetching operations.

    Interfaces

    Defines the shape of data objects used throughout the SDK, including:

    • Allowance: Token allowance information.
    • BuildOrderDataInput: Input for building orders.
    • TransactionParams / TxSendOverrides: Parameters for executing transactions.
    • EthersV5ProviderDeps / EthersV6ProviderDeps: Dependencies required for Ethers providers.

    Type Aliases

    The SDK uses extensive type aliases to ensure type safety for complex operations like:

    • Order Management: BuildDeltaOrderParams, CancelDeltaOrder, OTCOrder, TWAPDeltaOrder.
    • Pricing & Quotes: QuoteResponse, DeltaPrice, OptimalRate.
    • Token Data: Token, Address, TokenCategory.
    • SDK Variants: SimpleSDK, SimpleFetchSDK.

    Variables

    • API_URL: The base URL for the Velora API.
    • DEFAULT_VERSION: The default API version used by the SDK.
    • OrderHelpers: Utility functions for managing orders.
  3. What is a Duplex stream?

    master
    A Duplex stream is a type of stream that implements both the Readable and Writable interfaces simultaneously. This allows the stream to be used for both reading data from and writing data to the same object. Common real-world examples of Duplex streams include TCP sockets, zlib streams, and crypto streams.
  4. Use writable.cork() and writable.uncork() to buffer writes

    master

    The writable.cork() method is used to buffer multiple small chunks written to a stream in rapid succession. Instead of immediately forwarding them to the destination, they are buffered until writable.uncork() is called. When uncorked, all buffered chunks are passed to writable._writev() (if implemented).

    Note: Using cork() without implementing _writev() may negatively impact throughput.

    // Note: This is a conceptual usage pattern for Writable streams
    // writable.cork();
    // writable.write(chunk1);
    // writable.write(chunk2);
    // writable.uncork();
  5. Create and link new Web3Context objects

    master

    The Web3Context provides mechanisms to manage context hierarchies and create new specialized objects that share the same underlying context.

    • use(ContextRef, ...args): Creates a new object of type T (which must extend Web3Context) and automatically links it to the current context. This is the preferred way to initiate a global context and derive new objects from it.
    • link(parentContext): Manually links the current context to another parentContext.
    • fromContextObject(contextObject, ...args): A static method used to reconstruct a Web3Context instance from a Web3ContextObject.
    // Using 'use' to create a new context-linked object
    const newContext = web3Context.use(MyCustomContextClass, arg1, arg2);
  6. What are External Delta Orders?

    master

    External Delta Orders allow for complex DeFi strategies by delegating token handling to an external handler contract. While the Delta protocol manages the auction, settlement, and signature verification, the handler contract executes the specific logic (e.g., Aave flash loans, collateral/debt swaps).

    Key differences from Standard Delta Orders:

    • Required fields: handler (address of the contract) and data (protocol-specific bytes).
    • Unsupported features: bridge field and cross-chain support are not available for External Orders.
    • Supported features: beneficiary is supported (defaults to owner).

    As with all Delta v2 orders, the Order is built by the server based on the route returned by getDeltaPrice. You sign the returned typed data and post it.

  7. Use the NonPayableMethodObject interface

    master

    The NonPayableMethodObject<Inputs, Outputs> interface represents a contract method that does not require a value transfer (non-payable). It is used to interact with specific functions on a smart contract via the SDK.

    To use this interface, you must provide the expected types for Inputs and Outputs as type parameters. The object contains the arguments required for the method call and provides several utility methods for transaction lifecycle management, such as encoding data, estimating gas, and sending the transaction.

    // Example conceptual usage
    const method: NonPayableMethodObject<[string, number], [boolean]> = {
      arguments: ['some-id', 123],
      // ... other properties and methods
    };
  8. Use the PassThrough stream class

    master

    The PassThrough class is a trivial implementation of a Transform stream that passes input bytes directly to the output without modification. While primarily used for testing and examples, it can serve as a building block for creating custom stream pipelines where you need a transparent bridge between a readable and a writable stream.

    // Example conceptual usage of PassThrough
    import { PassThrough } from 'stream';
    
    const passThrough = new PassThrough();
    // Input written to passThrough will be emitted by its readable side
    passThrough.write('data');
    passThrough.pipe(process.stdout);
  9. Convert Ethereum addresses to IBAN and vice versa using the Iban class

    master

    The Iban class provides utilities to convert between Ethereum addresses and IBAN (International Bank Account Number) or BBAN (Basic Bank Account Number) addresses. It supports both Direct IBANs (length 34 or 35) and Indirect IBANs (length 20).

    Key Capabilities:

    • Address Conversion: Convert an Ethereum address to an IBAN string or derive an Ethereum address from a Direct IBAN.
    • Validation: Check if an IBAN string is valid using checksum verification.
    • Parsing: Extract the institution and client identifiers from an IBAN.
    • Creation: Generate IBANs from BBANs or by combining an institution and identifier.
    // Example: Converting an address to an IBAN
    const ibanString = web3.eth.Iban.toIban("0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8");
    
    // Example: Converting a Direct IBAN to an Ethereum address
    const address = web3.eth.Iban.toAddress("XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS");
  10. Specify amounts in External Orders

    master

    Amounts are derived server-side from the quoted route. You can control them in two ways:

    1. Using slippage (Recommended): Pass a slippage value in basis points (e.g., 50 for 0.5%) to the build method. The server computes the slippage-adjusted amount.
    2. Using limitAmount: Pass an explicit limitAmount. The server uses this as the SELL destAmount (for 'SELL' side) or BUY srcAmount (for 'BUY' side) and as the expectedAmount.
    // Using slippage
    await deltaSDK.buildExternalDeltaOrder({
      route: deltaPrice.route,
      side: deltaPrice.side,
      // ...
      slippage: 50,
    });
    
    // Using explicit limitAmount
    await deltaSDK.buildExternalDeltaOrder({
      route: deltaPrice.route,
      side: deltaPrice.side,
      // ...
      limitAmount: destAmountLimit,
    });
  11. Avoid direct use of BaseTransaction

    master
    The BaseTransaction<TransactionObject> class is an abstract base class and is not recommended for direct use. It is subject to refactoring as new Ethereum transaction types are introduced. Instead, use the specific transaction implementations provided by the SDK that correspond to the transaction type you are handling (e.g., EIP-1559 or EIP-2930).