PoolPlayground
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
Name | Type | Description |
---|---|---|
_contractAddresses | ContractAddress[] | 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
Name | Type | Description |
---|---|---|
_userTokenAmounts | TokenAmounts | The amount of tokens to mint for the user. |
_poolTokenAmounts | TokenAmounts[] | The amount of tokens to add to the Uniswap pools. |
createTokens
Create tokens.
function createTokens(TokenAmounts memory _mintTokenAmounts) internal returns (TokenAddresses memory tokenAddresses);
Parameters
Name | Type | Description |
---|---|---|
_mintTokenAmounts | TokenAmounts | The total amount of tokens to mint. |
createUniswapV2Pools
Create UniswapV2 pools.
function createUniswapV2Pools(TokenAddresses memory _tokenAddresses, TokenAmounts[] memory _poolTokenAmounts)
internal;
Parameters
Name | Type | Description |
---|---|---|
_tokenAddresses | TokenAddresses | The token addresses for the user. |
_poolTokenAmounts | TokenAmounts[] | 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
Name | Type | Description |
---|---|---|
uniswapV2Router | IUniswapV2Router02 | |
tokenA | address | The address of the first token. |
tokenB | address | The address of the second token. |
amountA | uint256 | The amount of tokenA to add to the pool. |
amountB | uint256 | The amount of tokenB to add to the pool. |
sendRemainingTokens
Send remaining tokens to the user.
function sendRemainingTokens(TokenAddresses memory _tokenAddresses) internal;
Parameters
Name | Type | Description |
---|---|---|
_tokenAddresses | TokenAddresses | The token addresses for the user. |
getContractAddress
Get the contract address for a given identifier.
function getContractAddress(string memory _identifier) public view returns (address);
Parameters
Name | Type | Description |
---|---|---|
_identifier | string | The identifier of the contract. |
getUserTokens
Get the deployed tokens for a user.
function getUserTokens(address _user) public view returns (TokenAddresses memory);
Parameters
Name | Type | Description |
---|---|---|
_user | address | The 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
Name | Type | Description |
---|---|---|
_user | address | The address of the user. |
Returns
Name | Type | Description |
---|---|---|
userTokenBalances | TokenAmounts | The 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
Name | Type | Description |
---|---|---|
_user | address | The address of the user. |
Returns
Name | Type | Description |
---|---|---|
userInitialTokenBalances | TokenAmounts | The 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;
}