diff --git a/__tests__/config/jest.setup.js b/__tests__/config/jest.setup.js index 6bc3b5bd6..3aafdd4dc 100644 --- a/__tests__/config/jest.setup.js +++ b/__tests__/config/jest.setup.js @@ -1,5 +1,8 @@ + /* Mock init http */ jest.mock('../../src/infrastructure/http', () => { + const { Address } = require('symbol-sdk'); + return { networkType: 152, epochAdjustment: 1615853185, @@ -15,7 +18,8 @@ jest.mock('../../src/infrastructure/http', () => { NetworkType: 152, NemsisTimestamp: 1637848847, NamespaceGraceDuration: 2880, - TotalChainImportance: 7842928625000000 + TotalChainImportance: 7842928625000000, + MosaicRentalSinkAddress: Address.createFromRawAddress('TATYW7IJN2TDBINTESRU66HHS5HMC4YVW7GGDRA') }, nativeNamespaces: [ { diff --git a/__tests__/infrastructure/CreateTransaction.spec.js b/__tests__/infrastructure/CreateTransaction.spec.js index 862f0f113..8cd180a5b 100644 --- a/__tests__/infrastructure/CreateTransaction.spec.js +++ b/__tests__/infrastructure/CreateTransaction.spec.js @@ -8,6 +8,7 @@ import { Convert, Mosaic, MosaicId, + MosaicNonce, NamespaceId, NamespaceName, NetworkType, @@ -70,11 +71,16 @@ describe('CreateTransaction', () => { targetMosaicId: new MosaicId('7F2D26E89342D398') }; - const getMosaicAliasNamesStub = stub(Helper, 'getMosaicAliasNames'); - getMosaicAliasNamesStub.returns(Promise.resolve(['N/A'])); - // Act: - const mosaicMetadataObject = await CreateTransaction.mosaicMetadata(mockMosaicMetadata); + const mosaicMetadataObject = await CreateTransaction.mosaicMetadata(mockMosaicMetadata, { + mosaicNames: [{ + names: [], + mosaicId: '7F2D26E89342D398' + }], + unresolvedMosaicsMap: { + '7F2D26E89342D398': '7F2D26E89342D398' + } + }); // Assert: expect(mosaicMetadataObject.transactionBody).toEqual({ @@ -282,4 +288,114 @@ describe('CreateTransaction', () => { }] }); }); + + it('returns mosaicDefinition', async () => { + // Arrange: + const mockMosaicDefinition = { + type: 16717, + network: 152, + version: 1, + mosaicId: new MosaicId('7F2D26E89342D398'), + divisibility: 0, + duration: UInt64.fromUint(1000), + nonce: MosaicNonce.createRandom(), + flags: { + supplyMutable: false, + transferable: true, + restrictable: true, + revokable: true + } + }; + + // Act: + const mosaicDefinitionObject = await CreateTransaction.mosaicDefinition(mockMosaicDefinition, { + unresolvedMosaicsMap: { + '7F2D26E89342D398': '7F2D26E89342D398' + } + }); + + // Assert: + expect(mosaicDefinitionObject.transactionBody).toEqual({ + transactionType: mockMosaicDefinition.type, + recipient: 'TATYW7IJN2TDBINTESRU66HHS5HMC4YVW7GGDRA', + mosaicId: '7F2D26E89342D398', + divisibility: 0, + duration: 1000, + nonce: mockMosaicDefinition.nonce.toHex(), + supplyMutable: false, + transferable: true, + restrictable: true, + revokable: true + }); + }); + + const assertMosaicSupplyChange = async (action, expectedAction) => { + // Arrange: + const mockMosaicSupplyChange = { + type: 16973, + mosaicId: new MosaicId('7F2D26E89342D398'), + action: action, + delta: UInt64.fromUint(100) + }; + + // Act: + const mosaicSupplyChangeObject = await CreateTransaction.mosaicSupplyChange(mockMosaicSupplyChange, { + unresolvedMosaicsMap: { + '7F2D26E89342D398': '7F2D26E89342D398' + } + }); + + // Assert: + expect(mosaicSupplyChangeObject.transactionBody).toEqual({ + transactionType: mockMosaicSupplyChange.type, + mosaicId: '7F2D26E89342D398', + action: expectedAction, + delta: 100 + }); + }; + + it('returns mosaicSupplyChange in increase action', async () => { + await assertMosaicSupplyChange(1, 'Increase'); + }); + + it('returns mosaicSupplyChange in decrease action', async () => { + await assertMosaicSupplyChange(0, 'Decrease'); + }); + + it('returns mosaicAddressRestriction', async () => { + // Arrange: + const mockMosaicAddressRestriction = { + type: 16977, + mosaicId: new MosaicId('7F2D26E89342D398'), + targetAddress: randomAddress, + restrictionKey: UInt64.fromUint(1), + previousRestrictionValue: UInt64.fromUint(10), + newRestrictionValue: UInt64.fromUint(20), + transactionInfo: { + height: UInt64.fromUint(1) + } + }; + + // Act: + const mosaicAddressRestrictionObject = await CreateTransaction.mosaicAddressRestriction(mockMosaicAddressRestriction, { + mosaicNames: [{ + names: [], + mosaicId: '7F2D26E89342D398' + }], + unresolvedMosaicsMap: { + '7F2D26E89342D398': '7F2D26E89342D398' + } + }); + + // Assert: + expect(mosaicAddressRestrictionObject.transactionBody).toEqual({ + transactionType: mockMosaicAddressRestriction.type, + mosaicId: '7F2D26E89342D398', + mosaicAliasNames: ['N/A'], + targetAddress: randomAddress.plain(), + restrictionKey: '0000000000000001', + previousRestrictionValue: '10', + newRestrictionValue: '20' + }); + }); }); diff --git a/src/infrastructure/CreateTransaction.js b/src/infrastructure/CreateTransaction.js index d34ffade5..293fb2537 100644 --- a/src/infrastructure/CreateTransaction.js +++ b/src/infrastructure/CreateTransaction.js @@ -19,7 +19,7 @@ import http from './http'; import { Constants } from '../config'; import helper from '../helper'; -import { NamespaceService } from '../infrastructure'; +import { MosaicService, NamespaceService } from '../infrastructure'; import { Address, Convert, Mosaic, MosaicId } from 'symbol-sdk'; class CreateTransaction { @@ -116,15 +116,16 @@ class CreateTransaction { }; }; - static mosaicDefinition = async transactionObj => { - const resolvedMosaic = await helper.resolveMosaicId(transactionObj.mosaicId); + static mosaicDefinition = async (transactionObj, { + unresolvedMosaicsMap + }) => { return { ...transactionObj, transactionBody: { transactionType: transactionObj.type, recipient: http.networkConfig.MosaicRentalSinkAddress.address, - mosaicId: resolvedMosaic.toHex(), + mosaicId: unresolvedMosaicsMap[transactionObj.mosaicId.toHex()], divisibility: transactionObj.divisibility, duration: transactionObj.duration.compact(), nonce: transactionObj.nonce.toHex(), @@ -136,14 +137,14 @@ class CreateTransaction { }; }; - static mosaicSupplyChange = async transactionObj => { - const resolvedMosaic = await helper.resolveMosaicId(transactionObj.mosaicId); - + static mosaicSupplyChange = async (transactionObj, { + unresolvedMosaicsMap + }) => { return { ...transactionObj, transactionBody: { transactionType: transactionObj.type, - mosaicId: resolvedMosaic.toHex(), + mosaicId: unresolvedMosaicsMap[transactionObj.mosaicId.toHex()], action: Constants.MosaicSupplyChangeAction[transactionObj.action], delta: transactionObj.delta.compact() } @@ -331,23 +332,26 @@ class CreateTransaction { }; }; - static mosaicAddressRestriction = async transactionObj => { + static mosaicAddressRestriction = async (transactionObj, { + mosaicNames, + unresolvedMosaicsMap + }) => { const { transactionInfo } = transactionObj; - const [resolvedMosaic, targetAddress] = await Promise.all([ - helper.resolveMosaicId(transactionObj.mosaicId), - helper.resolvedAddress( - transactionObj.targetAddress, - transactionInfo.height - ) - ]); - const mosaicAliasNames = await helper.getMosaicAliasNames(resolvedMosaic); + const targetAddress = await helper.resolvedAddress( + transactionObj.targetAddress, + transactionInfo.height + ); + + const mosaicAliasNames = MosaicService.extractMosaicNamespace({ + mosaicId: unresolvedMosaicsMap[transactionObj.mosaicId.toHex()] + },mosaicNames); return { ...transactionObj, transactionBody: { transactionType: transactionObj.type, - mosaicId: resolvedMosaic.toHex(), + mosaicId: unresolvedMosaicsMap[transactionObj.mosaicId.toHex()], mosaicAliasNames, targetAddress: targetAddress, restrictionKey: transactionObj.restrictionKey.toHex(), @@ -404,24 +408,26 @@ class CreateTransaction { }; }; - static mosaicMetadata = async transactionObj => { + static mosaicMetadata = async (transactionObj, { + mosaicNames, + unresolvedMosaicsMap + }) => { const { transactionInfo } = transactionObj; - const [resolvedMosaic, resolvedAddress] = await Promise.all([ - helper.resolveMosaicId(transactionObj.targetMosaicId), - helper.resolvedAddress( - transactionObj.targetAddress, - transactionInfo.height - ) - ]); + const resolvedAddress = await helper.resolvedAddress( + transactionObj.targetAddress, + transactionInfo.height + ); - const mosaicAliasNames = await helper.getMosaicAliasNames(resolvedMosaic); + const mosaicAliasNames = MosaicService.extractMosaicNamespace({ + mosaicId: unresolvedMosaicsMap[transactionObj.targetMosaicId.toHex()] + }, mosaicNames); return { ...transactionObj, transactionBody: { transactionType: transactionObj.type, scopedMetadataKey: transactionObj.scopedMetadataKey.toHex(), - targetMosaicId: resolvedMosaic.toHex(), + targetMosaicId: unresolvedMosaicsMap[transactionObj.targetMosaicId.toHex()], targetMosaicAliasNames: mosaicAliasNames, targetAddress: resolvedAddress, metadataValue: `${Convert.uint8ToHex(transactionObj.value)} (Text: ${Convert.uint8ToUtf8(transactionObj.value)})`,