A Javascript library for interacting with the HumbleSwap DEx.
Methods are listed below, along with usage examples where possible.
Home Page
Swapping
Swapping utility functions.
calculatePriceImpact
function calculatePriceImpact(amtA: string | number, opts: SwapTxnOpts): string | 0;
Compares the size of expected swap output to the size of an “ideal” output.
Price impact computes
- the output if the pool was (hypothetically) “perfectly balanced”
- the expected output of a swap with the pool’s current state
and compares the two.
So e.g. if calculatePriceImpact(x, { ... })
returns 20
, it means that trade
will result in 20% less value than if the pool was balanced by (e.g.) more liquidity.
calculatePriceImpact Example
See Perform Swap for usage.
calculatePriceImpact Parameters
amtA: string | number
input amount (can represent amount for tokenA
orB
)opts: SwapTxnOpts
calculatePriceImpact Returns
string
Price impact value as a string. Value is a percentage (so e.g.5
===5%
)
calculateTokenSwap
function calculateTokenSwap(opts: SwapTxnOpts): SwapInfo
Calculates how much you should expect from a swap, based on the amount put in and the state of the pool.
calculateTokenSwap Example
See Perform Swap for usage.
calculateTokenSwap Parameters
calculateTokenSwap Returns
SwapInfo
An object with your input amount and expected outputs. You can use this object to perform a swap transaction.
performSwap
Renamed to Swap Tokens
swapTokens
function swapTokens(acct: ReachAccount, opts: SwapTxnOpts): Promise<TransactionResult>
Swap one token in a Liquidity Pool for another.
swapTokens Example: Swap
See fetchLiquidityPool
or subscribeToPoolStream
for pool sources.
const pool = /* pool source */
// Calculate expected swap output
const { tokenAId, tokenBId } = pool;
const amountA = 100
const swap = calculateTokenSwap({
pool,
swap: { amountA, tokenAId, tokenBId }
});
// Perform swap
const swapOpts = { poolAddress: pool.poolAddress, swap, pool };
const { data, message, succeeded } = await swapTokens(acc, swapOpts);
// if (succeeded) data == { amountIn: string; amountOut: string }
swapTokens Example: Price Impact
(Optional) Calculate price impact to ensure the best price. Price impact is inversely proportional to expected swap output. In other words, the higher the price impact, the smaller the swap output.
💡 Why would I need this?
Because, simply,Higher impact === lower trade quality.
A very high value returned bycalculatePriceImpact
might be because of low pool liquidity. The HumbleSwap UI flags trades that with a >5% trade impact.
This is only included for illustration. Price impact calculation is separate from swapping, and doesn’t affect it unless you make it do so.
const pool = /* pool source */
const { tokenAId, tokenBId } = pool;
const amountA = 100
const calcOpts = { pool, swap: { amountA, tokenAId, tokenBId } };
const impact = calculatePriceImpact(amountA, calcOpts);
console.log(impact); // "3.04"
swapTokens Parameters
acc: ReachAccount
: reach account abstractionopts: SwapTxnOpts
swapTokens Returns
TransactionResult
wheredata
={ amountIn: string; amountOut: string }
when successful