Skip to content

Commit

Permalink
[explorer] bug: mosaics missing in receipts section #1109
Browse files Browse the repository at this point in the history
* [explorer] task: moving out MosaicInfoAndNamespace logic to independent, so it can be reuse in receipt statement

* [explorer] task: fix showing hashLock and secretLock

* [explorer] bug: fix mosaics object missing on receipt table

* [explorer] task: add hash lock and secret lock unit test into AccountService and TransactionService

* [explorer] task: add receipt statement unit test

* [explorer] task: add artifact expiry receipt unit test

* [explorer] task: patch review fix

* [explorer] task: rename CreateReceiptTransaction -> ReceiptExtractor

* [explorer] task: update words to lowerCamelCase
  • Loading branch information
AnthonyLaw authored Sep 16, 2022
1 parent 9a758ad commit 3f75524
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 44 deletions.
69 changes: 68 additions & 1 deletion __tests__/TestHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Mosaic,
UInt64,
MosaicId,
TransactionType
TransactionType,
} from 'symbol-sdk';

const generateRandomHash = (length = 32) => {
Expand Down Expand Up @@ -41,6 +41,13 @@ const transactionCommonField = {
},
}

const receiptCommonField = {
amount: UInt64.fromUint(10000000),
height: UInt64.fromUint(1000),
mosaicId: new MosaicId('6BED913FA20223F8'),
version: 1
}

