Skip to content

Commit

Permalink
Merge pull request #16 from Aut-Labs/verify-discord-update
Browse files Browse the repository at this point in the history
Verify discord update
  • Loading branch information
AntGe authored Oct 29, 2024
2 parents 9cdb19d + f9eb0b9 commit 7f4a4ba
Show file tree
Hide file tree
Showing 8 changed files with 1,775 additions and 3,047 deletions.
4,004 changes: 1,255 additions & 2,749 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@aut-labs/abi-types": "^0.0.86-dev",
"@aut-labs/connector": "^0.0.203",
"@aut-labs/d-aut": "^1.0.204-dev",
"@aut-labs/sdk": "^0.0.219-dev",
"@aut-labs/sdk": "^0.0.221-dev",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.15.7",
Expand Down
70 changes: 56 additions & 14 deletions src/api/discord.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { environment } from "./environment";
import { useMutation, UseMutationOptions } from "@tanstack/react-query";

export interface TaskData {
role: string;
Expand All @@ -19,7 +20,7 @@ export const oauthGetToken = (code: string) => {
params.append("client_secret", environment.discordClientSecret);
params.append("grant_type", "authorization_code");
params.append("redirect_uri", environment.discordRedirectUri);
params.append("scope", "identify");
params.append("scope", "identify guilds");
params.append("code", code);
return fetch("https://discord.com/api/oauth2/token", {
method: "POST",
Expand Down Expand Up @@ -56,21 +57,62 @@ export const getUserGuilds = (accessToken: string) => {
.then((res) => res.data);
};

export const verifyDiscordServerOwnership = createAsyncThunk(
"discord/verify",
async (
guildVerificationData: GuildVerificationData,
{ rejectWithValue, getState }
) => {
const guilds = await getUserGuilds(guildVerificationData.accessToken);
const guild = guilds.find((g) => g.id === guildVerificationData.guildId);
if (!guild.owner) {
return rejectWithValue("User is not the owner.");
}
return true;
// export const verifyDiscordServerOwnership = createAsyncThunk(
// "discord/verify",
// async (
// guildVerificationData: GuildVerificationData,
// { rejectWithValue, getState }
// ) => {
// const guilds = await axios
// .get(`${environment.discordApiUrl}/users/@me/guilds`, {
// headers: {
// Authorization: `Bearer ${accessToken}`
// }
// })
// .then((res) => res.data);
// const guild = guilds.find((g) => g.id === guildVerificationData.guildId);
// if (!guild.owner) {
// return false;
// }
// return true;
// }
// );

const verifyGuildOwnership = async (
guildVerificationData: GuildVerificationData
): Promise<VerificationResult> => {
const { accessToken, guildId } = guildVerificationData;

const guilds: any[] = await axios
.get(`${environment.discordApiUrl}/users/@me/guilds`, {
headers: {
Authorization: `Bearer ${accessToken}`
}
})
.then((res) => res.data);

const guild = guilds.find((g) => g.id === guildId);

if (!guild || !guild.owner) {
return { isAdmin: false };
}
);

return { isAdmin: true, guild };
};

interface VerificationResult {
isAdmin: boolean;
guild?: any;
}

export const useVerifyGuildOwnershipMutation = (
options?: UseMutationOptions<VerificationResult, Error, GuildVerificationData>
) => {
return useMutation({
...options,
mutationFn: verifyGuildOwnership
});
};
export interface DiscordMessageInputField {
name: string;
value: string;
Expand Down
6 changes: 5 additions & 1 deletion src/api/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export const swEnvVariables = {
// IPFS storage
ipfsApiKey: "VITE_IPFS_API_KEY",
ipfsApiSecret: "VITE_IPFS_API_SECRET",
ipfsGatewayUrl: "VITE_IPFS_GATEWAY_URL"
ipfsGatewayUrl: "VITE_IPFS_GATEWAY_URL",


twitterClientId: "VITE_TWITTER_CLIENT_ID",
githubClientId: "VITE_GITHUB_CLIENT_ID"
};

export const environment: typeof swEnvVariables =
Expand Down
19 changes: 17 additions & 2 deletions src/api/hub.api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

import { HubOSHub } from "./hub.model";
import { HubOSAutID } from "./aut.model";
import AutSDK, {
AutIDNFT,
fetchMetadata,
getOverrides,
Hub,
HubArchetype,
HubArchetypeParameters,
Expand Down Expand Up @@ -413,7 +413,21 @@ export const updateHub = async (body: HubOSHub, api: BaseQueryApi) => {
const sdk = await AutSDK.getInstance();
const updatedHub = HubOSHub.updateHubNFT(body);
const uri = await sdk.client.sendJSONToIPFS(updatedHub as any);
console.log("uri", uri);
const hubService: Hub = sdk.initService<Hub>(Hub, body.properties.address);
const overrides = await getOverrides(sdk.signer, 4000);
const getMDres = await hubService.contract.metadata.getMetadataUri();
console.log("getMDres", getMDres);
// const tx = await (
// await hubService.contract.metadata.functions.setMetadataUri(
// uri,
// overrides
// )
// ).wait();
// console.log("tx", tx);

// const result = tx.getResult();
// console.log("tx", result);
const result = await hubService.contract.metadata.setMetadataUri(uri);

if (!result?.isSuccess) {
Expand All @@ -422,9 +436,10 @@ export const updateHub = async (body: HubOSHub, api: BaseQueryApi) => {
};
}
return {
data: body
data: { body }
};
} catch (error) {
console.log("error", error);
return {
error: error?.message
};
Expand Down
Loading

0 comments on commit 7f4a4ba

Please sign in to comment.