Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #194

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9ddd26c
Ban user (#182)
Ronfflex Jul 22, 2024
a3f20da
Unban (#183)
Ronfflex Jul 22, 2024
2580cd7
Merge branch 'develop' of https://github.com/MTthoas/Dex into develop
MTthoas Jul 22, 2024
b92322d
merge wagmi-config
MTthoas Jul 22, 2024
8ec78ff
Merge pull request #185 from MTthoas/transactions-hook
MTthoas Jul 22, 2024
935dc58
hotfix
Jul 22, 2024
bac90a2
feat: Contract Management
MTthoas Jul 22, 2024
dedd97e
add staking factory
Guiiz94 Jul 23, 2024
f58dd98
merge with develop
Guiiz94 Jul 23, 2024
2482142
resolve conflict in wagmi abi
Guiiz94 Jul 23, 2024
dfd01a4
Merge pull request #186 from MTthoas/merge/StakingFactory
Guiiz94 Jul 23, 2024
a05ecf3
feat: tokens
MTthoas Jul 23, 2024
095b680
Merge pull request #187 from MTthoas/tokens
MTthoas Jul 23, 2024
37b594e
add admin factory
Guiiz94 Jul 23, 2024
8278fd2
Merge pull request #188 from MTthoas/admin/StakingFactory
Guiiz94 Jul 23, 2024
a6132b1
Users table (#189)
Ronfflex Jul 23, 2024
f3feee7
feat: tokens page
MTthoas Jul 23, 2024
074e4fe
Merge branch 'develop' of https://github.com/MTthoas/Dex into develop
MTthoas Jul 23, 2024
28f643d
Merge pull request #190 from MTthoas/tokenPage
MTthoas Jul 23, 2024
0b25281
Merge branch 'develop' of https://github.com/MTthoas/Dex into develop
MTthoas Jul 23, 2024
aa79779
fix in front
Guiiz94 Jul 23, 2024
39a1ecc
Merge pull request #191 from MTthoas/fix/StakingFactory
Guiiz94 Jul 23, 2024
0e1c566
feat: transactions and beautify stake page
MTthoas Jul 23, 2024
69f3749
merge develop
MTthoas Jul 23, 2024
8fbe262
resolve conflict
MTthoas Jul 23, 2024
722183e
Admin access (#192)
Ronfflex Jul 23, 2024
1e0cd1c
feat: graph
MTthoas Jul 23, 2024
f49f8ce
Merge branch 'develop' of https://github.com/MTthoas/Dex into develop
MTthoas Jul 23, 2024
bb5ada3
fix: add and delete
MTthoas Jul 23, 2024
90a3c00
fix: color
MTthoas Jul 23, 2024
5c4888a
feat: globe and graph data
MTthoas Jul 23, 2024
f6f77e5
User register (#193)
Ronfflex Jul 23, 2024
7aed78f
Adding natspec comment on sc
Jul 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions api/controllers/token.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"net/http"
"strconv"

"github.com/MTthoas/dex/api/models"
"github.com/MTthoas/dex/api/platform/database"
Expand All @@ -15,7 +16,7 @@ import (
// @Tags Tokens
// @Accept json
// @Produce json
// @Success 200 {object} []models.Token
// @Success 200 {object} []models.Coin
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/tokens/ethereum [get]
func GetEthereumTokensController(c *fiber.Ctx) error {
Expand Down Expand Up @@ -76,6 +77,22 @@ func CreateTokenController(c *fiber.Ctx) error {
})
}

// Verify if the token already exists with the same address
existingTokenByAddress, err := db.TokenQueries.GetTokenByAddress(token.Address)
if err == nil && existingTokenByAddress.Id != 0 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Token with this address already exists",
})
}

// Verify if the token already exists with the same symbol
existingTokenBySymbol, err := db.TokenQueries.GetTokenBySymbol(token.Symbol)
if err == nil && existingTokenBySymbol.Id != 0 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Token with this symbol already exists",
})
}

createdToken, err := db.TokenQueries.CreateToken(*token)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
Expand Down Expand Up @@ -108,14 +125,23 @@ func UpdateTokenController(c *fiber.Ctx) error {
}

