DEV Community

ASHDEEP SINGH
ASHDEEP SINGH

Posted on

Token Marketplace

This week I learnt about token marketplace and created a basic token marketplace where users can buy and sell GLD tokens. The contract is designed to adjust the token price based on demand automatically and allows the owner to withdraw tokens or Ether accumulated in the contract.

Here is the github code :
Github Code

Key Components
Libraries and Inheritance:
SafeERC20: A library from OpenZeppelin that provides safe methods for interacting with ERC20 tokens, preventing common issues such as failing transactions.
SafeMath: Another library from OpenZeppelin, which prevents overflow and underflow in arithmetic operations (like addition, subtraction, multiplication).
Ownable: A contract module from OpenZeppelin that provides basic access control, where there's an owner (typically the deployer of the contract) who can perform specific functions.

State Variables:
tokenPrice: The current price of the GLD token in wei (smallest unit of Ether). It starts at 2e16 wei, which equals 0.02 Ether per token.
sellerCount and buyerCount: Counters tracking the number of sellers and buyers. These values are used to adjust the token price dynamically.
gldToken: The address of the GLD token contract that will be bought and sold in this marketplace.

Events:
Various events like TokenPriceUpdated, TokenBought, TokenSold, TokensWithdrawn, EtherWithdrawn, and CalculateTokenPrice are emitted at different stages of the contract's operations to provide transparency and allow external observers to track actions.

Functions

Constructor:
The constructor initializes the contract by setting the gldToken address and calling the Ownable constructor to set the owner of the contract.

adjustTokenPriceBasedOnDemand:
This function recalculates the price of the GLD token based on market demand. The price adjusts by comparing the number of buyers to sellers, with a smoothing factor to avoid sudden price jumps.
If the new calculated price is below a minimum price (0.02 Ether), it resets to the minimum price.

buyGLDToken:
Allows users to buy GLD tokens by sending Ether to the contract. The function checks that the user is sending the correct amount of Ether required for the number of tokens they wish to purchase. It then transfers the tokens to the buyer and increments the buyerCount.

calculateTokenPrice:
Calculates the total cost of buying a certain number of GLD tokens based on the current tokenPrice.
This function also calls adjustTokenPriceBasedOnDemand to ensure the price is always up to date.

sellGLDToken:
This function allows users to sell their GLD tokens back to the marketplace. It first checks that the user has enough tokens to sell and then calculates the total amount of Ether they will receive based on the current tokenPrice.
Note: This function was incomplete in your code, and it should ideally transfer the appropriate amount of Ether to the seller after transferring the tokens to the contract. It also needs to update sellerCount.

withdrawTokens:
Allows the owner to withdraw any excess GLD tokens that are held in the contract.

withdrawEther:
Allows the owner to withdraw Ether that has accumulated in the contract.

My 2 Cents
Making such a contract can be easy once we know how market works. We use the buyer-seller ratio to know if our token is in demand or not (if ratio is > 1, it means more people are buying it and less are selling it thus making it look like a good token).
After this, look at how each function has been implemented (just do what the comment above each function says).
Finally note that all transactions happen via market place, i.e., each person can buy / sell only from / to market place only. Incase any transaction is being carried out note the currency in which it is carried out and even-it-out. For example, if user sends 5 ether to contract calculate how much tokens can he buy for it, if user sells 100 token to market place, know how much ether will the user be elligible for.

That's all for this time folks.........
Stay tuned for more.

Peace.

Top comments (0)