/
main.sw
202 lines (175 loc) · 6.85 KB
/
main.sw
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
contract;
mod cast;
mod data_structures;
mod errors;
mod events;
mod interface;
mod utils;
use cast::*;
use contract_message_receiver::MessageReceiver;
use errors::BridgeFungibleTokenError;
use data_structures::MessageData;
use events::{DepositEvent, RefundRegisteredEvent, WithdrawalEvent};
use interface::{FRC20, FungibleBridge};
use reentrancy::reentrancy_guard;
use std::{
call_frames::{
contract_id,
msg_asset_id,
},
constants::ZERO_B256,
context::msg_amount,
hash::sha256,
inputs::input_message_sender,
message::send_message,
token::{
burn,
mint,
transfer,
},
u256::U256,
};
use utils::{adjust_deposit_decimals, adjust_withdrawal_decimals, encode_data};
configurable {
DECIMALS: u8 = 9u8,
BRIDGED_TOKEN_DECIMALS: u8 = 18u8,
BRIDGED_TOKEN_GATEWAY: b256 = 0x00000000000000000000000096c53cd98B7297564716a8f2E1de2C83928Af2fe,
BRIDGED_TOKEN: b256 = 0x00000000000000000000000000000000000000000000000000000000deadbeef,
NAME: str[64] = "MY_TOKEN ",
SYMBOL: str[32] = "MYTKN ",
}
storage {
refund_amounts: StorageMap<b256, StorageMap<b256, b256>> = StorageMap {},
tokens_minted: u64 = 0,
}
// Implement the process_message function required to be a message receiver
impl MessageReceiver for Contract {
#[payable]
#[storage(read, write)]
fn process_message(msg_idx: u64) {
// Protect against reentrancy attacks that could allow replaying messages
reentrancy_guard();
let input_sender = input_message_sender(msg_idx);
require(input_sender.value == BRIDGED_TOKEN_GATEWAY, BridgeFungibleTokenError::UnauthorizedSender);
let message_data = MessageData::parse(msg_idx);
require(message_data.amount != ZERO_B256, BridgeFungibleTokenError::NoCoinsSent);
// register a refund if tokens don't match
if (message_data.token != BRIDGED_TOKEN) {
register_refund(message_data.from, message_data.token, message_data.amount);
return;
};
let res_amount = adjust_deposit_decimals(message_data.amount, DECIMALS, BRIDGED_TOKEN_DECIMALS);
match res_amount {
Result::Err(_) => {
// register a refund if value can't be adjusted
register_refund(message_data.from, message_data.token, message_data.amount);
},
Result::Ok(amount) => {
let subId: SubId = ZERO_B256;
let tokenId = sha256((contract_id(), subId));
// mint tokens & update storage
mint(subId, amount);
match storage.tokens_minted.try_read() {
Option::Some(value) => storage.tokens_minted.write(value + amount),
Option::None => storage.tokens_minted.write(amount),
};
// when depositing to an address, msg_data.len is 160 bytes.
// when depositing to a contract, msg_data.len is 161 bytes.
// If msg_data.len is > 161 bytes, we must call `process_message()` on the receiving contract, forwarding the newly minted coins with the call.
match message_data.len {
160 => {
transfer(message_data.to, tokenId, amount);
},
161 => {
transfer(message_data.to, tokenId, amount);
},
_ => {
if let Identity::ContractId(id) = message_data.to {
let dest_contract = abi(MessageReceiver, id.into());
dest_contract.process_message {
coins: amount,
asset_id: tokenId,
}(msg_idx);
};
},
}
log(DepositEvent {
to: message_data.to,
from: message_data.from,
amount: amount,
});
}
}
}
}
impl FungibleBridge for Contract {
#[storage(read, write)]
fn claim_refund(originator: b256, asset: b256) {
let stored_amount = storage.refund_amounts.get(originator).get(asset).read();
require(stored_amount != ZERO_B256, BridgeFungibleTokenError::NoRefundAvailable);
// reset the refund amount to 0
storage.refund_amounts.get(originator).insert(asset, ZERO_B256);
// send a message to unlock this amount on the base layer gateway contract
send_message(BRIDGED_TOKEN_GATEWAY, encode_data(originator, stored_amount, asset), 0);
}
#[payable]
#[storage(read, write)]
fn withdraw(to: b256) {
let amount = msg_amount();
let asset_id = msg_asset_id();
require(amount != 0, BridgeFungibleTokenError::NoCoinsSent);
// TODO: We should store all the asset ids minted
// and check that the asset_id is on the list of minted assets
let origin_contract_id = sha256((contract_id(), ZERO_B256));
require(asset_id == origin_contract_id, BridgeFungibleTokenError::IncorrectAssetDeposited);
// attempt to adjust amount into base layer decimals and burn the sent tokens
let adjusted_amount = adjust_withdrawal_decimals(amount, DECIMALS, BRIDGED_TOKEN_DECIMALS).unwrap();
storage.tokens_minted.write(storage.tokens_minted.read() - amount);
burn(ZERO_B256, amount);
// send a message to unlock this amount on the base layer gateway contract
let sender = msg_sender().unwrap();
send_message(BRIDGED_TOKEN_GATEWAY, encode_data(to, adjusted_amount, BRIDGED_TOKEN), 0);
log(WithdrawalEvent {
to: to,
from: sender,
amount: amount,
});
}
fn bridged_token() -> b256 {
BRIDGED_TOKEN
}
fn bridged_token_decimals() -> u8 {
BRIDGED_TOKEN_DECIMALS
}
fn bridged_token_gateway() -> b256 {
BRIDGED_TOKEN_GATEWAY
}
}
impl FRC20 for Contract {
#[storage(read)]
fn total_supply() -> U256 {
U256::from((0, 0, 0, storage.tokens_minted.read()))
}
fn name() -> str[64] {
NAME
}
fn symbol() -> str[32] {
SYMBOL
}
fn decimals() -> u8 {
DECIMALS
}
}
// Storage-dependant private functions
#[storage(write)]
fn register_refund(from: b256, asset: b256, amount: b256) {
let previous_amount = U256::from(storage.refund_amounts.get(from).get(asset).try_read().unwrap_or(ZERO_B256));
let new_amount = U256::from(amount).add(previous_amount); // U256 has overflow checks built in;
let new_amount_b256 = <U256 as From<b256>>::into(new_amount);
storage.refund_amounts.get(from).insert(asset, new_amount_b256);
log(RefundRegisteredEvent {
from,
asset,
amount,
});
}