Skip to content

Commit

Permalink
Readded code that was lost due to poor rebasing. Applied formatting f…
Browse files Browse the repository at this point in the history
…ixes and ran eslint.
  • Loading branch information
eviterin committed Mar 16, 2024
1 parent c290114 commit cde5752
Show file tree
Hide file tree
Showing 6 changed files with 481 additions and 405 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/src/Inventory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ contract Inventory is Ownable {
{
return decks[player][deckID].cards;
}

// ---------------------------------------------------------------------------------------------

// Returns the decks of a given player.
Expand Down
278 changes: 158 additions & 120 deletions packages/webapp/src/actions/getDeck.ts
Original file line number Diff line number Diff line change
@@ -1,120 +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 getAllDecksArgs = {
playerAddress: Address
onSuccess: () => void
}

// -------------------------------------------------------------------------------------------------

/**
* Fetches all decks of the given player by sending the `getAllDecks` transaction.
*
* Returns `true` iff the transaction is successful.
*/
export async function getAllDecks(args: getAllDecksArgs): Promise<any> {
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<any> {
try {
return await getDeckImpl(args)
} catch (err) {
defaultErrorHandling("getDeck", err)
return false
}
}

// -------------------------------------------------------------------------------------------------

async function getAllDecksImpl(args: getAllDecksArgs): Promise<any> {
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<any> {
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<any> {
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<any> {
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
}
}

// =================================================================================================
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<any> {
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 `getDeckReal` transaction.
*
* Returns `true` iff the transaction is successful.
*/
export async function getDeck(args: GetDeckAtArgs): Promise<any> {
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<any> {
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<any> {
try {
return await getDeckNamesImpl(args)
} catch (err) {
defaultErrorHandling("getDeckNames", err)
return false
}
}

// -------------------------------------------------------------------------------------------------

async function getAllDecksImpl(args: GetDeckArgs): Promise<any> {
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<any> {
try {
const result = await contractWriteThrowing({
contract: deployment.Inventory,
abi: inventoryABI,
functionName: "getDeckReal",
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<any> {
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<any> {
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
}
}

// =================================================================================================
Loading

0 comments on commit cde5752

Please sign in to comment.