Skip to content

Commit 748d72d

Browse files
committed
Merge branch 'update_stablecoin_decimal_formatting'
2 parents 52fe42d + 18b9f3d commit 748d72d

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Enable search transactions by note, address, or txid
2323
- Move "Export" (export transactions) to account info page
2424
- Show coinfinty logo when requesting an address
25+
- Update decimal formatting for stablecoin transactions
2526

2627
## v4.48.7
2728
- ios: fix Pocket user verification button

backend/coins/eth/coin.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,15 @@ func (coin *Coin) unitFactor(isFee bool) *big.Int {
158158
func (coin *Coin) FormatAmount(amount coinpkg.Amount, isFee bool) string {
159159
factor := coin.unitFactor(isFee)
160160
s := new(big.Rat).SetFrac(amount.BigInt(), factor).FloatString(18)
161-
return strings.TrimRight(strings.TrimRight(s, "0"), ".")
161+
s = strings.TrimRight(strings.TrimRight(s, "0"), ".")
162+
// For USDC/USDT, display 2 decimals when there's only 1
163+
if coin.unit == "USDC" || coin.unit == "USDT" {
164+
parts := strings.Split(s, ".")
165+
if len(parts) == 2 && len(parts[1]) == 1 {
166+
s += "0"
167+
}
168+
}
169+
return s
162170
}
163171

164172
// ToUnit implements coin.Coin.

backend/coins/eth/coin_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type testSuite struct {
2828
suite.Suite
2929
coin *Coin
3030
ERC20Coin *Coin
31+
USDTCoin *Coin
32+
USDCCoin *Coin
3133
}
3234

3335
func (s *testSuite) SetupTest() {
@@ -54,6 +56,30 @@ func (s *testSuite) SetupTest() {
5456
nil,
5557
erc20.NewToken("0x0000000000000000000000000000000000000001", 12),
5658
)
59+
60+
s.USDTCoin = NewCoin(
61+
nil,
62+
"eth-erc20-usdt",
63+
"Tether USD",
64+
"USDT",
65+
"ETH",
66+
params.MainnetChainConfig,
67+
"",
68+
nil,
69+
erc20.NewToken("0xdac17f958d2ee523a2206206994597c13d831ec7", 6),
70+
)
71+
72+
s.USDCCoin = NewCoin(
73+
nil,
74+
"eth-erc20-usdc",
75+
"USD Coin",
76+
"USDC",
77+
"ETH",
78+
params.MainnetChainConfig,
79+
"",
80+
nil,
81+
erc20.NewToken("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", 6),
82+
)
5783
}
5884

5985
func TestSuite(t *testing.T) {
@@ -92,6 +118,29 @@ func (s *testSuite) TestFormatAmount() {
92118
"0.123456789012345678",
93119
s.ERC20Coin.FormatAmount(coin.NewAmountFromInt64(123456789012345678), true),
94120
)
121+
122+
stablecoinTests := []struct {
123+
amount int64
124+
expected string
125+
}{
126+
{1100000, "1.10"},
127+
{1000000, "1"},
128+
{1120000, "1.12"},
129+
{1123000, "1.123"},
130+
{100000, "0.10"},
131+
{1000000000, "1000"},
132+
}
133+
134+
for _, test := range stablecoinTests {
135+
s.Require().Equal(
136+
test.expected,
137+
s.USDTCoin.FormatAmount(coin.NewAmountFromInt64(test.amount), false),
138+
)
139+
s.Require().Equal(
140+
test.expected,
141+
s.USDCCoin.FormatAmount(coin.NewAmountFromInt64(test.amount), false),
142+
)
143+
}
95144
}
96145

97146
func (s *testSuite) TestSetAmount() {

0 commit comments

Comments
 (0)