id := c.Params("id")
uintID, err := strconv.ParseUint(id, 10, 32)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid ID format",
})
}

token := new(models.Token)
if err := c.BodyParser(token); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Failed to parse request body",
})
}

updatedToken, err := db.TokenQueries.GetTokenById(id)
token.Id = uint(uintID) // Set the ID of the token to be updated

updatedToken, err := db.TokenQueries.UpdateToken(*token)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
Expand All @@ -125,7 +151,6 @@ func UpdateTokenController(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"data": updatedToken,
})

}

// DeleteToken function to delete a token
Expand Down
27 changes: 27 additions & 0 deletions api/controllers/user.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ func GetUserByAddress(c *fiber.Ctx) error {
})
}

// GetUsersBanned function to get all banned users
// @Summary Get all banned users
// @Description Retrieves a list of all banned users in the database.
// @Tags User
// @Accept json
// @Produce json
// @Success 200 {object} models.User
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/users/banned [get]
func GetUsersBanned(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
users, err := db.UserQueries.GetAllUsersBanned()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"data": users,
})
}

// CreateUser function to create a new user
// @Summary Create a new user
// @Description Creates a new user in the database.
Expand Down
2 changes: 1 addition & 1 deletion api/models/base.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type Base struct {
Id uint `json:"id" gorm:"unique;primaryKey;autoIncrement"`
Id uint `json:"id" gorm:"unique;primaryKey;autoIncrement"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime" example:"2021-08-01T00:00:00Z"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime" example:"2021-08-01T00:00:00Z"`
}
18 changes: 18 additions & 0 deletions api/models/coins.model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package models

