Skip to content

Commit

Permalink
Add fees to sale price when buying NFTs from a pool (#38)
Browse files Browse the repository at this point in the history
* Add fees to sale price when buying NFTs from a pool

* Schema changes, remove justfile
  • Loading branch information
tasiov authored Mar 25, 2024
1 parent 979196d commit 1a25503
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 128 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/infinity-factory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "infinity-factory"
version = "0.2.1"
version = "0.3.0"
edition = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion contracts/infinity-pair/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "infinity-pair"
version = "0.2.1"
version = "0.3.0"
edition = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
Expand Down
48 changes: 43 additions & 5 deletions contracts/infinity-pair/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ pub struct PayoutContext {
}

impl PayoutContext {
pub fn build_quote_summary(&self, pair: &Pair, sale_ammount: Uint128) -> Option<QuoteSummary> {
if sale_ammount < self.min_price.amount {
return None;
}

fn _derive_quote_summary_parts(
&self,
pair: &Pair,
sale_ammount: Uint128,
) -> (TokenPayment, Option<TokenPayment>, Option<TokenPayment>) {
let fair_burn = TokenPayment {
recipient: self.global_config.fair_burn.clone(),
amount: sale_ammount.mul_ceil(self.global_config.fair_burn_fee_percent),
Expand Down Expand Up @@ -92,6 +92,44 @@ impl PayoutContext {
None
};

(fair_burn, royalty, swap)
}

pub fn build_buy_from_pair_quote_summary(
&self,
pair: &Pair,
sale_ammount: Uint128,
) -> Option<QuoteSummary> {
if sale_ammount < self.min_price.amount {
return None;
}

let (fair_burn, royalty, swap) = self._derive_quote_summary_parts(pair, sale_ammount);

// The seller (pair owner) receives the full sale amount when buying a user buys an NFT from the pair.
// Fees are added on top of the sale amount, and are paid by the buyer.
let seller_amount = sale_ammount;

Some(QuoteSummary {
fair_burn,
royalty,
swap,
seller_amount,
})
}

pub fn build_sell_to_pair_quote_summary(
&self,
pair: &Pair,
sale_ammount: Uint128,
) -> Option<QuoteSummary> {
if sale_ammount < self.min_price.amount {
return None;
}

let (fair_burn, royalty, swap) = self._derive_quote_summary_parts(pair, sale_ammount);

// The seller (user) receives the the sale amount minus the fees, when selling an NFT to the pair.
let seller_amount = sale_ammount
- fair_burn.amount
- royalty.as_ref().map_or(Uint128::zero(), |r| r.amount)
Expand Down
19 changes: 16 additions & 3 deletions contracts/infinity-pair/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{
constants::{CONTRACT_NAME, CONTRACT_VERSION},
error::ContractError,
events::{PairInternalEvent, UpdatePairEvent},
helpers::load_pair,
helpers::{load_pair, load_payout_context},
state::INFINITY_GLOBAL,
};

use cosmwasm_std::{ensure, DepsMut, Empty, Env, Event, StdError};
Expand Down Expand Up @@ -31,9 +32,21 @@ pub fn migrate(deps: DepsMut, env: Env, _msg: Empty) -> Result<Response, Contrac

cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let pair = load_pair(&env.contract.address, deps.storage, &deps.querier)?;
let mut pair = load_pair(&env.contract.address, deps.storage, &deps.querier)?;

let response = Response::new()
let infinity_global = INFINITY_GLOBAL.load(deps.storage)?;

let payout_context = load_payout_context(
deps.as_ref(),
&infinity_global,
&pair.immutable.collection,
&pair.immutable.denom,
)?;

let mut response =
pair.save_and_update_indices(deps.storage, &payout_context, Response::new())?;

response = response
.add_event(
Event::new("migrate")
.add_attribute("from_name", prev_contract_version.contract)
Expand Down
6 changes: 4 additions & 2 deletions contracts/infinity-pair/src/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl Pair {

self.internal.sell_to_pair_quote_summary = match sale_amount_option {
Some(sale_amount) if sale_amount <= self.total_tokens => {
payout_context.build_quote_summary(self, sale_amount)
payout_context.build_sell_to_pair_quote_summary(self, sale_amount)
},
_ => None,
};
Expand Down Expand Up @@ -320,7 +320,9 @@ impl Pair {
};

self.internal.buy_from_pair_quote_summary = match sale_amount_option {
Some(sale_amount) => payout_context.build_quote_summary(self, sale_amount),
Some(sale_amount) => {
payout_context.build_buy_from_pair_quote_summary(self, sale_amount)
},
_ => None,
};
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/infinity-router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "infinity-router"
version = "0.2.0"
version = "0.3.0"
edition = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
Expand Down
77 changes: 0 additions & 77 deletions justfile

This file was deleted.

2 changes: 1 addition & 1 deletion schema/infinity-factory/infinity-factory.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "infinity-factory",
"contract_version": "0.2.1",
"contract_version": "0.3.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
2 changes: 1 addition & 1 deletion schema/infinity-pair/infinity-pair.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "infinity-pair",
"contract_version": "0.2.1",
"contract_version": "0.3.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
2 changes: 1 addition & 1 deletion schema/infinity-router/infinity-router.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "infinity-router",
"contract_version": "0.2.0",
"contract_version": "0.3.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
18 changes: 9 additions & 9 deletions unit-tests/src/infinity_pair_tests/nft_pair_swap_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn try_nft_pair_invalid_swaps() {
amount: Uint128::from(500_000u128),
}),
swap: None,
seller_amount: Uint128::from(9_400_000u128),
seller_amount: Uint128::from(10_000_000u128),
})
);

Expand Down Expand Up @@ -175,7 +175,7 @@ fn try_nft_pair_invalid_swaps() {
token_id,
asset_recipient: None,
},
&[coin(10_000_000u128, UOSMO)],
&[coin(10_600_000u128, UOSMO)],
);
assert_error(response, "Must send reserve token 'ustars'".to_string());

Expand All @@ -187,7 +187,7 @@ fn try_nft_pair_invalid_swaps() {
token_id: "99999".to_string(),
asset_recipient: None,
},
&[coin(10_000_000u128, NATIVE_DENOM)],
&[coin(10_600_000u128, NATIVE_DENOM)],
);
assert_error(
response,
Expand Down Expand Up @@ -271,7 +271,7 @@ fn try_nft_pair_linear_user_submits_tokens_swap() {
amount: Uint128::from(500_000u128),
}),
swap: None,
seller_amount: Uint128::from(9_400_000u128),
seller_amount: Uint128::from(10_000_000u128),
})
);

