Skip to content

Commit b394ad6

Browse files
committed
Use Uint256 for Coin amount
1 parent 520004e commit b394ad6

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

packages/std/src/coin.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use serde::{Deserialize, Serialize};
44

55
use crate::prelude::*;
66
use crate::CoinFromStrError;
7-
use crate::Uint128;
7+
use crate::Uint256;
88

99
#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq, JsonSchema)]
1010
pub struct Coin {
1111
pub denom: String,
12-
pub amount: Uint128,
12+
pub amount: Uint256,
1313
}
1414

1515
impl Coin {
16-
pub fn new(amount: impl Into<Uint128>, denom: impl Into<String>) -> Self {
16+
pub fn new(amount: impl Into<Uint256>, denom: impl Into<String>) -> Self {
1717
Coin {
1818
amount: amount.into(),
1919
denom: denom.into(),
@@ -116,7 +116,7 @@ mod tests {
116116
#[test]
117117
fn coin_implements_display() {
118118
let a = Coin {
119-
amount: Uint128::new(123),
119+
amount: Uint256::new(123),
120120
denom: "ucosm".to_string(),
121121
};
122122

@@ -131,7 +131,7 @@ mod tests {
131131
assert_eq!(
132132
a,
133133
Coin {
134-
amount: Uint128::new(123),
134+
amount: Uint256::new(123),
135135
denom: "ucosm".to_string()
136136
}
137137
);
@@ -140,7 +140,7 @@ mod tests {
140140
assert_eq!(
141141
zero,
142142
Coin {
143-
amount: Uint128::new(0),
143+
amount: Uint256::new(0),
144144
denom: "ucosm".to_string()
145145
}
146146
);
@@ -149,7 +149,7 @@ mod tests {
149149
assert_eq!(
150150
string_denom,
151151
Coin {
152-
amount: Uint128::new(42),
152+
amount: Uint256::new(42),
153153
denom: "ucosm".to_string()
154154
}
155155
);
@@ -161,7 +161,7 @@ mod tests {
161161
assert_eq!(
162162
a,
163163
vec![Coin {
164-
amount: Uint128::new(123),
164+
amount: Uint256::new(123),
165165
denom: "ucosm".to_string()
166166
}]
167167
);
@@ -170,7 +170,7 @@ mod tests {
170170
assert_eq!(
171171
zero,
172172
vec![Coin {
173-
amount: Uint128::new(0),
173+
amount: Uint256::new(0),
174174
denom: "ucosm".to_string()
175175
}]
176176
);
@@ -179,7 +179,7 @@ mod tests {
179179
assert_eq!(
180180
string_denom,
181181
vec![Coin {
182-
amount: Uint128::new(42),
182+
amount: Uint256::new(42),
183183
denom: "ucosm".to_string()
184184
}]
185185
);

packages/std/src/coins.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::fmt;
33
use core::str::FromStr;
44

55
use crate::prelude::*;
6-
use crate::{Coin, CoinsError, OverflowError, OverflowOperation, StdError, StdResult, Uint128};
6+
use crate::{Coin, CoinsError, OverflowError, OverflowOperation, StdError, StdResult, Uint256};
77

88
/// A collection of coins, similar to Cosmos SDK's `sdk.Coins` struct.
99
///
@@ -133,11 +133,11 @@ impl Coins {
133133
}
134134

135135
/// Returns the amount of the given denom or zero if the denom is not present.
136-
pub fn amount_of(&self, denom: &str) -> Uint128 {
136+
pub fn amount_of(&self, denom: &str) -> Uint256 {
137137
self.0
138138
.get(denom)
139139
.map(|c| c.amount)
140-
.unwrap_or_else(Uint128::zero)
140+
.unwrap_or_else(Uint256::zero)
141141
}
142142

143143
/// Returns the amount of the given denom if and only if this collection contains only
@@ -159,7 +159,7 @@ impl Coins {
159159
/// let coins: Coins = [coin(100, "uatom"), coin(200, "uusd")].try_into().unwrap();
160160
/// assert_eq!(coins.contains_only("uatom"), None);
161161
/// ```
162-
pub fn contains_only(&self, denom: &str) -> Option<Uint128> {
162+
pub fn contains_only(&self, denom: &str) -> Option<Uint256> {
163163
if self.len() == 1 {
164164
self.0.get(denom).map(|c| c.amount)
165165
} else {
@@ -222,11 +222,11 @@ impl Coins {
222222
///
223223
/// let uatom = iterator.next().unwrap();
224224
/// assert_eq!(uatom.denom, "uatom");
225-
/// assert_eq!(uatom.amount.u128(), 1000);
225+
/// assert_eq!(uatom.amount, Uint256::new(1000));
226226
///
227227
/// let uluna = iterator.next().unwrap();
228228
/// assert_eq!(uluna.denom, "uluna");
229-
/// assert_eq!(uluna.amount.u128(), 500);
229+
/// assert_eq!(uluna.amount, Uint256::new(500));
230230
///
231231
/// assert_eq!(iterator.next(), None);
232232
/// ```
@@ -398,14 +398,14 @@ mod tests {
398398
fn handling_zero_amount() {
399399
// create a Vec<Coin> that contains zero amounts
400400
let mut vec = mock_vec();
401-
vec[0].amount = Uint128::zero();
401+
vec[0].amount = Uint256::zero();
402402

403403
let coins = Coins::try_from(vec).unwrap();
404404
assert_eq!(coins.len(), 2);
405-
assert_ne!(coins.amount_of("ibc/1234ABCD"), Uint128::zero());
405+
assert_ne!(coins.amount_of("ibc/1234ABCD"), Uint256::zero());
406406
assert_ne!(
407407
coins.amount_of("factory/osmo1234abcd/subdenom"),
408-
Uint128::zero()
408+
Uint256::zero()
409409
);
410410

411411
// adding a coin with zero amount should not be added
@@ -432,15 +432,15 @@ mod tests {
432432
// existing denom
433433
coins.add(coin(12345, "uatom")).unwrap();
434434
assert_eq!(coins.len(), 3);
435-
assert_eq!(coins.amount_of("uatom").u128(), 24690);
435+
assert_eq!(coins.amount_of("uatom"), Uint256::new(24690));
436436

437437
// new denom
438438
coins.add(coin(123, "uusd")).unwrap();
439439
assert_eq!(coins.len(), 4);
440440

441441
// zero amount
442442
coins.add(coin(0, "uusd")).unwrap();
443-
assert_eq!(coins.amount_of("uusd").u128(), 123);
443+
assert_eq!(coins.amount_of("uusd"), Uint256::new(123));
444444

445445
// zero amount, new denom
446446
coins.add(coin(0, "utest")).unwrap();
@@ -462,7 +462,7 @@ mod tests {
462462
// partial sub
463463
coins.sub(coin(1, "uatom")).unwrap();
464464
assert_eq!(coins.len(), 1);
465-
assert_eq!(coins.amount_of("uatom").u128(), 12344);
465+
assert_eq!(coins.amount_of("uatom"), Uint256::new(12344));
466466

467467
// full sub
468468
coins.sub(coin(12344, "uatom")).unwrap();
@@ -476,7 +476,7 @@ mod tests {
476476
// sub zero, non-existent denom
477477
coins.sub(coin(0, "uatom")).unwrap();
478478
assert_eq!(coins.len(), 1);
479-
assert_eq!(coins.amount_of("uatom").u128(), 12345);
479+
assert_eq!(coins.amount_of("uatom"), Uint256::new(12345));
480480
}
481481

482482
#[test]
@@ -488,7 +488,7 @@ mod tests {
488488
// happy path
489489
let coins = Coins::from(coin(12345, "uatom"));
490490
assert_eq!(coins.len(), 1);
491-
assert_eq!(coins.amount_of("uatom").u128(), 12345);
491+
assert_eq!(coins.amount_of("uatom"), Uint256::new(12345));
492492
}
493493

494494
#[test]
@@ -524,6 +524,6 @@ mod tests {
524524
.eq(coins.to_vec().iter().map(|c| &c.denom)));
525525

526526
// can still use the coins afterwards
527-
assert_eq!(coins.amount_of("uatom").u128(), 12345);
527+
assert_eq!(coins.amount_of("uatom"), Uint256::new(12345));
528528
}
529529
}

packages/std/src/testing/message_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn message_info(sender: &Addr, funds: &[Coin]) -> MessageInfo {
4949

5050
#[cfg(test)]
5151
mod tests {
52-
use crate::{coins, Uint128};
52+
use crate::{coins, Uint256};
5353

5454
use super::*;
5555

@@ -72,7 +72,7 @@ mod tests {
7272
MessageInfo {
7373
sender: addr.clone(),
7474
funds: vec![Coin {
75-
amount: Uint128::new(123),
75+
amount: Uint256::new(123),
7676
denom: "foo".to_string(),
7777
}],
7878
}

packages/std/src/testing/mock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::query::{DelegatorWithdrawAddressResponse, DistributionQuery};
3838
use crate::results::{ContractResult, Empty, SystemResult};
3939
use crate::traits::{Api, Querier, QuerierResult};
4040
use crate::types::{BlockInfo, ContractInfo, Env, TransactionInfo};
41-
use crate::{from_json, to_json_binary, Binary, Uint128};
41+
use crate::{from_json, to_json_binary, Binary, Uint256};
4242
#[cfg(feature = "cosmwasm_1_3")]
4343
use crate::{
4444
query::{AllDenomMetadataResponse, DecCoin, DenomMetadataResponse},
@@ -743,7 +743,7 @@ impl Default for WasmQuerier {
743743
pub struct BankQuerier {
744744
#[allow(dead_code)]
745745
/// BTreeMap<denom, amount>
746-
supplies: BTreeMap<String, Uint128>,
746+
supplies: BTreeMap<String, Uint256>,
747747
/// A map from address to balance. The address is the String conversion of `Addr`,
748748
/// i.e. the bech32 encoded address.
749749
balances: BTreeMap<String, Vec<Coin>>,
@@ -784,15 +784,15 @@ impl BankQuerier {
784784
.collect();
785785
}
786786

787-
fn calculate_supplies(balances: &BTreeMap<String, Vec<Coin>>) -> BTreeMap<String, Uint128> {
787+
fn calculate_supplies(balances: &BTreeMap<String, Vec<Coin>>) -> BTreeMap<String, Uint256> {
788788
let mut supplies = BTreeMap::new();
789789

790790
let all_coins = balances.iter().flat_map(|(_, coins)| coins);
791791

792792
for coin in all_coins {
793793
*supplies
794794
.entry(coin.denom.clone())
795-
.or_insert_with(Uint128::zero) += coin.amount;
795+
.or_insert_with(Uint256::zero) += coin.amount;
796796
}
797797

798798
supplies
@@ -806,7 +806,7 @@ impl BankQuerier {
806806
.supplies
807807
.get(denom)
808808
.cloned()
809-
.unwrap_or_else(Uint128::zero);
809+
.unwrap_or_else(Uint256::zero);
810810
let bank_res = SupplyResponse {
811811
amount: Coin {
812812
amount,

packages/std/src/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ mod tests {
651651

652652
use super::*;
653653
use crate::testing::MockQuerier;
654-
use crate::{coins, Uint128};
654+
use crate::{coins, Uint256};
655655

656656
// this is a simple demo helper to prove we can use it
657657
fn demo_helper(_querier: &dyn Querier) -> u64 {
@@ -688,7 +688,7 @@ mod tests {
688688
.unwrap()
689689
.unwrap();
690690
let balance: BalanceResponse = from_json(raw).unwrap();
691-
assert_eq!(balance.amount.amount, Uint128::new(5));
691+
assert_eq!(balance.amount.amount, Uint256::new(5));
692692
}
693693

694694
#[cfg(feature = "cosmwasm_1_1")]

packages/vm/src/instance.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ mod tests {
541541
mock_instance_with_balances, mock_instance_with_failing_api, mock_instance_with_gas_limit,
542542
mock_instance_with_options, MockInstanceOptions,
543543
};
544-
use cosmwasm_std::{coin, coins, from_json, BalanceResponse, BankQuery, Empty, QueryRequest};
544+
use cosmwasm_std::{
545+
coin, coins, from_json, BalanceResponse, BankQuery, Empty, QueryRequest, Uint256,
546+
};
545547
use wasmer::FunctionEnvMut;
546548

547549
const KIB: usize = 1024;
@@ -1005,7 +1007,7 @@ mod tests {
10051007
.unwrap()
10061008
.unwrap();
10071009
let BalanceResponse { amount, .. } = from_json(response).unwrap();
1008-
assert_eq!(amount.amount.u128(), 8000);
1010+
assert_eq!(amount.amount, Uint256::new(8000));
10091011
assert_eq!(amount.denom, "silver");
10101012
Ok(())
10111013
})
@@ -1036,7 +1038,7 @@ mod tests {
10361038
.unwrap()
10371039
.unwrap();
10381040
let BalanceResponse { amount, .. } = from_json(response).unwrap();
1039-
assert_eq!(amount.amount.u128(), 500);
1041+
assert_eq!(amount.amount, Uint256::new(500));
10401042
Ok(())
10411043
})
10421044
.unwrap();
@@ -1065,7 +1067,7 @@ mod tests {
10651067
.unwrap()
10661068
.unwrap();
10671069
let BalanceResponse { amount, .. } = from_json(response).unwrap();
1068-
assert_eq!(amount.amount.u128(), 8000);
1070+
assert_eq!(amount.amount, Uint256::new(8000));
10691071
Ok(())
10701072
})
10711073
.unwrap();

0 commit comments

Comments
 (0)