type Coin struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
Image string `json:"image"`
CurrentPrice float64 `json:"current_price"`
MarketCap float64 `json:"market_cap"`
MarketCapRank int `json:"market_cap_rank"`
TotalVolume float64 `json:"total_volume"`
PriceChange24h float64 `json:"price_change_24h"`
PriceChangePercentage24h float64 `json:"price_change_percentage_24h"`
MarketCapChange24h float64 `json:"market_cap_change_24h"`
MarketCapChangePercentage float64 `json:"market_cap_change_percentage_24h"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
LastUpdated string `json:"last_updated"`
}
2 changes: 2 additions & 0 deletions api/models/transaction.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const (
Send EnumTransactionType = "send"
Swap EnumTransactionType = "swap"
Stack EnumTransactionType = "stack"
Unstack EnumTransactionType = "unstack"
Claim EnumTransactionType = "claim"
AddLiquidity EnumTransactionType = "add_liquidity"
RemoveLiquidity EnumTransactionType = "remove_liquidity"
)
Expand Down
1 change: 1 addition & 0 deletions api/models/user.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type User struct {
Base
Name string `json:"name" gorm:"not null" example:"John Doe"`
Address string `json:"address" gorm:"unique" example:"0x123abc" validate:"required"`
Banned string `json:"banned" gorm:"default:false" example:"false" validate:"required"`
}

func (user *User) TableName() string {
Expand Down
34 changes: 30 additions & 4 deletions api/queries/token.query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type TokenQueries struct {
DB *gorm.DB
}

func GetEthereumTokens() []models.Token {
func GetEthereumTokens() []models.Coin {
apiUrl := "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&category=ethereum-ecosystem"

req, err := http.NewRequest("GET", apiUrl, nil)
Expand Down Expand Up @@ -47,7 +47,9 @@ func GetEthereumTokens() []models.Token {
return nil
}

var tokens []models.Token
// print body

var tokens []models.Coin
err = json.Unmarshal(body, &tokens)
if err != nil {
log.Printf("Erreur lors du décodage du JSON : %v", err)
Expand Down Expand Up @@ -113,10 +115,34 @@ func (tq *TokenQueries) CreateToken(token models.Token) (models.Token, error) {
}

func (tq *TokenQueries) UpdateToken(token models.Token) (models.Token, error) {
if err := tq.DB.Save(&token).Error; err != nil {
var existingToken models.Token
if err := tq.DB.First(&existingToken, token.Id).Error; err != nil {
return models.Token{}, err // Return an error if the token doesn't exist
}

// Update the fields
existingToken.Name = token.Name
existingToken.Symbol = token.Symbol
existingToken.Image = token.Image
existingToken.Address = token.Address
existingToken.CurrentPrice = token.CurrentPrice
existingToken.MarketCap = token.MarketCap
existingToken.MarketCapRank = token.MarketCapRank
existingToken.TotalVolume = token.TotalVolume
existingToken.PriceChange24h = token.PriceChange24h
existingToken.PriceChangePercentage24h = token.PriceChangePercentage24h
existingToken.MarketCapChange24h = token.MarketCapChange24h
existingToken.MarketCapChangePercentage = token.MarketCapChangePercentage
existingToken.TotalSupply = token.TotalSupply
existingToken.MaxSupply = token.MaxSupply

// print the updated token
fmt.Println(existingToken)

if err := tq.DB.Save(&existingToken).Error; err != nil {
return models.Token{}, err
}
return token, nil
return existingToken, nil
}

func (tq *TokenQueries) DeleteToken(id string) error {
Expand Down
8 changes: 8 additions & 0 deletions api/queries/user.query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (uq *UserQueries) GetUserByAddress(address string) (models.User, error) {
return user, nil
}

func (uq *UserQueries) GetAllUsersBanned() ([]models.User, error) {
var users []models.User
if err := uq.DB.Where("banned = ?", "true").Find(&users).Error; err != nil {
return []models.User{}, err
}
return users, nil
}

func (uq *UserQueries) CreateUser(user models.User) (models.User, error) {
if err := uq.DB.Create(&user).Error; err != nil {
return models.User{}, err
Expand Down
4 changes: 2 additions & 2 deletions api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func PublicRoutes(a *fiber.App) {

route.Get("/healthCheck", controllers.HealthCheck)

// Routes for GET method:
route.Get("/tokens", controllers.GetTokensController)
route.Get("/tokens/ethereum", controllers.GetEthereumTokensController)
route.Get("/tokens/ethereum/:addresses", controllers.GetEthereumTokenByAddressController)
Expand All @@ -26,8 +25,9 @@ func PublicRoutes(a *fiber.App) {
route.Get("/users", controllers.GetUsers)
route.Get("/users/:id", controllers.GetUsers)
route.Get("/users/address/:address", controllers.GetUserByAddress)
route.Get("/users/banned", controllers.GetUsersBanned)
route.Post("/users", controllers.CreateUser)
route.Put("/users", controllers.UpdateUser)
route.Put("/users/:id", controllers.UpdateUser)
route.Delete("/users/:id", controllers.DeleteUser)

route.Get("/transactions", controllers.GetTransactions)
Expand Down
1 change: 0 additions & 1 deletion contracts/lib/openzeppelin-contracts-upgradeable
2 changes: 1 addition & 1 deletion contracts/script/deploy_erc20.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract deployERC20 is Script {

function run() external {
vm.startBroadcast(admin);
Token tokenA = new Token("Dorian", "DONX", 24000000 * 10 ** 18);
Token tokenA = new Token("Helder", "PORTO", 24000000 * 10 ** 18);
vm.stopBroadcast();
}
}
8 changes: 4 additions & 4 deletions contracts/script/test/Swap.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pragma solidity ^0.8.0;

import "forge-std/Script.sol";
import "../src/token/Token.sol";
import "../src/token/LiquidityPoolToken.sol";
import "../src/LiquidityPool.sol";
import "../src/LiquidityPoolFactory.sol";
import "../../src/token/Token.sol";
import "../../src/token/LiquidityPoolToken.sol";
import "../../src/LiquidityPool.sol";
import "../../src/LiquidityPoolFactory.sol";

contract SwapTest is Script {
Token tokenA;
Expand Down
2 changes: 1 addition & 1 deletion contracts/script/test/claimRewards.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contract LiquidityPoolTest is Script {

// Remove liquidity
uint256 liquidityToRemove = liquidityToken.balanceOf(user) / 2;
liquidityPool.removeLiquidity(liquidityToRemove);
liquidityPool.removeLiquidity(liquidityToRemove, liquidityToRemove);

// Get reserves after removing liquidity
(reserveA, reserveB) = liquidityPool.getReserves();
Expand Down
Loading