Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Metadata #21

Merged
merged 3 commits into from
Nov 23, 2021
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
3 changes: 2 additions & 1 deletion cattleia-backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ INFURA_RINKEBY=
GANACHE=
STRIPE_SECRET=
STRIPE_PUBLISHABLE=
TEST_PRIVATE_KEY=
TEST_PRIVATE_KEY=
TRUFFLE_KEY=
3 changes: 2 additions & 1 deletion cattleia-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules/
coverage/
dist/
src/abis/**.json
junit.xml
.env
.secret
.secret
2 changes: 2 additions & 0 deletions cattleia-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"wsreq": "^1.1.2"
},
"dependencies": {
"@openzeppelin/contracts": "^4.3.3",
"@truffle/hdwallet-provider": "^1.7.0",
"argon2": "^0.28.2",
"axios": "^0.24.0",
"cookie-parser": "^1.4.5",
Expand Down
15 changes: 15 additions & 0 deletions cattleia-backend/src/contracts/CattleiaERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract CattleiaERC20 is ERC20 {
constructor(
string memory name_,
string memory symbol_,
uint256 supply_
) ERC20(name_, symbol_) {
_mint(address(this), supply_);
_transfer(address(this), msg.sender, uint256((supply_ * 10) / 100));
}
}
32 changes: 32 additions & 0 deletions cattleia-backend/src/contracts/CattleiaERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol";

contract CattleiaERC721 is ERC721PresetMinterPauserAutoId {
string private _baseUri;

event BaseURIChanged(string _old, string _new);

constructor(
string memory name_,
string memory symbol_,
string memory baseURI_
) ERC721PresetMinterPauserAutoId(name_, symbol_, baseURI_) {
_baseUri = baseURI_;
}

function changeBaseURI(string memory _newURI) public {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender));
emit BaseURIChanged(_baseUri, _newURI);
_baseUri = _newURI;
}

function baseURI() public view returns (string memory) {
return _baseURI();
}

function _baseURI() internal view virtual override returns (string memory) {
return _baseUri;
}
}
71 changes: 0 additions & 71 deletions cattleia-backend/src/contracts/CattleiaToken.sol

This file was deleted.

2 changes: 1 addition & 1 deletion cattleia-backend/src/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
pragma solidity ^0.8.0;

contract Migrations {
address public owner = msg.sender;
Expand Down
65 changes: 59 additions & 6 deletions cattleia-backend/src/controllers/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
import { Request, Response } from "express";
import { errors } from "../libs";
import { errors, Meta } from "../libs";
import { MetaModel } from "../models";

export const admin = async (req: Request, res: Response): Promise<Response> => {
if (req.role !== "admin")
return res.status(401).json({
export const admin = (_req: Request, res: Response): Response => {
return res.json({ ok: true, msg: "admin" });
};

export const addMeta = async (
req: Request<any, any, Meta>,
res: Response
): Promise<Response> => {
const { attributes, description, external_url, image, name, nft } = req.body;

const found = await MetaModel.findOne({ nft });

console.log(req.body);

if (found) {
return res.status(400).json({
ok: false,
error: errors.metadataAlreadyExist,
});
}

const meta = new MetaModel({
attributes,
description,
external_url,
nft,
image,
name,
});

await meta.save();

return res.json({ ok: true });
};

export const editMeta = async (
req: Request<any, any, Meta>,
res: Response
): Promise<Response> => {
const { id: nft } = req.params;
const { attributes, description, external_url, image, name } = req.body;

const found = await MetaModel.findOne({ nft });

if (!found) {
return res.status(404).json({
ok: false,
error: errors.noAdmin,
error: errors.metadataDoesntExist,
});
}

found.name = name;
found.description = description;
found.image = image;
found.external_url = external_url;
found.attributes = attributes;

await found.save();

return res.json({ msg: "admin" });
return res.json({ ok: true });
};
18 changes: 4 additions & 14 deletions cattleia-backend/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,10 @@ export const signUp = async (

_user.rank = _ranks[0];

let balance = "";

let address = "";

if (_user.account.payload) {
const balanceWeis = await web3.eth.getBalance(
_user.account.payload.address
);
balance = web3.utils.fromWei(balanceWeis, "ether");
address = _user.account.payload.address;
}

await _user.save();

res.cookie("jid", createRefreshToken(_user), cookieConf);

return res.json({
ok: true,
user: {
Expand All @@ -152,8 +142,8 @@ export const signUp = async (
role: _user.role.name,
account: {
hasAccount: false,
balance,
address,
balance: "",
address: "",
},
rank: {
color: rankColor(_user.rank.name),
Expand Down
5 changes: 3 additions & 2 deletions cattleia-backend/src/controllers/index.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const refreshToken = async (
res: Response
): Promise<Response> => {
const token = req.cookies.jid;

if (!token)
return res.status(400).json({
ok: false,
Expand All @@ -39,7 +40,7 @@ export const refreshToken = async (
res.cookie("jid", createRefreshToken(user), cookieConf);
return res.json({
ok: true,
accessToken: createAcessToken(user),
token: createAcessToken(user),
});
} catch (error) {
return res.status(400).json({
Expand All @@ -58,7 +59,7 @@ export const revokeRefreshTokens = async (
if (!user)
return res.status(400).json({
ok: false,
error: errors.invalidIDorNoWallet(id),
error: errors.invalidID(id),
});

user.tokenVersion += 1;
Expand Down
1 change: 1 addition & 0 deletions cattleia-backend/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./index.controller";
export * from "./nft.controller";
export * from "./auth.controller";
export * from "./admin.controller";
export * from "./payment.controller";
26 changes: 26 additions & 0 deletions cattleia-backend/src/controllers/nft.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Request, Response } from "express";
import { errors } from "../libs";
import { MetaModel } from "../models";

export const metadata = async (
req: Request,
res: Response
): Promise<Response> => {
const { id } = req.params;

const meta = await MetaModel.findOne({ nft: id });

if (!meta)
return res.status(404).json({
ok: false,
error: errors.metadataDoesntExist,
});

return res.json({
name: meta.name,
description: meta.description,
image: meta.image,
external_url: meta.external_url,
attributes: meta.attributes,
});
};
10 changes: 9 additions & 1 deletion cattleia-backend/src/libs/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface Stacked {
message: string;
code: number;
}

/* istanbul ignore next */
export const errorStack = (stack: Stacked[], e: Error) => {
if (
e.message.includes("is invalid, the capitalization checksum test failed")
Expand All @@ -28,6 +28,14 @@ export const errors = {
message: "Invalid role.",
code: 8020,
},
metadataAlreadyExist: {
message: "The metadata for this NFT already exist.",
code: 7001,
},
metadataDoesntExist: {
message: "The metadata for this NFT doesn't exist.",
code: 7004,
},
invalidAmount: {
message: "Invalid amount, must be greater than 0.5$",
code: 6060,
Expand Down
15 changes: 15 additions & 0 deletions cattleia-backend/src/libs/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ export interface Payload {
iat: number;
exp: number;
}

interface Traits {
display_types?: string;
trait_type: string;
value: string | number;
}

export interface Meta extends Document {
nft: string;
name: string;
description: string;
image?: string;
external_url?: string;
attributes?: Traits[];
}
13 changes: 13 additions & 0 deletions cattleia-backend/src/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ export const validateToken = (
}
next();
};

export const validateAdmin = (
req: Request,
res: Response,
next: NextFunction
) => {
if (req.role !== "admin")
return res.status(401).json({
ok: false,
error: errors.noAdmin,
});
next();
};
8 changes: 8 additions & 0 deletions cattleia-backend/src/migrations/2_ERC20_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const CattleiaERC20 = artifacts.require("CattleiaERC20");

const name = "CattleiaToken";
const symbol = "CTT";
const supply = 100000000;
module.exports = function (deployer) {
deployer.deploy(CattleiaERC20, name, symbol, supply);
};
5 changes: 0 additions & 5 deletions cattleia-backend/src/migrations/2_deploy_cattleia_token.js

This file was deleted.

Loading