Skip to content

Commit

Permalink
change request
Browse files Browse the repository at this point in the history
  • Loading branch information
hakymulla committed Nov 30, 2024
1 parent 019668b commit 28f1a83
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 73 deletions.
116 changes: 66 additions & 50 deletions onchain/cairo/src/afk_id/nameservice.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub mod UserNameClaimErrors {
pub const BID_LOW: felt252 = 'Bid too low';
pub const INVALID_CANCEL_ORDER: felt252 = 'Invalid cancel order';
pub const USER_NOT_AUCTION_OWNER: felt252 = 'Not the auction owner';

pub const ACCEPTED_PRICE_REACHED: felt252 = 'Accepted Price Reached';
pub const ORDER_INACTIVE: felt252 = 'Order is Inactive';
}

const ADMIN_ROLE: felt252 = selector!("ADMIN_ROLE");
Expand Down Expand Up @@ -100,19 +101,21 @@ pub mod Nameservice {
pub is_claimed: bool
}

#[derive(Drop, Debug, Serde, Copy, starknet::Store, PartialEq)]
#[derive(Drop, Debug, Serde, Copy, starknet::Store)]
pub struct Auction {
pub owner: ContractAddress,
pub minimal_price: u256,
pub is_accepted_price_reached: bool,
pub highest_bid: u256,
pub highest_bidder: ContractAddress
pub highest_bidder: ContractAddress,
}

#[derive(Drop, Debug, Serde, Copy, starknet::Store, PartialEq)]
struct Order {
id: u256,
bidder: ContractAddress,
amount: u256,
is_active: bool
}

