LFJ Developer Docs
  • Liquidity Book
  • Introduction
  • LB V2.2 Key Changes
  • Guides
    • Swap Tokens
    • Add/Remove Liquidity
    • Tracking Volume
    • Tracking Pool Balances
    • Finding The Best Quote
    • Byte32 Decoding
    • Price From Bin Id
    • Bin Id From Price
    • Finding Liquidity Depth
    • User Balances
  • Concepts
    • Concentrated Liquidity
    • Bin Math
    • Bin Liquidity
    • Swaps
    • Fees
    • Oracle
  • Contracts
    • Interfaces
      • ILBLegacyFactory
      • ILBLegacyToken
      • ILBLegacyPair
      • ILBLegacyRouter
      • ILBFlashLoanCallback
      • IPendingOwnable
      • IJoeFactory
      • IJoePair
      • IJoeRouter01
      • IJoeRouter02
      • IWNATIVE
      • ILBFactory
      • ILBHooks
      • ILBPair
      • ILBRouter
      • ILBToken
    • Libraries
      • Math
        • BitMath
        • Encoded
        • LiquidityConfigurations
        • PackedUint128Math
        • SafeCast
        • SampleMath
        • TreeMath
        • Uint128x128Math
        • Uint256x256Math
      • BinHelper
      • Clone
      • Constants
      • FeeHelper
      • Hooks
      • ImmutableClone
      • JoeLibrary
      • OracleHelper
      • PairParameterHelper
      • PriceHelper
      • ReentrancyGuardUpgradeable
      • TokenHelper
    • LBBaseHooks
    • LBFactory
    • LBPair
    • LBQuoter
    • LBRouter
    • LBToken
  • Deployment Addresses
    • Avalanche C-Chain
    • Fuji Testnet
    • Arbitrum One
    • Binance Smart Chain
    • Binance Smart Chain Testnet
    • Ethereum Mainnet
    • Monad Testnet
  • SDK
    • Introduction
    • Making a Trade
    • Adding Liquidity
    • Removing Liquidity
  • Audits
  • AMM
    • Joe V1 Contracts
    • Joe V1 Audits
  • LFJ DEX API
    • Dex Analytics
    • Pools
    • Rewards
    • User
    • User Lifetime Stats
    • Vaults
    • Models
  • LFJ Aggregator API
    • Default
    • Models
Powered by GitBook
On this page
  1. Guides

Byte32 Decoding

In v2.1 we introduced byte32 encoding for function and event variables (to save storage and gas costs). We provide examples to decode them.

Many parameters (like amountsIn, totalFees in Swap event) are packed with the amounts in for both tokens X and Y and encoded into bytes32. Below is an example of how to decode them.

function decodeAmounts(amounts: Bytes): [bigint, bigint] {
  /**
   * Decodes the amounts bytes input as 2 integers.
   *
   * @param amounts - amounts to decode.
   * @return tuple of BigInts with the values decoded.
   */

  // Convert amounts to a BigInt
  const amountsBigInt = BigInt(
    `0x${Buffer.from(amounts, "hex").toString("hex")}`
  );

  // Read the right 128 bits of the 256 bits
  const amountsX = amountsBigInt & (BigInt(2) ** BigInt(128) - BigInt(1));

  // Read the left 128 bits of the 256 bits
  const amountsY = amountsBigInt >> BigInt(128);

  return [amountsX, amountsY];
}
def decodeAmounts(amounts: bytes) -> tuple:
    """
    Decodes the amounts bytes input as 2 integers.

    :param amounts: amounts to decode.
    :return: tuple of ints with the values decoded.
    """

    amounts = bytes.fromhex(amounts)

    # Read the right 128 bits of the 256 bits
    amountsX = int.from_bytes(random_bytes, byteorder="big") & (2 ** 128 - 1)

    # Read the left 128 bits of the 256 bits
    amountsY = int.from_bytes(random_bytes, byteorder="big") >> 128

    return (amountsX, amountsY)
SELECT
  TX_HASH,
  BLOCK_TIMESTAMP,
  ethereum.public.udf_hex_to_int (SUBSTR(SUBSTR(DATA, 3 + 2 * 64, 64), 33, 32)) :: integer AS AMOUNT_X_OUT,
  ethereum.public.udf_hex_to_int (SUBSTR(SUBSTR(DATA, 3 + 2 * 64, 64), 1, 32)) :: integer as AMOUNT_Y_OUT,
FROM
  avalanche.core.fact_event_logs
WHERE
  TOPICS [0] = '0xad7d6f97abf51ce18e17a38f4d70e975be9c0708474987bb3e26ad21bd93ca70'
  AND CONTRACT_ADDRESS = LOWER('0xD446eb1660F766d533BeCeEf890Df7A69d26f7d1')
LIMIT
  100
SELECT
  tx_hash,
  block_time,
  bytearray_to_int256(bytearray_substring (DATA, 1 + 2 * 32 + 16, 16)) as AMOUNT_X_OUT,
  bytearray_to_int256(bytearray_substring (DATA, 1 + 2 * 32, 16)) as AMOUNT_Y_OUT
FROM
  avalanche_c.logs
WHERE
  topic0 = 0xad7d6f97abf51ce18e17a38f4d70e975be9c0708474987bb3e26ad21bd93ca70
  and contract_address = 0xD446eb1660F766d533BeCeEf890Df7A69d26f7d1
LIMIT
  100
PreviousFinding The Best QuoteNextPrice From Bin Id

Last updated 9 days ago