-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtrimmed_amount.rs
238 lines (205 loc) · 7.01 KB
/
trimmed_amount.rs
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Amounts represented in VAAs are capped at 8 decimals. This
//! means that any amount that's given as having more decimals is truncated to 8
//! decimals. On the way out, these amount have to be scaled back to the
//! original decimal amount. This module defines [`TrimmedAmount`], which
//! represents amounts that have been capped at 8 decimals.
//!
//! The functions [`trim`] and [`untrim`] take care of convertion to/from
//! this type given the original amount's decimals.
use std::io;
use crate::errors::ScalingError;
#[cfg(feature = "anchor")]
use anchor_lang::prelude::{borsh, AnchorDeserialize, AnchorSerialize, InitSpace};
use wormhole_io::{Readable, Writeable};
pub const TRIMMED_DECIMALS: u8 = 8;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(
feature = "anchor",
derive(AnchorSerialize, AnchorDeserialize, InitSpace)
)]
pub struct TrimmedAmount {
pub amount: u64,
pub decimals: u8,
}
impl PartialEq for TrimmedAmount {
fn eq(&self, other: &Self) -> bool {
assert_eq!(self.decimals, other.decimals);
self.amount == other.amount
}
}
impl Eq for TrimmedAmount {}
impl TrimmedAmount {
pub fn new(amount: u64, decimals: u8) -> Self {
Self { amount, decimals }
}
pub fn change_decimals(&self, new_decimals: u8) -> Result<Self, ScalingError> {
if new_decimals == self.decimals {
return Ok(*self);
}
let amount = self.untrim(new_decimals)?;
Ok(Self {
amount,
decimals: new_decimals,
})
}
// Integer division is allowed here. The purpose of using it here is to remove the remainder so
// there is no risk.
#[allow(clippy::integer_division)]
fn scale(amount: u64, from_decimals: u8, to_decimals: u8) -> Result<u64, ScalingError> {
if from_decimals == to_decimals {
return Ok(amount);
}
if from_decimals > to_decimals {
// [`u64::checked_pow`] expects a u32 argument
let power: u32 = (from_decimals - to_decimals).into();
// Exponentiation will overflow u64 when `power` is greater than 18
let scaling_factor: u64 = 10u64
.checked_pow(power)
.ok_or(ScalingError::OverflowExponent)?;
Ok(amount / scaling_factor)
} else {
// [`u64::checked_pow`] expects a u32 argument
let power: u32 = (to_decimals - from_decimals).into();
// Exponentiation will overflow u64 when `power` is greater than 18
let scaling_factor: u64 = 10u64
.checked_pow(power)
.ok_or(ScalingError::OverflowExponent)?;
amount
.checked_mul(scaling_factor)
.ok_or(ScalingError::OverflowScaledAmount)
}
}
pub fn trim(
amount: u64,
from_decimals: u8,
to_decimals: u8,
) -> Result<TrimmedAmount, ScalingError> {
let to_decimals = TRIMMED_DECIMALS.min(from_decimals).min(to_decimals);
let amount = Self::scale(amount, from_decimals, to_decimals)?;
Ok(Self {
amount,
decimals: to_decimals,
})
}
pub fn untrim(&self, to_decimals: u8) -> Result<u64, ScalingError> {
Self::scale(self.amount, self.decimals, to_decimals)
}
/// Removes dust from an amount, returning the amount with the removed
/// dust (expressed in the original decimals) and the trimmed amount.
/// The two amounts returned are equivalent, but (potentially) expressed in
/// different decimals.
/// Modifies `amount` as a side-effect.
pub fn remove_dust(
amount: &mut u64,
from_decimals: u8,
to_decimals: u8,
) -> Result<TrimmedAmount, ScalingError> {
let trimmed = Self::trim(*amount, from_decimals, to_decimals)?;
*amount = trimmed.untrim(from_decimals)?;
Ok(trimmed)
}
pub fn amount(&self) -> u64 {
self.amount
}
}
impl Readable for TrimmedAmount {
const SIZE: Option<usize> = Some(1 + 8);
fn read<R>(reader: &mut R) -> io::Result<Self>
where
Self: Sized,
R: io::Read,
{
// The fields of this struct are intentionally read in reverse order compared to how they are declared in the
// `TrimmedAmount` struct. This is consistent with the equivalent code in the EVM NTT implementation.
let decimals = Readable::read(reader)?;
let amount = Readable::read(reader)?;
Ok(Self { amount, decimals })
}
}
impl Writeable for TrimmedAmount {
fn write<W>(&self, writer: &mut W) -> io::Result<()>
where
W: io::Write,
{
let TrimmedAmount { amount, decimals } = self;
// The fields of this struct are intentionally written in reverse order compared to how they are declared in the
// `TrimmedAmount` struct. This is consistent with the equivalent code in the EVM NTT implementation.
decimals.write(writer)?;
amount.write(writer)?;
Ok(())
}
fn written_size(&self) -> usize {
Self::SIZE.unwrap()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_scale_overflow_exponent() {
// Check that the correct error is returned for exponent overflows.
assert_eq!(
Err(ScalingError::OverflowExponent),
TrimmedAmount::scale(100, 0, 255) // to > from
);
assert_eq!(
Err(ScalingError::OverflowExponent), // from > to
TrimmedAmount::scale(100, 255, 0)
);
}
#[test]
fn test_scale_overflow_scaled_amount() {
// Amount scaling overflow for [`scale`]. This can occur when toDecimals is greater than
// fromDecimals
assert_eq!(
Err(ScalingError::OverflowScaledAmount),
TrimmedAmount::scale(u64::MAX, 10, 11)
);
}
#[test]
fn test_trim() {
assert_eq!(
TrimmedAmount::trim(100_000_000_000_000_000, 18, 13)
.unwrap()
.amount(),
10_000_000
);
// NOOP: 11 is reduced to 7, then returns just the amount.
assert_eq!(
TrimmedAmount::trim(100_000_000_000_000_000, 7, 11)
.unwrap()
.amount(),
100_000_000_000_000_000
);
assert_eq!(
TrimmedAmount::trim(100_555_555_555_555_555, 18, 9)
.unwrap()
.untrim(18)
.unwrap(),
100_555_550_000_000_000
);
assert_eq!(
TrimmedAmount::trim(100_555_555_555_555_555, 18, 1)
.unwrap()
.untrim(18)
.unwrap(),
100_000_000_000_000_000
);
assert_eq!(
TrimmedAmount::trim(158434, 6, 3).unwrap(),
TrimmedAmount {
amount: 158,
decimals: 3
}
);
assert_eq!(
TrimmedAmount {
amount: 1,
decimals: 6,
}
.untrim(13)
.unwrap(),
10000000
);
}
}