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.
To use or modify this project, follow these steps:
- Clone the repository:
git clone https://github.com/FranciscoCosta/Backend-ProtoCoin.git
- Navigate to the project directory:
cd Backend-ProtoCoin
- Install the dependencies:
npm install
- 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
To start the server, run:
npm start
The server will be running on the port specified in your .env
file.
Returns a simple "Hello World" message.
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}
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}
); });
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;
}
Frontend project: https://github.com/FranciscoCosta/Faucet-Protocoin
Backend project: https://github.com/FranciscoCosta/Backend-ProtoCoin
Token project: https://github.com/FranciscoCosta/Faucet-Protocoin
This project is licensed under the MIT License.