You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use a NewType `struct Wad(i128)` instead of a type alias:
70
48
71
49
```rust
72
-
//❌ BAD: Type alias
50
+
// Type alias
73
51
typeWad=i128;
74
52
75
-
//✅ GOOD: NewType
53
+
// NewType
76
54
pubstructWad(i128);
77
55
```
78
56
@@ -81,7 +59,7 @@ pub struct Wad(i128);
81
59
-**Operator Overloading**: Can implement `+`, `-`, `*`, `/` with correct semantics
82
60
-**Semantic Clarity**: Makes intent explicit in function signatures
83
61
84
-
## 3. No `From`/`Into` Traits
62
+
## 2. No `From`/`Into` Traits
85
63
86
64
We deliberately **DID NOT** implement `From<i128>` or `Into<i128>` because it's ambiguous:
87
65
@@ -96,17 +74,16 @@ Instead, we provide explicit constructors:
96
74
-`Wad::from_integer(e, 5)` - Creates 5.0 (scaled)
97
75
-`Wad::from_raw(5)` - Creates raw value 5 (0.000000000000000005)
98
76
99
-
## 4. Truncation vs Rounding
77
+
## 3. Truncation vs Rounding
100
78
101
79
All operations truncate toward zero rather than rounding:
102
80
103
81
**Why truncation?**
104
82
-**Predictable**: Same behavior as integer division
105
83
-**Conservative**: In financial calculations, truncation is often safer (e.g., don't over-calculate interest)
106
84
-**Fast**: No additional logic needed
107
-
-**Standard**: Matches Solidity and most fixed-point libraries
108
85
109
-
## 5. Operator Overloading
86
+
## 4. Operator Overloading
110
87
111
88
We provide operator overloading (`+`, `-`, `*`, `/`, `-`) for convenience:
112
89
@@ -130,7 +107,7 @@ Operator overloading is supported across WAD and native i128 types where unambig
130
107
Just like regular Rust, operator overloading does not include overflow checks:
131
108
132
109
- Use `checked_*` methods (`checked_add()`, `checked_sub()`, `checked_mul()`, etc.) when handling user inputs or when overflow is possible. These return `Option<Wad>` for safe error handling.
133
-
- Use operator overloads (`+`, `-`, `*`, `/`) when you want to save gas by skipping overflow checks, or when you're confident the operation cannot overflow.
110
+
- Use operator overloads (`+`, `-`, `*`, `/`) when you want to reduce computational overhead by skipping overflow checks, or when you're confident the operation cannot overflow.
134
111
135
112
This design follows Rust's standard library pattern: operators for performance, checked methods for safety.
136
113
@@ -206,7 +183,7 @@ overflow `i128`, but the final scaled result would still fit in `i128`.
206
183
207
184
## Token Conversions
208
185
209
-
Different tokens have different decimal places (USDC: 6, ETH: 18, BTC: 8). WAD handles these conversions:
186
+
Different tokens have different decimal places (USDC: 6, XLM: 7, ETH: 18, BTC: 8). WAD handles these conversions:
210
187
211
188
```rust
212
189
// Convert from USDC (6 decimals) to WAD
@@ -261,7 +238,7 @@ let result2 = a * (b * c); // Truncates after inner multiplication
0 commit comments