From 851f71c5a2af301e7e80daf366cdbdbe08314b61 Mon Sep 17 00:00:00 2001 From: eviterin Date: Tue, 26 Mar 2024 19:51:26 +0100 Subject: [PATCH] Resolved all warnings when running make check. Then ran linter in webdev/src --- packages/contracts/src/Inventory.sol | 9 +- packages/webapp/src/actions/getDeck.ts | 316 +++++++++--------- packages/webapp/src/actions/setDeck.ts | 182 +++++----- .../src/components/collection/deckList.tsx | 163 +++++---- .../src/components/collection/deckPanel.tsx | 13 +- packages/webapp/src/pages/collection.tsx | 314 +++++++++-------- 6 files changed, 491 insertions(+), 506 deletions(-) diff --git a/packages/contracts/src/Inventory.sol b/packages/contracts/src/Inventory.sol index 464df773..39d9c190 100644 --- a/packages/contracts/src/Inventory.sol +++ b/packages/contracts/src/Inventory.sol @@ -343,15 +343,10 @@ contract Inventory is Ownable { // --------------------------------------------------------------------------------------------- // Returns the list of cards in the given deck of the given player. - function getDeck(address player, uint8 deckID) - external - view - exists(player, deckID) - returns (Deck memory) - { + function getDeck(address player, uint8 deckID) external view exists(player, deckID) returns (Deck memory) { return decks[player][deckID]; } - + // --------------------------------------------------------------------------------------------- // Returns the decks of a given player. diff --git a/packages/webapp/src/actions/getDeck.ts b/packages/webapp/src/actions/getDeck.ts index c17f5168..74df826a 100644 --- a/packages/webapp/src/actions/getDeck.ts +++ b/packages/webapp/src/actions/getDeck.ts @@ -1,158 +1,158 @@ -import { defaultErrorHandling } from "src/actions/errors" -import { contractWriteThrowing } from "src/actions/libContractWrite" -import { Address } from "src/chain" -import { deployment } from "src/deployment" -import { inventoryABI } from "src/generated" - -// ================================================================================================= - -export type GetDeckArgs = { - playerAddress: Address - onSuccess: () => void -} - -export type GetDeckAtArgs = { - playerAddress: Address - onSuccess: () => void - index: number -} - -// ------------------------------------------------------------------------------------------------- - -/** - * Fetches all decks of the given player by sending the `getAllDecks` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function getAllDecks(args: GetDeckArgs): Promise { - try { - return await getAllDecksImpl(args) - } catch (err) { - defaultErrorHandling("getAllDecks", err) - return false - } -} - -/** - * Fetches the deck of the given player of a given ID by sending the `getDeck` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function getDeck(args: GetDeckAtArgs): Promise { - try { - return await getDeckImpl(args) - } catch (err) { - defaultErrorHandling("getDeck", err) - return false - } -} - -// ------------------------------------------------------------------------------------------------- - -/** - * Fetches deck count of the given player by sending the `getNumDecks` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function getNumDecks(args: GetDeckArgs): Promise { - try { - return await getNumDecksImpl(args) - } catch (err) { - defaultErrorHandling("getNumDecks", err) - return false - } -} - -// ------------------------------------------------------------------------------------------------- - -/** - * Fetches deck count of the given player by sending the `getNumDecks` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function getDeckNames(args: GetDeckArgs): Promise { - try { - return await getDeckNamesImpl(args) - } catch (err) { - defaultErrorHandling("getDeckNames", err) - return false - } -} - -// ------------------------------------------------------------------------------------------------- - -async function getAllDecksImpl(args: GetDeckArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getAllDecks", - args: [args.playerAddress], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching decks:", error) - return null - } - } - -// ------------------------------------------------------------------------------------------------- - -async function getDeckImpl(args: GetDeckAtArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getDeck", - args: [args.playerAddress, args.index], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching deck:", error) - return null - } -} - -// ------------------------------------------------------------------------------------------------- - -async function getNumDecksImpl(args: GetDeckArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getNumDecks", - args: [args.playerAddress], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching decks:", error) - return null - } -} - -// ------------------------------------------------------------------------------------------------- - -async function getDeckNamesImpl(args: GetDeckArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getDeckNames", - args: [args.playerAddress], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching decks:", error) - return null - } -} - -// ================================================================================================= \ No newline at end of file +import { defaultErrorHandling } from "src/actions/errors" +import { contractWriteThrowing } from "src/actions/libContractWrite" +import { Address } from "src/chain" +import { deployment } from "src/deployment" +import { inventoryABI } from "src/generated" + +// ================================================================================================= + +export type GetDeckArgs = { + playerAddress: Address + onSuccess: () => void +} + +export type GetDeckAtArgs = { + playerAddress: Address + onSuccess: () => void + index: number +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Fetches all decks of the given player by sending the `getAllDecks` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getAllDecks(args: GetDeckArgs): Promise { + try { + return await getAllDecksImpl(args) + } catch (err) { + defaultErrorHandling("getAllDecks", err) + return false + } +} + +/** + * Fetches the deck of the given player of a given ID by sending the `getDeck` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getDeck(args: GetDeckAtArgs): Promise { + try { + return await getDeckImpl(args) + } catch (err) { + defaultErrorHandling("getDeck", err) + return false + } +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Fetches deck count of the given player by sending the `getNumDecks` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getNumDecks(args: GetDeckArgs): Promise { + try { + return await getNumDecksImpl(args) + } catch (err) { + defaultErrorHandling("getNumDecks", err) + return false + } +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Fetches deck count of the given player by sending the `getNumDecks` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getDeckNames(args: GetDeckArgs): Promise { + try { + return await getDeckNamesImpl(args) + } catch (err) { + defaultErrorHandling("getDeckNames", err) + return false + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getAllDecksImpl(args: GetDeckArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getAllDecks", + args: [args.playerAddress], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching decks:", error) + return null + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getDeckImpl(args: GetDeckAtArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getDeck", + args: [args.playerAddress, args.index], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching deck:", error) + return null + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getNumDecksImpl(args: GetDeckArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getNumDecks", + args: [args.playerAddress], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching decks:", error) + return null + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getDeckNamesImpl(args: GetDeckArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getDeckNames", + args: [args.playerAddress], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching decks:", error) + return null + } +} + +// ================================================================================================= diff --git a/packages/webapp/src/actions/setDeck.ts b/packages/webapp/src/actions/setDeck.ts index 01f8ffdb..c114aeae 100644 --- a/packages/webapp/src/actions/setDeck.ts +++ b/packages/webapp/src/actions/setDeck.ts @@ -1,92 +1,90 @@ -import { defaultErrorHandling } from "src/actions/errors" -import { contractWriteThrowing } from "src/actions/libContractWrite" -import { Address } from "src/chain" -import { deployment } from "src/deployment" -import { inventoryABI } from "src/generated" -import { checkFresh, freshWrap } from "src/store/checkFresh" -import { Deck } from "src/store/types" - -// ================================================================================================= - -export type SaveArgs = { - deck: Deck - playerAddress: Address - onSuccess: () => void -} - -export type ModifyArgs = { - deck: Deck - playerAddress: Address - index: number - onSuccess: () => void -} - - -// ------------------------------------------------------------------------------------------------- - -/** - * Saves a deck created by the player by sending the `saveDeck` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function save(args: SaveArgs): Promise { - try { - return await saveImpl(args) - } catch (err) { - return defaultErrorHandling("save", err) - } -} - -/** - * Modifies a deck owned by the player by sending the `modifyDeck` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function modify(args: ModifyArgs): Promise { - try { - return await modifyImpl(args) - } catch (err) { - return defaultErrorHandling("modify", err) - } -} - -// ------------------------------------------------------------------------------------------------- - -async function saveImpl(args: SaveArgs): Promise { - const cardBigInts = args.deck.cards.map(card => card.id) - - checkFresh(await freshWrap( - contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "addDeck", - args: [ - args.playerAddress, - { name: args.deck.name, cards: cardBigInts } - ], - }))) - - args.onSuccess() - return true -} - -async function modifyImpl(args: ModifyArgs): Promise { - const cardBigInts = args.deck.cards.map(card => card.id) - console.log("INDEX: " + args.index) - checkFresh(await freshWrap( - contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "replaceDeck", - args: [ - args.playerAddress, - args.index, - { name: args.deck.name, cards: cardBigInts } - ], - }))) - - args.onSuccess() - return true -} - -// ================================================================================================= \ No newline at end of file +import { defaultErrorHandling } from "src/actions/errors" +import { contractWriteThrowing } from "src/actions/libContractWrite" +import { Address } from "src/chain" +import { deployment } from "src/deployment" +import { inventoryABI } from "src/generated" +import { checkFresh, freshWrap } from "src/store/checkFresh" +import { Deck } from "src/store/types" + +// ================================================================================================= + +export type SaveArgs = { + deck: Deck + playerAddress: Address + onSuccess: () => void +} + +export type ModifyArgs = { + deck: Deck + playerAddress: Address + index: number + onSuccess: () => void +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Saves a deck created by the player by sending the `saveDeck` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function save(args: SaveArgs): Promise { + try { + return await saveImpl(args) + } catch (err) { + return defaultErrorHandling("save", err) + } +} + +/** + * Modifies a deck owned by the player by sending the `modifyDeck` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function modify(args: ModifyArgs): Promise { + try { + return await modifyImpl(args) + } catch (err) { + return defaultErrorHandling("modify", err) + } +} + +// ------------------------------------------------------------------------------------------------- + +async function saveImpl(args: SaveArgs): Promise { + const cardBigInts = args.deck.cards.map((card) => card.id) + + checkFresh( + await freshWrap( + contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "addDeck", + args: [args.playerAddress, { name: args.deck.name, cards: cardBigInts }], + }) + ) + ) + + args.onSuccess() + return true +} + +async function modifyImpl(args: ModifyArgs): Promise { + const cardBigInts = args.deck.cards.map((card) => card.id) + console.log("INDEX: " + args.index) + checkFresh( + await freshWrap( + contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "replaceDeck", + args: [args.playerAddress, args.index, { name: args.deck.name, cards: cardBigInts }], + }) + ) + ) + + args.onSuccess() + return true +} + +// ================================================================================================= diff --git a/packages/webapp/src/components/collection/deckList.tsx b/packages/webapp/src/components/collection/deckList.tsx index f3a2f493..d357174a 100644 --- a/packages/webapp/src/components/collection/deckList.tsx +++ b/packages/webapp/src/components/collection/deckList.tsx @@ -1,85 +1,78 @@ -import React, { useEffect, useState, useCallback } from "react" -import Link from "src/components/link" -import { Deck } from 'src/store/types' -import { Button } from "src/components/ui/button" -import { getAllDecks, getNumDecks, getDeckNames } from "src/actions/getDeck" -import * as store from "src/store/hooks" -import { Deck } from "src/store/types" - -interface DeckCollectionDisplayProps { - decks: Deck[] - setDecks: React.Dispatch> - onDeckSelect: (deckID: number) => void -} - -const DeckCollectionDisplay: React.FC = ({ decks, setDecks, onDeckSelect }) => { - const playerAddress = store.usePlayerAddress() - const [ deckNames, setDeckNames] = useState([]) - const [ isLoadingDecks, setIsLoadingDecks ] = useState(false) - - function deckCount(): Promise { - return new Promise((resolve) => { - getNumDecks({ - playerAddress: playerAddress!, - onSuccess: () => { } - }) - }) - } - - const loadDeckNames = useCallback(() => { - if (playerAddress) { - setIsLoadingDecks(true) - getDeckNames({ - playerAddress: playerAddress, - onSuccess: () => { - }, - }).then(response => { - if(!response.simulatedResult) return - const receivedDecks = response.simulatedResult as string[] - setDeckNames(receivedDecks) - setIsLoadingDecks(false) - }).catch(error => { - console.error("Error fetching decks:", error) - }) - } - }, [playerAddress]) - - - useEffect(() => { - loadDeckNames() - }, [loadDeckNames]) - - return ( -
- {/* New Deck Button */} - - - {/* Loading Button */} - {isLoadingDecks && ( - - )} - - {/* Deck Buttons */} - {deckNames.map((deckname, deckID) => ( - - ))} -
- ) -} - -export default DeckCollectionDisplay +import React, { useCallback, useEffect, useState } from "react" + +import { getDeckNames } from "src/actions/getDeck" +import Link from "src/components/link" +import { Button } from "src/components/ui/button" +import * as store from "src/store/hooks" + +interface DeckCollectionDisplayProps { + onDeckSelect: (deckID: number) => void +} + +const DeckCollectionDisplay: React.FC = ({ onDeckSelect }) => { + const playerAddress = store.usePlayerAddress() + const [deckNames, setDeckNames] = useState([]) + const [isLoadingDecks, setIsLoadingDecks] = useState(false) + + const loadDeckNames = useCallback(() => { + if (playerAddress) { + setIsLoadingDecks(true) + getDeckNames({ + playerAddress: playerAddress, + onSuccess: () => {}, + }) + .then((response) => { + if (!response.simulatedResult) return + const receivedDecks = response.simulatedResult as string[] + setDeckNames(receivedDecks) + setIsLoadingDecks(false) + }) + .catch((error) => { + console.error("Error fetching decks:", error) + }) + } + }, [playerAddress]) + + useEffect(() => { + loadDeckNames() + }, [loadDeckNames]) + + return ( +
+ {/* New Deck Button */} + + + {/* Loading Button */} + {isLoadingDecks && ( + + )} + + {/* Deck Buttons */} + {deckNames.map((deckname, deckID) => ( + + ))} +
+ ) +} + +export default DeckCollectionDisplay diff --git a/packages/webapp/src/components/collection/deckPanel.tsx b/packages/webapp/src/components/collection/deckPanel.tsx index 46c23f45..9699d065 100644 --- a/packages/webapp/src/components/collection/deckPanel.tsx +++ b/packages/webapp/src/components/collection/deckPanel.tsx @@ -62,14 +62,15 @@ const DeckConstructionPanel: React.FC = ({ {/* Counter Row */}
-
-
+
+
-
- {selectedCards.length}/{MAX_CARDS} + {selectedCards.length}/{MAX_CARDS}
@@ -77,7 +78,7 @@ const DeckConstructionPanel: React.FC = ({