@@ -24,7 +24,7 @@ import {
2424} from '@bitgo/sdk-core' ;
2525import { TestBitGo , TestBitGoAPI } from '@bitgo/sdk-test' ;
2626import { coins } from '@bitgo/statics' ;
27- import { KeyPair , Sol , SolVerifyTransactionOptions , Tsol } from '../../src' ;
27+ import { getAmountBasedOnEndianness , KeyPair , Sol , SolVerifyTransactionOptions , Tsol } from '../../src' ;
2828import { Transaction } from '../../src/lib' ;
2929import { AtaInit , InstructionParams , TokenTransfer } from '../../src/lib/iface' ;
3030import { getAssociatedTokenAccountAddress } from '../../src/lib/utils' ;
@@ -601,6 +601,93 @@ describe('SOL:', function () {
601601 } ) ;
602602 } ) ;
603603
604+ describe ( 'getAmountBasedOnEndianness' , ( ) => {
605+ let originalArch : string ;
606+
607+ beforeEach ( ( ) => {
608+ originalArch = process . arch ;
609+ } ) ;
610+
611+ afterEach ( ( ) => {
612+ Object . defineProperty ( process , 'arch' , {
613+ value : originalArch ,
614+ writable : true ,
615+ configurable : true ,
616+ } ) ;
617+ } ) ;
618+
619+ it ( 'should return amount unchanged on non-s390x architectures' , function ( ) {
620+ Object . defineProperty ( process , 'arch' , {
621+ value : 'x64' ,
622+ writable : true ,
623+ configurable : true ,
624+ } ) ;
625+
626+ getAmountBasedOnEndianness ( '300000' ) . should . equal ( '300000' ) ;
627+ getAmountBasedOnEndianness ( '10000' ) . should . equal ( '10000' ) ;
628+ getAmountBasedOnEndianness ( '504403158265495552' ) . should . equal ( '504403158265495552' ) ;
629+ } ) ;
630+
631+ it ( 'should byte-swap small amounts on s390x (small becomes huge)' , function ( ) {
632+ Object . defineProperty ( process , 'arch' , {
633+ value : 's390x' ,
634+ writable : true ,
635+ configurable : true ,
636+ } ) ;
637+
638+ // Small amount 10,000 (0x2710) swaps to 1,163,899,028,698,562,560
639+ getAmountBasedOnEndianness ( '10000' ) . should . equal ( '1163899028698562560' ) ;
640+ } ) ;
641+
642+ it ( 'should byte-swap large amounts on s390x (large becomes tiny)' , function ( ) {
643+ Object . defineProperty ( process , 'arch' , {
644+ value : 's390x' ,
645+ writable : true ,
646+ configurable : true ,
647+ } ) ;
648+
649+ // Large amount 504,403,158,265,495,552 (0x0700000000000000) swaps to 7
650+ getAmountBasedOnEndianness ( '504403158265495552' ) . should . equal ( '7' ) ;
651+ } ) ;
652+
653+ it ( 'should handle numeric input' , function ( ) {
654+ Object . defineProperty ( process , 'arch' , {
655+ value : 's390x' ,
656+ writable : true ,
657+ configurable : true ,
658+ } ) ;
659+
660+ // Should work with numbers, not just strings
661+ getAmountBasedOnEndianness ( 10000 ) . should . equal ( '1163899028698562560' ) ;
662+ } ) ;
663+
664+ it ( 'should handle standard transaction amounts on s390x' , function ( ) {
665+ Object . defineProperty ( process , 'arch' , {
666+ value : 's390x' ,
667+ writable : true ,
668+ configurable : true ,
669+ } ) ;
670+
671+ // Standard amount 300,000 (0x493E0) swaps to large value
672+ const result = getAmountBasedOnEndianness ( '300000' ) ;
673+ // Verify it's different (swapped)
674+ result . should . not . equal ( '300000' ) ;
675+ // Verify swapping back gives original
676+ getAmountBasedOnEndianness ( result ) . should . equal ( '300000' ) ;
677+ } ) ;
678+
679+ it ( 'should handle invalid input gracefully' , function ( ) {
680+ Object . defineProperty ( process , 'arch' , {
681+ value : 's390x' ,
682+ writable : true ,
683+ configurable : true ,
684+ } ) ;
685+
686+ // Invalid BigInt input should return original string
687+ getAmountBasedOnEndianness ( 'not-a-number' ) . should . equal ( 'not-a-number' ) ;
688+ } ) ;
689+ } ) ;
690+
604691 it ( 'should accept valid address' , function ( ) {
605692 goodAddresses . forEach ( ( addr ) => {
606693 basecoin . isValidAddress ( addr ) . should . equal ( true ) ;
0 commit comments