Skip to content

Commit

Permalink
Add more tests and see if amman will work
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-metaplex committed Oct 28, 2022
1 parent d99fef9 commit 0fc8400
Show file tree
Hide file tree
Showing 31 changed files with 820 additions and 125 deletions.
39 changes: 39 additions & 0 deletions .github/actions/install-solana/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Install Solana

inputs:
solana_version:
description: Version of Solana to install
required: true

runs:
using: "composite"
steps:
- name: Cache Solana Install
if: ${{ !env.ACT }}
id: cache-solana-install
uses: actions/cache@v2
with:
path: "$HOME/.local/share/solana/install/releases/${{ inputs.solana_version }}"
key: ${{ runner.os }}-Solana-v${{ inputs.solana_version }}

- name: Install Solana
if: ${{ !env.ACT }} && steps.cache-solana-install.cache-hit != 'true'
run: |
sh -c "$(curl -sSfL https://release.solana.com/v${{ inputs.solana_version }}/install)"
shell: bash

- name: Set Active Solana Version
run: |
rm -f "$HOME/.local/share/solana/install/active_release"
ln -s "$HOME/.local/share/solana/install/releases/${{ inputs.solana_version }}/solana-release" "$HOME/.local/share/solana/install/active_release"
shell: bash

- name: Add Solana bin to Path
run: |
echo "$HOME/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
shell: bash

- name: Verify Solana install
run: |
solana --version
shell: bash
44 changes: 43 additions & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,55 @@ jobs:
build:

runs-on: macos-12
strategy:
matrix:
solana: ["1.10.34"]

steps:
- uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Install Solana
uses: ./.github/actions/install-solana
with:
solana_version: ${{ matrix.solana }}

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-

- name: Install dependencies
run:
npm i @metaplex-foundation/js @metaplex-foundation/amman

- name: Download required files for Amman
run: |
mkdir -p programs
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/metaplex-foundation/js/contents/programs/mpl_auction_house.so > programs/mpl_auction_house.so
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/metaplex-foundation/js/contents/programs/mpl_candy_guard.so > programs/mpl_candy_guard.so
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/metaplex-foundation/js/contents/programs/mpl_candy_machine_core.so > programs/mpl_candy_machine_core.so
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/metaplex-foundation/js/contents/programs/mpl_candy_machine.so > programs/mpl_candy_machine.so
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/metaplex-foundation/js/contents/programs/mpl_token_metadata.so > programs/mpl_token_metadata.so
curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/metaplex-foundation/js/contents/programs/solana_gateway_program.so > programs/solana_gateway_program.so
curl -O https://raw.githubusercontent.com/metaplex-foundation/js/main/.ammanrc.js
- name: Start local validator using Amman
run: npx amman start

- name: Build
run: swift build

- name: Run tests
run: swift test --enable-code-coverage

