Skip to content

Commit b00a3f9

Browse files
committed
add utils scale_amount
1 parent cd48d7f commit b00a3f9

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

cairo/crates/contracts/src/utils/utils.cairo

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use starknet::ContractAddress;
2+
use core::num::traits::Pow;
23

34
pub impl U256TryIntoContractAddress of TryInto<u256, ContractAddress> {
45
fn try_into(self: u256) -> Option<ContractAddress> {
@@ -9,3 +10,49 @@ pub impl U256TryIntoContractAddress of TryInto<u256, ContractAddress> {
910
}
1011
}
1112
}
13+
14+
pub fn scale_amount(amount: u256, source_decimals: u8, target_decimals: u8) -> u256 {
15+
if source_decimals > target_decimals {
16+
amount / (10_u256.pow((source_decimals - target_decimals).into()))
17+
} else if source_decimals < target_decimals {
18+
amount * (10_u256.pow((target_decimals - source_decimals).into()))
19+
} else {
20+
amount
21+
}
22+
}
23+
24+
#[test]
25+
fn test_scale_amount_down() {
26+
let amount = 1000 * 10_u256.pow(18);
27+
let source_decimals = 18;
28+
let target_decimals = 6;
29+
let expected = 1000 * 10_u256.pow(6);
30+
assert(scale_amount(amount, source_decimals, target_decimals) == expected, 'scale down failed');
31+
}
32+
33+
#[test]
34+
fn test_scale_amount_up() {
35+
let amount = 1000 * 10_u256.pow(6);
36+
let source_decimals = 6;
37+
let target_decimals = 18;
38+
let expected = 1000 * 10_u256.pow(18);
39+
assert(scale_amount(amount, source_decimals, target_decimals) == expected, 'scale up failed');
40+
}
41+
42+
#[test]
43+
fn test_scale_amount_same_decimals() {
44+
let amount = 1000 * 10_u256.pow(18);
45+
let source_decimals = 18;
46+
let target_decimals = 18;
47+
assert(scale_amount(amount, source_decimals, target_decimals) == amount, 'same decimals failed');
48+
}
49+
50+
#[test]
51+
fn test_scale_amount_zero() {
52+
let amount = 0;
53+
let source_decimals = 18;
54+
let target_decimals = 6;
55+
assert(scale_amount(amount, source_decimals, target_decimals) == 0, 'zero amount failed');
56+
}
57+
58+

cairo/crates/token/src/extensions/hyp_erc20_dex_collateral.cairo

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ mod HypErc20DexCollateral {
1616
use contracts::client::router_component::RouterComponent;
1717
use contracts::paradex::interface::{IParaclearDispatcher, IParaclearDispatcherTrait};
1818
use contracts::utils::utils::U256TryIntoContractAddress;
19+
use contracts::utils::utils::scale_amount;
1920
use core::array::ArrayTrait;
20-
use core::num::traits::Pow;
2121
use openzeppelin::access::ownable::OwnableComponent;
2222
use openzeppelin::token::erc20::{ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
2323
use openzeppelin::upgrades::interface::IUpgradeable;
@@ -31,12 +31,6 @@ mod HypErc20DexCollateral {
3131
},
3232
};
3333

34-
// NOTE: Starknet’s version of the Keccak hash function (denoted by sn_keccak)
35-
// is defined as the first 250 bits of Ethereum’s keccak256
36-
// sn_keccak of selector for "deposit_on_behalf_of" function in the DEX contract
37-
const DEX_DEPOSIT_ON_BEHALF_OF_SELECTOR: felt252 =
38-
152884417735717128974538630286950396387019428546378603946454937413393931990;
39-
4034
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
4135
component!(path: MailboxclientComponent, storage: mailbox, event: MailBoxClientEvent);
4236
component!(path: RouterComponent, storage: router, event: RouterEvent);
@@ -173,17 +167,12 @@ mod HypErc20DexCollateral {
173167
let dex_decimals: u8 = dex_dispatcher.decimals();
174168

175169
// Scale amount from token decimals to DEX decimals
176-
let scaled_amount = if token_decimals > dex_decimals {
177-
amount_or_id / (10_u256.pow((token_decimals - dex_decimals).into()))
178-
} else if token_decimals < dex_decimals {
179-
amount_or_id * (10_u256.pow((dex_decimals - token_decimals).into()))
180-
} else {
181-
amount_or_id
182-
};
183-
184-
// Approve the DEX to spend the tokens
170+
let scaled_amount = scale_amount(amount_or_id, token_decimals, dex_decimals);
171+
172+
// Approve the DEX to spend the tokens (the original amount)
185173
token_dispatcher.approve(dex_address, amount_or_id);
186174

175+
// Deposit the tokens (the scaled amount)
187176
let amount: felt252 = scaled_amount.try_into().unwrap();
188177
dex_dispatcher.deposit_on_behalf_of(recipient, token_address, amount);
189178

0 commit comments

Comments
 (0)