Git Product home page Git Product logo

agnostic-orderbook's Introduction

Asset agnostic orderbook

UNAUDITED, PLEASE DO NOT USE UNLESS YOU ACCEPT RESPONSIBILITY FOR ADJUSTING TO LARGE AND SUDDEN CHANGES

Orderbook program which can be used with generic assets.

Overview

This program is intended to be called upon by other programs that implement specific on-chain orderbooks. These "caller" program can use the agnostic orderbook as an underlying infrastructure.

Documentation

Run cargo doc --open in the program directory to open detailed API documentation.

FAQ

What does FP32 mean and how does it work?

FP32 stands for Fixed Point at 32 bit number. Floating point numbers are too computationally expensive to manipulate in the Solana runtime. The idea behind FP32 is to store floats as integers with a fixed exponent. For instance, if we want to express the number 2.5 as an FP1 number, we use the integer 5 because 5 = floor(2.5 * (2 ^ 1)). FP32 numbers work in the same way : if we want to express a number n, we use the integer floor(n * (2 ** 32)) instead.

We use powers of two as constants because multiplications and divisions by powers of two are extremely easy and efficient in binary representation, for the same reason that multiplications and divisions by 10 are easy in decimal representation. The operation used to multiply by a power of two is called a bitshift, because that's exactly what is happening : bits are shifted to the right to divide by 2, and to the left to multiply by 2 :

  • Multiplication by 2 : n * (2**k) = n << k
  • Integer division by 2 : n / (2**k) = n >> k

Let's take a look at how basic math operations work on FP32 number :

Operation Int FP32
Addition a + b a + b
Substraction a - b a - b
Multiplication a * b (a * b) >> 32
Division a / b (a << 32) / b

In practice, the multiplication and division formulae have one issue : the a * b and a << 32 operations have a high chance of overflowing their u64 integer representations. To eliminate this problem, we always cast a and b to an integer type with a higher bitdepth :

  • Multiplication : (((a as u128) * (b as u128)) >> 32) as u64
  • Division : (((a as u128) << 32) / (b as u128)) as u64

Utility function for FP32 operations can be found here.

agnostic-orderbook's People

Contributors

ellttben avatar dr497 avatar lcchy avatar jarry-xiao avatar mi-yu avatar yuhanfang avatar jhlx 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.