Skip to content

FranciscoCosta/Backend-ProtoCoin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ProtoCoin Backend

Introduction

This project provides the backend services for ProtoCoin, including minting and transferring tokens. It is built with Express, and it interacts with the ProtoCoin smart contract on the Ethereum blockchain using Web3.

Installation

To use or modify this project, follow these steps:

  1. Clone the repository: git clone https://github.com/FranciscoCosta/Backend-ProtoCoin.git
  2. Navigate to the project directory: cd Backend-ProtoCoin
  3. Install the dependencies: npm install
  4. Create a .env file in the root directory with the following variables:

NODE_URL=https://your-node-url
PRIVATE_KEY=your-private-key
CONTRACT_ADDRESS=your-contract-address
WALLET_ADDRESS=your-wallet-address
PORT=3000
CORS_ORIGIN=your-frontend-origin

Usage

To start the server, run:

npm start

The server will be running on the port specified in your .env file.

Endpoints

GET /

Returns a simple "Hello World" message.

POST /mint/:wallet

Mints ProtoCoin tokens to the specified wallet address. The endpoint enforces a rate limit, allowing minting once per day per wallet.

curl -X POST http://localhost:3000/mint/{wallet_address}

Code Overview

Server Setup (server.ts)

typescript
import express, { Request, Response, NextFunction, json } from 'express';
import dotenv from 'dotenv';
import morgan from 'morgan';
import helmet from 'helmet';
import { mintAndTransfer } from './Web3Provider';
import cors from 'cors';

dotenv.config();

const PORT = process.env.PORT || 3000; const app = express();

app.use(cors()); app.use(morgan('dev')); app.use(helmet()); app.use(cors({ origin: process.env.CORS_ORIGIN || '*' }));

app.get('/', (req: Request, res: Response, next: NextFunction) => { res.send('Hello World'); });

const nextMint = new Map<string, number>();

app.post("/mint/:wallet", async(req: Request, res: Response, next: NextFunction) => { try{ if(nextMint.has(req.params.wallet) && nextMint.get(req.params.wallet)! > Date.now()){ return res.status(429).send('You can mint only once a day.'); } const tx = await mintAndTransfer(req.params.wallet); res.send(tx); }catch(err: any){ res.status(500).json(err); }finally{ nextMint.set(req.params.wallet, Date.now() + 1000 * 60 * 60 * 24); } });

app.listen(PORT, () => { console.log(Server is running on port ${PORT}); });

Web3 Provider (Web3Provider.ts)

typescript
import Web3 from "web3";
const ABI = require('./abi.json');

// Initialize web3 instance connected to the node const web3 = new Web3(${process.env.NODE_URL});

// Inject the private key to the account object, adding 0x to turn it into a hex string const account = web3.eth.accounts.privateKeyToAccount(0x${process.env.PRIVATE_KEY});

// Add the account to the wallet web3.eth.accounts.wallet.add(account);

export async function mintAndTransfer(to: string): Promise { // Connect to the contract using the contract ABI and contract address const contract = new web3.eth.Contract(ABI, ${process.env.CONTRACT_ADDRESS}, { from: ${process.env.WALLET_ADDRESS} });

// Call the mint function of the contract
const tx = await contract.methods.mint(to).send();

// Return the transaction hash
return tx.transactionHash;

}

References

Frontend project: https://github.com/FranciscoCosta/Faucet-Protocoin

Backend project: https://github.com/FranciscoCosta/Backend-ProtoCoin

Token project: https://github.com/FranciscoCosta/Faucet-Protocoin

License

This project is licensed under the MIT License.

About

The backend for the protocoin project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published