Peripheral EOSIO smart contracts for interacting with Uniswap V2
#include "uniswap.hpp"
// Inputs
const uint64_t amount_in = 10000;
const uint64_t reserve_in = 45851931234;
const uint64_t reserve_out = 125682033533;
const uint8_t fee = 30;
// Calculation
const uint64_t out = uniswap::get_amount_out( amount_in, reserve_in, reserve_out, fee );
// => 27328
function get_amount_out( amount_in, reserve_in, reserve_out, fee ) {
amount_in_with_fee = amount_in * (10000 - fee);
numerator = amount_in_with_fee * reserve_out;
denominator = reserve_in * 10000 + amount_in_with_fee;
amount_out = numerator / denominator;
return amount_out;
}
Given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
{uint64_t} amount_in
- amount input{uint64_t} reserve_in
- reserve input{uint64_t} reserve_out
- reserve output{uint8_t} [fee=30]
- (optional) trade fee (pips 1/100 of 1%)
// Inputs
const uint64_t amount_in = 10000;
const uint64_t reserve_in = 45851931234;
const uint64_t reserve_out = 125682033533;
const uint8_t fee = 30;
// Calculation
const uint64_t amount_out = uniswap::get_amount_out( amount_in, reserve_in, reserve_out, fee );
// => 27328
Given an output amount of an asset and pair reserves, returns a required input amount of the other asset.
{uint64_t} amount_in
- amount input{uint64_t} reserve_in
- reserve input{uint64_t} reserve_out
- reserve output{uint8_t} [fee=30]
- (optional) trade fee (pips 1/100 of 1%)
// Inputs
const uint64_t amount_out = 27328;
const uint64_t reserve_in = 45851931234;
const uint64_t reserve_out = 125682033533;
const uint8_t fee = 30;
// Calculation
const uint64_t amount_in = uniswap::get_amount_in( amount_out, reserve_in, reserve_out, fee );
// => 10000
Given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
{uint64_t} amount_a
- amount A{uint64_t} reserve_a
- reserve A{uint64_t} reserve_b
- reserve B
// Inputs
const uint64_t amount_a = 10000;
const uint64_t reserve_a = 45851931234;
const uint64_t reserve_b = 125682033533;
// Calculation
const uint64_t amountB = uniswap::quote( amount_a, reserve_a, reserve_b );
// => 27410