Skip to content

Commit

Permalink
fix: updated guild validation url (#1363)
Browse files Browse the repository at this point in the history
* fix: updated guild validation url

* Update tokenGatedGroup.ts
  • Loading branch information
mishramonalisha76 authored Jun 20, 2024
1 parent 2c43806 commit 957e093
Showing 1 changed file with 43 additions and 61 deletions.
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 };

0 comments on commit 957e093

Please sign in to comment.