Hey guys
In progress of refactoring and optimisation my sway lend contarct I decided to storage info about user’s collaterals like compound do
I wrote a working prototype code on rust and test it
fn toggle(mut num: u16, index: u16) -> u16 {
num ^= 1 << index;
num
}
fn set_bit(mut assets_in: u16, asset_offset: u16) -> u16 {
assets_in |= 1 << asset_offset;
assets_in
}
fn clear_bit(mut assets_in: u16, asset_offset: u16) -> u16 {
assets_in &= !(1 << asset_offset);
assets_in
}
fn is_in_asset(assets_in: u16, asset_offset: u8) -> bool {
assets_in & (1 << asset_offset) != 0
}
But when I’m trying to use in sway I have errors like
error
--> /Users/alexey/projects/fuel/sway-lend/contracts/market/src/main.sw:167:24
|
165 |
166 | // set bit for asset
167 | assets_in ^= 1 << asset_offset;
| ^ Expected an expression.
168 | storage.supplied_collateral_assets.get(account, assets_in);
169 | } else if initial_user_balance != 0 && final_user_balance == 0 {
|
Or
error
--> /Users/alexey/projects/fuel/sway-lend/contracts/market/src/main.sw:167:24
|
165 |
166 | // set bit for asset
167 | assets_in |= 1 << asset_offset;
| ^ Expected an expression.
168 | storage.supplied_collateral_assets.get(account, assets_in);
169 | } else if initial_user_balance != 0 && final_user_balance == 0 {
and
error
--> /Users/alexey/projects/fuel/sway-lend/contracts/market/src/main.sw:171:24
|
169 |
170 | // clear bit for asset
171 | assets_in &= !(1 << asset_offset);
| ^ Expected an expression.
172 | storage.supplied_collateral_assets.get(account, assets_in);
173 | }
|
____
Please tell me how to use these operators in sway
And please help me write the functions set_bit
and clear_bit
.