|
6 | 6 | "crypto/ecdsa" |
7 | 7 | "fmt" |
8 | 8 | "math/big" |
| 9 | + "strings" |
9 | 10 | "sync" |
10 | 11 | "time" |
11 | 12 |
|
@@ -135,6 +136,81 @@ func (wallet *Wallet) GetNonce() uint64 { |
135 | 136 | return wallet.confirmedNonce |
136 | 137 | } |
137 | 138 |
|
| 139 | +func (wallet *Wallet) GetReadableBalance(unitDigits, maxPreCommaDigitsBeforeTrim, digits int, addPositiveSign, trimAmount bool) string { |
| 140 | + // Initialize trimmedAmount and postComma variables to "0" |
| 141 | + fullAmount := "" |
| 142 | + trimmedAmount := "0" |
| 143 | + postComma := "0" |
| 144 | + proceed := "" |
| 145 | + amount := wallet.GetPendingBalance() |
| 146 | + |
| 147 | + if amount != nil { |
| 148 | + s := amount.String() |
| 149 | + |
| 150 | + if amount.Sign() > 0 && addPositiveSign { |
| 151 | + proceed = "+" |
| 152 | + } else if amount.Sign() < 0 { |
| 153 | + proceed = "-" |
| 154 | + s = strings.Replace(s, "-", "", 1) |
| 155 | + } |
| 156 | + |
| 157 | + l := len(s) |
| 158 | + |
| 159 | + // Check if there is a part of the amount before the decimal point |
| 160 | + switch { |
| 161 | + case l > unitDigits: |
| 162 | + // Calculate length of preComma part |
| 163 | + l -= unitDigits |
| 164 | + // Set preComma to part of the string before the decimal point |
| 165 | + trimmedAmount = s[:l] |
| 166 | + // Set postComma to part of the string after the decimal point, after removing trailing zeros |
| 167 | + postComma = strings.TrimRight(s[l:], "0") |
| 168 | + |
| 169 | + // Check if the preComma part exceeds the maximum number of digits before the decimal point |
| 170 | + if maxPreCommaDigitsBeforeTrim > 0 && l > maxPreCommaDigitsBeforeTrim { |
| 171 | + // Reduce the number of digits after the decimal point by the excess number of digits in the preComma part |
| 172 | + l -= maxPreCommaDigitsBeforeTrim |
| 173 | + if digits < l { |
| 174 | + digits = 0 |
| 175 | + } else { |
| 176 | + digits -= l |
| 177 | + } |
| 178 | + } |
| 179 | + // Check if there is only a part of the amount after the decimal point, and no leading zeros need to be added |
| 180 | + case l == unitDigits: |
| 181 | + // Set postComma to part of the string after the decimal point, after removing trailing zeros |
| 182 | + postComma = strings.TrimRight(s, "0") |
| 183 | + // Check if there is only a part of the amount after the decimal point, and leading zeros need to be added |
| 184 | + case l != 0: |
| 185 | + // Use fmt package to add leading zeros to the string |
| 186 | + d := fmt.Sprintf("%%0%dd", unitDigits-l) |
| 187 | + // Set postComma to resulting string, after removing trailing zeros |
| 188 | + postComma = strings.TrimRight(fmt.Sprintf(d, 0)+s, "0") |
| 189 | + } |
| 190 | + |
| 191 | + fullAmount = trimmedAmount |
| 192 | + if len(postComma) > 0 { |
| 193 | + fullAmount += "." + postComma |
| 194 | + } |
| 195 | + |
| 196 | + // limit floating part |
| 197 | + if len(postComma) > digits { |
| 198 | + postComma = postComma[:digits] |
| 199 | + } |
| 200 | + |
| 201 | + // set floating point |
| 202 | + if len(postComma) > 0 { |
| 203 | + trimmedAmount += "." + postComma |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if trimAmount { |
| 208 | + return proceed + trimmedAmount |
| 209 | + } |
| 210 | + |
| 211 | + return proceed + fullAmount |
| 212 | +} |
| 213 | + |
138 | 214 | func (wallet *Wallet) AwaitReady(ctx context.Context) error { |
139 | 215 | select { |
140 | 216 | case <-ctx.Done(): |
|
0 commit comments