-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEtherBridgeV2.rb
100 lines (77 loc) · 3.02 KB
/
EtherBridgeV2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
pragma :rubidity, "1.0.0"
import "ERC20"
import "Upgradeable"
contract :EtherBridgeV2, is: [:ERC20, :Upgradeable], upgradeable: true do
event :BridgedIn, { to: :address, amount: :uint256 }
event :InitiateWithdrawal, { from: :address, amount: :uint256, withdrawalId: :bytes32 }
event :WithdrawalComplete, { to: :address, amount: :uint256, withdrawalId: :bytes32 }
address :public, :trustedSmartContract
mapping ({ address: :uint256 }), :public, :pendingWithdrawalAmounts
mapping ({ address: array(:bytes32) }), :public, :pendingUserWithdrawalIds
mapping ({ bytes32: :uint256 }), :public, :withdrawalIdAmount
mapping ({ address: :bytes32 }), :public, :userWithdrawalId
constructor(
name: :string,
symbol: :string,
trustedSmartContract: :address
) {
require(trustedSmartContract != address(0), "Invalid smart contract")
ERC20.constructor(name: name, symbol: symbol, decimals: 18)
Upgradeable.constructor(upgradeAdmin: msg.sender)
s.trustedSmartContract = trustedSmartContract
}
function :onUpgrade, { usersToProcess: [:address] }, :public do
require(msg.sender == address(this), "Only the contract can call this function")
for i in 0...usersToProcess.length
user = usersToProcess[i]
require(s.pendingUserWithdrawalIds[user].length == 1, "Migration not possible")
withdrawalId = s.pendingUserWithdrawalIds[user].pop
amount = s.pendingWithdrawalAmounts[user]
s.pendingWithdrawalAmounts[user] = 0
s.userWithdrawalId[user] = withdrawalId
s.withdrawalIdAmount[withdrawalId] = amount
end
nil
end
function :bridgeIn, { to: :address, amount: :uint256 }, :public do
require(
msg.sender == s.trustedSmartContract,
"Only the trusted smart contract can bridge in tokens"
)
_mint(to: to, amount: amount)
emit :BridgedIn, to: to, amount: amount
end
function :bridgeOut, { amount: :uint256 }, :public do
withdrawalId = esc.currentTransactionHash
require(
s.userWithdrawalId[msg.sender] == bytes32(0),
"Withdrawal pending"
)
require(
s.withdrawalIdAmount[withdrawalId] == 0,
"Already bridged out"
)
require(amount > 0, "Invalid amount")
s.userWithdrawalId[msg.sender] = withdrawalId
s.withdrawalIdAmount[withdrawalId] = amount
_burn(from: msg.sender, amount: amount)
emit :InitiateWithdrawal, from: msg.sender, amount: amount, withdrawalId: withdrawalId
end
function :markWithdrawalComplete, {
to: :address,
withdrawalId: :bytes32
}, :public do
require(
msg.sender == s.trustedSmartContract,
"Only the trusted smart contract can mark withdrawals as complete"
)
require(
s.userWithdrawalId[to] == withdrawalId,
"Withdrawal id not found"
)
amount = s.withdrawalIdAmount[withdrawalId]
s.withdrawalIdAmount[withdrawalId] = 0
s.userWithdrawalId[to] = bytes32(0)
emit :WithdrawalComplete, to: to, amount: amount, withdrawalId: withdrawalId
end
end