Skip to content

Commit 18d0f0c

Browse files
author
aradix16
committed
move initializer to certificate component
1 parent 12f760f commit 18d0f0c

6 files changed

Lines changed: 154 additions & 44 deletions

File tree

backend-sc/save.cairo

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
//use openzeppelin::token::erc721::interface::{ERC721ABIDispatcher, ERC721ABIDispatcherTrait};
3+
#[starknet::component]
4+
mod NFTComponent {
5+
use starknet::ContractAddress;
6+
7+
use carbon_locker::components::certificate::interface::INFTComponent;
8+
9+
use openzeppelin::introspection::src5::SRC5Component;
10+
use openzeppelin::token::erc721::{
11+
ERC721Component,
12+
ERC721Component::ERC721Impl,
13+
ERC721Component::InternalTrait,
14+
};
15+
16+
use openzeppelin::token::erc721::interface::{ERC721ABIDispatcher, ERC721ABIDispatcherTrait};
17+
18+
#[storage]
19+
struct Storage {
20+
wrapped_token: ERC721ABIDispatcher,
21+
}
22+
23+
#[embeddable_as(NFTComponentImpl)]
24+
impl NFTComponent<
25+
TContractState,
26+
+HasComponent<TContractState>,
27+
+Drop<TContractState>,
28+
+SRC5Component::HasComponent<TContractState>,
29+
impl ERC721: ERC721Component::HasComponent<TContractState>,
30+
> of INFTComponent<ComponentState<TContractState>> {
31+
32+
fn initialize(
33+
ref self: ComponentState<TContractState>,
34+
name: ByteArray,
35+
symbol: ByteArray,
36+
base_uri: ByteArray
37+
) {
38+
let mut erc721_comp = get_dep_component_mut!(ref self, ERC721);
39+
erc721_comp.initializer(name, symbol, base_uri);
40+
}
41+
42+
fn mint(ref self: ComponentState<TContractState>, to: ContractAddress, token_id: u256) {
43+
let mut erc721 = get_dep_component_mut!(ref self, ERC721);
44+
erc721._mint(to, token_id);
45+
}
46+
47+
}
48+
}
49+
50+
51+
52+
53+
54+
//fn balance_of(self: @ComponentState<TContractState>, account: ContractAddress) -> u256 {
55+
// self.wrapped_token.read().balance_of(account)
56+
//}
57+
58+
59+
60+
61+
use starknet::ContractAddress;
62+
63+
#[starknet::contract]
64+
mod NFTCertificate {
65+
use starknet::ContractAddress;
66+
67+
use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait};
68+
69+
use carbon_locker::components::certificate::certificate::NFTComponent;
70+
71+
use openzeppelin::token::erc721::ERC721Component;
72+
use openzeppelin::introspection::src5::SRC5Component;
73+
74+
component!(path: NFTComponent, storage: nft_component, event: NFTComponentEvent);
75+
component!(path: ERC721Component, storage: erc721, event: ERC721Event);
76+
component!(path: SRC5Component, storage: src5, event: SRC5Event);
77+
78+
#[abi(embed_v0)]
79+
impl ERC721MixinImpl = ERC721Component::ERC721MixinImpl<ContractState>;
80+
impl ERC721MetadataImpl = ERC721Component::ERC721MetadataImpl<ContractState>;
81+
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>;
82+
83+
#[abi(embed_v0)]
84+
impl NFTComponentImpl = NFTComponent::NFTComponentImpl<ContractState>;
85+
86+
#[storage]
87+
struct Storage {
88+
#[substorage(v0)]
89+
nft_component: NFTComponent::Storage,
90+
#[substorage(v0)]
91+
erc721: ERC721Component::Storage,
92+
#[substorage(v0)]
93+
src5: SRC5Component::Storage,
94+
}
95+
96+
#[event]
97+
#[derive(Drop, starknet::Event)]
98+
enum Event {
99+
#[flat]
100+
NFTComponentEvent: NFTComponent::Event,
101+
#[flat]
102+
ERC721Event: ERC721Component::Event,
103+
#[flat]
104+
SRC5Event: SRC5Component::Event,
105+
}
106+
107+
#[constructor]
108+
fn constructor(ref self: ContractState) {
109+
self.initialize("Certificate", "CERT", "");
110+
}
111+
}

