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

feat: transactions for swap/send #185

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
22 changes: 18 additions & 4 deletions api/models/transaction.model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package models

type EnumTransactionType string

const (
Send EnumTransactionType = "send"
Swap EnumTransactionType = "swap"
Stack EnumTransactionType = "stack"
AddLiquidity EnumTransactionType = "add_liquidity"
RemoveLiquidity EnumTransactionType = "remove_liquidity"
)

type Transaction struct {
Base
From string `json:"from" example:"0x123abc"`
To string `json:"to" example:"0x456def"`
Amount float64 `json:"amount" example:"99.99"`
Transaction string `json:"transaction" example:"tx_789ghi"`
From string `json:"from" example:"0x123abc"`
To string `json:"to" example:"0x456def"`
Hash string `json:"hash" example:"0x789ghi"`
AmountA float64 `json:"amount_a" example:"99.99"`
AmountB float64 `json:"amount_b" example:"99.99"`
Type EnumTransactionType `json:"type" example:"send"`
SymbolA *string `json:"symbol_a,omitempty" example:"ETH"`
SymbolB *string `json:"symbol_b,omitempty" example:"USDT"`
}
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
2 changes: 1 addition & 1 deletion api/queries/transaction.query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type TransactionQueries struct {

func (tq *TransactionQueries) GetAllTransactions() ([]models.Transaction, error) {
var transactions []models.Transaction
if err := tq.DB.Find(&transactions).Error; err != nil {
if err := tq.DB.Select("id", "from", "to", "hash", "amount_a", "amount_b", "type", "symbol_a", "symbol_b", "updated_at").Find(&transactions).Error; err != nil {
return nil, err
}
return transactions, nil
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
6 changes: 3 additions & 3 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,12 +25,13 @@ 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)
route.Get("/transaction/:id", controllers.GetTransaction)
route.Get("/transactions/:id", controllers.GetTransaction)
route.Post("/transactions", controllers.CreateTransaction)
route.Put("/transactions", controllers.UpdateTransaction)
route.Delete("/transactions/:id", controllers.DeleteTransaction)
Expand Down
1 change: 0 additions & 1 deletion contracts/lib/openzeppelin-contracts-upgradeable
2 changes: 1 addition & 1 deletion contracts/src/UserRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ contract UserRegistry is ReentrancyGuard, AccessControl {
* @param _name The name of the user.
* @return uint256 The user ID.
*/
function registerUser(string memory _name) public onlyRole(ADMIN_ROLE) nonReentrant returns (uint256) {
function registerUser(string memory _name) public nonReentrant returns (uint256) {
require(users[msg.sender].id == 0, "User already registered");

uint256 userId = registeredUserIds.length() + 1; // TODO: this is not working if we remove a user but can we remove a user ?
Expand Down
37 changes: 36 additions & 1 deletion next-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-switch": "^1.0.3",
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-tooltip": "^1.1.2",
"@rainbow-me/rainbowkit": "^2.0.5",
"@tanstack/react-query": "^5.32.0",
"@tanstack/react-table": "^8.16.0",
Expand All @@ -38,6 +39,7 @@
"next-themes": "^0.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-spring": "^9.7.4",
"sonner": "^1.5.0",
"tailwind-merge": "^2.4.0",
"tailwindcss-animate": "^1.0.7",
Expand All @@ -58,4 +60,4 @@
"volta": {
"node": "20.14.0"
}
}
}
4 changes: 2 additions & 2 deletions next-app/src/abi/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export const MaxeAddress = "0x583e5178113ab52d556e05a034cb68e0c3070865";
export const StakingAddress = "0x937cD4Aa88445B27f8eBe6d35A760a4aF18aE682";
export const StakingFactoryAddress = "0xEfb5b5FDfB52074F41e4DA095D8a3CCE65370232";
export const TokenManagerAddress = "0xe54947ce60667E6bA9C15619D85169b20299BF63";
export const UserRegistryAddress = "0x2c95Cb4E5C4Faf654513352De35a0470E956241e";
export const UserRegistryAddress = "0x76F6F2cdd70129Bef9e73C89dDCA4df253a33100";

export const ContractsOwnerAddress = "0x0a629f2643b4ef012fe9fd91fed130e01db895dc";
export const ContractsOwnerAddress = "0x3964D0011EB003488Ab59B1ce0C235baA3998bdd";
26 changes: 4 additions & 22 deletions next-app/src/app/administration/histories/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"use client";
import React from "react";
import UsersTable from "@/components/administration/histories/UsersTable";
import TransactionsTable from "@/components/administration/histories/TransactionsTable";
import { getTransactions } from "@/hook/transactions.hook";
import { getUsers } from "@/hook/users.hook";
import { getTransactions } from "@/hook/users.hook";
import { Hook, Transaction, User } from "@/types/hookResponse.type";
import { useQuery } from "@tanstack/react-query";
import { Input } from "@/components/ui/input";
import { SearchIcon } from "@/components/Icons";
import React from "react";

const HistoriesPage: React.FC = () => {
const users = useQuery<Hook<User[]>>({
Expand All @@ -25,31 +22,16 @@ const HistoriesPage: React.FC = () => {
<div className="flex-1">
<h1 className="font-semibold text-lg">Histories</h1>
</div>
<div className="flex flex-1 items-center gap-4 md:ml-auto md:gap-2 lg:gap-4">
<form className="ml-auto flex-1 sm:flex-initial">
<div className="relative">
<SearchIcon className="absolute left-2.5 top-2.5 h-4 w-4 text-gray-500 dark:text-gray-400" />
<Input
type="search"
placeholder="Search..."
className="pl-8 sm:w-[300px] md:w-[200px] lg:w-[300px] bg-white"
/>
</div>
</form>
</div>
</header>
<main className="flex flex-1 flex-col gap-4 p-4 md:gap-8 md:p-6">
<div className="flex items-center">
{/* <div className="flex items-center">
<h2 className="font-semibold text-lg md:text-2xl">Users</h2>
</div>
{users.data?.isError && (
<p>Error loading users: {users.data.error?.message}</p>
)}
<UsersTable users={users.data ? users.data?.data : []} />
<UsersTable users={users.data ? users.data?.data : []} /> */}

<div className="flex items-center">
<h2 className="font-semibold text-lg md:text-2xl">Transactions</h2>
</div>
{transactions.data?.isError && (
<p>Error loading transactions: {transactions.data.error?.message}</p>
)}
Expand Down
6 changes: 3 additions & 3 deletions next-app/src/app/administration/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Sidebar from "@/components/administration/Sidebar";

export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div className="mx-auto max-w-7xl px-6 py-2 lg:px-8 mt-5">
<div className="grid min-h-screen w-full lg:grid-cols-[280px_1fr]">
<div className="mx-auto max-w-7xl px-6 py-2 lg:px-8 mt-5 z-40 bg-white dark:bg-black">
<div className="grid min-h-screen w-full lg:grid-cols-[200px_1fr] z-40">
<Sidebar />
<div className="flex flex-col">{children}</div>
<div className="flex flex-col z-40 bg-transparant">{children}</div>
</div>
</div>
);
Expand Down
6 changes: 5 additions & 1 deletion next-app/src/app/administration/platform/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Input } from "@/components/ui/input";
import PlatformFee from "@/components/administration/platform/PlatformFee";
import BanUserAddress from "@/components/administration/platform/BanUserAddress";
import { SearchIcon } from "@/components/Icons";
import UnBanUserAddress from "@/components/administration/platform/UnBanUserAddress";


const PlatformPage: React.FC = () => {
Expand All @@ -28,8 +29,11 @@ const PlatformPage: React.FC = () => {
</header>
<main className="flex flex-1 flex-col gap-4 p-4 md:gap-8 md:p-6">
<div className="grid grid-cols-1 gap-4 md:grid-cols-1 lg:grid-cols-2">
<PlatformFee />
<BanUserAddress />
<UnBanUserAddress />
</div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-1 lg:grid-cols-2">
<PlatformFee />
</div>
</main>
</>
Expand Down
1 change: 1 addition & 0 deletions next-app/src/components/actions/actions.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export interface SendCardProps {
setCryptoSelected: any;
account: any;
chainId: number;
queryClient: any;
}

5 changes: 5 additions & 0 deletions next-app/src/components/actions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SwapCard from "@/components/actions/swap/page";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { chains } from "@/context";
import { useFetchTokensPairsByAddressList } from "@/hook/useFetchTokenPairs";
import { useQueryClient } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { polygonAmoy } from "viem/chains";
import { useAccount, useReadContract } from "wagmi";
Expand All @@ -25,6 +26,8 @@ export default function ActionPage() {
const { address } = account;
const chainId = account.chainId ? account.chainId : polygonAmoy.id;

const queryClient = useQueryClient();

useEffect(() => {
setIsSSR(false); // Set to false after the first render
}, []);
Expand Down Expand Up @@ -83,6 +86,7 @@ export default function ActionPage() {
cryptoSelected={cryptoSelected}
setCryptoSelected={setCryptoSelected}
chainId={chainId}
queryClient={queryClient}
/>
) : (
<Skeleton className="h-[510px] w-full rounded-xl" />
Expand All @@ -100,6 +104,7 @@ export default function ActionPage() {
setCryptoSelected={setCryptoSelected}
account={account}
chainId={chainId}
queryClient={queryClient}
/>
) : (
<Skeleton className="h-[510px] w-[250px] rounded-xl" />
Expand Down
Loading