#[storage]
Expand All @@ -123,8 +126,8 @@ pub mod Nameservice {
subscription_expiry: Map::<ContractAddress, u64>,
username_storage: Map::<felt252, NameserviceStorage>,
auctions: Map::<felt252, Auction>,
orders: Map<felt252, Order>,
order_count: Map<felt252, u64>,
orders: Map::<(felt252, u256), Order>,
order_count: Map::<felt252, u256>,
order_return: Map::<ContractAddress, Map<felt252, u256>>,
subscription_price: u256,
token_quote: ContractAddress,
Expand Down Expand Up @@ -372,6 +375,7 @@ pub mod Nameservice {
);

let existing_auction = self.auctions.read(username);
// TODO change ERROR NAME
assert(
existing_auction.minimal_price == 0,
UserNameClaimErrors::AUCTION_DOESNT_EXIST
Expand Down Expand Up @@ -410,50 +414,64 @@ pub mod Nameservice {

// Create a new order
let bidder = get_caller_address();
let quote_token = self.token_quote.read();

// let quote_token = self.token_quote.read();
// check if new bidder already has an outbidded amount for the username(still in the contract)
let bidder_amount = self.order_return.entry(bidder).entry(username).read();
self.order_return.entry(bidder).entry(username).write(0);

if amount > bidder_amount {
if self.is_payment_enabled.read() {
let payment_token = IERC20Dispatcher { contract_address: quote_token };
payment_token.transfer_from(bidder, get_contract_address(), (amount - bidder_amount));
}
}
// let bidder_amount = self.order_return.entry(bidder).entry(username).read();
// self.order_return.entry(bidder).entry(username).write(0);

let order_id = self.order_count.read(username) + 1;
let new_order = Order { bidder: bidder, amount: amount };
// if amount > bidder_amount {
// if self.is_payment_enabled.read() {
// let payment_token = IERC20Dispatcher { contract_address: quote_token };
// payment_token.transfer_from(bidder, get_contract_address(), (amount - bidder_amount));
// }
// }

let old_order = self.orders.read(username);
let payment_token = IERC20Dispatcher { contract_address: self.token_quote.read() };
payment_token.transfer_from(bidder, get_contract_address(), amount);

// Store the order
self.orders.write(username, new_order);
self.order_count.write(username, order_id);
let order_id = self.order_count.entry(username).read() + 1_u256;
let new_order = Order { id: order_id, bidder: bidder, amount: amount, is_active: true };

// let old_order = self.orders.read(username);

let order_return_amount = self.order_return.entry(old_order.bidder).entry(username).read();
self.order_return.entry(old_order.bidder).entry(username).write((order_return_amount + old_order.amount));
// Store the order
self.orders.entry((username, order_id)).write(new_order);
self.order_count.entry(username).write(order_id);

// Update auction if this is the highest bid
let mut updated_auction = auction;
updated_auction.highest_bid = amount;
updated_auction.highest_bidder = bidder;
updated_auction.is_accepted_price_reached = true;
updated_auction.is_accepted_price_reached = false;
self.auctions.write(username, updated_auction);
}



// TODO
fn accept_order(ref self: ContractState, username: felt252) {
fn accept_order(ref self: ContractState, username: felt252, id: u64) {
let caller = get_caller_address();
let id: u256 = id.into();

let auction = self.auctions.read(username);
assert(auction.owner == caller, UserNameClaimErrors::USER_NOT_AUCTION_OWNER);
assert(auction.is_accepted_price_reached == false, UserNameClaimErrors::ACCEPTED_PRICE_REACHED);

let order = self.orders.entry((username, id)).read();
assert(order.is_active == true, UserNameClaimErrors::ORDER_INACTIVE);

let order = self.orders.read(username);
let bidder = order.bidder;

// Update Order to inactive
let mut updated_order = order;
updated_order.is_active = false;
self.orders.entry((username, id)).write(order);

// Update auction is_accepted_price_reached
let mut updated_auction = auction;
updated_auction.is_accepted_price_reached = true;
self.auctions.write(username, updated_auction);

// Send token from contract to auction owner
let token = IERC20Dispatcher { contract_address: self.token_quote.read() };
token.transfer(caller, order.amount);

Expand All @@ -470,30 +488,29 @@ pub mod Nameservice {
}

// TODO
fn cancel_order(ref self: ContractState, username: felt252) {
let bidder = get_caller_address();
fn cancel_order(ref self: ContractState, username: felt252, id: u64) {
let caller = get_caller_address();
let auction = self.auctions.read(username);
let order = self.orders.read(username);

let order_return_amount = self.order_return.entry(bidder).entry(username).read();

assert((order.bidder == bidder) || (order_return_amount >= 0), UserNameClaimErrors::INVALID_CANCEL_ORDER);
// Update auction if this is the highest bid
let mut amount = 0;
if order.bidder == bidder {
amount = order.amount;
let mut updated_auction = auction;
updated_auction.highest_bid = 0;
updated_auction.highest_bidder = contract_address_const::<0>();
updated_auction.is_accepted_price_reached = false;
self.auctions.write(username, updated_auction);

} else {
amount = order_return_amount;
}
let id: u256 = id.into();
let order = self.orders.entry((username, id)).read();

assert((order.bidder == caller), UserNameClaimErrors::INVALID_CANCEL_ORDER);
// Update order
let amount = order.amount;
let mut updated_order = order;
updated_order.amount = 0;
updated_order.is_active = false;
self.orders.entry((username, id)).write(updated_order);

// Update auction if the caller is the highest bid
let mut updated_auction = auction;
updated_auction.highest_bid = 0;
updated_auction.highest_bidder = contract_address_const::<0>();
updated_auction.is_accepted_price_reached = false;
self.auctions.write(username, updated_auction);

let token = IERC20Dispatcher { contract_address: self.token_quote.read() };
token.transfer(bidder, amount);
token.transfer(caller, amount);
}

fn get_auction(self: @ContractState, username: felt252) -> Auction {
Expand Down Expand Up @@ -552,7 +569,6 @@ pub mod Nameservice {
);
}


// ADMIN
// Admin functions
fn update_subscription_price(ref self: ContractState, new_price: u256) {
Expand Down
4 changes: 2 additions & 2 deletions onchain/cairo/src/interfaces/nameservice.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub trait INameservice<TContractState> {
is_accepted_price_reached: bool
);
fn place_order(ref self: TContractState, username: felt252, amount: u256);
fn cancel_order(ref self: TContractState, username: felt252);
fn accept_order(ref self: TContractState, username: felt252);
fn cancel_order(ref self: TContractState, username: felt252, id: u64);
fn accept_order(ref self: TContractState, username: felt252, id: u64);
fn get_username(self: @TContractState, address: ContractAddress) -> felt252;
fn get_username_address(self: @TContractState, key: felt252) -> ContractAddress;
fn get_token_quote(self: @TContractState,) -> ContractAddress;
Expand Down
101 changes: 80 additions & 21 deletions onchain/cairo/src/tests/nameservice_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ mod nameservice_tests {
payment_token_mintable_dispatcher
.set_role(recipient: ADMIN(), role: MINTER_ROLE, is_enable: true);
payment_token_mintable_dispatcher.mint(CALLER(), 20_u256); // Reduced amount
payment_token_mintable_dispatcher.mint(NEW_CALLER(), 20_u256); // Reduced amount
payment_token_mintable_dispatcher.mint(NEW_CALLER(), 30_u256); // Reduced amount
payment_token_mintable_dispatcher.mint(THIRD_CALLER(), 20_u256); // Reduced amount
stop_cheat_caller_address(payment_token_mintable_dispatcher.contract_address);

Expand All @@ -404,7 +404,7 @@ mod nameservice_tests {
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(payment_token_dispatcher.contract_address, NEW_CALLER()); //2
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256);
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 30_u256);
stop_cheat_caller_address(payment_token_dispatcher.contract_address);
start_cheat_caller_address(nameservice_dispatcher.contract_address, NEW_CALLER());
nameservice_dispatcher.place_order(username, 10_u256);
Expand All @@ -426,7 +426,7 @@ mod nameservice_tests {
let third_caller_balance = payment_token_dispatcher.balance_of(THIRD_CALLER());
let contract_balance = payment_token_dispatcher
.balance_of(nameservice_dispatcher.contract_address);
assert(contract_balance == 43_u256, 'token balance incorrect');
assert(contract_balance == 53_u256, 'token balance incorrect');
assert(caller_balance == 10_u256, 'caller balance incorrect');
assert(new_caller_balance == 2_u256, 'new_caller balance incorrect');
assert(third_caller_balance == 5_u256, 'third_caller balance incorrect');
Expand Down Expand Up @@ -527,42 +527,88 @@ mod nameservice_tests {
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, NEW_CALLER());
nameservice_dispatcher.place_order(username, 18_u256);
nameservice_dispatcher.cancel_order(username, 1);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

let caller_balance = payment_token_dispatcher.balance_of(CALLER());
let new_caller_balance = payment_token_dispatcher.balance_of(NEW_CALLER());
let third_caller_balance = payment_token_dispatcher.balance_of(THIRD_CALLER());
let contract_balance = payment_token_dispatcher
.balance_of(nameservice_dispatcher.contract_address);
assert(contract_balance == 43_u256, 'token balance incorrect');
assert(contract_balance == 25_u256, 'token balance incorrect');
assert(caller_balance == 10_u256, 'caller balance incorrect');
assert(new_caller_balance == 2_u256, 'new_caller balance incorrect');
assert(new_caller_balance == 20_u256, 'new_caller balance incorrect');
assert(third_caller_balance == 5_u256, 'third_caller balance incorrect');

}

start_cheat_caller_address(nameservice_dispatcher.contract_address, THIRD_CALLER());
nameservice_dispatcher.cancel_order(username);
#[test]
fn test_accept_order() {
let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) =
setup();

let MINTER_ROLE: felt252 = selector!("MINTER_ROLE");

start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN());
payment_token_mintable_dispatcher
.set_role(recipient: ADMIN(), role: MINTER_ROLE, is_enable: true);
payment_token_mintable_dispatcher.mint(CALLER(), 20_u256); // Reduced amount
payment_token_mintable_dispatcher.mint(NEW_CALLER(), 20_u256); // Reduced amount
payment_token_mintable_dispatcher.mint(THIRD_CALLER(), 20_u256); // Reduced amount
stop_cheat_caller_address(payment_token_mintable_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, ADMIN());
nameservice_dispatcher.set_is_payment_enabled(true);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(payment_token_dispatcher.contract_address, CALLER());
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256);
stop_cheat_caller_address(payment_token_dispatcher.contract_address);

let username = selector!("test");
start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER());
nameservice_dispatcher.claim_username(username);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

let stored_username = nameservice_dispatcher.get_username(CALLER());
assert(stored_username == username, 'Username not set');

let stored_address = nameservice_dispatcher.get_username_address(username);
assert(stored_address == CALLER(), 'Address not set');

start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER()); //10
nameservice_dispatcher.create_auction_for_username(username, 5_u256, false);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(payment_token_dispatcher.contract_address, NEW_CALLER()); //2
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256);
stop_cheat_caller_address(payment_token_dispatcher.contract_address);
start_cheat_caller_address(nameservice_dispatcher.contract_address, NEW_CALLER());
nameservice_dispatcher.cancel_order(username);
nameservice_dispatcher.place_order(username, 10_u256);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER());
nameservice_dispatcher.accept_order(username, 1);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);


