Skip to content

Commit

Permalink
Merge pull request #24 from metaplex-foundation/feature/findbyowner
Browse files Browse the repository at this point in the history
Find NFt by owner
  • Loading branch information
ajamaica authored Apr 21, 2022
2 parents 7e3d905 + 46c8369 commit 24b5b24
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// FindNftsByOwnerOnChainOperationHandler.swift
//
//
// Created by Arturo Jamaica on 4/21/22.
//

import Foundation
import Solana

typealias FindNftsByOwnerOperation = OperationResult<PublicKey, OperationError>

class FindNftsByOwnerOnChainOperationHandler: OperationHandler {
typealias I = PublicKey
typealias O = Array<NFT?>

var metaplex: Metaplex
init(metaplex: Metaplex){
self.metaplex = metaplex
}

func handle(operation: FindNftsByOwnerOperation) -> OperationResult<Array<NFT?>, OperationError> {
operation.flatMap { owner in
return TokenProgram()
.tokenAccounts(connection: self.metaplex.connection)
.selectMint()
.whereOwner(owner: owner)
.whereAmount(amount: 1)
.getDataAsPublicKeys()
.mapError { _ in OperationError.couldNotFindPDA }
}.flatMap { publicKeys in
let operation = FindNftsByMintListOnChainOperationHandler(metaplex: self.metaplex)
return operation.handle(operation: FindNftsByMintListOperation.pure(.success(
publicKeys
)))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// MintGpaBuilder.swift
//
//
// Created by Arturo Jamaica on 4/21/22.
//

import Foundation
import Solana

class MintGpaBuilder: TokenProgramGpaBuilder {
func whereDoesntHaveMintAuthority() -> MintGpaBuilder {
return self.where(offset: 0, byte: 0)
}

func whereHasMintAuthority() -> MintGpaBuilder {
return self.where(offset:0, byte: 1);
}

func whereMintAuthority(mintAuthority: PublicKey) -> MintGpaBuilder {
return self.whereHasMintAuthority().where(offset: 4, publicKey: mintAuthority);
}

func whereSupply(supply: Int) -> MintGpaBuilder {
return self.where(offset: 36, int: supply);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// File.swift
//
//
// Created by Arturo Jamaica on 4/21/22.
//

import Foundation
import Solana

class TokenGpaBuilder: TokenProgramGpaBuilder {
func selectMint() -> TokenGpaBuilder {
return self.slice(offset: 0, length: 32)
}

func whereMint(mint: PublicKey) -> TokenGpaBuilder {
return self.where(offset: 0, publicKey: mint)
}

func selectOwner() -> TokenGpaBuilder {
return self.slice(offset: 32, length: 32)
}

func whereOwner(owner: PublicKey) -> TokenGpaBuilder {
return self.where(offset: 32, publicKey: owner)
}

func selectAmount() -> TokenGpaBuilder {
return self.slice(offset: 64, length: 8)
}

func whereAmount(amount: Int) -> TokenGpaBuilder {
return self.where(offset: 64, int: amount)
}

func whereDoesntHaveDelegate() -> TokenGpaBuilder {
return self.where(offset:72, byte: 0)
}

func whereHasDelegate() -> TokenGpaBuilder {
return self.where(offset: 72, byte: 1)
}

func whereDelegate(delegate: PublicKey) -> TokenGpaBuilder {
return self.whereHasDelegate().where(offset: 76, publicKey: delegate)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// TokenProgramGpaBuilder.swift
//
//
// Created by Arturo Jamaica on 4/21/22.
//

import Foundation

let MINT_SIZE: UInt = 162
let ACCOUNT_SIZE: UInt = 254
class TokenProgramGpaBuilder: GpaBuilder {
func mintAccounts() -> MintGpaBuilder {
return MintGpaBuilder.from(builder: self).whereSize(dataSize: MINT_SIZE)
}

func tokenAccounts() -> TokenGpaBuilder {
return TokenGpaBuilder.from(builder: self).whereSize(dataSize: ACCOUNT_SIZE);
}
}
25 changes: 25 additions & 0 deletions Sources/Metaplex/Programs/Tokens/TokenProgram.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// TokenProgram.swift
//
//
// Created by Arturo Jamaica on 4/21/22.
//

import Foundation
import Solana

class TokenProgram {
let publicKey = PublicKey.tokenProgramId

func accounts(connection: Connection) -> TokenProgramGpaBuilder {
return TokenProgramGpaBuilder(connection: connection, programId: publicKey)
}

func mintAccounts(connection: Connection) -> MintGpaBuilder {
return self.accounts(connection: connection).mintAccounts()
}

func tokenAccounts(connection: Connection) -> TokenGpaBuilder {
return self.accounts(connection: connection).tokenAccounts()
}
}

0 comments on commit 24b5b24

Please sign in to comment.