Swaps

Introduction

Swapping is a kind of process in which, A user picks an input token and get an output token. They specify an input amount, and the Ballswapper protocol automatically calculates how much of the output tokens user will receive. They then execute the swap by clicking the “Swap” button, and immediately user receives the output token in the wallet.

Swapping the ERC-20 Token is quite easy in Ballswapper Protocol.

Ballswapper protocol is a decentralized trading platform. Ballswapper does not use an order book to represent liquidity or determine prices. Ballswapper uses an automated market maker mechanism to provide instant feedback on rates and slippage.

In this guide, we’ll look at what happens during a swap at the Ballswapper protocol level to gain a deeper understanding of how Ballswapper works.

This rule is the constant product formula. When either token is withdrawn (purchased), a proportional amount of the other must be deposited (sold), to maintain the constant.

As we learned in Protocol Overview, each pair on Ballswapper is actually underpinned by a liquidity pool. Smart contracts are used to maintain the Liquidity Pools of different Tokens.

Anatomy of a swap

In Ballswapper, all the Swaps are get done by calling the single function of swap:

function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data);

Receiving tokens

As is probably clear from the function signature, Ballswapper requires swap callers to specify how many output tokens they would like to receive via the amount{0,1}Out parameters, which correspond to the desired amount of token{0,1}.

Sending Tokens

What’s not as clear is how Ballswapper receives tokens as payment for the swap. Typically, smart contracts which need tokens to perform some functionality require callers to first make an approval on the token contract, then call a function that in turn calls transferFrom on the token contract. This is not how pairs accept tokens. Instead, pairs check their token balances at the end of every interaction. Then, at the beginning of the next interaction, current balances are differenced against the stored values to determine the amount of tokens that were sent by the current interactor. See the whitepaper for a justification of why this is the case.

Developer resources