Skip to content

Commit bc47507

Browse files
committed
Fixed issue on mosaic array sorting
Use Long to compare unsigned mosaicId Fixed issue on Uint64.toString issue (use long instead)
1 parent 13de478 commit bc47507

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/model/UInt64.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class UInt64 {
116116
* @return {string}
117117
*/
118118
public toString(): string {
119-
return this.compact().toString();
119+
return Long.fromBits(this.lower, this.higher, true).toString();
120120
}
121121

122122
/**

src/model/transaction/TransferTransaction.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as Long from 'long';
1718
import { Convert, Convert as convert } from '../../core/format';
1819
import { RawAddress } from '../../core/format/RawAddress';
1920
import { AmountDto } from '../../infrastructure/catbuffer/AmountDto';
@@ -181,7 +182,9 @@ export class TransferTransaction extends Transaction {
181182
*/
182183
public sortMosaics(): Mosaic[] {
183184
return this.mosaics.sort((a, b) => {
184-
return a.id.id.compact() - b.id.id.compact();
185+
const long_a = Long.fromBits(a.id.id.lower, a.id.id.higher, true);
186+
const long_b = Long.fromBits(b.id.id.lower, b.id.id.higher, true);
187+
return long_a.compare(long_b);
185188
});
186189
}
187190

test/model/transaction/TransferTransaction.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,31 @@ describe('TransferTransaction', () => {
332332
expect(sorted.mosaics[0].id.id.compact()).to.be.equal(100);
333333
expect(sorted.mosaics[1].id.id.compact()).to.be.equal(200);
334334
});
335+
336+
it('should sort the Mosaic array - using Hex MosaicId', () => {
337+
const mosaics = [
338+
new Mosaic(new MosaicId('D525AD41D95FCF29'), UInt64.fromUint(5)),
339+
new Mosaic(new MosaicId('77A1969932D987D7'), UInt64.fromUint(6)),
340+
new Mosaic(new MosaicId('67F2B76F28BD36BA'), UInt64.fromUint(10)),
341+
];
342+
343+
const transferTransaction = TransferTransaction.create(
344+
Deadline.create(),
345+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
346+
mosaics,
347+
PlainMessage.create('NEM'),
348+
NetworkType.MIJIN_TEST,
349+
);
350+
351+
expect(transferTransaction.mosaics[0].id.toHex()).to.be.equal('D525AD41D95FCF29');
352+
expect(transferTransaction.mosaics[1].id.toHex()).to.be.equal('77A1969932D987D7');
353+
expect(transferTransaction.mosaics[2].id.toHex()).to.be.equal('67F2B76F28BD36BA');
354+
355+
const signedTransaction = transferTransaction.signWith(account, generationHash);
356+
const sorted = CreateTransactionFromPayload(signedTransaction.payload) as TransferTransaction;
357+
expect(sorted.mosaics[0].id.toHex()).to.be.equal('67F2B76F28BD36BA');
358+
expect(sorted.mosaics[1].id.toHex()).to.be.equal('77A1969932D987D7');
359+
expect(sorted.mosaics[2].id.toHex()).to.be.equal('D525AD41D95FCF29');
360+
361+
});
335362
});

0 commit comments

Comments
 (0)