Skip to content

Commit

Permalink
Fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-metaplex committed Nov 1, 2022
1 parent 17e746c commit 4a7d9f6
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 84 deletions.
1 change: 0 additions & 1 deletion Sources/Metaplex/Modules/AuctionHouse/Args/BuyArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import AuctionHouse
import Foundation
import Solana

struct BuyArgs {
let tradeStateBump: UInt8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import AuctionHouse
import Foundation
import Solana

struct CancelArgs {
let buyerPrice: UInt64
Expand Down
1 change: 0 additions & 1 deletion Sources/Metaplex/Modules/AuctionHouse/Args/SaleArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import AuctionHouse
import Foundation
import Solana

struct SaleArgs {
let escrowPaymentBump: UInt8
Expand Down
1 change: 0 additions & 1 deletion Sources/Metaplex/Modules/AuctionHouse/Args/SellArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import AuctionHouse
import Foundation
import Solana

struct SellArgs {
let tradeStateBump: UInt8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public class AuctionHouseClient {
operation.handle(operation: FindAuctionHouseByAddressOperation.pure(.success(address))).run { onComplete($0) }
}

public func findByCreatorAndMint(_ creator: PublicKey, and treasuryMint: PublicKey, onComplete: @escaping (Result<Auctionhouse, OperationError>) -> Void) {
public func findByCreatorAndMint(
_ creator: PublicKey,
and treasuryMint: PublicKey,
onComplete: @escaping (Result<Auctionhouse, OperationError>) -> Void
) {
let operation = FindAuctionHouseByCreatorAndMintOperationHandler(metaplex: metaplex)
operation.handle(operation: FindAuctionHouseByCreatorAndMintOperation.pure(.success(
FindAuctionHouseByCreatorAndMintInput(creator: creator, treasuryMint: treasuryMint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import AuctionHouse
import Foundation
import Solana

public class AuctionHouseProgram {
static let programId = PROGRAM_ID!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ class CreateAuctionHouseOperationHandler: OperationHandler {

func handle(operation: CreateAuctionHouseOperation) -> OperationResult<Auctionhouse, OperationError> {
operation.flatMap { input in
guard let parameters = self.createParametersFromInput(input) else { return .failure(.couldNotFindPDA) } // TODO: Fix error here, maybe throw from `createParametersFromInput(_:)`
return self.createOperationResult(parameters)
switch self.createParametersFromInput(input) {
case .success(let parameters):
return self.createOperationResult(parameters)
case .failure(let error):
return .failure(error)
}
}
}

// MARK: - Private Helpers

private func createParametersFromInput(_ input: CreateAuctionHouseInput) -> CreateAuctionHouseBuilderParameters? {
private func createParametersFromInput(
_ input: CreateAuctionHouseInput
) -> Result<CreateAuctionHouseBuilderParameters, OperationError> {
let defaultIdentity = metaplex.identity()

let authority = input.authority ?? defaultIdentity
Expand All @@ -48,22 +54,32 @@ class CreateAuctionHouseOperationHandler: OperationHandler {
guard let auctionHousePda = try? Auctionhouse.pda(
creator: authority.publicKey,
treasuryMint: input.treasuryMint
).get(), let auctionHouseFeePda = try? Auctionhouse.feePda(
).get()
else { return .failure(.couldNotFindAuctionHouse) }

guard let auctionHouseFeePda = try? Auctionhouse.feePda(
auctionHouse: auctionHousePda.publicKey
).get(), let auctionHouseTreasuryPda = try? Auctionhouse.treasuryPda(
).get()
else { return .failure(.couldNotFindAuctionHouseFeePda) }

guard let auctionHouseTreasuryPda = try? Auctionhouse.treasuryPda(
auctionHouse: auctionHousePda.publicKey
).get(), let treasuryWithdrawalDestination else {
return nil
).get()
else { return .failure(.couldNotFindAuctionHouseTreasuryPda) }

guard let treasuryWithdrawalDestination else {
return .failure(.couldNotFindTreasuryWithdrawalDestination)
}

return CreateAuctionHouseBuilderParameters(
let parameters = CreateAuctionHouseBuilderParameters(
createAuctionHouseInput: input,
auctionHousePda: auctionHousePda,
auctionHouseFeePda: auctionHouseFeePda,
auctionHouseTreasuryPda: auctionHouseTreasuryPda,
treasuryWithdrawalDestination: treasuryWithdrawalDestination,
defaultIdentity: defaultIdentity
)
return .success(parameters)
}

private func createOperationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,38 @@ class CancelBidOperationHandler: OperationHandler {

func handle(operation: CancelBidOperation) -> OperationResult<SignatureStatus, OperationError> {
operation.flatMap { input in
guard let parameters = self.createParametersFromInput(input) else {
return .failure(.couldNotFindPDA)
switch self.createParametersFromInput(input) {
case .success(let parameters):
return self.createOperationResult(parameters)
case .failure(let error):
return .failure(error)
}
return self.createOperationResult(parameters)
}
}

// MARK: - Private Helpers

private func createParametersFromInput(_ input: CancelBidInput) -> CancelBidBuilderParameters? {
private func createParametersFromInput(
_ input: CancelBidInput
) -> Result<CancelBidBuilderParameters, OperationError> {
guard let tokenAccount = try? PublicKey.associatedTokenAddress(
walletAddress: input.bid.bidReceipt.buyer,
tokenMintAddress: input.bid.nft.mint
).get(), let auctionHouse = try? Auctionhouse.pda(
).get()
else { return .failure(.couldNotFindTokenAccount) }

guard let auctionHouse = try? Auctionhouse.pda(
creator: input.auctionHouse.creator,
treasuryMint: input.auctionHouse.treasuryMint
).get().publicKey else {
return nil // .failure(.couldNotFindPDA)
}
).get().publicKey
else { return .failure(.couldNotFindAuctionHouse) }

return CancelBidBuilderParameters(
let parameters = CancelBidBuilderParameters(
cancelBidInput: input,
tokenAccount: tokenAccount,
auctionHouse: auctionHouse
)
return .success(parameters)
}

private func createOperationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,48 @@ class CreateBidOperationHandler: OperationHandler {

func handle(operation: CreateBidOperation) -> OperationResult<Bid, OperationError> {
operation.flatMap { input in
guard let parameters = self.createParametersFromInput(input) else { return .failure(.couldNotFindPDA) } // TODO: Fix error here, maybe throw from `createParametersFromInput(_:)`
return self.createOperationResult(parameters, auctionHouse: input.auctionHouse)
switch self.createParametersFromInput(input) {
case .success(let parameters):
return self.createOperationResult(parameters, auctionHouse: input.auctionHouse)
case .failure(let error):
return .failure(error)
}
}
}

// MARK: - Private Helpers

private func createParametersFromInput(_ input: CreateBidInput) -> CreateBidBuilderParameters? {
private func createParametersFromInput(
_ input: CreateBidInput
) -> Result<CreateBidBuilderParameters, OperationError> {
let defaultIdentity = metaplex.identity()
let buyer = input.buyer ?? defaultIdentity

guard let auctionHouseAddress = input.auctionHouse.address else {
return nil //.failure(.couldNotFindPDA)
return .failure(.couldNotFindAuctionHouse)
}

let escrowPaymentAccount = try? Auctionhouse.buyerEscrowPda(
guard let escrowPaymentAccount = try? Auctionhouse.buyerEscrowPda(
auctionHouse: auctionHouseAddress,
buyer: buyer.publicKey
).get()
else { return .failure(.couldNotFindEscrowPaymentAccount) }

let paymentAccount = input.auctionHouse.isNative
guard let paymentAccount = input.auctionHouse.isNative
? buyer.publicKey
: PublicKey.findAssociatedTokenAccountPda(
mint: input.auctionHouse.treasuryMint,
owner: buyer.publicKey
)
else { return .failure(.couldNotFindPaymentAccount) }

let metadata = try? MetadataAccount.pda(mintKey: input.mintAccount).get()
guard let metadata = try? MetadataAccount.pda(mintKey: input.mintAccount).get()
else { return .failure(.couldNotFindMetadata) }

let buyerPrice = input.price ?? 0
let tokenSize = input.tokens ?? 1

let buyerTradeState = try? Auctionhouse.tradeStatePda(
guard let buyerTradeState = try? Auctionhouse.tradeStatePda(
auctionHouse: auctionHouseAddress,
wallet: buyer.publicKey,
treasuryMint: input.auctionHouse.treasuryMint,
Expand All @@ -64,22 +73,15 @@ class CreateBidOperationHandler: OperationHandler {
tokenSize: tokenSize,
tokenAccount: input.tokenAccount
).get()
else { return .failure(.couldNotFindBuyerTradeStatePda) }

let buyerTokenAccount = PublicKey.findAssociatedTokenAccountPda(
guard let buyerTokenAccount = PublicKey.findAssociatedTokenAccountPda(
mint: input.mintAccount,
owner: buyer.publicKey
)
else { return .failure(.couldNotFindBuyerTokenAccount) }

guard let buyerTradeState else { return nil /*.failure(.couldNotFindPDA)*/ }

guard let escrowPaymentAccount,
let paymentAccount,
let metadata,
let buyerTokenAccount else {
return nil //.failure(.couldNotFindPDA)
}

return CreateBidBuilderParameters(
let parameters = CreateBidBuilderParameters(
createBidInput: input,
escrowPaymentPda: escrowPaymentAccount,
buyerTradePda: buyerTradeState,
Expand All @@ -89,6 +91,7 @@ class CreateBidOperationHandler: OperationHandler {
auctionHouse: auctionHouseAddress,
buyerTokenAccount: buyerTokenAccount
)
return .success(parameters)
}

private func createOperationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,37 @@ class CreateListingOperationHandler: OperationHandler {

func handle(operation: CreateListingOperation) -> OperationResult<Listing, OperationError> {
operation.flatMap { input in
guard let parameters = self.createParametersFromInput(input) else { return .failure(.couldNotFindPDA) } // TODO: Fix error here, maybe throw from `createParametersFromInput(_:)`
return self.createOperationResult(parameters, auctionHouse: input.auctionHouse)
switch self.createParametersFromInput(input) {
case .success(let parameters):
return self.createOperationResult(parameters, auctionHouse: input.auctionHouse)
case .failure(let error):
return .failure(error)
}
}
}

// MARK: - Private Helpers

private func createParametersFromInput(_ input: CreateListingInput) -> CreateListingBuilderParameters? {
private func createParametersFromInput(
_ input: CreateListingInput
) -> Result<CreateListingBuilderParameters, OperationError> {
let defaultIdentity = metaplex.identity()
let seller = input.seller ?? defaultIdentity

let metadata = try? MetadataAccount.pda(mintKey: input.mintAccount).get()
let tokenAccount = input.tokenAccount ?? PublicKey.findAssociatedTokenAccountPda(
guard let metadata = try? MetadataAccount.pda(mintKey: input.mintAccount).get()
else { return .failure(.couldNotFindMetadata) }

guard let tokenAccount = input.tokenAccount ?? PublicKey.findAssociatedTokenAccountPda(
mint: input.mintAccount,
owner: seller.publicKey
)
else { return .failure(.couldNotFindTokenAccount) }

guard let auctionHouse = input.auctionHouse.address else {
return nil //.failure(.couldNotFindPDA)
return .failure(.couldNotFindAuctionHouse)
}

let sellerTradeStatePda = try? Auctionhouse.tradeStatePda(
guard let sellerTradeStatePda = try? Auctionhouse.tradeStatePda(
auctionHouse: auctionHouse,
wallet: seller.publicKey,
treasuryMint: input.auctionHouse.treasuryMint,
Expand All @@ -53,8 +62,9 @@ class CreateListingOperationHandler: OperationHandler {
tokenSize: input.tokens,
tokenAccount: tokenAccount
).get()
else { return .failure(.couldNotFindSellerTradeStatePda) }

let freeSellerTradeState = try? Auctionhouse.tradeStatePda(
guard let freeSellerTradeState = try? Auctionhouse.tradeStatePda(
auctionHouse: auctionHouse,
wallet: seller.publicKey,
treasuryMint: input.auctionHouse.treasuryMint,
Expand All @@ -63,18 +73,12 @@ class CreateListingOperationHandler: OperationHandler {
tokenSize: input.tokens,
tokenAccount: tokenAccount
).get()
else { return .failure(.couldNotFindFreeTradeStatePda) }

let programAsSignerPda = try? Auctionhouse.programAsSignerPda().get()

guard let metadata,
let tokenAccount,
let sellerTradeStatePda,
let freeSellerTradeState,
let programAsSignerPda else {
return nil // Handle error here
}
guard let programAsSignerPda = try? Auctionhouse.programAsSignerPda().get()
else { return .failure(.couldNotFindProgramAsSignerPda) }

return CreateListingBuilderParameters(
let parameters = CreateListingBuilderParameters(
createListingInput: input,
sellerTradeStatePda: sellerTradeStatePda,
freeSellerTradeStatePda: freeSellerTradeState,
Expand All @@ -84,6 +88,7 @@ class CreateListingOperationHandler: OperationHandler {
metadata: metadata,
auctionHouse: auctionHouse
)
return .success(parameters)
}

private func createOperationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class FindListingByReceiptOperationHandler: OperationHandler {
.mapError { OperationError.findListingByReceiptError($0) }
.flatMap { listingReceipt in
OperationResult<Listing, OperationError>.init { callback in
let lazyListing = LazyListing(auctionHouse: input.auctionHouse, listingReceipt: listingReceipt, publicKey: input.address)
let lazyListing = LazyListing(
auctionHouse: input.auctionHouse,
listingReceipt: listingReceipt,
publicKey: input.address
)
self.metaplex.auctionHouse.loadListing(lazyListing) {
callback($0)
}
Expand Down
Loading

0 comments on commit 4a7d9f6

Please sign in to comment.