Skip to content

Commit

Permalink
Add marketplace tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonatan-Chaverri committed Jan 19, 2025
1 parent 7f465c9 commit fd8a7e9
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/snfoundry/contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "contracts"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"openzeppelin",
"snforge_std",
Expand Down
10 changes: 9 additions & 1 deletion apps/snfoundry/contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "contracts"
edition = "2023_11"
version = "0.2.0"
version = "0.3.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

Expand All @@ -13,9 +13,17 @@ starknet = "2.8.2"
[dev-dependencies]
assert_macros = "2.8.2"

[scripts]
test = "snforge test"

[[target.starknet-contract]]
casm = true
sierra = true

[[tool.snforge.fork]]
name = "SEPOLIA_LATEST"
url = "https://starknet-sepolia.public.blastapi.io/rpc/v0_7"
block_id.tag = "latest"

[tool.fmt]
sort-module-level-items = true
9 changes: 9 additions & 0 deletions apps/snfoundry/contracts/src/cofi_collection.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ pub trait ICofiCollection<TContractState> {

/// Unpauses all token transfers.
fn unpause(ref self: TContractState);

// Update minter after deployment
fn set_minter(ref self: TContractState, minter: ContractAddress);
}

#[starknet::contract]
Expand Down Expand Up @@ -375,5 +378,11 @@ mod CofiCollection {
self.accesscontrol.assert_only_role(URI_SETTER_ROLE);
self.erc1155._set_base_uri(base_uri);
}

#[external(v0)]
fn set_minter(ref self: ContractState, minter: ContractAddress) {
self.accesscontrol.assert_only_role(DEFAULT_ADMIN_ROLE);
self.accesscontrol._grant_role(MINTER_ROLE, minter);
}
}
}
2 changes: 2 additions & 0 deletions apps/snfoundry/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod cofi_collection;
mod marketplace;

#[cfg(test)]
mod test {
mod test_cofi_collection;
mod test_marketplace;
}

mod mock_contracts {
Expand Down
28 changes: 16 additions & 12 deletions apps/snfoundry/contracts/src/marketplace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub trait IMarketplace<ContractState> {
fn buy_products(ref self: ContractState, token_ids: Span<u256>, token_amount: Span<u256>);
fn create_product(
ref self: ContractState, initial_stock: u256, price: u256, data: Span<felt252>
);
fn create_products(ref self: ContractState, initial_stock: Span<u256>, price: Span<u256>);
) -> u256;
fn create_products(ref self: ContractState, initial_stock: Span<u256>, price: Span<u256>) -> Span<u256>;
fn delete_product(ref self: ContractState, token_id: u256);
fn delete_products(ref self: ContractState, token_ids: Span<u256>);
fn claim(ref self: ContractState);
Expand Down Expand Up @@ -197,15 +197,19 @@ mod Marketplace {
let buyer = get_caller_address();
let contract_address = get_contract_address();
let strk_token_dispatcher = IERC20Dispatcher {
contract_address: contract_address_const::<STRK_TOKEN_ADDRESS>()
contract_address: contract_address_const::<STRK_TOKEN_ADDRESS>()
};
// Get payment from buyer
let mut producer_fee = self.listed_product_price.read(token_id) * token_amount;
let mut total_price = producer_fee
+ self.calculate_fee(producer_fee, self.market_fee.read());
assert(
strk_token_dispatcher.balance_of(get_caller_address()) >= total_price,
'insufficient funds'
strk_token_dispatcher.balance_of(get_caller_address()) >= total_price,
'insufficient funds'
);
assert(
strk_token_dispatcher.allowance(buyer, contract_address) >= total_price,
'insufficient allowance'
);
strk_token_dispatcher.transfer_from(buyer, contract_address, total_price);

Expand Down Expand Up @@ -317,7 +321,7 @@ mod Marketplace {
/// * `data` - Additional context or metadata for the token transfer process
fn create_product(
ref self: ContractState, initial_stock: u256, price: u256, data: Span<felt252>
) {
) -> u256 {
self.accesscontrol.assert_only_role(PRODUCER);
let token_id = self.current_token_id.read();
let cofi_collection = ICofiCollectionDispatcher {
Expand All @@ -326,13 +330,13 @@ mod Marketplace {
cofi_collection.mint(get_contract_address(), token_id, initial_stock, data);

let producer = get_caller_address();
self.seller_products.write(token_id, producer);

self.current_token_id.write(token_id + 1);
self.initialize_product(token_id, producer, initial_stock, price);
token_id
}

fn create_products(ref self: ContractState, initial_stock: Span<u256>, price: Span<u256>) {
fn create_products(ref self: ContractState, initial_stock: Span<u256>, price: Span<u256>) -> Span<u256> {
assert(initial_stock.len() == price.len(), 'wrong len of arrays');
self.accesscontrol.assert_only_role(PRODUCER);
let producer = get_caller_address();
Expand Down Expand Up @@ -372,6 +376,7 @@ mod Marketplace {
);
token_idx += 1;
};
token_ids.span()
}

fn delete_product(ref self: ContractState, token_id: u256) {
Expand Down Expand Up @@ -418,6 +423,7 @@ mod Marketplace {
self.update_stock(token_id, 0);
cofi_collection.burn(token_holder, token_id, amount_tokens);
self.emit(DeleteProduct { token_id });
token_idx += 1;
};
}

Expand All @@ -429,9 +435,7 @@ mod Marketplace {
let strk_token_dispatcher = IERC20Dispatcher {
contract_address: contract_address_const::<STRK_TOKEN_ADDRESS>()
};
strk_token_dispatcher.approve(producer, claim_balance);
let transfer = strk_token_dispatcher
.transfer_from(get_contract_address(), producer, claim_balance);
let transfer = strk_token_dispatcher.transfer(producer, claim_balance);
assert(transfer, 'Error claiming');

self.claim_balances.write(producer, 0);
Expand All @@ -448,7 +452,7 @@ mod Marketplace {
price: u256
) {
//let product = Product { stock, price };
//self.seller_products.entry(producer).write(token_id);
self.seller_products.write(token_id, producer);
self.listed_product_stock.write(token_id, stock);
self.listed_product_price.write(token_id, price);
self.emit(CreateProduct { token_id, initial_stock: stock });
Expand Down
Loading

0 comments on commit fd8a7e9

Please sign in to comment.