-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from metaplex-foundation/feature/findbyowner
Find NFt by owner
- Loading branch information
Showing
5 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Sources/Metaplex/Modules/nfts/Operations/FindNftsByOwnerOnChainOperationHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
))) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Sources/Metaplex/Programs/TokenMetadata/GpaBuilders/MintGpaBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Sources/Metaplex/Programs/TokenMetadata/GpaBuilders/TokenGpaBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Sources/Metaplex/Programs/TokenMetadata/GpaBuilders/TokenProgramGpaBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |