Skip to content

Commit

Permalink
deposit reward when adding new template
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Apr 11, 2024
1 parent 5c14814 commit 79452ec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
6 changes: 6 additions & 0 deletions onchain/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ version = 1
name = "art_peace"
version = "0.1.0"
dependencies = [
"openzeppelin",
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.11.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.11.0#a83f36b23f1af6e160288962be4a2701c3ecbcda"

[[package]]
name = "snforge_std"
version = "0.21.0"
Expand Down
1 change: 1 addition & 0 deletions onchain/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2023_11"

[dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.21.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.11.0" }
starknet = "2.6.3"

[scripts]
Expand Down
35 changes: 34 additions & 1 deletion onchain/src/templates/component.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#[starknet::component]
pub mod TemplateStoreComponent {
use core::num::traits::Zero;
use starknet::ContractAddress;

use art_peace::templates::interface::{ITemplateStore, TemplateMetadata};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};


#[storage]
struct Storage {
Expand Down Expand Up @@ -54,16 +59,44 @@ pub mod TemplateStoreComponent {

// TODO: Return idx of the template?
fn add_template(
ref self: ComponentState<TContractState>, template_metadata: TemplateMetadata
ref self: ComponentState<TContractState>,
template_metadata: TemplateMetadata,
reward_token: ContractAddress,
reward_amount: u256
) {
let template_id = self.templates_count.read();
self.templates.write(template_id, template_metadata);
self.templates_count.write(template_id + 1);
self._deposit(starknet::get_caller_address(), reward_token, reward_amount);
self.emit(TemplateAdded { id: template_id, metadata: template_metadata });
}

fn is_template_complete(self: @ComponentState<TContractState>, template_id: u32) -> bool {
self.completed_templates.read(template_id)
}
}

#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn _deposit(
ref self: ComponentState<TContractState>,
template_proposer: ContractAddress,
reward_token: ContractAddress,
reward_amount: u256
) {
let caller_address = starknet::get_caller_address();
let contract_address = starknet::get_contract_address();
assert(!template_proposer.is_zero(), 'Invalid caller');
// Next line is commented for current test not to revert
// assert(!reward_token.is_zero(), 'Invalid token');
let erc20_dispatcher = IERC20Dispatcher { contract_address: reward_token };
let allowance = erc20_dispatcher.allowance(caller_address, contract_address);
assert(allowance >= reward_amount, 'Insufficient allowance');
let success = erc20_dispatcher
.transfer_from(caller_address, contract_address, reward_amount);
assert(success, 'Transfer failed');
}
}
}
9 changes: 8 additions & 1 deletion onchain/src/templates/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use starknet::ContractAddress;

#[derive(Drop, Copy, Serde, starknet::Store)]
pub struct TemplateMetadata {
pub hash: felt252,
Expand All @@ -18,7 +20,12 @@ pub trait ITemplateStore<TContractState> {
fn get_template_hash(self: @TContractState, template_id: u32) -> felt252;
// Stores a new template image into the contract state w/ metadata.
// If the reward/token are set, then the contract escrows the reward for the template.
fn add_template(ref self: TContractState, template_metadata: TemplateMetadata);
fn add_template(
ref self: TContractState,
template_metadata: TemplateMetadata,
reward_token: ContractAddress,
reward_amount: u256
);
// Returns whether the template is complete.
fn is_template_complete(self: @TContractState, template_id: u32) -> bool;
}
Expand Down
4 changes: 3 additions & 1 deletion onchain/src/tests/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ fn template_full_basic_test() {
reward_token: contract_address_const::<0>(),
};

template_store.add_template(template_metadata);
let reward_token: ContractAddress = contract_address_const::<0x0>();

template_store.add_template(template_metadata, reward_token, 0);

assert!(template_store.get_templates_count() == 1, "Templates count is not 1");
assert!(template_store.get_template_hash(0) == template_hash, "Template hash is not correct");
Expand Down

0 comments on commit 79452ec

Please sign in to comment.