Skip to content

Commit

Permalink
handle modify order
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPrimata committed Nov 1, 2024
1 parent 26863cd commit 9f0b562
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 122 deletions.
118 changes: 65 additions & 53 deletions api/rate-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type ExtendedIncomingMessage = IncomingMessage & {
};
};


function getXForwardedFor(req: ExtendedIncomingMessage): string | undefined {
return req.headers["x-forwarded-for"];
}
Expand All @@ -32,70 +31,84 @@ export default async function handler(request: any, response: any) {
const verificationUrl = `https://challenges.cloudflare.com/turnstile/v0/siteverify?secret=${secretKey}&response=${token}`;
if (!secretKey || !process.env.FAUCET_AUTH_TOKEN) {
console.log(`secret key not set`);
return request.status(500).json({error: "faucet auth token or secret key not set"});
return request
.status(500)
.json({error: "faucet auth token or secret key not set"});
}
const ip = ips(request)?.[0] ?? "127.0.0.1";
const {success, pending, limit, reset, remaining} = await ratelimit.limit(
ip,
);
const {success : addressSuccess, pending: addressPending, limit: addressLimit, reset: addressReset, remaining: addressRemaining} = await ratelimit.limit(
address,
);

const {success, pending, limit, reset, remaining} = await ratelimit.limit(ip);
const {
success: addressSuccess,
pending: addressPending,
limit: addressLimit,
reset: addressReset,
remaining: addressRemaining,
} = await ratelimit.limit(address);

if (!success) {
console.log(`ip rate limit`);
return response.status(429).json({success: false, error: "IP rate limited"});
return response
.status(429)
.json({success: false, error: "IP rate limited"});
}

if (!addressSuccess) {
console.log(`address rate limit`);
return response.status(429).json({success: false, error: "Address rate limited"});
return response
.status(429)
.json({success: false, error: "Address rate limited"});
}

const result = await fetch(verificationUrl, {
body: JSON.stringify({
secret: secretKey,
response: token,
remoteip: ip
}),
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
try {
const data = await result.json();
if (data.success == false) {
return response
.status(400)
.json({success: false, error: "Invalid Turnstile verification token"});
}
let fund;
if (network == "mevm") {
fund = await mevmRequest(address, token, config);
}

fund = await movementRequest(address, network, config);

if (!fund.success) {
console.log(`failed to fund account`);
return response
.status(400)
.json({success: false, error: "Failed to fund account"});
let fund;
if (network == "mevm") {
console.log("request funds from mevm");
fund = await mevmRequest(address, token, config);
// mevm must not verify turnstile token
} else {
const result = await fetch(verificationUrl, {
body: JSON.stringify({
secret: secretKey,
response: token,
remoteip: ip,
}),
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
try {
const data = await result.json();
if (data.success == false) {
return response.status(400).json({
success: false,
error: "Invalid Turnstile verification token",
});
}

fund = await movementRequest(address, network, config);
} catch (error) {
console.log(error);
return response.status(500).json({
success: false,
error:
"Sorry but we ran into an issue. Please try again in a few minutes.",
});
}
}

if (!fund.success) {
console.log(`failed to fund account`);
return response
.status(200)
.json({success: true, hash: fund.hash, limit: limit});
} catch (error) {
console.log(error);
return response.status(500).json({success: false, error: "Sorry but we ran into an issue. Please try again in a few minutes."});
.status(400)
.json({success: false, error: "Failed to fund account"});
}

return response
.status(200)
.json({success: true, hash: fund.hash, limit: limit});
}

async function movementRequest(address: string, network: string, config: any) {

const HEADERS = {
authorization: `Bearer ${process.env.FAUCET_AUTH_TOKEN}`,
};
Expand All @@ -116,11 +129,10 @@ async function movementRequest(address: string, network: string, config: any) {
return fund;
}


async function mevmRequest(
address: string,
token: string,
config: any
config: any,
): Promise<any> {
const requestData = {
jsonrpc: "2.0",
Expand All @@ -132,9 +144,9 @@ async function mevmRequest(
method: "POST",
headers: {
"Content-Type": "application/json",
"Token": token
Token: token,
},
body: JSON.stringify(requestData)
body: JSON.stringify(requestData),
});
const data = await res.json();
if (res.status !== 200) {
Expand All @@ -144,4 +156,4 @@ async function mevmRequest(
return {success: false, error: data.error.message};
}
return {success: true};
}
}
130 changes: 65 additions & 65 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,74 +321,74 @@ export async function movementRequestFaucet(
}
}

// export async function mevmRequestFaucet(
// address: string,
// token: string,
// config: any,
// ) {
// try {
// const response = await fetch("/api/rate-limit", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// token: token,
// address: address,
// network: "mevm",
// config: config
// }),
// });
// // Check if the response is an HTML page
// const contentType = response.headers.get("content-type");
// if (!contentType || !contentType.includes("application/json")) {
// const text = await response.text();
// throw new Error(`Expected JSON but received: ${text}`);
// }
// console.log(typeof response);
// console.log(response);
// const fundAccountData = await response.json();
// console.log("Limit:", fundAccountData.limit);
// if (response.status == 200) {
// return {success: fundAccountData.hash};
// } else {
// return {error: fundAccountData.error};
// }
// } catch (error: any) {
// console.error("Error funding account", error.message);
// return {error: error.message || "Undentified error"};
// }
// }

export async function mevmRequestFaucet(
mevmUrl: string,
address: string,
token: string,
): Promise<any> {

const requestData = {
jsonrpc: "2.0",
id: 1,
method: "eth_batch_faucet",
params: [
address
]
};

const res = await axios.post(mevmUrl, requestData, {
headers: {
"Content-Type": "application/json",
"Token": token
config: any,
) {
try {
const response = await fetch("/api/rate-limit", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: token,
address: address,
network: "mevm",
config: config
}),
});
// Check if the response is an HTML page
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
const text = await response.text();
throw new Error(`Expected JSON but received: ${text}`);
}
});

if(res.status !== 200) {
return {error: res.data};
}else{
if(res.data.error){
return {error: res.data.error.message};
}else{
return {success:res.data};
console.log(typeof response);
console.log(response);
const fundAccountData = await response.json();
console.log("Limit:", fundAccountData.limit);
if (response.status == 200) {
return {success: fundAccountData.hash};
} else {
return {error: fundAccountData.error};
}
} catch (error: any) {
console.error("Error funding account", error.message);
return {error: error.message || "Undentified error"};
}
}
}

// export async function mevmRequestFaucet(
// mevmUrl: string,
// address: string,
// token: string,
// ): Promise<any> {

// const requestData = {
// jsonrpc: "2.0",
// id: 1,
// method: "eth_batch_faucet",
// params: [
// address
// ]
// };

// const res = await axios.post(mevmUrl, requestData, {
// headers: {
// "Content-Type": "application/json",
// "Token": token
// }
// });

// if(res.status !== 200) {
// return {error: res.data};
// }else{
// if(res.data.error){
// return {error: res.data.error.message};
// }else{
// return {success:res.data};
// }
// }
// }
9 changes: 5 additions & 4 deletions src/pages/LandingPage/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ export default function LandingPage() {
token: string,
network: string,
) => {
return mevmRequestFaucet(CHAIN.mevm.url,address, token);
// return mevmRequestFaucet(address, token, CHAIN);
// return mevmRequestFaucet(CHAIN.mevm.url,address, token);
return mevmRequestFaucet(address, token, CHAIN);
};

useEffect(() => {
Expand Down Expand Up @@ -399,10 +399,11 @@ export default function LandingPage() {
label="Network"
onChange={handleChange}
>
<MenuItem value={"holesky"}>Ethereum Holesky</MenuItem>
<MenuItem value={"bardock"}>Movement Bardock</MenuItem>
<MenuItem value={"porto"}>Movement Porto</MenuItem>
<MenuItem value={"bardock"}>Movement Bardock</MenuItem>
<MenuItem value={"holesky"}>Ethereum Holesky</MenuItem>
<MenuItem value={"evm"}>MEVM</MenuItem>

</Select>
</FormControl>
{mock == "holesky" ? (
Expand Down

0 comments on commit 9f0b562

Please sign in to comment.