| 1 | import "package:cw_core/amount/money.dart"; |
| 2 | import "package:cw_core/spl_token.dart"; |
| 3 | import "package:flutter_test/flutter_test.dart"; |
| 4 | |
| 5 | void main() { |
| 6 | group("SPL token raw amounts", () { |
| 7 | test("6 decimal token renders raw base units", () { |
| 8 | final jup = SPLToken( |
| 9 | name: "Jupiter", |
| 10 | symbol: "JUP", |
| 11 | mint: "jup", |
| 12 | mintAddress: "JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN", |
| 13 | decimal: 6, |
| 14 | ); |
| 15 | |
| 16 | expect(Money(BigInt.from(100000), jup).toStringWithPrecision(), "0.1"); |
| 17 | expect(Money(BigInt.from(931614), jup).toStringWithPrecision(), "0.931614"); |
| 18 | }); |
| 19 | |
| 20 | test("8 decimal token renders raw base units", () { |
| 21 | final xstock = SPLToken( |
| 22 | name: "Amazon xStock", |
| 23 | symbol: "AMZNX", |
| 24 | mint: "amznx", |
| 25 | mintAddress: "mintAddressWithEightDecimals", |
| 26 | decimal: 8, |
| 27 | ); |
| 28 | |
| 29 | expect(Money(BigInt.from(341694), xstock).toStringWithPrecision(), "0.00341694"); |
| 30 | }); |
| 31 | |
| 32 | test("the same raw amount means different values per decimals", () { |
| 33 | final raw = BigInt.from(100000); |
| 34 | |
| 35 | final six = SPLToken( |
| 36 | name: "Six", |
| 37 | symbol: "SIX", |
| 38 | mint: "six", |
| 39 | mintAddress: "mintAddressWithSixDecimals", |
| 40 | decimal: 6, |
| 41 | ); |
| 42 | |
| 43 | final nine = SPLToken( |
| 44 | name: "Nine", |
| 45 | symbol: "NINE", |
| 46 | mint: "nine", |
| 47 | mintAddress: "mintAddressWithNineDecimals", |
| 48 | decimal: 9, |
| 49 | ); |
| 50 | |
| 51 | final zero = SPLToken( |
| 52 | name: "Zero", |
| 53 | symbol: "ZERO", |
| 54 | mint: "zero", |
| 55 | mintAddress: "mintAddressWithZeroDecimals", |
| 56 | decimal: 0, |
| 57 | ); |
| 58 | |
| 59 | expect(Money(raw, six).toStringWithPrecision(), "0.1"); |
| 60 | expect(Money(raw, nine).toStringWithPrecision(), "0.0001"); |
| 61 | expect(Money(raw, zero).toStringWithPrecision(), "100000"); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | group("SPL token amount parsing", () { |
| 66 | test("parses whole amounts for a zero decimal token", () { |
| 67 | final zero = SPLToken( |
| 68 | name: "Zero", |
| 69 | symbol: "ZERO", |
| 70 | mint: "zero", |
| 71 | mintAddress: "mintAddressWithZeroDecimals", |
| 72 | decimal: 0, |
| 73 | ); |
| 74 | |
| 75 | expect(Money.parse("5", zero).amount, BigInt.from(5)); |
| 76 | expect(Money.parse("0", zero).amount, BigInt.zero); |
| 77 | }); |
| 78 | |
| 79 | test("parses fractional amounts for a 6 decimal token", () { |
| 80 | final jup = SPLToken( |
| 81 | name: "Jupiter", |
| 82 | symbol: "JUP", |
| 83 | mint: "jup", |
| 84 | mintAddress: "JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN", |
| 85 | decimal: 6, |
| 86 | ); |
| 87 | |
| 88 | expect(Money.parse("0.1", jup).amount, BigInt.from(100000)); |
| 89 | expect(Money.parse("10.339089", jup).amount, BigInt.from(10339089)); |
| 90 | }); |
| 91 | |
| 92 | test("rejects more fractional digits than the token has decimals", () { |
| 93 | final zero = SPLToken( |
| 94 | name: "Zero", |
| 95 | symbol: "ZERO", |
| 96 | mint: "zero", |
| 97 | mintAddress: "mintAddressWithZeroDecimals", |
| 98 | decimal: 0, |
| 99 | ); |
| 100 | |
| 101 | expect(() => Money.parse("5.5", zero), throwsFormatException); |
| 102 | }); |
| 103 | }); |
| 104 | } |