Skip to content

Commit

Permalink
impr: format the code and remove debug lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagadeeshftw authored and PoulavBhowmick03 committed Feb 27, 2025
1 parent b3e0aa5 commit 7c7477e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 49 deletions.
3 changes: 1 addition & 2 deletions contracts/src/NFTDutchAuction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub mod NFTDutchAuction {
};

let caller = get_caller_address();
println!("Caller inside the buy: {:?}", caller);
// Get NFT price
let price: u256 = self.get_price().into();
let buyer_balance: u256 = erc20_dispatcher.balance_of(caller).into();
Expand All @@ -86,4 +85,4 @@ pub mod NFTDutchAuction {
self.purchase_count.write(self.purchase_count.read() + 1);
}
}
}
}
2 changes: 1 addition & 1 deletion contracts/src/interfaces/IERC721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub trait IERC721<TContractState> {
ref self: TContractState, from: ContractAddress, to: ContractAddress, token_id: u256,
);
fn mint(ref self: TContractState, to: ContractAddress, token_id: u256);
}
}
2 changes: 1 addition & 1 deletion contracts/src/interfaces/INFTDutchAuction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
pub trait INFTDutchAuction<TContractState> {
fn buy(ref self: TContractState, token_id: u256);
fn get_price(self: @TContractState) -> u64;
}
}
5 changes: 2 additions & 3 deletions contracts/src/mock_erc721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub trait IERC721<TContractState> {

#[starknet::contract]
mod MockERC721 {

use starknet::{ContractAddress, get_caller_address};
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
Expand Down Expand Up @@ -143,7 +142,6 @@ mod MockERC721 {

#[generate_trait]
impl ERC721HelperImpl of ERC721HelperTrait {

fn _exists(self: @ContractState, token_id: u256) -> bool {
// check that owner of token is not zero
self.owner_of(token_id).is_non_zero()
Expand Down Expand Up @@ -218,4 +216,5 @@ mod MockERC721 {
self.emit(Transfer { from: owner, to: Zero::zero(), token_id: token_id });
}
}
}
}

79 changes: 37 additions & 42 deletions contracts/tests/test_nft_dutch.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use starknet::ContractAddress;
use snforge_std::{
declare, ContractClassTrait, DeclareResultTrait,
cheat_block_timestamp, CheatSpan,
cheat_caller_address
declare, ContractClassTrait, DeclareResultTrait, cheat_block_timestamp, CheatSpan,
cheat_caller_address,
};

use core::traits::TryInto;
use core::option::OptionTrait;

use contracts::interfaces::INFTDutchAuction::{INFTDutchAuctionDispatcher, INFTDutchAuctionDispatcherTrait};
use contracts::interfaces::INFTDutchAuction::{
INFTDutchAuctionDispatcher, INFTDutchAuctionDispatcherTrait,
};
use contracts::mock_erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use contracts::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait};
fn deploy_contract(name: ByteArray) -> ContractAddress {
Expand Down Expand Up @@ -44,7 +45,7 @@ fn deploy_dutch_auction(
seller: ContractAddress,
duration: u64,
discount_rate: u64,
total_supply: u128
total_supply: u128,
) -> ContractAddress {
let contract = declare("NFTDutchAuction").unwrap().contract_class();
let mut calldata = array![];
Expand All @@ -67,13 +68,13 @@ fn test_dutch_auction_constructor() {
let erc721_token = deploy_erc721();

let auction = deploy_dutch_auction(
erc20_token,
erc721_token,
erc20_token,
erc721_token,
1000, // starting price
owner,
100, // duration
10, // discount rate
5 // total supply
owner,
100, // duration
10, // discount rate
5 // total supply
);

let dutch_auction_dispatcher = INFTDutchAuctionDispatcher { contract_address: auction };
Expand All @@ -84,19 +85,18 @@ fn test_dutch_auction_constructor() {

#[test]
fn test_price_decreases_after_some_time() {

let owner = starknet::contract_address_const::<0x123>();
let erc20_token = deploy_erc20();
let erc721_token = deploy_erc721();

let nft_auction_address = deploy_dutch_auction(
erc20_token,
erc721_token,
erc20_token,
erc721_token,
1000, // starting price
owner,
100, // duration
10, // discount rate
5 // total supply
owner,
100, // duration
10, // discount rate
5 // total supply
);

let nft_auction_dispatcher = INFTDutchAuctionDispatcher {
Expand All @@ -117,19 +117,18 @@ fn test_price_decreases_after_some_time() {

#[test]
fn test_buy_asset() {

let seller = starknet::contract_address_const::<0x123>();
let erc20_address = deploy_erc20();
let erc721_address = deploy_erc721();

let nft_auction_address = deploy_dutch_auction(
erc20_address,
erc721_address,
erc20_address,
erc721_address,
500, // starting price
seller,
60, // duration
5, // discount rate
2 // total supply
seller,
60, // duration
5, // discount rate
2 // total supply
);

let erc721_dispatcher = IERC721Dispatcher { contract_address: erc721_address };
Expand Down Expand Up @@ -185,7 +184,6 @@ fn test_buy_asset() {
}



#[test]
#[should_panic(expected: 'auction has ended')]
fn test_buy_should_panic_when_total_supply_reached() {
Expand All @@ -194,13 +192,13 @@ fn test_buy_should_panic_when_total_supply_reached() {
let erc721_address = deploy_erc721();

let nft_auction_address = deploy_dutch_auction(
erc20_address,
erc721_address,
erc20_address,
erc721_address,
500, // starting price
owner,
60, // duration
5, // discount rate
2 // total supply
owner,
60, // duration
5, // discount rate
2 // total supply
);
let erc20_dispatcher = IERC20Dispatcher { contract_address: erc20_address };
let nft_auction_dispatcher = INFTDutchAuctionDispatcher {
Expand Down Expand Up @@ -236,12 +234,10 @@ fn test_buy_should_panic_when_total_supply_reached() {
cheat_block_timestamp(nft_auction_address, forward_blocktime_by, CheatSpan::TargetCalls(1));
let nft_price = nft_auction_dispatcher.get_price().into();


// buyer approves nft auction contract to spend own erc20 token
cheat_caller_address(erc20_address, buyer, CheatSpan::TargetCalls(1));
erc20_dispatcher.approve(nft_auction_address, nft_price);


cheat_caller_address(nft_auction_address, buyer, CheatSpan::TargetCalls(1));
cheat_block_timestamp(nft_auction_address, forward_blocktime_by, CheatSpan::TargetCalls(1));
nft_auction_dispatcher.buy(nft_id_2);
Expand All @@ -257,7 +253,6 @@ fn test_buy_should_panic_when_total_supply_reached() {
// Buy token
cheat_caller_address(nft_auction_address, buyer, CheatSpan::TargetCalls(4));
nft_auction_dispatcher.buy(nft_id_3);

}

#[test]
Expand All @@ -268,13 +263,13 @@ fn test_buy_should_panic_when_duration_ended() {
let erc721_address = deploy_erc721();

let nft_auction_address = deploy_dutch_auction(
erc20_address,
erc721_address,
erc20_address,
erc721_address,
500, // starting price
owner,
60, // duration
5, // discount rate
2 // total supply
owner,
60, // duration
5, // discount rate
2 // total supply
);
let erc20_dispatcher = IERC20Dispatcher { contract_address: erc20_address };
let nft_auction_dispatcher = INFTDutchAuctionDispatcher {
Expand Down Expand Up @@ -318,4 +313,4 @@ fn test_buy_should_panic_when_duration_ended() {
cheat_block_timestamp(nft_auction_address, forward_blocktime_by, CheatSpan::TargetCalls(1));
cheat_caller_address(nft_auction_address, buyer, CheatSpan::TargetCalls(1));
nft_auction_dispatcher.buy(nft_id_2);
}
}

1 comment on commit 7c7477e

@vercel
Copy link

@vercel vercel bot commented on 7c7477e Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.