Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 46 additions & 1 deletion creator-keys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum ContractError {
HandleTooLong = 13,
InvalidHandleCharacter = 14,
ZeroAddress = 15,
SlippageExceeded = 16,
}

pub mod fee {
Expand Down Expand Up @@ -526,6 +527,40 @@ fn credit_protocol_fee_recipient_balance(env: &Env, amount: i128) -> Result<(),
Ok(())
}

fn assert_buy_price_slippage(price: i128, max_price: Option<i128>) -> Result<(), ContractError> {
if let Some(max) = max_price {
if price > max {
return Err(ContractError::SlippageExceeded);
}
}
Ok(())
}

fn compute_sell_proceeds(env: &Env, price: i128) -> Result<i128, ContractError> {
let (creator_fee, protocol_fee) =
CreatorKeysContract::compute_fees_for_payment(env.clone(), price)?;
let fees = fee::checked_fee_sum(creator_fee, protocol_fee).ok_or(ContractError::Overflow)?;
fee::checked_sub_i128(price, fees).ok_or(ContractError::SellUnderflow)
}

fn assert_sell_proceeds_slippage(
env: &Env,
min_proceeds: Option<i128>,
) -> Result<(), ContractError> {
if let Some(min) = min_proceeds {
let price: i128 = env
.storage()
.persistent()
.get(&constants::storage::KEY_PRICE)
.ok_or(ContractError::KeyPriceNotSet)?;
let proceeds = compute_sell_proceeds(env, price)?;
if proceeds < min {
return Err(ContractError::SlippageExceeded);
}
}
Ok(())
}

fn accrue_sell_protocol_fee(env: &Env) -> Result<(), ContractError> {
if env
.storage()
Expand Down Expand Up @@ -695,6 +730,7 @@ impl CreatorKeysContract {
creator: Address,
buyer: Address,
payment: i128,
max_price: Option<i128>,
) -> Result<u32, ContractError> {
buyer.require_auth();

Expand All @@ -708,6 +744,8 @@ impl CreatorKeysContract {
.get(&constants::storage::KEY_PRICE)
.ok_or(ContractError::KeyPriceNotSet)?;

assert_buy_price_slippage(price, max_price)?;

if payment < price {
return Err(ContractError::InsufficientPayment);
}
Expand Down Expand Up @@ -756,7 +794,12 @@ impl CreatorKeysContract {
Ok(profile.supply)
}

pub fn sell_key(env: Env, creator: Address, seller: Address) -> Result<u32, ContractError> {
pub fn sell_key(
env: Env,
creator: Address,
seller: Address,
min_proceeds: Option<i128>,
) -> Result<u32, ContractError> {
seller.require_auth();

let mut profile: CreatorProfile = read_registered_creator_profile(&env, &creator)?;
Expand All @@ -768,6 +811,8 @@ impl CreatorKeysContract {
return Err(ContractError::InsufficientBalance);
}

assert_sell_proceeds_slippage(&env, min_proceeds)?;

let new_balance = current_balance
.checked_sub(1)
.ok_or(ContractError::SellUnderflow)?;
Expand Down
16 changes: 8 additions & 8 deletions creator-keys/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn test_buy_key_fails_if_not_registered() {
let creator = Address::generate(&env);
let buyer = Address::generate(&env);

let result = client.try_buy_key(&creator, &buyer, &100);
let result = client.try_buy_key(&creator, &buyer, &100, &None);
assert_eq!(result, Err(Ok(ContractError::NotRegistered)));
assert_no_events(&env);
}
Expand All @@ -228,7 +228,7 @@ fn test_buy_key_success() {
client.register_creator(&creator, &handle);

let buyer = Address::generate(&env);
let supply = client.buy_key(&creator, &buyer, &100);
let supply = client.buy_key(&creator, &buyer, &100, &None);
assert_eq!(supply, 1);

let profile = client.get_creator(&creator);
Expand All @@ -253,9 +253,9 @@ fn test_get_creator_holder_count_counts_unique_holders() {
let holder_one = Address::generate(&env);
let holder_two = Address::generate(&env);

client.buy_key(&creator, &holder_one, &100);
client.buy_key(&creator, &holder_one, &100);
client.buy_key(&creator, &holder_two, &100);
client.buy_key(&creator, &holder_one, &100, &None);
client.buy_key(&creator, &holder_one, &100, &None);
client.buy_key(&creator, &holder_two, &100, &None);

let first_read = client.get_creator_holder_count(&creator);
let second_read = client.get_creator_holder_count(&creator);
Expand Down Expand Up @@ -292,7 +292,7 @@ fn test_buy_key_insufficient_payment() {
client.register_creator(&creator, &handle);

let buyer = Address::generate(&env);
let result = client.try_buy_key(&creator, &buyer, &99);
let result = client.try_buy_key(&creator, &buyer, &99, &None);
assert_eq!(result, Err(Ok(ContractError::InsufficientPayment)));
assert_no_events(&env);
}
Expand Down Expand Up @@ -484,7 +484,7 @@ fn test_get_sell_quote_success() {
client.register_creator(&creator, &handle);

let buyer = Address::generate(&env);
client.buy_key(&creator, &buyer, &1000);
client.buy_key(&creator, &buyer, &1000, &None);

let quote = client.get_sell_quote(&creator, &buyer);
assert_eq!(quote.price, 1000);
Expand Down Expand Up @@ -745,7 +745,7 @@ fn test_buy_event_topic_and_data_order_is_stable() {
client.register_creator(&creator, &handle);

let buyer = Address::generate(&env);
client.buy_key(&creator, &buyer, &500);
client.buy_key(&creator, &buyer, &500, &None);

let all_events = env.events().all();
// Each client call is a separate invocation; env.events().all() returns events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"hi": 0,
"lo": 100
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -100,7 +101,8 @@
"hi": 0,
"lo": 100
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -128,7 +130,8 @@
"hi": 0,
"lo": 100
}
}
},
"void"
]
}
},
Expand All @@ -152,7 +155,8 @@
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4"
}
},
"void"
]
}
},
Expand All @@ -175,7 +179,8 @@
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK3IM"
}
},
"void"
]
}
},
Expand All @@ -198,7 +203,8 @@
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMDR4"
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 1000
}
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 200
}
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 2000
}
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 2000
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -200,7 +201,8 @@
"hi": 0,
"lo": 2000
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -302,7 +304,8 @@
"hi": 0,
"lo": 2000
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -404,7 +407,8 @@
"hi": 0,
"lo": 2000
}
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 2
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -200,7 +201,8 @@
"hi": 0,
"lo": 4
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -302,7 +304,8 @@
"hi": 0,
"lo": 6
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -404,7 +407,8 @@
"hi": 0,
"lo": 20
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -506,7 +510,8 @@
"hi": 0,
"lo": 198
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -608,7 +613,8 @@
"hi": 0,
"lo": 200
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -710,7 +716,8 @@
"hi": 0,
"lo": 202
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -812,7 +819,8 @@
"hi": 0,
"lo": 1998
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -914,7 +922,8 @@
"hi": 0,
"lo": 2000
}
}
},
"void"
]
}
},
Expand Down Expand Up @@ -1016,7 +1025,8 @@
"hi": 0,
"lo": 20000
}
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 1998
}
}
},
"void"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"hi": 0,
"lo": 2
}
}
},
"void"
]
}
},
Expand Down
Loading
Loading