Files
hash-wallet/cw_core/test/parse_fixed_test.dart
T
David AdegokeandGitHub 703cc22b33 fix: solana wallet bugs and security issues (#3484)
* fix android CI

* fix: duplicate outgoing tx for jup swaps and stuck pending state

* fix solana security risk by handling duplicate token symbols in wallet transactions

* feat: implement additional cost handling for pending transactions in Solana wallet.

* fix: Items on security audit list for solana wallet

* refactor: streamline fee payer index handling and improve error logging

* fix: verifySignature for solana and handle wrong mint on default token

* fix: token decimals defaulting to zero and breaking amount parsing in solana

* fix: use token mask in tx history

* refactor: apply lint to modified code

* fix: merge conflicts

* fix: use Money and mint decimals when parsing sol swaps

* test: add unit tests for SPL token amount handling and parsing

* test: add more tests

* fix: update decimal handling for fetched token and remove unused fields
2026-08-24 23:21:29 +03:00

111 lines
4.3 KiB
Dart

import 'package:cw_core/parse_fixed.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('parseFixed', () {
group('parseFixed, positive', () {
test('should parse 1.000001 as 1000001',
() => expect(parseFixed("1.000001", 6), BigInt.from(1000001)));
test('should parse 1 as 1000000', () => expect(parseFixed("1", 6), BigInt.from(1000000)));
test('should parse 1. as 1000000', () => expect(parseFixed("1.", 6), BigInt.from(1000000)));
test('should parse 1.1 as 1100000', () => expect(parseFixed("1.1", 6), BigInt.from(1100000)));
test('should parse 01.1 as 1100000',
() => expect(parseFixed("01.1", 6), BigInt.from(1100000)));
test('should parse 1100000 as 11000000',
() => expect(parseFixed("1100000", 1), BigInt.from(11000000)));
});
group('parseFixed, negative', () {
test('should parse -1.000001 as -1000001',
() => expect(parseFixed("-1.000001", 6), BigInt.from(-1000001)));
test('should parse -1 as 1000000', () => expect(parseFixed("-1", 6), BigInt.from(-1000000)));
});
group('parseFixed, no leading 0', () {
test('should parse .000001 as 1', () => expect(parseFixed(".000001", 6), BigInt.from(1)));
test('should parse .00002 as 20', () => expect(parseFixed(".00002", 6), BigInt.from(20)));
test('should parse -.00002 as -20', () => expect(parseFixed("-.00002", 6), BigInt.from(-20)));
});
group('parseFixed, failing', () {
test('should fail to parse .000.0010, too many fractional digits',
() => expect(() => parseFixed(".000.0010", 6), throwsFormatException));
test('should fail to parse .0000010, fractional component exceeds decimals',
() => expect(() => parseFixed(".0000010", 6), throwsFormatException));
test('should fail to parse `.`, missing value',
() => expect(() => parseFixed(".", 6), throwsFormatException));
});
group("parseFixed, zero decimal currency", () {
test("should parse 5 as 5", () => expect(parseFixed("5", 0), BigInt.from(5)));
test("should parse 0 as 0", () => expect(parseFixed("0", 0), BigInt.from(0)));
test("should parse 1. as 1", () => expect(parseFixed("1.", 0), BigInt.from(1)));
test("should parse -3 as -3", () => expect(parseFixed("-3", 0), BigInt.from(-3)));
test("should fail to parse 5.5, fractional component exceeds decimals",
() => expect(() => parseFixed("5.5", 0), throwsFormatException));
});
});
group('tryParseFixed', () {
group('tryParseFixed, positive', () {
test('should parse 1.000001 as 1000001',
() => expect(tryParseFixed("1.000001", 6), BigInt.from(1000001)));
test('should parse 1 as 1000000', () => expect(tryParseFixed("1", 6), BigInt.from(1000000)));
test(
'should parse 1. as 1000000', () => expect(tryParseFixed("1.", 6), BigInt.from(1000000)));
test('should parse 1.1 as 1100000',
() => expect(tryParseFixed("1.1", 6), BigInt.from(1100000)));
test('should parse 01.1 as 1100000',
() => expect(tryParseFixed("01.1", 6), BigInt.from(1100000)));
test('should parse 1100000 as 11000000',
() => expect(tryParseFixed("1100000", 1), BigInt.from(11000000)));
});
group('tryParseFixed, negative', () {
test('should parse -1.000001 as -1000001',
() => expect(tryParseFixed("-1.000001", 6), BigInt.from(-1000001)));
test('should parse -1 as 1000000',
() => expect(tryParseFixed("-1", 6), BigInt.from(-1000000)));
});
group('tryParseFixed, no leading 0', () {
test('should parse .000001 as 1', () => expect(tryParseFixed(".000001", 6), BigInt.from(1)));
test('should parse .00002 as 20', () => expect(tryParseFixed(".00002", 6), BigInt.from(20)));
test('should parse -.00002 as -20',
() => expect(tryParseFixed("-.00002", 6), BigInt.from(-20)));
});
group('tryParseFixed, return `null`', () {
test('should parse .000.0010 as null, too many fractional digits',
() => expect(tryParseFixed(".000.0010", 6), isNull));
test('should parse .0000010 as null, fractional component exceeds decimals',
() => expect(tryParseFixed(".0000010", 6), isNull));
test('should parse . as `null`, missing value', () => expect(tryParseFixed(".", 6), isNull));
});
});
}