PoolPlayground

Git Source

Author: EridianAlpha

An interactive educational playground for visualizing and learning Uniswap V2 mechanics by swapping testnet ERC20 tokens.

State Variables

TOKEN_DECIMALS

uint256 public constant TOKEN_DECIMALS = 10 ** 18;

MARKET_PRICE_USD

TokenAmounts public MARKET_PRICE_USD = TokenAmounts({diamond: 100, wood: 20, stone: 2});

s_userTokens

mapping(address => TokenAddresses) internal s_userTokens;

s_userInitialTokenBalances

mapping(address => TokenAmounts) internal s_userInitialTokenBalances;

s_contractAddresses

mapping(string => address) internal s_contractAddresses;

Functions

constructor

Constructor to set the Uniswap contract addresses for the network.

constructor(ContractAddress[] memory _contractAddresses);

Parameters

NameTypeDescription
_contractAddressesContractAddress[]An array of ContractAddress structs.

deploy

Deploy a new playground instance.

Overwrites any existing tokens and pools for the user.

function deploy(TokenAmounts calldata _userTokenAmounts, TokenAmounts[] calldata _poolTokenAmounts) public;

Parameters

NameTypeDescription
_userTokenAmountsTokenAmountsThe amount of tokens to mint for the user.
_poolTokenAmountsTokenAmounts[]The amount of tokens to add to the Uniswap pools.

createTokens

Create tokens.

function createTokens(TokenAmounts memory _mintTokenAmounts) internal returns (TokenAddresses memory tokenAddresses);

Parameters

NameTypeDescription
_mintTokenAmountsTokenAmountsThe total amount of tokens to mint.

createUniswapV2Pools

Create UniswapV2 pools.

function createUniswapV2Pools(TokenAddresses memory _tokenAddresses, TokenAmounts[] memory _poolTokenAmounts)
    internal;

Parameters

NameTypeDescription
_tokenAddressesTokenAddressesThe token addresses for the user.
_poolTokenAmountsTokenAmounts[]The amount of tokens to add to the Uniswap pools.

createPairAndAddLiquidity

Create UniSwapV2 pair and add liquidity.

function createPairAndAddLiquidity(
    IUniswapV2Router02 uniswapV2Router,
    address tokenA,
    address tokenB,
    uint256 amountA,
    uint256 amountB
) internal;

Parameters

NameTypeDescription
uniswapV2RouterIUniswapV2Router02
tokenAaddressThe address of the first token.
tokenBaddressThe address of the second token.
amountAuint256The amount of tokenA to add to the pool.
amountBuint256The amount of tokenB to add to the pool.

sendRemainingTokens

Send remaining tokens to the user.

function sendRemainingTokens(TokenAddresses memory _tokenAddresses) internal;

Parameters

NameTypeDescription
_tokenAddressesTokenAddressesThe token addresses for the user.

getContractAddress

Get the contract address for a given identifier.

function getContractAddress(string memory _identifier) public view returns (address);

Parameters

NameTypeDescription
_identifierstringThe identifier of the contract.

getUserTokens

Get the deployed tokens for a user.

function getUserTokens(address _user) public view returns (TokenAddresses memory);

Parameters

NameTypeDescription
_useraddressThe address of the user.

getUserTokenBalances

Get the all the token balances for a user.

function getUserTokenBalances(address _user) public view returns (TokenAmounts memory userTokenBalances);

Parameters

NameTypeDescription
_useraddressThe address of the user.

Returns

NameTypeDescription
userTokenBalancesTokenAmountsThe token balances for the user.

getUserInitialTokenBalances

Get the initial token balances for a user.

function getUserInitialTokenBalances(address _user)
    public
    view
    returns (TokenAmounts memory userInitialTokenBalances);

Parameters

NameTypeDescription
_useraddressThe address of the user.

Returns

NameTypeDescription
userInitialTokenBalancesTokenAmountsThe initial token balances for the user.

Events

TokensAndPoolsCreated

event TokensAndPoolsCreated(address indexed user);

Structs

ContractAddress

struct ContractAddress {
    string identifier;
    address contractAddress;
}

TokenAddresses

struct TokenAddresses {
    address diamond;
    address wood;
    address stone;
}

TokenAmounts

struct TokenAmounts {
    uint256 diamond;
    uint256 wood;
    uint256 stone;
}