@@ -23,7 +23,7 @@ impl ClaimEncodeImpl of Encode<Claim> {
23
23
self . deposit_id,
24
24
recipient_address ,
25
25
gas_token_address ,
26
- * self . gas_amount
26
+ * self . gas_amount,
27
27
)
28
28
}
29
29
}
@@ -58,7 +58,7 @@ type NostrPublicKey = u256;
58
58
#[derive(Copy , Debug , Drop , Serde )]
59
59
pub enum DepositResult {
60
60
Transfer : ContractAddress ,
61
- Deposit : DepositId
61
+ Deposit : DepositId ,
62
62
}
63
63
64
64
#[derive(Copy , Debug , Drop , PartialEq , starknet:: Store , Serde )]
@@ -78,13 +78,14 @@ pub trait IDepositEscrow<TContractState> {
78
78
amount : u256 ,
79
79
token_address : ContractAddress ,
80
80
nostr_recipient : NostrPublicKey ,
81
- timelock : u64
81
+ timelock : u64 ,
82
82
) -> DepositResult ;
83
83
fn cancel (ref self : TContractState , deposit_id : DepositId );
84
- fn claim (ref self : TContractState , request : SocialRequest <Claim >, gas_amount : u256 );
84
+ fn claim (ref self : TContractState , request : SocialRequest <Claim >);
85
+ fn claim_with_gas (ref self : TContractState , request : SocialRequest <Claim >, gas_amount : u256 );
85
86
fn get_starknet_address (self : @ TContractState , nostr_pubkey : NostrPublicKey ) -> ContractAddress ;
86
87
fn get_nostr_address (
87
- self : @ TContractState , starknet_address : ContractAddress
88
+ self : @ TContractState , starknet_address : ContractAddress ,
88
89
) -> NostrPublicKey ;
89
90
}
90
91
@@ -96,10 +97,10 @@ pub mod DepositEscrow {
96
97
use core :: num :: traits :: Zero ;
97
98
use starknet :: account :: Call ;
98
99
use starknet :: storage :: {
99
- StoragePointerReadAccess , StoragePointerWriteAccess , StoragePathEntry , Map
100
+ StoragePointerReadAccess , StoragePointerWriteAccess , StoragePathEntry , Map ,
100
101
};
101
102
use starknet :: {get_block_timestamp, get_caller_address, get_contract_address, ContractAddress };
102
- use super :: super :: request :: {SocialRequest , SocialRequestImpl , SocialRequestTrait , };
103
+ use super :: super :: request :: {SocialRequest , SocialRequestImpl , SocialRequestTrait };
103
104
104
105
use super :: {Deposit , DepositId , DepositResult , IDepositEscrow , NostrPublicKey , Claim };
105
106
@@ -111,7 +112,7 @@ pub mod DepositEscrow {
111
112
amount : 0. into (),
112
113
token_address : 0. try_into (). unwrap (),
113
114
recipient : 0_u256 ,
114
- ttl : 0_u64
115
+ ttl : 0_u64 ,
115
116
}
116
117
}
117
118
}
@@ -125,7 +126,7 @@ pub mod DepositEscrow {
125
126
}
126
127
127
128
#[derive(Drop , starknet:: Event )]
128
- struct ClaimEvent {
129
+ pub struct ClaimEvent {
129
130
#[key]
130
131
deposit_id : DepositId ,
131
132
#[key]
@@ -137,7 +138,7 @@ pub mod DepositEscrow {
137
138
amount : u256 ,
138
139
token_address : ContractAddress ,
139
140
gas_token_address : ContractAddress ,
140
- gas_amount : u256
141
+ gas_amount : u256 ,
141
142
}
142
143
143
144
#[derive(Drop , starknet:: Event )]
@@ -197,13 +198,13 @@ pub mod DepositEscrow {
197
198
}
198
199
199
200
fn get_nostr_address (
200
- self : @ ContractState , starknet_address : ContractAddress
201
+ self : @ ContractState , starknet_address : ContractAddress ,
201
202
) -> NostrPublicKey {
202
203
self . sn_to_nostr. read (starknet_address )
203
204
}
204
205
205
206
fn get_starknet_address (
206
- self : @ ContractState , nostr_pubkey : NostrPublicKey
207
+ self : @ ContractState , nostr_pubkey : NostrPublicKey ,
207
208
) -> ContractAddress {
208
209
self . nostr_to_sn. read (nostr_pubkey )
209
210
}
@@ -213,7 +214,7 @@ pub mod DepositEscrow {
213
214
amount : u256 ,
214
215
token_address : ContractAddress ,
215
216
nostr_recipient : NostrPublicKey ,
216
- timelock : u64
217
+ timelock : u64 ,
217
218
) -> DepositResult {
218
219
let recipient = self . nostr_to_sn. read (nostr_recipient );
219
220
@@ -227,8 +228,8 @@ pub mod DepositEscrow {
227
228
nostr_recipient ,
228
229
starknet_recipient : recipient ,
229
230
amount : amount ,
230
- token_address : token_address
231
- }
231
+ token_address : token_address ,
232
+ },
232
233
);
233
234
return DepositResult :: Transfer (recipient );
234
235
}
@@ -248,8 +249,19 @@ pub mod DepositEscrow {
248
249
amount ,
249
250
token_address ,
250
251
recipient : nostr_recipient ,
251
- ttl : get_block_timestamp () + timelock
252
- }
252
+ ttl : get_block_timestamp () + timelock ,
253
+ },
254
+ );
255
+
256
+ self
257
+ . emit (
258
+ DepositEvent {
259
+ deposit_id ,
260
+ sender : get_caller_address (),
261
+ nostr_recipient ,
262
+ amount ,
263
+ token_address ,
264
+ },
253
265
);
254
266
255
267
DepositResult :: Deposit (deposit_id )
@@ -260,7 +272,7 @@ pub mod DepositEscrow {
260
272
assert! (deposit != Default :: default (), " can't find deposit" );
261
273
assert! (deposit . sender == get_caller_address (), " not authorized" );
262
274
assert! (
263
- deposit . ttl <= get_block_timestamp (), " can't cancel before timelock expiration"
275
+ deposit . ttl <= get_block_timestamp (), " can't cancel before timelock expiration" ,
264
276
);
265
277
266
278
let erc20 = IERC20Dispatcher { contract_address : deposit . token_address };
@@ -274,12 +286,43 @@ pub mod DepositEscrow {
274
286
sender : get_caller_address (),
275
287
nostr_recipient : deposit . recipient,
276
288
amount : deposit . amount,
277
- token_address : deposit . token_address
278
- }
289
+ token_address : deposit . token_address,
290
+ },
279
291
);
280
292
}
281
293
282
- fn claim (ref self : ContractState , request : SocialRequest <Claim >, gas_amount : u256 ) {
294
+ fn claim (ref self : ContractState , request : SocialRequest <Claim >) {
295
+ let claim = @ request . content;
296
+ let deposit = self . deposits. read (* claim . deposit_id);
297
+ assert! (deposit != Default :: default (), " can't find deposit" );
298
+ assert! (request . public_key == deposit . recipient, " invalid recipient" );
299
+ request . verify (). expect (' can\ ' t verify signature' );
300
+
301
+ let erc20 = IERC20Dispatcher { contract_address : deposit . token_address };
302
+ erc20 . transfer (* claim . starknet_recipient, deposit . amount);
303
+
304
+ self . nostr_to_sn. entry (request . public_key). write (* claim . starknet_recipient);
305
+ self . sn_to_nostr. entry (* claim . starknet_recipient). write (request . public_key);
306
+ self . deposits. entry (* claim . deposit_id). write (Default :: default ());
307
+
308
+ self
309
+ . emit (
310
+ ClaimEvent {
311
+ deposit_id : * claim . deposit_id,
312
+ sender : get_caller_address (),
313
+ nostr_recipient : request . public_key,
314
+ amount : deposit . amount,
315
+ starknet_recipient : * claim . starknet_recipient,
316
+ token_address : deposit . token_address,
317
+ gas_token_address : * claim . gas_token_address,
318
+ gas_amount : * claim . gas_amount,
319
+ },
320
+ );
321
+ }
322
+
323
+ fn claim_with_gas (
324
+ ref self : ContractState , request : SocialRequest <Claim >, gas_amount : u256 ,
325
+ ) {
283
326
let claim = @ request . content;
284
327
let deposit = self . deposits. read (* claim . deposit_id);
285
328
assert! (deposit != Default :: default (), " can't find deposit" );
@@ -308,8 +351,8 @@ pub mod DepositEscrow {
308
351
starknet_recipient : * claim . starknet_recipient,
309
352
token_address : deposit . token_address,
310
353
gas_token_address : * claim . gas_token_address,
311
- gas_amount : * claim . gas_amount
312
- }
354
+ gas_amount : * claim . gas_amount,
355
+ },
313
356
);
314
357
}
315
358
}
@@ -322,13 +365,13 @@ mod tests {
322
365
use snforge_std :: {
323
366
declare, ContractClass , ContractClassTrait , start_cheat_caller_address,
324
367
start_cheat_caller_address_global, stop_cheat_caller_address_global,
325
- start_cheat_block_timestamp, DeclareResultTrait
368
+ start_cheat_block_timestamp, DeclareResultTrait , spy_events, EventSpyAssertionsTrait ,
326
369
};
327
- use starknet :: {ContractAddress , get_block_timestamp, };
370
+ use starknet :: {ContractAddress , get_block_timestamp};
328
371
329
372
use super :: super :: request :: {SocialRequest , Signature };
330
373
use super :: {DepositResult , NostrPublicKey , Claim };
331
- use super :: {IDepositEscrowDispatcher , IDepositEscrowDispatcherTrait };
374
+ use super :: {DepositEscrow , IDepositEscrowDispatcher , IDepositEscrowDispatcherTrait };
332
375
333
376
fn declare_escrow () -> @ ContractClass {
334
377
declare (" DepositEscrow" ). unwrap (). contract_class ()
@@ -351,7 +394,7 @@ mod tests {
351
394
name : felt252 ,
352
395
symbol : felt252 ,
353
396
initial_supply : u256 ,
354
- recipient : ContractAddress
397
+ recipient : ContractAddress ,
355
398
) -> IERC20Dispatcher {
356
399
let mut calldata = array! [];
357
400
@@ -367,7 +410,7 @@ mod tests {
367
410
}
368
411
369
412
fn request_fixture_custom_classes (
370
- erc20_class : ContractClass , escrow_class : ContractClass
413
+ erc20_class : ContractClass , escrow_class : ContractClass ,
371
414
) -> (
372
415
SocialRequest <Claim >,
373
416
NostrPublicKey ,
@@ -394,7 +437,7 @@ mod tests {
394
437
deposit_id : 1 ,
395
438
starknet_recipient : recipient_address ,
396
439
gas_amount : 0 ,
397
- gas_token_address : erc20 . contract_address
440
+ gas_token_address : erc20 . contract_address,
398
441
};
399
442
400
443
let request = SocialRequest {
@@ -405,8 +448,8 @@ mod tests {
405
448
content : claim ,
406
449
sig : Signature {
407
450
r : 0xf1dac3f8d0d19767805ca85933bdf0e744594aeee04058eedaa29e26de087be9_u256 ,
408
- s : 0x144c4636083c7d0e3b8186c8c0bc6fa38bd9c6a629ec6e2ce5e437797a6e911c_u256
409
- }
451
+ s : 0x144c4636083c7d0e3b8186c8c0bc6fa38bd9c6a629ec6e2ce5e437797a6e911c_u256 ,
452
+ },
410
453
};
411
454
412
455
(request , recipient_public_key , sender_address , erc20 , escrow )
@@ -448,12 +491,12 @@ mod tests {
448
491
449
492
// Recipient user claim deposit
450
493
let recipient_balance_before_claim = erc20 . balance_of (recipient_address );
451
- escrow . claim (request , 0_u256 );
494
+ escrow . claim_with_gas (request , 0_u256 );
452
495
453
496
// Sender check
454
497
assert! (
455
498
sender_balance_before_deposit - amount == sender_balance_after_deposit ,
456
- " sender amount to deposit not send"
499
+ " sender amount to deposit not send" ,
457
500
);
458
501
459
502
// Recipient check
@@ -481,16 +524,16 @@ mod tests {
481
524
deposit_id : 1 ,
482
525
starknet_recipient : recipient_address ,
483
526
gas_amount : gas_amount ,
484
- gas_token_address : erc20 . contract_address
527
+ gas_token_address : erc20 . contract_address,
485
528
};
486
529
487
530
let request_gas_amount = SocialRequest {
488
531
content : claim_gas_amount ,
489
532
sig : Signature {
490
533
r : 0x68e441c1f8756b5278c815cc110efb302c2a08bcf0349328ba7bd7683e8b0b29_u256 ,
491
- s : 0xd592a5a5e9fc85334ab6801d6dde984c85d67fcd726fce38b9fb06874c25832e_u256
534
+ s : 0xd592a5a5e9fc85334ab6801d6dde984c85d67fcd726fce38b9fb06874c25832e_u256 ,
492
535
},
493
- .. request
536
+ .. request ,
494
537
};
495
538
496
539
start_cheat_caller_address_global (sender_address );
@@ -511,20 +554,20 @@ mod tests {
511
554
// Sender check
512
555
assert! (
513
556
sender_balance_before_deposit - amount == sender_balance_after_deposit ,
514
- " sender deposit amount not send"
557
+ " sender deposit amount not send" ,
515
558
);
516
559
517
560
// AFK account claim user for recipient with gas fees paid by the claim deposit
518
561
let escrow_balance_before_claim = erc20 . balance_of (escrow . contract_address);
519
562
let recipient_balance_before_claim = erc20 . balance_of (recipient_address );
520
- escrow . claim (request_gas_amount , gas_amount );
563
+ escrow . claim_with_gas (request_gas_amount , gas_amount );
521
564
522
565
// Recipient check
523
566
let recipient_balance_after_claim = erc20 . balance_of (recipient_address );
524
567
assert! (recipient_balance_before_claim == 0 , " recipient balance before claim != 0" );
525
568
assert! (
526
569
recipient_balance_after_claim == amount - gas_amount ,
527
- " recipient after claim != (amount - gas)"
570
+ " recipient after claim != (amount - gas)" ,
528
571
);
529
572
530
573
// Check gas amount receive by AFK account
@@ -552,7 +595,7 @@ mod tests {
552
595
start_cheat_caller_address (escrow . contract_address, sender_address );
553
596
escrow . deposit (amount , erc20 . contract_address, recipient_nostr_key , 10_u64 );
554
597
555
- escrow . claim (request , 1_u256 );
598
+ escrow . claim_with_gas (request , 1_u256 );
556
599
}
557
600
558
601
#[test]
@@ -579,7 +622,7 @@ mod tests {
579
622
},
580
623
.. request ,
581
624
};
582
- escrow . claim (request , 0_u256 );
625
+ escrow . claim_with_gas (request , 0_u256 );
583
626
}
584
627
585
628
@@ -607,7 +650,7 @@ mod tests {
607
650
},
608
651
.. request ,
609
652
};
610
- escrow . claim (request , 0_u256 );
653
+ escrow . claim_with_gas (request , 0_u256 );
611
654
}
612
655
613
656
#[test]
@@ -713,7 +756,7 @@ mod tests {
713
756
}
714
757
715
758
start_cheat_caller_address (escrow . contract_address, recipient_address );
716
- escrow . claim (request , 0_u256 );
759
+ escrow . claim_with_gas (request , 0_u256 );
717
760
718
761
start_cheat_caller_address (escrow . contract_address, sender_address );
719
762
let result = escrow . deposit (amount , erc20 . contract_address, recipient_nostr_key , 0_u64 );
0 commit comments