@interest/constant-product

Version 2 - mainnet - 0xe6...c4d5e3

Published by@interest · 9 packages

Readme

Versions

2

Dependencies

0

Dependents

0

Analytics

On the way

Interest Constant Product

Constant Product is a library to calculate the constant product invariant for automated market makers (AMMs).

Overview

The package provides core mathematical functions for implementing constant product AMMs, following the x * y = k formula. It includes functions for:

  • Calculating the constant product (k)
  • Computing input amounts based on desired output
  • Computing output amounts based on input

These functions are essential for implementing decentralized exchanges and liquidity pools that use the constant product formula.

Modules

  • constant_product: Contains the core mathematical functions for constant product calculations
  • errors: Contains all error codes thrown by the package
  • constant_product_tests: Contains the tests for the constant_product.

Installing

# testnet
mvr add @interest/constant-product --network testnet

# mainnet
mvr add @interest/constant-product --network mainnet

Manual

To add this library to your project, add this to your Move.toml.

# goes into [dependencies] section
interest_constant_product = { r.mvr = "@interest/constant-product" }

# add this section to your Move.toml
[r.mvr]
network = "mainnet"

Package Ids

The package is deployed on Sui Network mainnet at: 0x32c8ce857be1e93ebff6d6b460868ad450d7f18f0e2769c122c49d0f632e5f9b

The package is deployed on Sui Network testnet at: 0x9c142049e44dbfebeb0e75199ed6b81dcfb629fb4044b85d1711d0511e1c17ee

How to use

In your code, import and use the package as:

module my::awesome_project;

use interest_constant_product::constant_product;

public fun calculate_k(x: u64, y: u64): u128 {
    constant_product::k!(x, y)
}

public fun get_input_amount(amount_out: u64, reserve_in: u64, reserve_out: u64): u64 {
    constant_product::get_amount_in!(amount_out, reserve_in, reserve_out)
}

public fun get_output_amount(amount_in: u64, reserve_in: u64, reserve_out: u64): u64 {
    constant_product::get_amount_out!(amount_in, reserve_in, reserve_out)
}

API Reference

k: Calculates the constant product (k) for two reserves.

public macro fun k(x: u64, y: u64): u128

get_amount_in: Calculates the required input amount for a desired output amount, given the current reserves.

public macro fun get_amount_in(coin_out_amount: u64, balance_in: u64, balance_out: u64): u64

get_amount_out: Calculates the output amount for a given input amount, given the current reserves.

public macro fun get_amount_out(coin_in_amount: u64, balance_in: u64, balance_out: u64): u64

Errors

Errors are encoded in u64.

Error codeReason
0Insufficient liquidity in the pool or one of the reserves is equal to zero.
1Attempted to calculate an output from a zero value input

Changelog

Commit - 8d2da8a1c9ed2682e3f5a18dc8959879607db50b

Deploys version 2 of the Constant product package to fix a critical error due to visibility of the error macro functions.

Disclaimer

This is provided on an "as is" and "as available" basis.

We do not give any warranties and will not be liable for any loss incurred through any use of this codebase.

While Interest Constant Product has been tested, there may be parts that may exhibit unexpected emergent behavior when used with other code, or may break in future Move versions.

Please always include your own thorough tests when using Interest Constant Product to make sure it works correctly with your code.

License

This package is licensed under Apache-2.0.

Install

You can install this package in your Move project by calling

mvr add @interest/constant-product
Interoperating Transactions

Description

Constant Product is a library to calculate the constant product invariant for automated market makers (AMMs). k = x * y

Contact

jose@interestprotocol.com