Skip to content

Commit

Permalink
[explorer] fix: Refactor few transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLaw committed Jul 25, 2024
1 parent fb218fa commit 02243ad
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 33 deletions.
6 changes: 5 additions & 1 deletion __tests__/config/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

/* Mock init http */
jest.mock('../../src/infrastructure/http', () => {
const { Address } = require('symbol-sdk');

return {
networkType: 152,
epochAdjustment: 1615853185,
Expand All @@ -15,7 +18,8 @@ jest.mock('../../src/infrastructure/http', () => {
NetworkType: 152,
NemsisTimestamp: 1637848847,
NamespaceGraceDuration: 2880,
TotalChainImportance: 7842928625000000
TotalChainImportance: 7842928625000000,
MosaicRentalSinkAddress: Address.createFromRawAddress('TATYW7IJN2TDBINTESRU66HHS5HMC4YVW7GGDRA')
},
nativeNamespaces: [
{
Expand Down
124 changes: 120 additions & 4 deletions __tests__/infrastructure/CreateTransaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Convert,
Mosaic,
MosaicId,
MosaicNonce,
NamespaceId,
NamespaceName,
NetworkType,
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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'
});
});
});
62 changes: 34 additions & 28 deletions src/infrastructure/CreateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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()
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)})`,
Expand Down

0 comments on commit 02243ad

Please sign in to comment.