1
1
use SimpleLoanSimpleProposal :: Proposal ;
2
+ use pwn :: loan :: lib :: signature_checker :: Signature ;
2
3
use pwn :: loan :: terms :: simple :: loan :: types :: Terms ;
3
4
4
5
#[starknet:: interface]
@@ -10,24 +11,39 @@ trait ISimpleLoanSimpleProposal<TState> {
10
11
refinancing_loan_id : felt252 ,
11
12
proposal_data : Array <felt252 >,
12
13
proposal_inclusion_proof : Array <felt252 >,
13
- signature : felt252
14
+ signature : Signature
14
15
) -> (felt252 , Terms );
15
16
fn get_proposal_hash (self : @ TState , proposal : Proposal ) -> felt252 ;
16
- fn encoded_proposal_data (self : @ TState , proposal : Proposal ) -> felt252 ;
17
- fn decode_proposal_data (self : @ TState , encoded_data : felt252 ) -> Proposal ;
17
+ fn encode_proposal_data (self : @ TState , proposal : Proposal ) -> Array < felt252 > ;
18
+ fn decode_proposal_data (self : @ TState , encoded_data : Array < felt252 > ) -> Proposal ;
18
19
}
19
20
20
21
#[starknet:: contract]
21
22
mod SimpleLoanSimpleProposal {
22
23
use pwn :: ContractAddressDefault ;
24
+ use pwn :: loan :: lib :: serialization;
25
+ use pwn :: loan :: terms :: simple :: proposal :: simple_loan_proposal :: {
26
+ SimpleLoanProposalComponent , SimpleLoanProposalComponent :: ProposalBase
27
+ };
23
28
use pwn :: multitoken :: library :: MultiToken ;
24
29
use starknet :: ContractAddress ;
30
+ use super :: {Signature , Terms };
31
+
32
+ component! (
33
+ path : SimpleLoanProposalComponent , storage : simple_loan , event : SimpleLoanProposalEvent
34
+ );
35
+
36
+ #[abi(embed_v0)]
37
+ impl SimpleLoanProposalImpl =
38
+ SimpleLoanProposalComponent :: SimpleLoanProposalImpl <ContractState >;
39
+ impl SimpleLoanProposalInternal = SimpleLoanProposalComponent :: InternalImpl <ContractState >;
25
40
26
41
// NOTE: we can hard code this by calculating the poseidon hash of the string
27
42
// in the Solidity contract offline.
28
- const PROPOSAL_TYPEHASH : felt252 = 0 ;
43
+ const PROPOSAL_TYPEHASH : felt252 =
44
+ 0x035f73ebb16e8e796c727d020a1a8f0190005ba40559ee87674fb8de78939b1a ;
29
45
30
- #[derive(Default , Drop , Serde )]
46
+ #[derive(Copy , Default , Drop , Serde )]
31
47
pub struct Proposal {
32
48
collateral_category : MultiToken :: Category ,
33
49
collateral_address : ContractAddress ,
@@ -44,7 +60,7 @@ mod SimpleLoanSimpleProposal {
44
60
expiration : u64 ,
45
61
allowed_acceptor : ContractAddress ,
46
62
proposer : ContractAddress ,
47
- proposer_specHash : felt252 ,
63
+ proposer_spec_hash : felt252 ,
48
64
is_offer : bool ,
49
65
refinancing_loan_id : felt252 ,
50
66
nonce_space : felt252 ,
@@ -54,12 +70,17 @@ mod SimpleLoanSimpleProposal {
54
70
55
71
56
72
#[storage]
57
- struct Storage {}
73
+ struct Storage {
74
+ #[substorage(v0)]
75
+ simple_loan : SimpleLoanProposalComponent :: Storage ,
76
+ }
58
77
59
78
#[event]
60
79
#[derive(Drop , starknet:: Event )]
61
80
enum Event {
62
81
ProposalMade : ProposalMade ,
82
+ #[flat]
83
+ SimpleLoanProposalEvent : SimpleLoanProposalComponent :: Event ,
63
84
}
64
85
65
86
#[derive(Drop , starknet:: Event )]
@@ -74,34 +95,108 @@ mod SimpleLoanSimpleProposal {
74
95
ref self : ContractState ,
75
96
hub : ContractAddress ,
76
97
revoke_nonce : ContractAddress ,
77
- config : ContractAddress
78
- ) {}
98
+ config : ContractAddress ,
99
+ name : felt252 ,
100
+ version : felt252 ,
101
+ ) {
102
+ self . simple_loan. _initialize (hub , revoke_nonce , config , name , version );
103
+ }
79
104
80
105
#[abi(embed_v0)]
81
106
impl SimpleLoanSimpleProposalImpl of super :: ISimpleLoanSimpleProposal <ContractState > {
82
- fn make_proposal (ref self : ContractState , proposal : Proposal ) {}
107
+ fn make_proposal (ref self : ContractState , proposal : Proposal ) {
108
+ let proposal_hash = self . get_proposal_hash (proposal );
109
+ self . simple_loan. _make_proposal (proposal_hash , proposal . proposer);
110
+
111
+ self . emit (ProposalMade { proposal_hash , proposer : proposal . proposer, proposal , });
112
+ }
83
113
84
114
fn accept_proposal (
85
115
ref self : ContractState ,
86
116
acceptor : starknet :: ContractAddress ,
87
117
refinancing_loan_id : felt252 ,
88
118
proposal_data : Array <felt252 >,
89
119
proposal_inclusion_proof : Array <felt252 >,
90
- signature : felt252
120
+ signature : Signature
91
121
) -> (felt252 , super :: Terms ) {
92
122
(0 , Default :: default ())
93
123
}
94
124
95
125
fn get_proposal_hash (self : @ ContractState , proposal : Proposal ) -> felt252 {
96
- 0
126
+ let mut serialized_proposal = array! [];
127
+ proposal . serialize (ref serialized_proposal );
128
+ self . simple_loan. _get_proposal_hash (PROPOSAL_TYPEHASH , serialized_proposal )
129
+ }
130
+
131
+ fn encode_proposal_data (self : @ ContractState , proposal : Proposal ,) -> Array <felt252 > {
132
+ let mut serialized_proposal = array! [];
133
+ proposal . serialize (ref serialized_proposal );
134
+
135
+ serialized_proposal
97
136
}
98
137
99
- fn encoded_proposal_data (self : @ ContractState , proposal : Proposal , ) -> felt252 {
100
- 0
138
+ fn decode_proposal_data (self : @ ContractState , encoded_data : Array < felt252 > ) -> Proposal {
139
+ self . decode_serde_proposal ( encoded_data . span ())
101
140
}
141
+ }
142
+
143
+ #[generate_trait]
144
+ impl Private of PrivateTrait {
145
+ fn decode_serde_proposal (self : @ ContractState , data : Span <felt252 >) -> Proposal {
146
+ let collateral_category = match * data . at (0 ) {
147
+ 0 => MultiToken :: Category :: ERC20 ,
148
+ 1 => MultiToken :: Category :: ERC721 ,
149
+ 2 => MultiToken :: Category :: ERC1155 ,
150
+ _ => panic! (" Invalid collateral category" ),
151
+ };
152
+ let collateral_address : ContractAddress = (* data . at (1 )). try_into (). unwrap ();
153
+ let collateral_low : u128 = (* data . at (3 )). try_into (). unwrap ();
154
+ let collateral_high : u128 = (* data . at (4 )). try_into (). unwrap ();
155
+ let credit_address : ContractAddress = (* data . at (7 )). try_into (). unwrap ();
156
+ let credit_low : u128 = (* data . at (8 )). try_into (). unwrap ();
157
+ let credit_high : u128 = (* data . at (9 )). try_into (). unwrap ();
158
+ let credit_limit_low : u128 = (* data . at (10 )). try_into (). unwrap ();
159
+ let credit_limit_high : u128 = (* data . at (11 )). try_into (). unwrap ();
160
+ let fixed_interest_low : u128 = (* data . at (12 )). try_into (). unwrap ();
161
+ let fixed_interest_high : u128 = (* data . at (13 )). try_into (). unwrap ();
162
+ let accruing_interest_APR: u32 = (* data . at (14 )). try_into (). unwrap ();
163
+ let duration : u64 = (* data . at (15 )). try_into (). unwrap ();
164
+ let expiration : u64 = (* data . at (16 )). try_into (). unwrap ();
165
+ let allowed_acceptor : ContractAddress = (* data . at (17 )). try_into (). unwrap ();
166
+ let proposer : ContractAddress = (* data . at (18 )). try_into (). unwrap ();
167
+ let loan_contract : ContractAddress = (* data . at (24 )). try_into (). unwrap ();
102
168
103
- fn decode_proposal_data (self : @ ContractState , encoded_data : felt252 ) -> Proposal {
104
- Default :: default ()
169
+ Proposal {
170
+ collateral_category ,
171
+ collateral_address ,
172
+ collateral_id : * data . at (2 ),
173
+ collateral_amount : u256 { low : collateral_low , high : collateral_high },
174
+ check_collateral_state_fingerprint : if * data . at (5 ) == 1 {
175
+ true
176
+ } else {
177
+ false
178
+ },
179
+ collateral_state_fingerprint : * data . at (6 ),
180
+ credit_address ,
181
+ credit_amount : u256 { low : credit_low , high : credit_high },
182
+ available_credit_limit : u256 { low : credit_limit_low , high : credit_limit_high },
183
+ fixed_interest_amount : u256 { low : fixed_interest_low , high : fixed_interest_high },
184
+ accruing_interest_APR,
185
+ duration ,
186
+ expiration ,
187
+ allowed_acceptor ,
188
+ proposer ,
189
+ proposer_spec_hash : * data . at (19 ),
190
+ is_offer : if * data . at (20 ) == 1 {
191
+ true
192
+ } else {
193
+ false
194
+ },
195
+ refinancing_loan_id : * data . at (21 ),
196
+ nonce_space : * data . at (22 ),
197
+ nonce : * data . at (23 ),
198
+ loan_contract ,
199
+ }
105
200
}
106
201
}
107
202
}
0 commit comments