-
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.
add createNftOnChain and refactor files and folders (#53)
* add createNftOnChain and refactor files and folders * use NetworkingRouter implementation for SolanaRouter * Max supply for Master Edition test * fix namespace issue and only retry on nilStatus * update swift packages Co-authored-by: Arturo Jamaica <me@arturojamaica.com>
- Loading branch information
1 parent
42686c8
commit fded5a8
Showing
27 changed files
with
1,055 additions
and
198 deletions.
There are no files selected for viewing
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
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
113 changes: 113 additions & 0 deletions
113
Sources/Metaplex/Modules/NFTS/Operations/CreateNftOnChainOperationHandler.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,113 @@ | ||
// | ||
// CreateNftOnChainOperationHandler.swift | ||
// | ||
// | ||
// Created by Michael J. Huber Jr. on 9/2/22. | ||
// | ||
|
||
import Foundation | ||
import Solana | ||
|
||
public enum AccountState { | ||
case new(Account) | ||
case existing(Account) | ||
|
||
var account: Account { | ||
switch self { | ||
case .new(let account): | ||
return account | ||
case .existing(let account): | ||
return account | ||
} | ||
} | ||
} | ||
|
||
struct CreateNftInput { | ||
let mintAccountState: AccountState | ||
let account: Account | ||
let name: String | ||
let symbol: String? | ||
let uri: String | ||
let sellerFeeBasisPoints: UInt16 | ||
let hasCreators: Bool | ||
let addressCount: UInt32 | ||
let creators: [MetaplexCreator] | ||
let collection: MetaplexCollection? = nil | ||
let uses: MetaplexUses? = nil | ||
let isMutable: Bool | ||
} | ||
|
||
typealias CreateNftOperation = OperationResult<CreateNftInput, OperationError> | ||
|
||
class CreateNftOnChainOperationHandler: OperationHandler { | ||
var metaplex: Metaplex | ||
|
||
typealias I = CreateNftInput | ||
typealias O = NFT | ||
|
||
init(metaplex: Metaplex) { | ||
self.metaplex = metaplex | ||
} | ||
|
||
func handle(operation: CreateNftOperation) -> OperationResult<NFT, OperationError> { | ||
let builder = InstructionBuilder(metaplex: metaplex) | ||
return operation.flatMap { input in | ||
OperationResult<[TransactionInstruction], Error>.init { callback in | ||
builder.createNftInstructions(input: input) { | ||
callback($0) | ||
} | ||
}.mapError { | ||
OperationError.buildInstructionsError($0) | ||
}.flatMap { instructions in | ||
OperationResult<String, Error>.init { callback in | ||
self.metaplex.connection.serializeTransaction(instructions: instructions, recentBlockhash: nil, signers: [input.account, input.mintAccountState.account]) { | ||
callback($0) | ||
} | ||
} | ||
.mapError { OperationError.serializeTransactionError($0) } | ||
}.flatMap { serializedTransaction in | ||
OperationResult<TransactionID, IdentityDriverError>.init { callback in | ||
self.metaplex.sendTransaction(serializedTransaction: serializedTransaction) { | ||
callback($0) | ||
} | ||
} | ||
.mapError { OperationError.sendTransactionError($0) } | ||
}.flatMap { signature in | ||
let operation: () -> OperationResult<SignatureStatus, Retry<Error>> = { | ||
OperationResult<SignatureStatus, Error>.init { callback in | ||
// We are sleeping here in order to wait for the transaction to finalize on the chain. | ||
#warning("This needs to be refactored into something more elegant.") | ||
sleep(3) | ||
self.metaplex.confirmTransaction( | ||
signature: signature, | ||
configs: nil | ||
) { result in | ||
switch result { | ||
case .success(let signature): | ||
guard let signature = signature, let status = signature.confirmationStatus, status == .finalized else { | ||
callback(.failure(OperationError.nilSignatureStatus)) | ||
return | ||
} | ||
callback(.success(signature)) | ||
case .failure(let error): | ||
callback(.failure(error)) | ||
} | ||
} | ||
} | ||
.mapError { error in | ||
if case OperationError.nilSignatureStatus = error { | ||
return Retry.retry(error) | ||
} | ||
return Retry.doNotRetry(error) | ||
} | ||
} | ||
let retry = OperationResult<SignatureStatus, Error>.retry(attempts: 5, operation: operation) | ||
.mapError { OperationError.confirmTransactionError($0) } | ||
return retry | ||
}.flatMap { (status: SignatureStatus) -> OperationResult<NFT, OperationError> in | ||
let findNft = FindNftByMintOnChainOperationHandler(metaplex: self.metaplex) | ||
return findNft.handle(operation: FindNftByMintOperation.pure(.success(input.mintAccountState.account.publicKey))) | ||
} | ||
} | ||
} | ||
} |
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
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
31 changes: 31 additions & 0 deletions
31
Sources/Metaplex/Programs/TokenMetadata/Data/MetaplexCollection.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,31 @@ | ||
// | ||
// MetaplexCollection.swift | ||
// | ||
// | ||
// Created by Michael J. Huber Jr. on 9/12/22. | ||
// | ||
|
||
import Foundation | ||
import Solana | ||
|
||
public struct MetaplexCollection: BorshCodable, BufferLayout { | ||
public static var BUFFER_LENGTH: UInt64 = 10 | ||
|
||
public let verified: Bool | ||
public let key: PublicKey | ||
|
||
public init(verified: Bool, key: PublicKey){ | ||
self.verified = verified | ||
self.key = key | ||
} | ||
|
||
public init(from reader: inout BinaryReader) throws { | ||
self.verified = try .init(from: &reader) | ||
self.key = try .init(from: &reader) | ||
} | ||
|
||
public func serialize(to writer: inout Data) throws { | ||
try verified.serialize(to: &writer) | ||
try key.serialize(to: &writer) | ||
} | ||
} |
Oops, something went wrong.