backend-sc/src/components/certificate/certificate.cairo

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ mod NFTComponent {
2121
+Drop<TContractState>,
2222
+SRC5Component::HasComponent<TContractState>,
2323
impl ERC721: ERC721Component::HasComponent<TContractState>,
24-
> of INFTComponent<ComponentState<TContractState>> {
25-
fn mint(ref self: ComponentState<TContractState>, to: ContractAddress, token_id: u256) {
26-
let mut erc721 = get_dep_component_mut!(ref self, ERC721);
27-
erc721._mint(to, token_id);
24+
> of INFTComponent<ComponentState<TContractState>> {
25+
26+
fn initializer(
27+
ref self: ComponentState<TContractState>,
28+
name: ByteArray,
29+
symbol: ByteArray,
30+
base_uri: ByteArray
31+
) {
32+
let mut erc721_comp = get_dep_component_mut!(ref self, ERC721);
33+
erc721_comp.initializer(name, symbol, base_uri);
34+
}
35+
36+
fn mint(ref self: ComponentState<TContractState>, to: ContractAddress, token_id: u256) {
37+
let mut erc721 = get_dep_component_mut!(ref self, ERC721);
38+
erc721._mint(to, token_id);
39+
}
2840
}
29-
}
3041
}

backend-sc/src/components/certificate/interface.cairo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ use starknet::ContractAddress;
22

33
#[starknet::interface]
44
trait INFTComponent<TContractState> {
5+
fn initializer(ref self: TContractState, name: ByteArray, symbol: ByteArray, base_uri: ByteArray);
56
fn mint(ref self: TContractState, to: ContractAddress, token_id: u256);
67
}

backend-sc/src/contracts/certificate.cairo

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use starknet::ContractAddress;
22

33
#[starknet::contract]
4-
mod Certificate {
4+
mod NFTCertificate {
55
use starknet::ContractAddress;
66

77
use carbon_locker::components::certificate::certificate::NFTComponent;
@@ -15,11 +15,10 @@ mod Certificate {
1515

1616
#[abi(embed_v0)]
1717
impl ERC721MixinImpl = ERC721Component::ERC721MixinImpl<ContractState>;
18-
impl ERC721MetadataImpl = ERC721Component::ERC721MetadataImpl<ContractState>;
1918
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>;
2019

2120
#[abi(embed_v0)]
22-
impl CounterImpl = NFTComponent::NFTComponentImpl<ContractState>;
21+
impl NFTComponentImpl = NFTComponent::NFTComponentImpl<ContractState>;
2322

2423
#[storage]
2524
struct Storage {
@@ -44,11 +43,6 @@ mod Certificate {
4443

4544
#[constructor]
4645
fn constructor(ref self: ContractState) {
47-
let name = "Certificate";
48-
let symbol = "CERT";
49-
let base_uri = "";
50-
51-
self.erc721.initializer(name, symbol, base_uri);
46+
self.initializer("Certificate", "CERT", "");
5247
}
5348
}
54-

backend-sc/tests/lib.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod tests_locker;
1+
//mod tests_locker;
22
mod tests_certificate;
Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
use starknet::{ContractAddress, contract_address_const, get_caller_address};
22

3-
use openzeppelin::token::erc721::interface::IERC721;
4-
//use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait};
5-
6-
use openzeppelin::token::erc721::interface::{
7-
IERC721Dispatcher,
8-
IERC721DispatcherTrait,
9-
IERC721Metadata,
10-
IERC721MetadataDispatcher,
11-
IERC721MetadataDispatcherTrait
12-
};
13-
143
use snforge_std as snf;
154
use snforge_std::{
165
ContractClassTrait, EventSpy, start_cheat_caller_address, stop_cheat_caller_address, spy_events,
@@ -21,45 +10,49 @@ use carbon_locker::components::certificate::interface::{
2110
INFTComponent, INFTComponentDispatcher, INFTComponentDispatcherTrait
2211
};
2312

24-
use carbon_locker::contracts::certificate::Certificate;
13+
use carbon_locker::contracts::certificate::NFTCertificate;
14+
15+
use openzeppelin::token::erc721::interface::IERC721;
16+
17+
use openzeppelin::token::erc721::interface::{
18+
IERC721Dispatcher,
19+
IERC721DispatcherTrait,
20+
IERC721Metadata,
21+
IERC721MetadataDispatcher,
22+
IERC721MetadataDispatcherTrait
23+
};
2524

2625
fn deploy_nft_certificate() -> ContractAddress {
27-
let contract = snf::declare("Certificate").expect('Declaration failed');
26+
let contract = snf::declare("NFTCertificate").expect('Declaration failed');
2827

2928
let mut calldata: Array<felt252> = array![];
29+
let (contract_address, _) = contract.deploy(@calldata).expect('Certificate deployment failed');
3030

31-
let (contract_address, _) = contract.deploy(@calldata).expect('NFT deployment failed');
3231
contract_address
3332
}
3433

3534
#[test]
3635
fn test_proper_initialization() {
3736
let certificate_address = deploy_nft_certificate();
3837
let user_address: ContractAddress = contract_address_const::<'USER'>();
39-
start_cheat_caller_address(certificate_address, user_address);
4038
let erc721 = IERC721Dispatcher { contract_address: certificate_address };
4139
let erc721_metadata = IERC721MetadataDispatcher { contract_address: certificate_address };
4240

43-
let name = erc721_metadata.name();
44-
assert(name == "Certificate", 'Name should be Certificate');
45-
let symbol = erc721_metadata.symbol();
46-
assert(symbol == "CERT", 'Symbol should be CERT');
47-
48-
let balance = erc721.balance_of(user_address);
41+
let mut balance = erc721.balance_of(user_address);
4942
assert(balance == 0, 'Balance should be 0');
43+
44+
let name = erc721_metadata.name();
45+
assert(name == "Certificate", 'Token name mismatch');
5046
}
5147

5248
#[test]
53-
fn test_mint() {
49+
fn test_mint_function() {
5450
let certificate_address = deploy_nft_certificate();
55-
let erc721 = IERC721Dispatcher { contract_address: certificate_address };
51+
let certificate = INFTComponentDispatcher { contract_address: certificate_address };
5652
let user_address: ContractAddress = contract_address_const::<'USER'>();
57-
start_cheat_caller_address(certificate_address, user_address);
58-
59-
let nft_component = INFTComponentDispatcher { contract_address: certificate_address };
60-
let nft_id = 1;
61-
nft_component.mint(user_address, nft_id);
53+
let erc721 = IERC721Dispatcher { contract_address: certificate_address };
6254

63-
let balance = erc721.balance_of(user_address);
55+
certificate.mint(user_address, 1);
56+
let mut balance = erc721.balance_of(user_address);
6457
assert(balance == 1, 'Balance should be 1');
6558
}

0 commit comments

Comments
 (0)