1
+ module 0x1 ::M2ETHBridge {
2
+ use std::signer;
3
+ use aptos_framework::coin;
4
+ use aptos_framework::event;
5
+
6
+ struct Deposit has drop , store {
7
+ owner: address ,
8
+ token_id: u128 ,
9
+ nonce: u256 ,
10
+ amount: u128 ,
11
+ }
12
+
13
+ struct DepositEvent has drop , store {
14
+ deposit: Deposit ,
15
+ }
16
+
17
+ struct PendingWithdrawalRequest has drop , store {
18
+ owner: address ,
19
+ token_id: u128 ,
20
+ amount: u128 ,
21
+ }
22
+
23
+ struct PendingWithdrawal has drop , store {
24
+ request: PendingWithdrawalRequest ,
25
+ nonce: u256 ,
26
+ }
27
+
28
+ struct PendingWithdrawalEvent has drop , store {
29
+ pending_withdrawal: PendingWithdrawal ,
30
+ }
31
+
32
+ public entry fun deposit (
33
+ trusted: &signer ,
34
+ deposit: Deposit
35
+ ) acquires BridgeAccount {
36
+ let trusted_address = signer ::address_of (trusted);
37
+ assert !(trusted_address == @M2ETHBridge , 1 ); // Verify trusted signer
38
+
39
+ let bridge_account = borrow_global_mut <BridgeAccount >(trusted_address);
40
+ let coin = coin::withdraw <AptosCoin >(&bridge_account.coin_store, deposit.amount);
41
+ coin::deposit (deposit.owner, coin);
42
+
43
+ event::emit_event (
44
+ &mut bridge_account.deposit_events,
45
+ DepositEvent { deposit }
46
+ );
47
+ }
48
+
49
+ public entry fun withdraw (
50
+ owner: &signer ,
51
+ request: PendingWithdrawalRequest
52
+ ) acquires BridgeAccount {
53
+ let owner_address = signer ::address_of (owner);
54
+ let coin = coin::withdraw <AptosCoin >(&owner_address, request.amount);
55
+
56
+ let bridge_account = borrow_global_mut <BridgeAccount >(@M2ETHBridge );
57
+ coin::deposit (&bridge_account.coin_store, coin);
58
+
59
+ let nonce = bridge_account.nonce;
60
+ bridge_account.nonce = nonce + 1 ;
61
+
62
+ let pending_withdrawal = PendingWithdrawal {
63
+ request,
64
+ nonce,
65
+ };
66
+
67
+ event::emit_event (
68
+ &mut bridge_account.pending_withdrawal_events,
69
+ PendingWithdrawalEvent { pending_withdrawal }
70
+ );
71
+ }
72
+
73
+ public entry fun close_withdrawal_request (
74
+ trusted: &signer ,
75
+ pending_withdrawal: PendingWithdrawal
76
+ ) acquires BridgeAccount {
77
+ let trusted_address = signer ::address_of (trusted);
78
+ assert !(trusted_address == @M2ETHBridge , 1 ); // Verify trusted signer
79
+
80
+ let bridge_account = borrow_global_mut <BridgeAccount >(trusted_address);
81
+ // Implement logic to confirm and close the withdrawal request
82
+ // Remove the pending withdrawal from bridge_account
83
+ }
84
+
85
+ public entry fun claim_withdrawal_request (
86
+ owner: &signer ,
87
+ pending_withdrawal: PendingWithdrawal
88
+ ) acquires BridgeAccount {
89
+ let owner_address = signer ::address_of (owner);
90
+ assert !(pending_withdrawal.request.owner == owner_address, 1 ); // Verify owner
91
+
92
+ let bridge_account = borrow_global_mut <BridgeAccount >(@M2ETHBridge );
93
+ // Implement logic to claim unsuccessful withdrawal request and close it
94
+ // Remove the pending withdrawal from bridge_account and transfer coins back to owner
95
+ }
96
+
97
+ struct BridgeAccount has key {
98
+ coin_store: coin::CoinStore <AptosCoin >,
99
+ deposit_events: event::EventHandle <DepositEvent >,
100
+ pending_withdrawal_events: event::EventHandle <PendingWithdrawalEvent >,
101
+ nonce: u256 ,
102
+ }
103
+
104
+ fun init_module (bridge: &signer ) {
105
+ let bridge_address = signer ::address_of (bridge);
106
+ assert !(bridge_address == @M2ETHBridge , 1 ); // Verify bridge signer
107
+
108
+ move_to (bridge, BridgeAccount {
109
+ coin_store: coin::create_coin_store (bridge),
110
+ deposit_events: account::new_event_handle <DepositEvent >(bridge),
111
+ pending_withdrawal_events: account::new_event_handle <PendingWithdrawalEvent >(bridge),
112
+ nonce: 0 ,
113
+ });
114
+ }
115
+ }
0 commit comments