Expand All @@ -285,7 +285,7 @@ fn try_nft_pair_linear_user_submits_tokens_swap() {
token_id: token_id.clone(),
asset_recipient: None,
},
&[coin(10_000_000u128, NATIVE_DENOM)],
&[coin(10_600_000u128, NATIVE_DENOM)],
);
assert!(response.is_ok());

Expand All @@ -309,7 +309,7 @@ fn try_nft_pair_linear_user_submits_tokens_swap() {
amount: Uint128::from(550_000u128),
}),
swap: None,
seller_amount: Uint128::from(10_340_000u128),
seller_amount: Uint128::from(11_000_000u128),
})
);
}
Expand Down Expand Up @@ -390,7 +390,7 @@ fn try_nft_pair_exponential_user_submits_tokens_swap() {
amount: Uint128::from(500_000u128),
}),
swap: None,
seller_amount: Uint128::from(9_400_000u128),
seller_amount: Uint128::from(10_000_000u128),
})
);

Expand All @@ -404,7 +404,7 @@ fn try_nft_pair_exponential_user_submits_tokens_swap() {
token_id: token_id.clone(),
asset_recipient: None,
},
&[coin(10_000_000u128, NATIVE_DENOM)],
&[coin(10_600_000u128, NATIVE_DENOM)],
);
assert!(response.is_ok());

Expand All @@ -428,7 +428,7 @@ fn try_nft_pair_exponential_user_submits_tokens_swap() {
amount: Uint128::from(560_000u128),
}),
swap: None,
seller_amount: Uint128::from(10_528_000u128),
seller_amount: Uint128::from(11_200_000u128),
})
);
}
Loading

0 comments on commit 1a25503

Please sign in to comment.