let stored_username = nameservice_dispatcher.get_username(NEW_CALLER());
assert(stored_username == username, 'Username not set');

let stored_address = nameservice_dispatcher.get_username_address(username);
assert(stored_address == NEW_CALLER(), 'Address not set');

let caller_balance = payment_token_dispatcher.balance_of(CALLER());
let new_caller_balance = payment_token_dispatcher.balance_of(NEW_CALLER());
let third_caller_balance = payment_token_dispatcher.balance_of(THIRD_CALLER());
let contract_balance = payment_token_dispatcher
.balance_of(nameservice_dispatcher.contract_address);
assert(contract_balance == 10_u256, 'token balance incorrect');
assert(caller_balance == 10_u256, 'caller balance incorrect');
assert(new_caller_balance == 20_u256, 'new_caller balance incorrect');
assert(third_caller_balance == 20_u256, 'third_caller balance incorrect');

assert(caller_balance == 20_u256, 'caller balance incorrect');
assert(new_caller_balance == 10_u256, 'new_caller balance incorrect');
}

#[test]
fn test_accept_order() {
fn test_accept_order_and_cancel_order() {
let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) =
setup();

Expand Down Expand Up @@ -606,23 +652,36 @@ mod nameservice_tests {
nameservice_dispatcher.place_order(username, 10_u256);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(payment_token_dispatcher.contract_address, THIRD_CALLER()); //2
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256);
stop_cheat_caller_address(payment_token_dispatcher.contract_address);
start_cheat_caller_address(nameservice_dispatcher.contract_address, THIRD_CALLER());
nameservice_dispatcher.place_order(username, 15_u256);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER());
nameservice_dispatcher.accept_order(username);
nameservice_dispatcher.accept_order(username, 2);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

