Skip to content

Commit

Permalink
Remove unusued code
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed May 20, 2024
1 parent ab276d6 commit 438e471
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 109 deletions.
118 changes: 38 additions & 80 deletions api/src/controllers/site.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,47 @@
import mailchimp from "@mailchimp/mailchimp_marketing";
import Web3 from "web3";
import RedisClient from '../clients/redis.js';
import RedisClient from "../clients/redis.js";
const redis = RedisClient.getInstance();

mailchimp.setConfig({
apiKey: process.env.MAILCHIMP_API_KEY,
server: "us8"
apiKey: process.env.MAILCHIMP_API_KEY,
server: "us8",
});

export const subscribe = async (req, res, next) => {
const { email } = req.body;
try {
const response = await mailchimp.lists.addListMember(process.env.MAILCHIMP_LIST_ID, {
email_address: email,
status: "subscribed"
});
res.json({ success: true, message: "Subscription successful", data: response });
} catch (error) {
console.error('Mailchimp subscription error:', error);
if (error.response && error.response.body.title === "Member Exists") {
res.status(400).json({ success: false, message: "This email is already subscribed." });
} else if (error.response && error.response.body.title === "Invalid Resource") {
res.status(400).json({ success: false, message: "Invalid email address." });
} else {
const status = error.response ? error.response.status : 500;
const message = error.response ? error.response.body.detail : "An error occurred while subscribing.";
res.status(status).json({ success: false, message });
}
const { email } = req.body;
try {
const response = await mailchimp.lists.addListMember(
process.env.MAILCHIMP_LIST_ID,
{
email_address: email,
status: "subscribed",
},
);
res.json({
success: true,
message: "Subscription successful",
data: response,
});
} catch (error) {
console.error("Mailchimp subscription error:", error);
if (error.response && error.response.body.title === "Member Exists") {
res
.status(400)
.json({ success: false, message: "This email is already subscribed." });
} else if (
error.response &&
error.response.body.title === "Invalid Resource"
) {
res
.status(400)
.json({ success: false, message: "Invalid email address." });
} else {
const status = error.response ? error.response.status : 500;
const message = error.response
? error.response.body.detail
: "An error occurred while subscribing.";
res.status(status).json({ success: false, message });
}
}

export const faucet = async (req, res, next) => {
const { address } = req.query;

const isMember = await redis.sIsMember(`faucet`, address)
if(isMember){
return res.status(200).json({ success: false, message: "Already sent!" });
}
// Initialize Web3 with the provided RPC URL
const web3 = new Web3('https://chain-rpc.litprotocol.com/http');
// Check if connected to blockchain
try {
const isListening = await web3.eth.net.isListening();
if (isListening) {
console.log('Connected to LitProtocol');
}
} catch (error) {
return res.status(400).json({ success: false, message: "Faucet failed" });
}

// Your account address and private key
const accountAddress = process.env.FAUCET_ADDRESS;
const privateKey = process.env.FAUCET_PRIVATE_KEY

// Amount to send in LIT
const amount = web3.utils.toWei('0.0001', 'ether'); // 0.1 LIT, adjusted for 18 decimals

// Transaction details
const transaction = {
chainId: 175177, // LitProtocol Chain ID
gas: 21000,
to: address,
value: amount,
data: ''
};

// Estimate gas price
const estimatedGasPrice = await web3.eth.getGasPrice();
transaction.gasPrice = estimatedGasPrice;
console.log(`Estimated Gas Price: ${web3.utils.fromWei(estimatedGasPrice, 'gwei')} gwei`);

// Get the account nonce
const nonce = await web3.eth.getTransactionCount(accountAddress);
transaction.nonce = nonce;

// Sign the transaction
const signedTx = await web3.eth.accounts.signTransaction(transaction, privateKey);

// Send the transaction
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`Transaction Hash: ${receipt.transactionHash}`);
// Serialize the receipt, converting BigInts to strings
const serializedReceipt = JSON.stringify(receipt, (key, value) => (typeof value === 'bigint' ? value.toString() : value), 2);
console.log(`Transaction Receipt: ${serializedReceipt}`);

await redis.sAdd(`faucet`, address)
res.json({ success: true, message: "Faucet successful"});
}
}
};
41 changes: 24 additions & 17 deletions api/src/libs/lit/send_lit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import { ethers } from "ethers";
import Web3 from "web3";

export const sendLit = async (to, amount) => {


const web3 = new Web3(process.env.LIT_PROTOCOL_RPC_PROVIDER);
// Check if connected to blockchain
try {
const isListening = await web3.eth.net.isListening();
if (isListening) {
console.log('Connected to LitProtocol');
}
const isListening = await web3.eth.net.isListening();
if (isListening) {
console.log("Connected to LitProtocol");
}
} catch (error) {
return res.status(400).json({ success: false, message: "Faucet failed" });
return res.status(400).json({ success: false, message: "Transfer failed" });
}
const wallet = new ethers.Wallet(
process.env.INDEXER_WALLET_PRIVATE_KEY,
Expand All @@ -22,35 +20,44 @@ export const sendLit = async (to, amount) => {
// Your account address and private key

// Amount to send in LIT
const amountVal = web3.utils.toWei(amount, 'ether'); // 0.1 LIT, adjusted for 18 decimals
const amountVal = web3.utils.toWei(amount, "ether"); // 0.1 LIT, adjusted for 18 decimals

// Transaction details
const transaction = {
chainId: 175177, // LitProtocol Chain ID
gas: 21000,
to: to,
value: amountVal,
data: ''
chainId: 175177, // LitProtocol Chain ID
gas: 21000,
to: to,
value: amountVal,
data: "",
};

// Estimate gas price
const estimatedGasPrice = await web3.eth.getGasPrice();
transaction.gasPrice = estimatedGasPrice;
console.log(`Estimated Gas Price: ${web3.utils.fromWei(estimatedGasPrice, 'gwei')} gwei`);
console.log(
`Estimated Gas Price: ${web3.utils.fromWei(estimatedGasPrice, "gwei")} gwei`,
);

// Get the account nonce
const nonce = await web3.eth.getTransactionCount(wallet.address);
transaction.nonce = nonce;

// Sign the transaction
const signedTx = await web3.eth.accounts.signTransaction(transaction, wallet.privateKey);
const signedTx = await web3.eth.accounts.signTransaction(
transaction,
wallet.privateKey,
);

// Send the transaction
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`Transaction Hash: ${receipt.transactionHash}`);
// Serialize the receipt, converting BigInts to strings
const serializedReceipt = JSON.stringify(receipt, (key, value) => (typeof value === 'bigint' ? value.toString() : value), 2);
const serializedReceipt = JSON.stringify(
receipt,
(key, value) => (typeof value === "bigint" ? value.toString() : value),
2,
);
console.log(`Transaction Receipt: ${serializedReceipt}`);

return serializedReceipt;
}
};
12 changes: 0 additions & 12 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,18 +493,6 @@ app.post(
siteController.subscribe,
);

app.get(
"/site/faucet",
validator.query(
Joi.object({
address: Joi.string()
.regex(/^0x[a-fA-F0-9]{40}$/)
.required(),
}),
),
siteController.faucet,
);

app.get("/model/info", modelController.info);
app.post(
"/model/index/:id",
Expand Down

0 comments on commit 438e471

Please sign in to comment.