- name: Test coverage
uses: maxep/spm-lcov-action@0.3.0
with:
Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/metaplex-foundation/beet-swift",
"state" : {
"branch" : "1.0.2",
"revision" : "05657fa3511b8b72ea8da77745373215445c9885"
"branch" : "fix/fixable-beet-dictionary",
"revision" : "ce6aa4ad52f529cc6181bf0dc5d9be6d3dfe7e1b"
}
},
{
"identity" : "metaplex-swift-program-library",
"kind" : "remoteSourceControl",
"location" : "https://github.com/metaplex-foundation/metaplex-swift-program-library",
"state" : {
"branch" : "1.1.0",
"revision" : "33818f9d56a0788b6acf7c9f0435ae0d7c218f74"
"branch" : "chore/update-beet-1.0.3",
"revision" : "728edddfe3192e016f26890841ccde6d53bb034f"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
targets: ["Metaplex"]),
],
dependencies: [
.package(url: "https://github.com/metaplex-foundation/metaplex-swift-program-library", branch: "1.1.0")
.package(url: "https://github.com/metaplex-foundation/metaplex-swift-program-library", branch: "chore/update-beet-1.0.3")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ extension TransactionBuilder {
// MARK: - Receipt Instruction

let listingReceipt: (addListingReceipt: Bool, instruction: InstructionWithSigner?) = {
guard let purchaseReceipt = parameters.purchaseReceipt else {
guard let receipt = parameters.receipt else {
return (false, nil)
}

let accounts = CancelListingReceiptInstructionAccounts(
receipt: purchaseReceipt,
receipt: receipt,
instruction: PublicKey.sysvarInstructionsPublicKey
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct CancelListingBuilderParameters {
auctioneerAuthority: auctioneerAuthoritySigner.publicKey
).get()
}
var purchaseReceipt: PublicKey? { cancelListingInput.listing.listingReceipt.purchaseReceipt }
var receipt: PublicKey? { cancelListingInput.listing.listingReceipt.receipt?.publicKey }


// MARK: - Args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct CreateListingBuilderParameters {
// MARK: - Getters

var shouldPrintReceipt: Bool { createListingInput.printReceipt }
var receipt: Pda? { shouldPrintReceipt ? try? Bidreceipt.pda(tradeStateAddress: sellerTradeState).get() : nil }
var receipt: Pda? { shouldPrintReceipt ? try? Listingreceipt.pda(tradeStateAddress: sellerTradeState).get() : nil }

// MARK: - Accounts

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ExecuteSaleBuilderParameters {
private let defaultIdentity: Account

let tokenAccount: PublicKey
let metadata: PublicKey
let sellerPaymentReceiptAccount: PublicKey
let buyerReceiptTokenAccount: PublicKey
let auctionHouse: PublicKey
Expand All @@ -30,6 +31,7 @@ struct ExecuteSaleBuilderParameters {
programAsSignerPda: Pda,
defaultIdentity: Account,
tokenAccount: PublicKey,
metadata: PublicKey,
sellerPaymentReceiptAccount: PublicKey,
buyerReceiptTokenAccount: PublicKey,
auctionHouse: PublicKey
Expand All @@ -40,6 +42,7 @@ struct ExecuteSaleBuilderParameters {
self.programAsSignerPda = programAsSignerPda
self.defaultIdentity = defaultIdentity
self.tokenAccount = tokenAccount
self.metadata = metadata
self.sellerPaymentReceiptAccount = sellerPaymentReceiptAccount
self.buyerReceiptTokenAccount = buyerReceiptTokenAccount
self.auctionHouse = auctionHouse
Expand All @@ -53,14 +56,16 @@ struct ExecuteSaleBuilderParameters {
var nft: NFT { executeSaleInput.listing.nft }
var isNative: Bool { executeSaleInput.auctionHouse.isNative }
var shouldPrintReceipt: Bool { executeSaleInput.printReceipt }
var receipt: Pda? { shouldPrintReceipt ? try? Bidreceipt.pda(tradeStateAddress: sellerTradeState).get() : nil }
var receipt: Pda? {
shouldPrintReceipt ? try? Purchasereceipt.pda(sellerTradeState: sellerTradeState, buyerTradeState: buyerTradeState).get() : nil
}

// MARK: - Accounts

var buyer: PublicKey { executeSaleInput.bid.bidReceipt.buyer }
var seller: PublicKey { executeSaleInput.listing.listingReceipt.seller }
var tokenMint: PublicKey { nft.mint }
var metadata: PublicKey { nft.metadataAccount.mint }
// var metadata: PublicKey { nft.metadataAccount.mint }
var treasuryMint: PublicKey { executeSaleInput.auctionHouse.treasuryMint }
var escrowPaymentAccount: PublicKey { escrowPaymentPda.publicKey }
var authority: PublicKey { executeSaleInput.auctionHouse.authority }
Expand Down
12 changes: 12 additions & 0 deletions Sources/Metaplex/Modules/AuctionHouse/Models/Listing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ public struct LazyListing {
self.canceledAt = listingReceipt.canceledAt
}
}

extension Listingreceipt {
static func pda(tradeStateAddress: PublicKey) -> Result<Pda, Error> {
let seeds = [
"listing_receipt".bytes,
tradeStateAddress.bytes
].map { Data($0) }
return PublicKey.findProgramAddress(seeds: seeds, programId: PROGRAM_ID!).map {
Pda(publicKey: $0.0, bump: $0.1)
}
}
}
13 changes: 13 additions & 0 deletions Sources/Metaplex/Modules/AuctionHouse/Models/Purchase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,16 @@ public struct LazyPurchase {
self.createdAt = purchaseReceipt.createdAt
}
}

extension Purchasereceipt {
static func pda(sellerTradeState: PublicKey, buyerTradeState: PublicKey) -> Result<Pda, Error> {
let seeds = [
"purchase_receipt".bytes,
sellerTradeState.bytes,
buyerTradeState.bytes
].map { Data($0) }
return PublicKey.findProgramAddress(seeds: seeds, programId: PROGRAM_ID!).map {
Pda(publicKey: $0.0, bump: $0.1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import Solana

struct CancelListingInput {
let auctioneerAuthority: Account?
let auctionHouse: Auctionhouse
let auctionHouse: AuctionhouseArgs
let listing: Listing

init(
auctioneerAuthority: Account? = nil,
auctionHouse: AuctionhouseArgs,
listing: Listing
) {
self.auctioneerAuthority = auctioneerAuthority
self.auctionHouse = auctionHouse
self.listing = listing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ExecuteSaleOperationHandler: OperationHandler {
owner: input.listing.listingReceipt.seller
)

let metadata = try? MetadataAccount.pda(mintKey: input.listing.nft.mint).get()

guard let auctionHouse = input.auctionHouse.address else {
return nil
}
Expand All @@ -60,20 +62,22 @@ class ExecuteSaleOperationHandler: OperationHandler {
owner: input.bid.bidReceipt.buyer
)

#warning("Fix tokenSize.")
let isPartialSale = input.bid.bidReceipt.tokenSize < input.listing.listingReceipt.tokenSize
let tokenSize = isPartialSale ? input.listing.listingReceipt.tokenSize : input.bid.bidReceipt.tokenSize
let freeTradeStatePda = try? Auctionhouse.tradeStatePda(
auctionHouse: auctionHouse,
wallet: input.listing.listingReceipt.seller,
treasuryMint: input.auctionHouse.treasuryMint,
mintAccount: input.listing.nft.mint,
buyerPrice: Lamports(),
tokenSize: Lamports(),
tokenSize: tokenSize,
tokenAccount: tokenAccount
).get()

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

guard let tokenAccount,
let metadata,
let escrowPaymentPda,
let sellerPaymentReceiptAccount,
let buyerReceiptTokenAccount,
Expand All @@ -89,6 +93,7 @@ class ExecuteSaleOperationHandler: OperationHandler {
programAsSignerPda: programAsSignerPda,
defaultIdentity: defaultIdentity,
tokenAccount: tokenAccount,
metadata: metadata,
sellerPaymentReceiptAccount: sellerPaymentReceiptAccount,
buyerReceiptTokenAccount: buyerReceiptTokenAccount,
auctionHouse: auctionHouse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,100 +19,4 @@ struct AuctionHouseTestDataProvider {

static let expectedPublicKey = "5xN42RZCk7wA4GjQU2VVDhda8LBL8fAnrKZK921sybLF"
static let expectedAuctionHouse = AuctionHouseMock()

// MARK: - Auction House

static func createAuctionHouse(_ metaplex: Metaplex) -> Auctionhouse? {
var result: Result<Auctionhouse, OperationError>?

let lock = RunLoopSimpleLock()
lock.dispatch {
let operation = CreateAuctionHouseOperationHandler(metaplex: metaplex)
operation.handle(
operation: CreateAuctionHouseOperation.pure(.success(
CreateAuctionHouseInput(sellerFeeBasisPoints: 200, auctioneerAuthority: nil)
))
).run {
result = $0
lock.stop()
}
}
lock.run()

return try? result?.get()
}

// MARK: - Bid

static func createBid(_ metaplex: Metaplex, auctionHouse: AuctionhouseArgs, nft: NFT) -> Bid? {
var result: Result<Bid, OperationError>?

let lock = RunLoopSimpleLock()
lock.dispatch {
let operation = CreateBidOperationHandler(metaplex: metaplex)
operation.handle(operation: CreateBidOperation.pure(.success(
CreateBidInput(auctionHouse: auctionHouse, mintAccount: nft.mint, price: 650)
))).run {
result = $0
lock.stop()
}
}
lock.run()

return try? result?.get()
}

static func findBidByReceipt(_ metaplex: Metaplex, address: PublicKey, auctionHouse: AuctionhouseArgs) -> Bid? {
var result: Result<Bid, OperationError>?

let lock = RunLoopSimpleLock()
lock.dispatch {
let operation = FindBidByReceiptOperationHandler(metaplex: metaplex)
operation.handle(operation: FindBidByReceiptOperation.pure(.success(
FindBidByReceiptInput(address: address, auctionHouse: auctionHouse)
))).run {
result = $0
lock.stop()
}
}
lock.run()

return try? result?.get()
}

static func findBidByTradeState(_ metaplex: Metaplex, address: PublicKey, auctionHouse: AuctionhouseArgs) -> Bid? {
var result: Result<Bid, OperationError>?

let lock = RunLoopSimpleLock()
lock.dispatch {
let operation = FindBidByTradeStateOperationHandler(metaplex: metaplex)
operation.handle(operation: FindBidByTradeStateOperation.pure(.success(
FindBidByTradeStateInput(address: address, auctionHouse: auctionHouse)
))).run {
result = $0
lock.stop()
}
}
lock.run()

return try? result?.get()
}

static func cancelBid(_ metaplex: Metaplex, auctionHouse: AuctionhouseArgs, bid: Bid) -> SignatureStatus? {
var result: Result<SignatureStatus, OperationError>?

let lock = RunLoopSimpleLock()
lock.dispatch {
let operation = CancelBidOperationHandler(metaplex: metaplex)
operation.handle(operation: CancelBidOperation.pure(.success(
CancelBidInput(auctioneerAuthority: nil, auctionHouse: auctionHouse, bid: bid)
))).run {
result = $0
lock.stop()
}
}
lock.run()

return try? result?.get()
}
}
Loading

0 comments on commit 0fc8400

Please sign in to comment.