let stored_username = nameservice_dispatcher.get_username(NEW_CALLER());
start_cheat_caller_address(nameservice_dispatcher.contract_address, NEW_CALLER());
nameservice_dispatcher.cancel_order(username, 1);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

let stored_username = nameservice_dispatcher.get_username(THIRD_CALLER());
assert(stored_username == username, 'Username not set');

let stored_address = nameservice_dispatcher.get_username_address(username);
assert(stored_address == NEW_CALLER(), 'Address not set');
assert(stored_address == THIRD_CALLER(), 'Address not set');

let caller_balance = payment_token_dispatcher.balance_of(CALLER());
let new_caller_balance = payment_token_dispatcher.balance_of(NEW_CALLER());
let third_caller_balance = payment_token_dispatcher.balance_of(THIRD_CALLER());
let contract_balance = payment_token_dispatcher
.balance_of(nameservice_dispatcher.contract_address);
assert(contract_balance == 10_u256, 'token balance incorrect');
assert(caller_balance == 20_u256, 'caller balance incorrect');
assert(new_caller_balance == 10_u256, 'new_caller balance incorrect');
assert(caller_balance == 25_u256, 'caller balance incorrect');
assert(new_caller_balance == 20_u256, 'new_caller balance incorrect');
assert(third_caller_balance == 5_u256, 'third_caller balance incorrect');
}

#[test]
Expand Down Expand Up @@ -672,7 +731,7 @@ mod nameservice_tests {
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, THIRD_CALLER());
nameservice_dispatcher.accept_order(username);
nameservice_dispatcher.accept_order(username, 1);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);
}
}

0 comments on commit 28f1a83

Please sign in to comment.