Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge develop #263

Merged
merged 16 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Listing NFTs in orderbook creates a transfer cap (#244)
* Listing NFTs in orderbook creates a transfer cap

* One sentence per line
  • Loading branch information
porkbrain authored Feb 9, 2023
commit 587646af840dc0e628cdcee07e3b6f0e394e74fb
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ The format is based on [Keep a
Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- `orderbook::list_nft` and `orderbook::list_nft_with_commission` endpoints.
These allow the client to skip the creation of transfer caps and instead provide `OwnerCap` to `Safe` directly to the orderbook contract.

## [0.22.0] - 2023-02-02

### Added

- Introduced `DelegatedWitness` pattern
- Refactored `CreatorsDomain` to support `DelegatedWitness` and introduced `PluginDomain`

### Changed

- Renamed `CollectionMintEvent` to `MintCollectionEvent` to be consistent with
Expand Down
49 changes: 49 additions & 0 deletions sources/trading/orderbook.move
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,25 @@ module nft_protocol::orderbook {
)
}

/// Creates exclusive transfer cap and then calls [`create_ask`].
public entry fun list_nft<C, FT>(
book: &mut Orderbook<C, FT>,
requested_tokens: u64,
nft: ID,
owner_cap: &safe::OwnerCap,
seller_safe: &mut Safe,
ctx: &mut TxContext,
) {
let transfer_cap = safe::create_exclusive_transfer_cap(
nft,
owner_cap,
seller_safe,
ctx,
);

create_ask(book, requested_tokens, transfer_cap, seller_safe, ctx)
}

/// Same as [`create_ask`] but protected by
/// [collection witness](https://docs.originbyte.io/origin-byte/about-our-programs/liquidity-layer/orderbook#witness-protected-actions).
public fun create_ask_protected<W: drop, C, FT>(
Expand Down Expand Up @@ -384,6 +403,36 @@ module nft_protocol::orderbook {
)
}

/// Creates exclusive transfer cap and then calls
/// [`create_ask_with_commission`].
public entry fun list_nft_with_commission<C, FT>(
book: &mut Orderbook<C, FT>,
requested_tokens: u64,
nft: ID,
owner_cap: &safe::OwnerCap,
beneficiary: address,
commission: u64,
seller_safe: &mut Safe,
ctx: &mut TxContext,
) {
let transfer_cap = safe::create_exclusive_transfer_cap(
nft,
owner_cap,
seller_safe,
ctx,
);

create_ask_with_commission(
book,
requested_tokens,
transfer_cap,
beneficiary,
commission,
seller_safe,
ctx,
)
}

/// Same as [`create_ask_protected`] but with a
/// [commission](https://docs.originbyte.io/origin-byte/about-our-programs/liquidity-layer/orderbook#commission).
///
Expand Down
36 changes: 36 additions & 0 deletions tests/orderbook/commission.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ module nft_protocol::test_ob_commission {
// TODO: test trading with commission - check all assertions and that the
// funds are wrapped in the right way

use nft_protocol::orderbook::{Self as ob, Orderbook};
use nft_protocol::test_utils as test_ob;
use sui::sui::SUI;
use sui::test_scenario;
use sui::transfer::transfer;

const SELLER: address = @0xA1C06;
const CREATOR: address = @0xA1C05;
Expand All @@ -31,4 +34,37 @@ module nft_protocol::test_ob_commission {

test_scenario::end(scenario);
}

#[test]
fun it_lists_nft() {
let scenario = test_scenario::begin(CREATOR);

test_ob::create_collection_and_allowlist(&mut scenario);
let _ob_id = test_ob::create_ob<test_ob::Foo>(&mut scenario);
test_scenario::next_tx(&mut scenario, SELLER);
test_ob::create_safe(&mut scenario, SELLER);
let nft_id = test_ob::create_and_deposit_nft(&mut scenario, SELLER);
test_scenario::next_tx(&mut scenario, SELLER);

let (owner_cap, seller_safe) =
test_ob::owner_cap_and_safe(&scenario, SELLER);
let ob: Orderbook<test_ob::Foo, SUI> =
test_scenario::take_shared(&scenario);
ob::list_nft_with_commission(
&mut ob,
100,
nft_id,
&owner_cap,
CREATOR,
10,
&mut seller_safe,
test_scenario::ctx(&mut scenario),
);

test_scenario::return_shared(ob);
test_scenario::return_shared(seller_safe);
transfer(owner_cap, SELLER);

test_scenario::end(scenario);
}
}
34 changes: 33 additions & 1 deletion tests/orderbook/trade.move
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#[test_only]
module nft_protocol::test_ob_trade {
use originmate::crit_bit_u64 as crit_bit;
use nft_protocol::orderbook::{Self as ob, Orderbook};
use nft_protocol::safe;
use nft_protocol::test_utils::{Self as test_ob};
use originmate::crit_bit_u64 as crit_bit;
use std::option;
use sui::object::ID;
use sui::sui::SUI;
use sui::test_scenario::{Self, Scenario};
use sui::transfer::transfer;

const BUYER1: address = @0xA1C07;
const BUYER2: address = @0xA1C06;
Expand Down Expand Up @@ -305,6 +306,37 @@ module nft_protocol::test_ob_trade {
test_scenario::end(scenario);
}

#[test]
fun it_lists_nft() {
let scenario = test_scenario::begin(CREATOR);

test_ob::create_collection_and_allowlist(&mut scenario);
let _ob_id = test_ob::create_ob<test_ob::Foo>(&mut scenario);
test_scenario::next_tx(&mut scenario, SELLER1);
test_ob::create_safe(&mut scenario, SELLER1);
let nft_id = test_ob::create_and_deposit_nft(&mut scenario, SELLER1);
test_scenario::next_tx(&mut scenario, SELLER1);

let (owner_cap, seller_safe) =
test_ob::owner_cap_and_safe(&scenario, SELLER1);
let ob: Orderbook<test_ob::Foo, SUI> =
test_scenario::take_shared(&scenario);
ob::list_nft(
&mut ob,
100,
nft_id,
&owner_cap,
&mut seller_safe,
test_scenario::ctx(&mut scenario),
);

test_scenario::return_shared(ob);
test_scenario::return_shared(seller_safe);
transfer(owner_cap, SELLER1);

test_scenario::end(scenario);
}

fun most_recent_trade_intermediate_id(): ID {
let id = test_scenario::most_recent_id_shared<
ob::TradeIntermediate<test_ob::Foo, SUI>
Expand Down