Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated guild validation url #1363

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
104 changes: 43 additions & 61 deletions packages/uiweb/src/lib/components/chat/helpers/tokenGatedGroup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { ethers } from "ethers";
import { ethers } from 'ethers';

import { fetchERC20Info, fetchERC721nfo } from './tokenHelpers';
import {
Expand All @@ -13,10 +13,7 @@ import {
TYPE,
} from '../types';

const handleDefineCondition = (
entryCriteria: CriteriaStateType,
handlePrevious: (() => void) | undefined
) => {
const handleDefineCondition = (entryCriteria: CriteriaStateType, handlePrevious: (() => void) | undefined) => {
if (entryCriteria.isCondtionUpdateEnabled()) {
// handle update
entryCriteria.updateCondition();
Expand All @@ -30,20 +27,15 @@ const handleDefineCondition = (
}
};

const validateCustomEndpointData = async (
condition: Rule
): Promise<CriteriaValidationErrorType> => {
const validateCustomEndpointData = async (condition: Rule): Promise<CriteriaValidationErrorType> => {
const { data, type, subcategory } = condition;
const errors: CriteriaValidationErrorType = {};

if (!(data as PushData).url) {
return { url: 'URL is missing' };
} else {
// Protocol Validation
if (
!(data as PushData)?.url!.startsWith('http://') &&
!(data as PushData).url!.startsWith('https://')
) {
if (!(data as PushData)?.url!.startsWith('http://') && !(data as PushData).url!.startsWith('https://')) {
return {
url: 'Invalid URL protocol. Only "http://" and "https://" are allowed.',
};
Expand Down Expand Up @@ -73,9 +65,7 @@ const validateCustomEndpointData = async (
return {};
};

const validateGUILDData = async (
condition: Rule
): Promise<CriteriaValidationErrorType> => {
const validateGUILDData = async (condition: Rule): Promise<CriteriaValidationErrorType> => {
const { data } = condition;
const errors: CriteriaValidationErrorType = {};

Expand All @@ -84,9 +74,7 @@ const validateGUILDData = async (
return { ...errors, guildId: 'Guild ID is missing' };
} else {
try {
const response = await axios.get(
`https://api.guild.xyz/v1/guild/${(data as GuildData).id}`
);
const response = await axios.get(`https://api.guild.xyz/v2/guilds/guild-page/${(data as GuildData).id}`);

if (response.status !== 200) {
return { ...errors, guildId: 'Guild ID is missing' };
Expand All @@ -98,8 +86,7 @@ const validateGUILDData = async (
}
} else if ((data as GuildData).role) {
const roleExists = response.data.roles.some(
(role: { id: number }) =>
role.id.toString() === (data as GuildData).role
(role: { id: number }) => role.id.toString() === (data as GuildData).role
);
if (!roleExists) {
return { ...errors, guildRole: 'Invalid Guild Role ID' };
Expand All @@ -124,54 +111,49 @@ const validateGUILDData = async (
return {};
};

const validateTokenData = async (condition:Rule):Promise<CriteriaValidationErrorType> =>{
const data:PushData = condition.data;
const _contract = data.contract || ""
const _eip155Format = _contract.split(":")
const validateTokenData = async (condition: Rule): Promise<CriteriaValidationErrorType> => {
const data: PushData = condition.data;
const _contract = data.contract || '';
const _eip155Format = _contract.split(':');

if(_eip155Format.length !==3){
return {tokenError:"Invalid contract address"}
if (_eip155Format.length !== 3) {
return { tokenError: 'Invalid contract address' };
}
const [chainId, address] = [parseInt(_eip155Format[1]), _eip155Format[2]]
const [chainId, address] = [parseInt(_eip155Format[1]), _eip155Format[2]];

if(!ethers.utils.isAddress(address)){
return {tokenError:`Invalid contract address`}
if (!ethers.utils.isAddress(address)) {
return { tokenError: `Invalid contract address` };
}
const [err] = condition.category === CATEGORY.ERC721 ?
await fetchERC721nfo(address, chainId) : await fetchERC20Info(address, chainId);
const [err] =
condition.category === CATEGORY.ERC721
? await fetchERC721nfo(address, chainId)
: await fetchERC20Info(address, chainId);

if(err){
return {tokenError:`Invalid ${condition.category} contract`}
if (err) {
return { tokenError: `Invalid ${condition.category} contract` };
}
if(!data.amount){
return {tokenAmount:`Amount cannot be 0`}
}
else{
if(data.amount<0){
return {tokenAmount:`Amount cannot be in negative`}
if (!data.amount) {
return { tokenAmount: `Amount cannot be 0` };
} else {
if (data.amount < 0) {
return { tokenAmount: `Amount cannot be in negative` };
}
}
return {}
}

const validationCriteria = async (condition: Rule):Promise<CriteriaValidationErrorType> => {
if(condition.type === TYPE.GUILD)
{
return validateGUILDData(condition);
}else{
if(condition.category === CATEGORY.INVITE){
return {}
}else if (condition.category === CATEGORY.CustomEndpoint){
return validateCustomEndpointData(condition);
}else{
return validateTokenData(condition)
}
}

}
return {};
};

export {
handleDefineCondition,
validationCriteria,
validateCustomEndpointData,
const validationCriteria = async (condition: Rule): Promise<CriteriaValidationErrorType> => {
if (condition.type === TYPE.GUILD) {
return validateGUILDData(condition);
} else {
if (condition.category === CATEGORY.INVITE) {
return {};
} else if (condition.category === CATEGORY.CustomEndpoint) {
return validateCustomEndpointData(condition);
} else {
return validateTokenData(condition);
}
}
};

export { handleDefineCondition, validationCriteria, validateCustomEndpointData };
Loading