const TestHelper = {
/**
* Mock Account info.
Expand Down Expand Up @@ -295,6 +302,66 @@ const TestHelper = {
mosaic: new Mosaic(new MosaicId('6BED913FA20223F8'), UInt64.fromUint(10)),
type: TransactionType.HASH_LOCK,
};
},
createFormattedHashLockTransaction: (status) => {
return {
amount: UInt64.fromUint(10000000),
endHeight: 10,
hash: generateRandomHash(64),
mosaicId: new MosaicId('6BED913FA20223F8'),
ownerAddress: Account.generateNewAccount(NetworkType.TEST_NET).address.plain(),
recordId: '631FA269464297FBEBEFE0ED',
status,
version: 1
}
},
createFormattedSecretLockTransaction: (mosaicIdHex, amount, status) => {
return {
amount: UInt64.fromUint(amount),
compositeHash: generateRandomHash(64),
hashAlgorithm: "Sha3 256",
endHeight: 10,
mosaicId: new MosaicId(mosaicIdHex),
ownerAddress: Account.generateNewAccount(NetworkType.TEST_NET).address.plain(),
recipient: Account.generateNewAccount(NetworkType.TEST_NET).address.plain(),
recordId: '631FA269464297FBEBEFE0ED',
secret: "112233445566",
status,
version: 1
}

},
mockBalanceChangeReceipt: (amount, mosaicIdHex, type) => {
return {
...receiptCommonField,
amount: UInt64.fromUint(amount),
mosaicId: new MosaicId(mosaicIdHex),
targetAddress: Account.generateNewAccount(NetworkType.TEST_NET).address,
type,
}
},
mockBalanceTransferReceipt: (amount, type) => {
return {
...receiptCommonField,
amount: UInt64.fromUint(amount),
recipientAddress: Account.generateNewAccount(NetworkType.TEST_NET).address,
senderAddress: Account.generateNewAccount(NetworkType.TEST_NET).address,
type,
}
},
mockInflationReceipt: () => {
return {
...receiptCommonField,
type: 20803,
}
},
mockArtifactExpiryReceipt: (artifactId, type) => {
return {
artifactId,
height: UInt64.fromUint(1000),
version: 1,
type
}
}
};

Expand Down
126 changes: 122 additions & 4 deletions __tests__/infrastructure/AccountService.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AccountService, ChainService, NamespaceService, NodeService } from '../../src/infrastructure';
import { AccountService, ChainService, NamespaceService, NodeService, LockService } from '../../src/infrastructure';
import TestHelper from '../TestHelper';
import { restore, stub } from 'sinon';
import { Mosaic, UInt64, MosaicId, Account, NetworkType } from 'symbol-sdk';
import { Mosaic, UInt64, MosaicId, Account, NetworkType, NamespaceName, NamespaceId } from 'symbol-sdk';
import helper from '../../src/helper';

describe('Account Service', () => {
describe('getAccountInfo should', () => {
describe('getAccountInfo', () => {

let getAccount = {};
let getChainInfo = {};
Expand Down Expand Up @@ -74,7 +75,7 @@ describe('Account Service', () => {
});
});

describe('getAccountList should', () => {
describe('getAccountList', () => {
const pageInfo = {
pageNumber: 1,
pageSize: 10
Expand Down Expand Up @@ -251,4 +252,121 @@ describe('Account Service', () => {
runBasicNamespaceExpirationTests(['hello.world', 'hello'], 1000, 'in a month');
})
})

describe('getAccountHashLockList', () => {
it('returns Unused and Used hash lock transaction list ', async () => {
// Arrange:
const pageInfo = {
pageNumber: 1,
pageSize: 10
};

const mockSearchHashLocks = {
...pageInfo,
data: [
TestHelper.createFormattedHashLockTransaction('Unused'),
TestHelper.createFormattedHashLockTransaction('Used'),
]
};

stub(LockService, 'searchHashLocks').returns(Promise.resolve(mockSearchHashLocks));

// Act:
const hashLockList = await AccountService.getAccountHashLockList(pageInfo, mockSearchHashLocks.data[0].ownerAddress);

// Assert:
expect(hashLockList.pageNumber).toEqual(pageInfo.pageNumber);
expect(hashLockList.pageSize).toEqual(pageInfo.pageSize);
expect(hashLockList.data).toHaveLength(2);
expect(hashLockList.data[0].status).toBe('Unused');
expect(hashLockList.data[1].status).toBe('Used');

hashLockList.data.forEach((hashLock, index) => {
expect(hashLock.mosaics).toEqual([{
amount: '10.000000',
mosaicAliasName: 'symbol.xym',
mosaicId: "6BED913FA20223F8",
rawAmount: UInt64.fromUint(10000000)
}]);
expect(hashLock.endHeight).toBe(10);
expect(hashLock.hash).toBe(mockSearchHashLocks.data[index].hash);
expect(hashLock.ownerAddress).toBe(mockSearchHashLocks.data[index].ownerAddress);
});
})
})

describe('getAccountSecretLockList', () => {
it('resolves mosaics in secret lock transaction list', async () => {
// Arrange:
const pageInfo = {
pageNumber: 1,
pageSize: 10
};

const mockTestSecretLockMosaic = {
idHex: '22D2D90A27738AA0',
namespaceName: 'secret_lock_mosaic'
};

const mockSearchSecretLocks = {
...pageInfo,
data: [
TestHelper.createFormattedSecretLockTransaction(mockTestSecretLockMosaic.idHex, 30, 'Unused'),
TestHelper.createFormattedSecretLockTransaction(mockTestSecretLockMosaic.idHex, 10, 'Used'),
]
};

const mockMosaicInfoAndNamespace = {
mosaicInfos: [
TestHelper.mockMosaicInfo(mockTestSecretLockMosaic.idHex, 'TC46AZWUIZYZ2WVGLVEZYNZHSIFAD3AFDPUJMEA', 2, 0)
],
mosaicNames: [
{
names: [new NamespaceName(new NamespaceId(mockTestSecretLockMosaic.namespaceName), mockTestSecretLockMosaic.namespaceName)],
mosaicId: mockTestSecretLockMosaic.idHex
}
],
unresolvedMosaicsMap: {
"22D2D90A27738AA0": '22D2D90A27738AA0'
}
}

stub(LockService, 'searchSecretLocks').returns(Promise.resolve(mockSearchSecretLocks));
stub(helper, 'getMosaicInfoAndNamespace').returns(Promise.resolve(mockMosaicInfoAndNamespace));

// Act:
const secretLockList = await AccountService.getAccountSecretLockList(pageInfo, mockSearchSecretLocks.data[0].ownerAddress);

// Assert:
expect(secretLockList.pageNumber).toEqual(pageInfo.pageNumber);
expect(secretLockList.pageSize).toEqual(pageInfo.pageSize);
expect(secretLockList.data).toHaveLength(2);

const unusedData = secretLockList.data[0];
const usedData = secretLockList.data[1];

expect(unusedData.status).toBe('Unused');
expect(unusedData.mosaics).toEqual([{
amount: '30',
mosaicAliasName: mockTestSecretLockMosaic.namespaceName,
mosaicId: mockTestSecretLockMosaic.idHex,
rawAmount: UInt64.fromUint(30)
}]);

expect(usedData.status).toBe('Used');
expect(usedData.mosaics).toEqual([{
amount: '10',
mosaicAliasName: mockTestSecretLockMosaic.namespaceName,
mosaicId: mockTestSecretLockMosaic.idHex,
rawAmount: UInt64.fromUint(10)
}]);

secretLockList.data.forEach(secretLock => {
// Assert below are constants from createFormattedSecretLockTransaction
expect(secretLock.endHeight).toBe(10);
expect(secretLock.secret).toBe('112233445566');
expect(secretLock.hashAlgorithm).toBe('Sha3 256');
});
})
})
});
Loading

0 comments on commit 3f75524

Please sign in to comment.